xref: /openbmc/u-boot/drivers/mmc/mmc.c (revision 2051aefe71b646b14220c52e85c42b26be1e7cad)
1272cc70bSAndy Fleming /*
2272cc70bSAndy Fleming  * Copyright 2008, Freescale Semiconductor, Inc
3272cc70bSAndy Fleming  * Andy Fleming
4272cc70bSAndy Fleming  *
5272cc70bSAndy Fleming  * Based vaguely on the Linux code
6272cc70bSAndy Fleming  *
71a459660SWolfgang Denk  * SPDX-License-Identifier:	GPL-2.0+
8272cc70bSAndy Fleming  */
9272cc70bSAndy Fleming 
10272cc70bSAndy Fleming #include <config.h>
11272cc70bSAndy Fleming #include <common.h>
12272cc70bSAndy Fleming #include <command.h>
138e3332e2SSjoerd Simons #include <dm.h>
148e3332e2SSjoerd Simons #include <dm/device-internal.h>
15d4622df3SStephen Warren #include <errno.h>
16272cc70bSAndy Fleming #include <mmc.h>
17272cc70bSAndy Fleming #include <part.h>
18*2051aefeSPeng Fan #include <power/regulator.h>
19272cc70bSAndy Fleming #include <malloc.h>
20cf92e05cSSimon Glass #include <memalign.h>
21272cc70bSAndy Fleming #include <linux/list.h>
229b1f942cSRabin Vincent #include <div64.h>
23da61fa5fSPaul Burton #include "mmc_private.h"
24272cc70bSAndy Fleming 
253697e599SPeng Fan static const unsigned int sd_au_size[] = {
263697e599SPeng Fan 	0,		SZ_16K / 512,		SZ_32K / 512,
273697e599SPeng Fan 	SZ_64K / 512,	SZ_128K / 512,		SZ_256K / 512,
283697e599SPeng Fan 	SZ_512K / 512,	SZ_1M / 512,		SZ_2M / 512,
293697e599SPeng Fan 	SZ_4M / 512,	SZ_8M / 512,		(SZ_8M + SZ_4M) / 512,
303697e599SPeng Fan 	SZ_16M / 512,	(SZ_16M + SZ_8M) / 512,	SZ_32M / 512,	SZ_64M / 512,
313697e599SPeng Fan };
323697e599SPeng Fan 
338ca51e51SSimon Glass #ifndef CONFIG_DM_MMC_OPS
34750121c3SJeroen Hofstee __weak int board_mmc_getwp(struct mmc *mmc)
35d23d8d7eSNikita Kiryanov {
36d23d8d7eSNikita Kiryanov 	return -1;
37d23d8d7eSNikita Kiryanov }
38d23d8d7eSNikita Kiryanov 
39d23d8d7eSNikita Kiryanov int mmc_getwp(struct mmc *mmc)
40d23d8d7eSNikita Kiryanov {
41d23d8d7eSNikita Kiryanov 	int wp;
42d23d8d7eSNikita Kiryanov 
43d23d8d7eSNikita Kiryanov 	wp = board_mmc_getwp(mmc);
44d23d8d7eSNikita Kiryanov 
45d4e1da4eSPeter Korsgaard 	if (wp < 0) {
4693bfd616SPantelis Antoniou 		if (mmc->cfg->ops->getwp)
4793bfd616SPantelis Antoniou 			wp = mmc->cfg->ops->getwp(mmc);
48d4e1da4eSPeter Korsgaard 		else
49d4e1da4eSPeter Korsgaard 			wp = 0;
50d4e1da4eSPeter Korsgaard 	}
51d23d8d7eSNikita Kiryanov 
52d23d8d7eSNikita Kiryanov 	return wp;
53d23d8d7eSNikita Kiryanov }
54d23d8d7eSNikita Kiryanov 
55cee9ab7cSJeroen Hofstee __weak int board_mmc_getcd(struct mmc *mmc)
56cee9ab7cSJeroen Hofstee {
5711fdade2SStefano Babic 	return -1;
5811fdade2SStefano Babic }
598ca51e51SSimon Glass #endif
6011fdade2SStefano Babic 
618635ff9eSMarek Vasut #ifdef CONFIG_MMC_TRACE
62c0c76ebaSSimon Glass void mmmc_trace_before_send(struct mmc *mmc, struct mmc_cmd *cmd)
63c0c76ebaSSimon Glass {
64c0c76ebaSSimon Glass 	printf("CMD_SEND:%d\n", cmd->cmdidx);
65c0c76ebaSSimon Glass 	printf("\t\tARG\t\t\t 0x%08X\n", cmd->cmdarg);
66c0c76ebaSSimon Glass }
67c0c76ebaSSimon Glass 
68c0c76ebaSSimon Glass void mmmc_trace_after_send(struct mmc *mmc, struct mmc_cmd *cmd, int ret)
69c0c76ebaSSimon Glass {
705db2fe3aSRaffaele Recalcati 	int i;
715db2fe3aSRaffaele Recalcati 	u8 *ptr;
725db2fe3aSRaffaele Recalcati 
737863ce58SBin Meng 	if (ret) {
747863ce58SBin Meng 		printf("\t\tRET\t\t\t %d\n", ret);
757863ce58SBin Meng 	} else {
765db2fe3aSRaffaele Recalcati 		switch (cmd->resp_type) {
775db2fe3aSRaffaele Recalcati 		case MMC_RSP_NONE:
785db2fe3aSRaffaele Recalcati 			printf("\t\tMMC_RSP_NONE\n");
795db2fe3aSRaffaele Recalcati 			break;
805db2fe3aSRaffaele Recalcati 		case MMC_RSP_R1:
815db2fe3aSRaffaele Recalcati 			printf("\t\tMMC_RSP_R1,5,6,7 \t 0x%08X \n",
825db2fe3aSRaffaele Recalcati 				cmd->response[0]);
835db2fe3aSRaffaele Recalcati 			break;
845db2fe3aSRaffaele Recalcati 		case MMC_RSP_R1b:
855db2fe3aSRaffaele Recalcati 			printf("\t\tMMC_RSP_R1b\t\t 0x%08X \n",
865db2fe3aSRaffaele Recalcati 				cmd->response[0]);
875db2fe3aSRaffaele Recalcati 			break;
885db2fe3aSRaffaele Recalcati 		case MMC_RSP_R2:
895db2fe3aSRaffaele Recalcati 			printf("\t\tMMC_RSP_R2\t\t 0x%08X \n",
905db2fe3aSRaffaele Recalcati 				cmd->response[0]);
915db2fe3aSRaffaele Recalcati 			printf("\t\t          \t\t 0x%08X \n",
925db2fe3aSRaffaele Recalcati 				cmd->response[1]);
935db2fe3aSRaffaele Recalcati 			printf("\t\t          \t\t 0x%08X \n",
945db2fe3aSRaffaele Recalcati 				cmd->response[2]);
955db2fe3aSRaffaele Recalcati 			printf("\t\t          \t\t 0x%08X \n",
965db2fe3aSRaffaele Recalcati 				cmd->response[3]);
975db2fe3aSRaffaele Recalcati 			printf("\n");
985db2fe3aSRaffaele Recalcati 			printf("\t\t\t\t\tDUMPING DATA\n");
995db2fe3aSRaffaele Recalcati 			for (i = 0; i < 4; i++) {
1005db2fe3aSRaffaele Recalcati 				int j;
1015db2fe3aSRaffaele Recalcati 				printf("\t\t\t\t\t%03d - ", i*4);
102146bec79SDirk Behme 				ptr = (u8 *)&cmd->response[i];
1035db2fe3aSRaffaele Recalcati 				ptr += 3;
1045db2fe3aSRaffaele Recalcati 				for (j = 0; j < 4; j++)
1055db2fe3aSRaffaele Recalcati 					printf("%02X ", *ptr--);
1065db2fe3aSRaffaele Recalcati 				printf("\n");
1075db2fe3aSRaffaele Recalcati 			}
1085db2fe3aSRaffaele Recalcati 			break;
1095db2fe3aSRaffaele Recalcati 		case MMC_RSP_R3:
1105db2fe3aSRaffaele Recalcati 			printf("\t\tMMC_RSP_R3,4\t\t 0x%08X \n",
1115db2fe3aSRaffaele Recalcati 				cmd->response[0]);
1125db2fe3aSRaffaele Recalcati 			break;
1135db2fe3aSRaffaele Recalcati 		default:
1145db2fe3aSRaffaele Recalcati 			printf("\t\tERROR MMC rsp not supported\n");
1155db2fe3aSRaffaele Recalcati 			break;
1165db2fe3aSRaffaele Recalcati 		}
1177863ce58SBin Meng 	}
118c0c76ebaSSimon Glass }
119c0c76ebaSSimon Glass 
120c0c76ebaSSimon Glass void mmc_trace_state(struct mmc *mmc, struct mmc_cmd *cmd)
121c0c76ebaSSimon Glass {
122c0c76ebaSSimon Glass 	int status;
123c0c76ebaSSimon Glass 
124c0c76ebaSSimon Glass 	status = (cmd->response[0] & MMC_STATUS_CURR_STATE) >> 9;
125c0c76ebaSSimon Glass 	printf("CURR STATE:%d\n", status);
126c0c76ebaSSimon Glass }
1275db2fe3aSRaffaele Recalcati #endif
128c0c76ebaSSimon Glass 
1298ca51e51SSimon Glass #ifndef CONFIG_DM_MMC_OPS
130c0c76ebaSSimon Glass int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
131c0c76ebaSSimon Glass {
132c0c76ebaSSimon Glass 	int ret;
133c0c76ebaSSimon Glass 
134c0c76ebaSSimon Glass 	mmmc_trace_before_send(mmc, cmd);
135c0c76ebaSSimon Glass 	ret = mmc->cfg->ops->send_cmd(mmc, cmd, data);
136c0c76ebaSSimon Glass 	mmmc_trace_after_send(mmc, cmd, ret);
137c0c76ebaSSimon Glass 
1388635ff9eSMarek Vasut 	return ret;
139272cc70bSAndy Fleming }
1408ca51e51SSimon Glass #endif
141272cc70bSAndy Fleming 
142da61fa5fSPaul Burton int mmc_send_status(struct mmc *mmc, int timeout)
1435d4fc8d9SRaffaele Recalcati {
1445d4fc8d9SRaffaele Recalcati 	struct mmc_cmd cmd;
145d617c426SJan Kloetzke 	int err, retries = 5;
1465d4fc8d9SRaffaele Recalcati 
1475d4fc8d9SRaffaele Recalcati 	cmd.cmdidx = MMC_CMD_SEND_STATUS;
1485d4fc8d9SRaffaele Recalcati 	cmd.resp_type = MMC_RSP_R1;
149aaf3d41aSMarek Vasut 	if (!mmc_host_is_spi(mmc))
150aaf3d41aSMarek Vasut 		cmd.cmdarg = mmc->rca << 16;
1515d4fc8d9SRaffaele Recalcati 
1521677eef4SAndrew Gabbasov 	while (1) {
1535d4fc8d9SRaffaele Recalcati 		err = mmc_send_cmd(mmc, &cmd, NULL);
154d617c426SJan Kloetzke 		if (!err) {
155d617c426SJan Kloetzke 			if ((cmd.response[0] & MMC_STATUS_RDY_FOR_DATA) &&
156d617c426SJan Kloetzke 			    (cmd.response[0] & MMC_STATUS_CURR_STATE) !=
157d617c426SJan Kloetzke 			     MMC_STATE_PRG)
1585d4fc8d9SRaffaele Recalcati 				break;
159d617c426SJan Kloetzke 			else if (cmd.response[0] & MMC_STATUS_MASK) {
16056196826SPaul Burton #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
161d617c426SJan Kloetzke 				printf("Status Error: 0x%08X\n",
162d617c426SJan Kloetzke 					cmd.response[0]);
16356196826SPaul Burton #endif
164915ffa52SJaehoon Chung 				return -ECOMM;
165d617c426SJan Kloetzke 			}
166d617c426SJan Kloetzke 		} else if (--retries < 0)
167d617c426SJan Kloetzke 			return err;
1685d4fc8d9SRaffaele Recalcati 
1691677eef4SAndrew Gabbasov 		if (timeout-- <= 0)
1701677eef4SAndrew Gabbasov 			break;
1715d4fc8d9SRaffaele Recalcati 
1721677eef4SAndrew Gabbasov 		udelay(1000);
1731677eef4SAndrew Gabbasov 	}
1745d4fc8d9SRaffaele Recalcati 
175c0c76ebaSSimon Glass 	mmc_trace_state(mmc, &cmd);
1765b0c942fSJongman Heo 	if (timeout <= 0) {
17756196826SPaul Burton #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
1785d4fc8d9SRaffaele Recalcati 		printf("Timeout waiting card ready\n");
17956196826SPaul Burton #endif
180915ffa52SJaehoon Chung 		return -ETIMEDOUT;
1815d4fc8d9SRaffaele Recalcati 	}
1825d4fc8d9SRaffaele Recalcati 
1835d4fc8d9SRaffaele Recalcati 	return 0;
1845d4fc8d9SRaffaele Recalcati }
1855d4fc8d9SRaffaele Recalcati 
186da61fa5fSPaul Burton int mmc_set_blocklen(struct mmc *mmc, int len)
187272cc70bSAndy Fleming {
188272cc70bSAndy Fleming 	struct mmc_cmd cmd;
189272cc70bSAndy Fleming 
190786e8f81SAndrew Gabbasov 	if (mmc->ddr_mode)
191d22e3d46SJaehoon Chung 		return 0;
192d22e3d46SJaehoon Chung 
193272cc70bSAndy Fleming 	cmd.cmdidx = MMC_CMD_SET_BLOCKLEN;
194272cc70bSAndy Fleming 	cmd.resp_type = MMC_RSP_R1;
195272cc70bSAndy Fleming 	cmd.cmdarg = len;
196272cc70bSAndy Fleming 
197272cc70bSAndy Fleming 	return mmc_send_cmd(mmc, &cmd, NULL);
198272cc70bSAndy Fleming }
199272cc70bSAndy Fleming 
200ff8fef56SSascha Silbe static int mmc_read_blocks(struct mmc *mmc, void *dst, lbaint_t start,
201fdbb873eSKim Phillips 			   lbaint_t blkcnt)
202272cc70bSAndy Fleming {
203272cc70bSAndy Fleming 	struct mmc_cmd cmd;
204272cc70bSAndy Fleming 	struct mmc_data data;
205272cc70bSAndy Fleming 
2064a1a06bcSAlagu Sankar 	if (blkcnt > 1)
2074a1a06bcSAlagu Sankar 		cmd.cmdidx = MMC_CMD_READ_MULTIPLE_BLOCK;
2084a1a06bcSAlagu Sankar 	else
209272cc70bSAndy Fleming 		cmd.cmdidx = MMC_CMD_READ_SINGLE_BLOCK;
210272cc70bSAndy Fleming 
211272cc70bSAndy Fleming 	if (mmc->high_capacity)
2124a1a06bcSAlagu Sankar 		cmd.cmdarg = start;
213272cc70bSAndy Fleming 	else
2144a1a06bcSAlagu Sankar 		cmd.cmdarg = start * mmc->read_bl_len;
215272cc70bSAndy Fleming 
216272cc70bSAndy Fleming 	cmd.resp_type = MMC_RSP_R1;
217272cc70bSAndy Fleming 
218272cc70bSAndy Fleming 	data.dest = dst;
2194a1a06bcSAlagu Sankar 	data.blocks = blkcnt;
220272cc70bSAndy Fleming 	data.blocksize = mmc->read_bl_len;
221272cc70bSAndy Fleming 	data.flags = MMC_DATA_READ;
222272cc70bSAndy Fleming 
2234a1a06bcSAlagu Sankar 	if (mmc_send_cmd(mmc, &cmd, &data))
2244a1a06bcSAlagu Sankar 		return 0;
2254a1a06bcSAlagu Sankar 
2264a1a06bcSAlagu Sankar 	if (blkcnt > 1) {
2274a1a06bcSAlagu Sankar 		cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
2284a1a06bcSAlagu Sankar 		cmd.cmdarg = 0;
2294a1a06bcSAlagu Sankar 		cmd.resp_type = MMC_RSP_R1b;
2304a1a06bcSAlagu Sankar 		if (mmc_send_cmd(mmc, &cmd, NULL)) {
23156196826SPaul Burton #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
2324a1a06bcSAlagu Sankar 			printf("mmc fail to send stop cmd\n");
23356196826SPaul Burton #endif
2344a1a06bcSAlagu Sankar 			return 0;
2354a1a06bcSAlagu Sankar 		}
236272cc70bSAndy Fleming 	}
237272cc70bSAndy Fleming 
2384a1a06bcSAlagu Sankar 	return blkcnt;
239272cc70bSAndy Fleming }
240272cc70bSAndy Fleming 
24133fb211dSSimon Glass #ifdef CONFIG_BLK
2427dba0b93SSimon Glass ulong mmc_bread(struct udevice *dev, lbaint_t start, lbaint_t blkcnt, void *dst)
24333fb211dSSimon Glass #else
2447dba0b93SSimon Glass ulong mmc_bread(struct blk_desc *block_dev, lbaint_t start, lbaint_t blkcnt,
2457dba0b93SSimon Glass 		void *dst)
24633fb211dSSimon Glass #endif
247272cc70bSAndy Fleming {
24833fb211dSSimon Glass #ifdef CONFIG_BLK
24933fb211dSSimon Glass 	struct blk_desc *block_dev = dev_get_uclass_platdata(dev);
25033fb211dSSimon Glass #endif
251bcce53d0SSimon Glass 	int dev_num = block_dev->devnum;
252873cc1d7SStephen Warren 	int err;
2534a1a06bcSAlagu Sankar 	lbaint_t cur, blocks_todo = blkcnt;
254272cc70bSAndy Fleming 
2554a1a06bcSAlagu Sankar 	if (blkcnt == 0)
2564a1a06bcSAlagu Sankar 		return 0;
2574a1a06bcSAlagu Sankar 
2584a1a06bcSAlagu Sankar 	struct mmc *mmc = find_mmc_device(dev_num);
259272cc70bSAndy Fleming 	if (!mmc)
260272cc70bSAndy Fleming 		return 0;
261272cc70bSAndy Fleming 
26269f45cd5SSimon Glass 	err = blk_dselect_hwpart(block_dev, block_dev->hwpart);
263873cc1d7SStephen Warren 	if (err < 0)
264873cc1d7SStephen Warren 		return 0;
265873cc1d7SStephen Warren 
266c40fdca6SSimon Glass 	if ((start + blkcnt) > block_dev->lba) {
26756196826SPaul Burton #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
268ff8fef56SSascha Silbe 		printf("MMC: block number 0x" LBAF " exceeds max(0x" LBAF ")\n",
269c40fdca6SSimon Glass 			start + blkcnt, block_dev->lba);
27056196826SPaul Burton #endif
271d2bf29e3SLei Wen 		return 0;
272d2bf29e3SLei Wen 	}
273272cc70bSAndy Fleming 
27411692991SSimon Glass 	if (mmc_set_blocklen(mmc, mmc->read_bl_len)) {
27511692991SSimon Glass 		debug("%s: Failed to set blocklen\n", __func__);
276272cc70bSAndy Fleming 		return 0;
27711692991SSimon Glass 	}
278272cc70bSAndy Fleming 
2794a1a06bcSAlagu Sankar 	do {
28093bfd616SPantelis Antoniou 		cur = (blocks_todo > mmc->cfg->b_max) ?
28193bfd616SPantelis Antoniou 			mmc->cfg->b_max : blocks_todo;
28211692991SSimon Glass 		if (mmc_read_blocks(mmc, dst, start, cur) != cur) {
28311692991SSimon Glass 			debug("%s: Failed to read blocks\n", __func__);
2844a1a06bcSAlagu Sankar 			return 0;
28511692991SSimon Glass 		}
2864a1a06bcSAlagu Sankar 		blocks_todo -= cur;
2874a1a06bcSAlagu Sankar 		start += cur;
2884a1a06bcSAlagu Sankar 		dst += cur * mmc->read_bl_len;
2894a1a06bcSAlagu Sankar 	} while (blocks_todo > 0);
290272cc70bSAndy Fleming 
291272cc70bSAndy Fleming 	return blkcnt;
292272cc70bSAndy Fleming }
293272cc70bSAndy Fleming 
294fdbb873eSKim Phillips static int mmc_go_idle(struct mmc *mmc)
295272cc70bSAndy Fleming {
296272cc70bSAndy Fleming 	struct mmc_cmd cmd;
297272cc70bSAndy Fleming 	int err;
298272cc70bSAndy Fleming 
299272cc70bSAndy Fleming 	udelay(1000);
300272cc70bSAndy Fleming 
301272cc70bSAndy Fleming 	cmd.cmdidx = MMC_CMD_GO_IDLE_STATE;
302272cc70bSAndy Fleming 	cmd.cmdarg = 0;
303272cc70bSAndy Fleming 	cmd.resp_type = MMC_RSP_NONE;
304272cc70bSAndy Fleming 
305272cc70bSAndy Fleming 	err = mmc_send_cmd(mmc, &cmd, NULL);
306272cc70bSAndy Fleming 
307272cc70bSAndy Fleming 	if (err)
308272cc70bSAndy Fleming 		return err;
309272cc70bSAndy Fleming 
310272cc70bSAndy Fleming 	udelay(2000);
311272cc70bSAndy Fleming 
312272cc70bSAndy Fleming 	return 0;
313272cc70bSAndy Fleming }
314272cc70bSAndy Fleming 
315fdbb873eSKim Phillips static int sd_send_op_cond(struct mmc *mmc)
316272cc70bSAndy Fleming {
317272cc70bSAndy Fleming 	int timeout = 1000;
318272cc70bSAndy Fleming 	int err;
319272cc70bSAndy Fleming 	struct mmc_cmd cmd;
320272cc70bSAndy Fleming 
3211677eef4SAndrew Gabbasov 	while (1) {
322272cc70bSAndy Fleming 		cmd.cmdidx = MMC_CMD_APP_CMD;
323272cc70bSAndy Fleming 		cmd.resp_type = MMC_RSP_R1;
324272cc70bSAndy Fleming 		cmd.cmdarg = 0;
325272cc70bSAndy Fleming 
326272cc70bSAndy Fleming 		err = mmc_send_cmd(mmc, &cmd, NULL);
327272cc70bSAndy Fleming 
328272cc70bSAndy Fleming 		if (err)
329272cc70bSAndy Fleming 			return err;
330272cc70bSAndy Fleming 
331272cc70bSAndy Fleming 		cmd.cmdidx = SD_CMD_APP_SEND_OP_COND;
332272cc70bSAndy Fleming 		cmd.resp_type = MMC_RSP_R3;
333250de12bSStefano Babic 
334250de12bSStefano Babic 		/*
335250de12bSStefano Babic 		 * Most cards do not answer if some reserved bits
336250de12bSStefano Babic 		 * in the ocr are set. However, Some controller
337250de12bSStefano Babic 		 * can set bit 7 (reserved for low voltages), but
338250de12bSStefano Babic 		 * how to manage low voltages SD card is not yet
339250de12bSStefano Babic 		 * specified.
340250de12bSStefano Babic 		 */
341d52ebf10SThomas Chou 		cmd.cmdarg = mmc_host_is_spi(mmc) ? 0 :
34293bfd616SPantelis Antoniou 			(mmc->cfg->voltages & 0xff8000);
343272cc70bSAndy Fleming 
344272cc70bSAndy Fleming 		if (mmc->version == SD_VERSION_2)
345272cc70bSAndy Fleming 			cmd.cmdarg |= OCR_HCS;
346272cc70bSAndy Fleming 
347272cc70bSAndy Fleming 		err = mmc_send_cmd(mmc, &cmd, NULL);
348272cc70bSAndy Fleming 
349272cc70bSAndy Fleming 		if (err)
350272cc70bSAndy Fleming 			return err;
351272cc70bSAndy Fleming 
3521677eef4SAndrew Gabbasov 		if (cmd.response[0] & OCR_BUSY)
3531677eef4SAndrew Gabbasov 			break;
354272cc70bSAndy Fleming 
3551677eef4SAndrew Gabbasov 		if (timeout-- <= 0)
356915ffa52SJaehoon Chung 			return -EOPNOTSUPP;
357272cc70bSAndy Fleming 
3581677eef4SAndrew Gabbasov 		udelay(1000);
3591677eef4SAndrew Gabbasov 	}
3601677eef4SAndrew Gabbasov 
361272cc70bSAndy Fleming 	if (mmc->version != SD_VERSION_2)
362272cc70bSAndy Fleming 		mmc->version = SD_VERSION_1_0;
363272cc70bSAndy Fleming 
364d52ebf10SThomas Chou 	if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
365d52ebf10SThomas Chou 		cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
366d52ebf10SThomas Chou 		cmd.resp_type = MMC_RSP_R3;
367d52ebf10SThomas Chou 		cmd.cmdarg = 0;
368d52ebf10SThomas Chou 
369d52ebf10SThomas Chou 		err = mmc_send_cmd(mmc, &cmd, NULL);
370d52ebf10SThomas Chou 
371d52ebf10SThomas Chou 		if (err)
372d52ebf10SThomas Chou 			return err;
373d52ebf10SThomas Chou 	}
374d52ebf10SThomas Chou 
375998be3ddSRabin Vincent 	mmc->ocr = cmd.response[0];
376272cc70bSAndy Fleming 
377272cc70bSAndy Fleming 	mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
378272cc70bSAndy Fleming 	mmc->rca = 0;
379272cc70bSAndy Fleming 
380272cc70bSAndy Fleming 	return 0;
381272cc70bSAndy Fleming }
382272cc70bSAndy Fleming 
3835289b535SAndrew Gabbasov static int mmc_send_op_cond_iter(struct mmc *mmc, int use_arg)
384272cc70bSAndy Fleming {
3855289b535SAndrew Gabbasov 	struct mmc_cmd cmd;
386272cc70bSAndy Fleming 	int err;
387272cc70bSAndy Fleming 
3885289b535SAndrew Gabbasov 	cmd.cmdidx = MMC_CMD_SEND_OP_COND;
3895289b535SAndrew Gabbasov 	cmd.resp_type = MMC_RSP_R3;
3905289b535SAndrew Gabbasov 	cmd.cmdarg = 0;
3915a20397bSRob Herring 	if (use_arg && !mmc_host_is_spi(mmc))
3925a20397bSRob Herring 		cmd.cmdarg = OCR_HCS |
39393bfd616SPantelis Antoniou 			(mmc->cfg->voltages &
394a626c8d4SAndrew Gabbasov 			(mmc->ocr & OCR_VOLTAGE_MASK)) |
395a626c8d4SAndrew Gabbasov 			(mmc->ocr & OCR_ACCESS_MODE);
396e9550449SChe-Liang Chiou 
3975289b535SAndrew Gabbasov 	err = mmc_send_cmd(mmc, &cmd, NULL);
398e9550449SChe-Liang Chiou 	if (err)
399e9550449SChe-Liang Chiou 		return err;
4005289b535SAndrew Gabbasov 	mmc->ocr = cmd.response[0];
401e9550449SChe-Liang Chiou 	return 0;
402e9550449SChe-Liang Chiou }
403e9550449SChe-Liang Chiou 
404750121c3SJeroen Hofstee static int mmc_send_op_cond(struct mmc *mmc)
405e9550449SChe-Liang Chiou {
406e9550449SChe-Liang Chiou 	int err, i;
407e9550449SChe-Liang Chiou 
408272cc70bSAndy Fleming 	/* Some cards seem to need this */
409272cc70bSAndy Fleming 	mmc_go_idle(mmc);
410272cc70bSAndy Fleming 
41131cacbabSRaffaele Recalcati  	/* Asking to the card its capabilities */
412e9550449SChe-Liang Chiou 	for (i = 0; i < 2; i++) {
4135289b535SAndrew Gabbasov 		err = mmc_send_op_cond_iter(mmc, i != 0);
41431cacbabSRaffaele Recalcati 		if (err)
41531cacbabSRaffaele Recalcati 			return err;
41631cacbabSRaffaele Recalcati 
417e9550449SChe-Liang Chiou 		/* exit if not busy (flag seems to be inverted) */
418a626c8d4SAndrew Gabbasov 		if (mmc->ocr & OCR_BUSY)
419bd47c135SAndrew Gabbasov 			break;
420e9550449SChe-Liang Chiou 	}
421bd47c135SAndrew Gabbasov 	mmc->op_cond_pending = 1;
422bd47c135SAndrew Gabbasov 	return 0;
423e9550449SChe-Liang Chiou }
42431cacbabSRaffaele Recalcati 
425750121c3SJeroen Hofstee static int mmc_complete_op_cond(struct mmc *mmc)
426e9550449SChe-Liang Chiou {
427e9550449SChe-Liang Chiou 	struct mmc_cmd cmd;
428e9550449SChe-Liang Chiou 	int timeout = 1000;
429e9550449SChe-Liang Chiou 	uint start;
430e9550449SChe-Liang Chiou 	int err;
431e9550449SChe-Liang Chiou 
432e9550449SChe-Liang Chiou 	mmc->op_cond_pending = 0;
433cc17c01fSAndrew Gabbasov 	if (!(mmc->ocr & OCR_BUSY)) {
434d188b113SYangbo Lu 		/* Some cards seem to need this */
435d188b113SYangbo Lu 		mmc_go_idle(mmc);
436d188b113SYangbo Lu 
437e9550449SChe-Liang Chiou 		start = get_timer(0);
4381677eef4SAndrew Gabbasov 		while (1) {
4395289b535SAndrew Gabbasov 			err = mmc_send_op_cond_iter(mmc, 1);
440272cc70bSAndy Fleming 			if (err)
441272cc70bSAndy Fleming 				return err;
4421677eef4SAndrew Gabbasov 			if (mmc->ocr & OCR_BUSY)
4431677eef4SAndrew Gabbasov 				break;
444e9550449SChe-Liang Chiou 			if (get_timer(start) > timeout)
445915ffa52SJaehoon Chung 				return -EOPNOTSUPP;
446e9550449SChe-Liang Chiou 			udelay(100);
4471677eef4SAndrew Gabbasov 		}
448cc17c01fSAndrew Gabbasov 	}
449272cc70bSAndy Fleming 
450d52ebf10SThomas Chou 	if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
451d52ebf10SThomas Chou 		cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
452d52ebf10SThomas Chou 		cmd.resp_type = MMC_RSP_R3;
453d52ebf10SThomas Chou 		cmd.cmdarg = 0;
454d52ebf10SThomas Chou 
455d52ebf10SThomas Chou 		err = mmc_send_cmd(mmc, &cmd, NULL);
456d52ebf10SThomas Chou 
457d52ebf10SThomas Chou 		if (err)
458d52ebf10SThomas Chou 			return err;
459a626c8d4SAndrew Gabbasov 
460a626c8d4SAndrew Gabbasov 		mmc->ocr = cmd.response[0];
461d52ebf10SThomas Chou 	}
462d52ebf10SThomas Chou 
463272cc70bSAndy Fleming 	mmc->version = MMC_VERSION_UNKNOWN;
464272cc70bSAndy Fleming 
465272cc70bSAndy Fleming 	mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
466def816a2SStephen Warren 	mmc->rca = 1;
467272cc70bSAndy Fleming 
468272cc70bSAndy Fleming 	return 0;
469272cc70bSAndy Fleming }
470272cc70bSAndy Fleming 
471272cc70bSAndy Fleming 
472fdbb873eSKim Phillips static int mmc_send_ext_csd(struct mmc *mmc, u8 *ext_csd)
473272cc70bSAndy Fleming {
474272cc70bSAndy Fleming 	struct mmc_cmd cmd;
475272cc70bSAndy Fleming 	struct mmc_data data;
476272cc70bSAndy Fleming 	int err;
477272cc70bSAndy Fleming 
478272cc70bSAndy Fleming 	/* Get the Card Status Register */
479272cc70bSAndy Fleming 	cmd.cmdidx = MMC_CMD_SEND_EXT_CSD;
480272cc70bSAndy Fleming 	cmd.resp_type = MMC_RSP_R1;
481272cc70bSAndy Fleming 	cmd.cmdarg = 0;
482272cc70bSAndy Fleming 
483cdfd1ac6SYoshihiro Shimoda 	data.dest = (char *)ext_csd;
484272cc70bSAndy Fleming 	data.blocks = 1;
4858bfa195eSSimon Glass 	data.blocksize = MMC_MAX_BLOCK_LEN;
486272cc70bSAndy Fleming 	data.flags = MMC_DATA_READ;
487272cc70bSAndy Fleming 
488272cc70bSAndy Fleming 	err = mmc_send_cmd(mmc, &cmd, &data);
489272cc70bSAndy Fleming 
490272cc70bSAndy Fleming 	return err;
491272cc70bSAndy Fleming }
492272cc70bSAndy Fleming 
493c40704f4SSimon Glass int mmc_switch(struct mmc *mmc, u8 set, u8 index, u8 value)
494272cc70bSAndy Fleming {
495272cc70bSAndy Fleming 	struct mmc_cmd cmd;
4965d4fc8d9SRaffaele Recalcati 	int timeout = 1000;
4975d4fc8d9SRaffaele Recalcati 	int ret;
498272cc70bSAndy Fleming 
499272cc70bSAndy Fleming 	cmd.cmdidx = MMC_CMD_SWITCH;
500272cc70bSAndy Fleming 	cmd.resp_type = MMC_RSP_R1b;
501272cc70bSAndy Fleming 	cmd.cmdarg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
502272cc70bSAndy Fleming 				 (index << 16) |
503272cc70bSAndy Fleming 				 (value << 8);
504272cc70bSAndy Fleming 
5055d4fc8d9SRaffaele Recalcati 	ret = mmc_send_cmd(mmc, &cmd, NULL);
5065d4fc8d9SRaffaele Recalcati 
5075d4fc8d9SRaffaele Recalcati 	/* Waiting for the ready status */
50893ad0d18SJan Kloetzke 	if (!ret)
50993ad0d18SJan Kloetzke 		ret = mmc_send_status(mmc, timeout);
5105d4fc8d9SRaffaele Recalcati 
5115d4fc8d9SRaffaele Recalcati 	return ret;
5125d4fc8d9SRaffaele Recalcati 
513272cc70bSAndy Fleming }
514272cc70bSAndy Fleming 
515fdbb873eSKim Phillips static int mmc_change_freq(struct mmc *mmc)
516272cc70bSAndy Fleming {
5178bfa195eSSimon Glass 	ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
518272cc70bSAndy Fleming 	char cardtype;
519272cc70bSAndy Fleming 	int err;
520272cc70bSAndy Fleming 
521fc5b32fbSAndrew Gabbasov 	mmc->card_caps = 0;
522272cc70bSAndy Fleming 
523d52ebf10SThomas Chou 	if (mmc_host_is_spi(mmc))
524d52ebf10SThomas Chou 		return 0;
525d52ebf10SThomas Chou 
526272cc70bSAndy Fleming 	/* Only version 4 supports high-speed */
527272cc70bSAndy Fleming 	if (mmc->version < MMC_VERSION_4)
528272cc70bSAndy Fleming 		return 0;
529272cc70bSAndy Fleming 
530fc5b32fbSAndrew Gabbasov 	mmc->card_caps |= MMC_MODE_4BIT | MMC_MODE_8BIT;
531fc5b32fbSAndrew Gabbasov 
532272cc70bSAndy Fleming 	err = mmc_send_ext_csd(mmc, ext_csd);
533272cc70bSAndy Fleming 
534272cc70bSAndy Fleming 	if (err)
535272cc70bSAndy Fleming 		return err;
536272cc70bSAndy Fleming 
5370560db18SLei Wen 	cardtype = ext_csd[EXT_CSD_CARD_TYPE] & 0xf;
538272cc70bSAndy Fleming 
539272cc70bSAndy Fleming 	err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING, 1);
540272cc70bSAndy Fleming 
541272cc70bSAndy Fleming 	if (err)
542a5e27b41SHeiko Schocher 		return err;
543272cc70bSAndy Fleming 
544272cc70bSAndy Fleming 	/* Now check to see that it worked */
545272cc70bSAndy Fleming 	err = mmc_send_ext_csd(mmc, ext_csd);
546272cc70bSAndy Fleming 
547272cc70bSAndy Fleming 	if (err)
548272cc70bSAndy Fleming 		return err;
549272cc70bSAndy Fleming 
550272cc70bSAndy Fleming 	/* No high-speed support */
5510560db18SLei Wen 	if (!ext_csd[EXT_CSD_HS_TIMING])
552272cc70bSAndy Fleming 		return 0;
553272cc70bSAndy Fleming 
554272cc70bSAndy Fleming 	/* High Speed is set, there are two types: 52MHz and 26MHz */
555d22e3d46SJaehoon Chung 	if (cardtype & EXT_CSD_CARD_TYPE_52) {
556201d5ac4SAndrew Gabbasov 		if (cardtype & EXT_CSD_CARD_TYPE_DDR_1_8V)
557d22e3d46SJaehoon Chung 			mmc->card_caps |= MMC_MODE_DDR_52MHz;
558272cc70bSAndy Fleming 		mmc->card_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
559d22e3d46SJaehoon Chung 	} else {
560272cc70bSAndy Fleming 		mmc->card_caps |= MMC_MODE_HS;
561d22e3d46SJaehoon Chung 	}
562272cc70bSAndy Fleming 
563272cc70bSAndy Fleming 	return 0;
564272cc70bSAndy Fleming }
565272cc70bSAndy Fleming 
566f866a46dSStephen Warren static int mmc_set_capacity(struct mmc *mmc, int part_num)
567f866a46dSStephen Warren {
568f866a46dSStephen Warren 	switch (part_num) {
569f866a46dSStephen Warren 	case 0:
570f866a46dSStephen Warren 		mmc->capacity = mmc->capacity_user;
571f866a46dSStephen Warren 		break;
572f866a46dSStephen Warren 	case 1:
573f866a46dSStephen Warren 	case 2:
574f866a46dSStephen Warren 		mmc->capacity = mmc->capacity_boot;
575f866a46dSStephen Warren 		break;
576f866a46dSStephen Warren 	case 3:
577f866a46dSStephen Warren 		mmc->capacity = mmc->capacity_rpmb;
578f866a46dSStephen Warren 		break;
579f866a46dSStephen Warren 	case 4:
580f866a46dSStephen Warren 	case 5:
581f866a46dSStephen Warren 	case 6:
582f866a46dSStephen Warren 	case 7:
583f866a46dSStephen Warren 		mmc->capacity = mmc->capacity_gp[part_num - 4];
584f866a46dSStephen Warren 		break;
585f866a46dSStephen Warren 	default:
586f866a46dSStephen Warren 		return -1;
587f866a46dSStephen Warren 	}
588f866a46dSStephen Warren 
589c40fdca6SSimon Glass 	mmc_get_blk_desc(mmc)->lba = lldiv(mmc->capacity, mmc->read_bl_len);
590f866a46dSStephen Warren 
591f866a46dSStephen Warren 	return 0;
592f866a46dSStephen Warren }
593f866a46dSStephen Warren 
5947dba0b93SSimon Glass int mmc_switch_part(struct mmc *mmc, unsigned int part_num)
595bc897b1dSLei Wen {
596f866a46dSStephen Warren 	int ret;
597bc897b1dSLei Wen 
598f866a46dSStephen Warren 	ret = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONF,
599bc897b1dSLei Wen 			 (mmc->part_config & ~PART_ACCESS_MASK)
600bc897b1dSLei Wen 			 | (part_num & PART_ACCESS_MASK));
601f866a46dSStephen Warren 
6026dc93e70SPeter Bigot 	/*
6036dc93e70SPeter Bigot 	 * Set the capacity if the switch succeeded or was intended
6046dc93e70SPeter Bigot 	 * to return to representing the raw device.
6056dc93e70SPeter Bigot 	 */
606873cc1d7SStephen Warren 	if ((ret == 0) || ((ret == -ENODEV) && (part_num == 0))) {
6076dc93e70SPeter Bigot 		ret = mmc_set_capacity(mmc, part_num);
608fdbb139fSSimon Glass 		mmc_get_blk_desc(mmc)->hwpart = part_num;
609873cc1d7SStephen Warren 	}
6106dc93e70SPeter Bigot 
6116dc93e70SPeter Bigot 	return ret;
612bc897b1dSLei Wen }
613bc897b1dSLei Wen 
614ac9da0e0SDiego Santa Cruz int mmc_hwpart_config(struct mmc *mmc,
615ac9da0e0SDiego Santa Cruz 		      const struct mmc_hwpart_conf *conf,
616ac9da0e0SDiego Santa Cruz 		      enum mmc_hwpart_conf_mode mode)
617ac9da0e0SDiego Santa Cruz {
618ac9da0e0SDiego Santa Cruz 	u8 part_attrs = 0;
619ac9da0e0SDiego Santa Cruz 	u32 enh_size_mult;
620ac9da0e0SDiego Santa Cruz 	u32 enh_start_addr;
621ac9da0e0SDiego Santa Cruz 	u32 gp_size_mult[4];
622ac9da0e0SDiego Santa Cruz 	u32 max_enh_size_mult;
623ac9da0e0SDiego Santa Cruz 	u32 tot_enh_size_mult = 0;
6248dda5b0eSDiego Santa Cruz 	u8 wr_rel_set;
625ac9da0e0SDiego Santa Cruz 	int i, pidx, err;
626ac9da0e0SDiego Santa Cruz 	ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
627ac9da0e0SDiego Santa Cruz 
628ac9da0e0SDiego Santa Cruz 	if (mode < MMC_HWPART_CONF_CHECK || mode > MMC_HWPART_CONF_COMPLETE)
629ac9da0e0SDiego Santa Cruz 		return -EINVAL;
630ac9da0e0SDiego Santa Cruz 
631ac9da0e0SDiego Santa Cruz 	if (IS_SD(mmc) || (mmc->version < MMC_VERSION_4_41)) {
632ac9da0e0SDiego Santa Cruz 		printf("eMMC >= 4.4 required for enhanced user data area\n");
633ac9da0e0SDiego Santa Cruz 		return -EMEDIUMTYPE;
634ac9da0e0SDiego Santa Cruz 	}
635ac9da0e0SDiego Santa Cruz 
636ac9da0e0SDiego Santa Cruz 	if (!(mmc->part_support & PART_SUPPORT)) {
637ac9da0e0SDiego Santa Cruz 		printf("Card does not support partitioning\n");
638ac9da0e0SDiego Santa Cruz 		return -EMEDIUMTYPE;
639ac9da0e0SDiego Santa Cruz 	}
640ac9da0e0SDiego Santa Cruz 
641ac9da0e0SDiego Santa Cruz 	if (!mmc->hc_wp_grp_size) {
642ac9da0e0SDiego Santa Cruz 		printf("Card does not define HC WP group size\n");
643ac9da0e0SDiego Santa Cruz 		return -EMEDIUMTYPE;
644ac9da0e0SDiego Santa Cruz 	}
645ac9da0e0SDiego Santa Cruz 
646ac9da0e0SDiego Santa Cruz 	/* check partition alignment and total enhanced size */
647ac9da0e0SDiego Santa Cruz 	if (conf->user.enh_size) {
648ac9da0e0SDiego Santa Cruz 		if (conf->user.enh_size % mmc->hc_wp_grp_size ||
649ac9da0e0SDiego Santa Cruz 		    conf->user.enh_start % mmc->hc_wp_grp_size) {
650ac9da0e0SDiego Santa Cruz 			printf("User data enhanced area not HC WP group "
651ac9da0e0SDiego Santa Cruz 			       "size aligned\n");
652ac9da0e0SDiego Santa Cruz 			return -EINVAL;
653ac9da0e0SDiego Santa Cruz 		}
654ac9da0e0SDiego Santa Cruz 		part_attrs |= EXT_CSD_ENH_USR;
655ac9da0e0SDiego Santa Cruz 		enh_size_mult = conf->user.enh_size / mmc->hc_wp_grp_size;
656ac9da0e0SDiego Santa Cruz 		if (mmc->high_capacity) {
657ac9da0e0SDiego Santa Cruz 			enh_start_addr = conf->user.enh_start;
658ac9da0e0SDiego Santa Cruz 		} else {
659ac9da0e0SDiego Santa Cruz 			enh_start_addr = (conf->user.enh_start << 9);
660ac9da0e0SDiego Santa Cruz 		}
661ac9da0e0SDiego Santa Cruz 	} else {
662ac9da0e0SDiego Santa Cruz 		enh_size_mult = 0;
663ac9da0e0SDiego Santa Cruz 		enh_start_addr = 0;
664ac9da0e0SDiego Santa Cruz 	}
665ac9da0e0SDiego Santa Cruz 	tot_enh_size_mult += enh_size_mult;
666ac9da0e0SDiego Santa Cruz 
667ac9da0e0SDiego Santa Cruz 	for (pidx = 0; pidx < 4; pidx++) {
668ac9da0e0SDiego Santa Cruz 		if (conf->gp_part[pidx].size % mmc->hc_wp_grp_size) {
669ac9da0e0SDiego Santa Cruz 			printf("GP%i partition not HC WP group size "
670ac9da0e0SDiego Santa Cruz 			       "aligned\n", pidx+1);
671ac9da0e0SDiego Santa Cruz 			return -EINVAL;
672ac9da0e0SDiego Santa Cruz 		}
673ac9da0e0SDiego Santa Cruz 		gp_size_mult[pidx] = conf->gp_part[pidx].size / mmc->hc_wp_grp_size;
674ac9da0e0SDiego Santa Cruz 		if (conf->gp_part[pidx].size && conf->gp_part[pidx].enhanced) {
675ac9da0e0SDiego Santa Cruz 			part_attrs |= EXT_CSD_ENH_GP(pidx);
676ac9da0e0SDiego Santa Cruz 			tot_enh_size_mult += gp_size_mult[pidx];
677ac9da0e0SDiego Santa Cruz 		}
678ac9da0e0SDiego Santa Cruz 	}
679ac9da0e0SDiego Santa Cruz 
680ac9da0e0SDiego Santa Cruz 	if (part_attrs && ! (mmc->part_support & ENHNCD_SUPPORT)) {
681ac9da0e0SDiego Santa Cruz 		printf("Card does not support enhanced attribute\n");
682ac9da0e0SDiego Santa Cruz 		return -EMEDIUMTYPE;
683ac9da0e0SDiego Santa Cruz 	}
684ac9da0e0SDiego Santa Cruz 
685ac9da0e0SDiego Santa Cruz 	err = mmc_send_ext_csd(mmc, ext_csd);
686ac9da0e0SDiego Santa Cruz 	if (err)
687ac9da0e0SDiego Santa Cruz 		return err;
688ac9da0e0SDiego Santa Cruz 
689ac9da0e0SDiego Santa Cruz 	max_enh_size_mult =
690ac9da0e0SDiego Santa Cruz 		(ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT+2] << 16) +
691ac9da0e0SDiego Santa Cruz 		(ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT+1] << 8) +
692ac9da0e0SDiego Santa Cruz 		ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT];
693ac9da0e0SDiego Santa Cruz 	if (tot_enh_size_mult > max_enh_size_mult) {
694ac9da0e0SDiego Santa Cruz 		printf("Total enhanced size exceeds maximum (%u > %u)\n",
695ac9da0e0SDiego Santa Cruz 		       tot_enh_size_mult, max_enh_size_mult);
696ac9da0e0SDiego Santa Cruz 		return -EMEDIUMTYPE;
697ac9da0e0SDiego Santa Cruz 	}
698ac9da0e0SDiego Santa Cruz 
6998dda5b0eSDiego Santa Cruz 	/* The default value of EXT_CSD_WR_REL_SET is device
7008dda5b0eSDiego Santa Cruz 	 * dependent, the values can only be changed if the
7018dda5b0eSDiego Santa Cruz 	 * EXT_CSD_HS_CTRL_REL bit is set. The values can be
7028dda5b0eSDiego Santa Cruz 	 * changed only once and before partitioning is completed. */
7038dda5b0eSDiego Santa Cruz 	wr_rel_set = ext_csd[EXT_CSD_WR_REL_SET];
7048dda5b0eSDiego Santa Cruz 	if (conf->user.wr_rel_change) {
7058dda5b0eSDiego Santa Cruz 		if (conf->user.wr_rel_set)
7068dda5b0eSDiego Santa Cruz 			wr_rel_set |= EXT_CSD_WR_DATA_REL_USR;
7078dda5b0eSDiego Santa Cruz 		else
7088dda5b0eSDiego Santa Cruz 			wr_rel_set &= ~EXT_CSD_WR_DATA_REL_USR;
7098dda5b0eSDiego Santa Cruz 	}
7108dda5b0eSDiego Santa Cruz 	for (pidx = 0; pidx < 4; pidx++) {
7118dda5b0eSDiego Santa Cruz 		if (conf->gp_part[pidx].wr_rel_change) {
7128dda5b0eSDiego Santa Cruz 			if (conf->gp_part[pidx].wr_rel_set)
7138dda5b0eSDiego Santa Cruz 				wr_rel_set |= EXT_CSD_WR_DATA_REL_GP(pidx);
7148dda5b0eSDiego Santa Cruz 			else
7158dda5b0eSDiego Santa Cruz 				wr_rel_set &= ~EXT_CSD_WR_DATA_REL_GP(pidx);
7168dda5b0eSDiego Santa Cruz 		}
7178dda5b0eSDiego Santa Cruz 	}
7188dda5b0eSDiego Santa Cruz 
7198dda5b0eSDiego Santa Cruz 	if (wr_rel_set != ext_csd[EXT_CSD_WR_REL_SET] &&
7208dda5b0eSDiego Santa Cruz 	    !(ext_csd[EXT_CSD_WR_REL_PARAM] & EXT_CSD_HS_CTRL_REL)) {
7218dda5b0eSDiego Santa Cruz 		puts("Card does not support host controlled partition write "
7228dda5b0eSDiego Santa Cruz 		     "reliability settings\n");
7238dda5b0eSDiego Santa Cruz 		return -EMEDIUMTYPE;
7248dda5b0eSDiego Santa Cruz 	}
7258dda5b0eSDiego Santa Cruz 
726ac9da0e0SDiego Santa Cruz 	if (ext_csd[EXT_CSD_PARTITION_SETTING] &
727ac9da0e0SDiego Santa Cruz 	    EXT_CSD_PARTITION_SETTING_COMPLETED) {
728ac9da0e0SDiego Santa Cruz 		printf("Card already partitioned\n");
729ac9da0e0SDiego Santa Cruz 		return -EPERM;
730ac9da0e0SDiego Santa Cruz 	}
731ac9da0e0SDiego Santa Cruz 
732ac9da0e0SDiego Santa Cruz 	if (mode == MMC_HWPART_CONF_CHECK)
733ac9da0e0SDiego Santa Cruz 		return 0;
734ac9da0e0SDiego Santa Cruz 
735ac9da0e0SDiego Santa Cruz 	/* Partitioning requires high-capacity size definitions */
736ac9da0e0SDiego Santa Cruz 	if (!(ext_csd[EXT_CSD_ERASE_GROUP_DEF] & 0x01)) {
737ac9da0e0SDiego Santa Cruz 		err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
738ac9da0e0SDiego Santa Cruz 				 EXT_CSD_ERASE_GROUP_DEF, 1);
739ac9da0e0SDiego Santa Cruz 
740ac9da0e0SDiego Santa Cruz 		if (err)
741ac9da0e0SDiego Santa Cruz 			return err;
742ac9da0e0SDiego Santa Cruz 
743ac9da0e0SDiego Santa Cruz 		ext_csd[EXT_CSD_ERASE_GROUP_DEF] = 1;
744ac9da0e0SDiego Santa Cruz 
745ac9da0e0SDiego Santa Cruz 		/* update erase group size to be high-capacity */
746ac9da0e0SDiego Santa Cruz 		mmc->erase_grp_size =
747ac9da0e0SDiego Santa Cruz 			ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] * 1024;
748ac9da0e0SDiego Santa Cruz 
749ac9da0e0SDiego Santa Cruz 	}
750ac9da0e0SDiego Santa Cruz 
751ac9da0e0SDiego Santa Cruz 	/* all OK, write the configuration */
752ac9da0e0SDiego Santa Cruz 	for (i = 0; i < 4; i++) {
753ac9da0e0SDiego Santa Cruz 		err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
754ac9da0e0SDiego Santa Cruz 				 EXT_CSD_ENH_START_ADDR+i,
755ac9da0e0SDiego Santa Cruz 				 (enh_start_addr >> (i*8)) & 0xFF);
756ac9da0e0SDiego Santa Cruz 		if (err)
757ac9da0e0SDiego Santa Cruz 			return err;
758ac9da0e0SDiego Santa Cruz 	}
759ac9da0e0SDiego Santa Cruz 	for (i = 0; i < 3; i++) {
760ac9da0e0SDiego Santa Cruz 		err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
761ac9da0e0SDiego Santa Cruz 				 EXT_CSD_ENH_SIZE_MULT+i,
762ac9da0e0SDiego Santa Cruz 				 (enh_size_mult >> (i*8)) & 0xFF);
763ac9da0e0SDiego Santa Cruz 		if (err)
764ac9da0e0SDiego Santa Cruz 			return err;
765ac9da0e0SDiego Santa Cruz 	}
766ac9da0e0SDiego Santa Cruz 	for (pidx = 0; pidx < 4; pidx++) {
767ac9da0e0SDiego Santa Cruz 		for (i = 0; i < 3; i++) {
768ac9da0e0SDiego Santa Cruz 			err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
769ac9da0e0SDiego Santa Cruz 					 EXT_CSD_GP_SIZE_MULT+pidx*3+i,
770ac9da0e0SDiego Santa Cruz 					 (gp_size_mult[pidx] >> (i*8)) & 0xFF);
771ac9da0e0SDiego Santa Cruz 			if (err)
772ac9da0e0SDiego Santa Cruz 				return err;
773ac9da0e0SDiego Santa Cruz 		}
774ac9da0e0SDiego Santa Cruz 	}
775ac9da0e0SDiego Santa Cruz 	err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
776ac9da0e0SDiego Santa Cruz 			 EXT_CSD_PARTITIONS_ATTRIBUTE, part_attrs);
777ac9da0e0SDiego Santa Cruz 	if (err)
778ac9da0e0SDiego Santa Cruz 		return err;
779ac9da0e0SDiego Santa Cruz 
780ac9da0e0SDiego Santa Cruz 	if (mode == MMC_HWPART_CONF_SET)
781ac9da0e0SDiego Santa Cruz 		return 0;
782ac9da0e0SDiego Santa Cruz 
7838dda5b0eSDiego Santa Cruz 	/* The WR_REL_SET is a write-once register but shall be
7848dda5b0eSDiego Santa Cruz 	 * written before setting PART_SETTING_COMPLETED. As it is
7858dda5b0eSDiego Santa Cruz 	 * write-once we can only write it when completing the
7868dda5b0eSDiego Santa Cruz 	 * partitioning. */
7878dda5b0eSDiego Santa Cruz 	if (wr_rel_set != ext_csd[EXT_CSD_WR_REL_SET]) {
7888dda5b0eSDiego Santa Cruz 		err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
7898dda5b0eSDiego Santa Cruz 				 EXT_CSD_WR_REL_SET, wr_rel_set);
7908dda5b0eSDiego Santa Cruz 		if (err)
7918dda5b0eSDiego Santa Cruz 			return err;
7928dda5b0eSDiego Santa Cruz 	}
7938dda5b0eSDiego Santa Cruz 
794ac9da0e0SDiego Santa Cruz 	/* Setting PART_SETTING_COMPLETED confirms the partition
795ac9da0e0SDiego Santa Cruz 	 * configuration but it only becomes effective after power
796ac9da0e0SDiego Santa Cruz 	 * cycle, so we do not adjust the partition related settings
797ac9da0e0SDiego Santa Cruz 	 * in the mmc struct. */
798ac9da0e0SDiego Santa Cruz 
799ac9da0e0SDiego Santa Cruz 	err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
800ac9da0e0SDiego Santa Cruz 			 EXT_CSD_PARTITION_SETTING,
801ac9da0e0SDiego Santa Cruz 			 EXT_CSD_PARTITION_SETTING_COMPLETED);
802ac9da0e0SDiego Santa Cruz 	if (err)
803ac9da0e0SDiego Santa Cruz 		return err;
804ac9da0e0SDiego Santa Cruz 
805ac9da0e0SDiego Santa Cruz 	return 0;
806ac9da0e0SDiego Santa Cruz }
807ac9da0e0SDiego Santa Cruz 
8088ca51e51SSimon Glass #ifndef CONFIG_DM_MMC_OPS
80948972d90SThierry Reding int mmc_getcd(struct mmc *mmc)
81048972d90SThierry Reding {
81148972d90SThierry Reding 	int cd;
81248972d90SThierry Reding 
81348972d90SThierry Reding 	cd = board_mmc_getcd(mmc);
81448972d90SThierry Reding 
815d4e1da4eSPeter Korsgaard 	if (cd < 0) {
81693bfd616SPantelis Antoniou 		if (mmc->cfg->ops->getcd)
81793bfd616SPantelis Antoniou 			cd = mmc->cfg->ops->getcd(mmc);
818d4e1da4eSPeter Korsgaard 		else
819d4e1da4eSPeter Korsgaard 			cd = 1;
820d4e1da4eSPeter Korsgaard 	}
82148972d90SThierry Reding 
82248972d90SThierry Reding 	return cd;
82348972d90SThierry Reding }
8248ca51e51SSimon Glass #endif
82548972d90SThierry Reding 
826fdbb873eSKim Phillips static int sd_switch(struct mmc *mmc, int mode, int group, u8 value, u8 *resp)
827272cc70bSAndy Fleming {
828272cc70bSAndy Fleming 	struct mmc_cmd cmd;
829272cc70bSAndy Fleming 	struct mmc_data data;
830272cc70bSAndy Fleming 
831272cc70bSAndy Fleming 	/* Switch the frequency */
832272cc70bSAndy Fleming 	cmd.cmdidx = SD_CMD_SWITCH_FUNC;
833272cc70bSAndy Fleming 	cmd.resp_type = MMC_RSP_R1;
834272cc70bSAndy Fleming 	cmd.cmdarg = (mode << 31) | 0xffffff;
835272cc70bSAndy Fleming 	cmd.cmdarg &= ~(0xf << (group * 4));
836272cc70bSAndy Fleming 	cmd.cmdarg |= value << (group * 4);
837272cc70bSAndy Fleming 
838272cc70bSAndy Fleming 	data.dest = (char *)resp;
839272cc70bSAndy Fleming 	data.blocksize = 64;
840272cc70bSAndy Fleming 	data.blocks = 1;
841272cc70bSAndy Fleming 	data.flags = MMC_DATA_READ;
842272cc70bSAndy Fleming 
843272cc70bSAndy Fleming 	return mmc_send_cmd(mmc, &cmd, &data);
844272cc70bSAndy Fleming }
845272cc70bSAndy Fleming 
846272cc70bSAndy Fleming 
847fdbb873eSKim Phillips static int sd_change_freq(struct mmc *mmc)
848272cc70bSAndy Fleming {
849272cc70bSAndy Fleming 	int err;
850272cc70bSAndy Fleming 	struct mmc_cmd cmd;
851f781dd38SAnton staaf 	ALLOC_CACHE_ALIGN_BUFFER(uint, scr, 2);
852f781dd38SAnton staaf 	ALLOC_CACHE_ALIGN_BUFFER(uint, switch_status, 16);
853272cc70bSAndy Fleming 	struct mmc_data data;
854272cc70bSAndy Fleming 	int timeout;
855272cc70bSAndy Fleming 
856272cc70bSAndy Fleming 	mmc->card_caps = 0;
857272cc70bSAndy Fleming 
858d52ebf10SThomas Chou 	if (mmc_host_is_spi(mmc))
859d52ebf10SThomas Chou 		return 0;
860d52ebf10SThomas Chou 
861272cc70bSAndy Fleming 	/* Read the SCR to find out if this card supports higher speeds */
862272cc70bSAndy Fleming 	cmd.cmdidx = MMC_CMD_APP_CMD;
863272cc70bSAndy Fleming 	cmd.resp_type = MMC_RSP_R1;
864272cc70bSAndy Fleming 	cmd.cmdarg = mmc->rca << 16;
865272cc70bSAndy Fleming 
866272cc70bSAndy Fleming 	err = mmc_send_cmd(mmc, &cmd, NULL);
867272cc70bSAndy Fleming 
868272cc70bSAndy Fleming 	if (err)
869272cc70bSAndy Fleming 		return err;
870272cc70bSAndy Fleming 
871272cc70bSAndy Fleming 	cmd.cmdidx = SD_CMD_APP_SEND_SCR;
872272cc70bSAndy Fleming 	cmd.resp_type = MMC_RSP_R1;
873272cc70bSAndy Fleming 	cmd.cmdarg = 0;
874272cc70bSAndy Fleming 
875272cc70bSAndy Fleming 	timeout = 3;
876272cc70bSAndy Fleming 
877272cc70bSAndy Fleming retry_scr:
878f781dd38SAnton staaf 	data.dest = (char *)scr;
879272cc70bSAndy Fleming 	data.blocksize = 8;
880272cc70bSAndy Fleming 	data.blocks = 1;
881272cc70bSAndy Fleming 	data.flags = MMC_DATA_READ;
882272cc70bSAndy Fleming 
883272cc70bSAndy Fleming 	err = mmc_send_cmd(mmc, &cmd, &data);
884272cc70bSAndy Fleming 
885272cc70bSAndy Fleming 	if (err) {
886272cc70bSAndy Fleming 		if (timeout--)
887272cc70bSAndy Fleming 			goto retry_scr;
888272cc70bSAndy Fleming 
889272cc70bSAndy Fleming 		return err;
890272cc70bSAndy Fleming 	}
891272cc70bSAndy Fleming 
8924e3d89baSYauhen Kharuzhy 	mmc->scr[0] = __be32_to_cpu(scr[0]);
8934e3d89baSYauhen Kharuzhy 	mmc->scr[1] = __be32_to_cpu(scr[1]);
894272cc70bSAndy Fleming 
895272cc70bSAndy Fleming 	switch ((mmc->scr[0] >> 24) & 0xf) {
896272cc70bSAndy Fleming 	case 0:
897272cc70bSAndy Fleming 		mmc->version = SD_VERSION_1_0;
898272cc70bSAndy Fleming 		break;
899272cc70bSAndy Fleming 	case 1:
900272cc70bSAndy Fleming 		mmc->version = SD_VERSION_1_10;
901272cc70bSAndy Fleming 		break;
902272cc70bSAndy Fleming 	case 2:
903272cc70bSAndy Fleming 		mmc->version = SD_VERSION_2;
9041741c64dSJaehoon Chung 		if ((mmc->scr[0] >> 15) & 0x1)
9051741c64dSJaehoon Chung 			mmc->version = SD_VERSION_3;
906272cc70bSAndy Fleming 		break;
907272cc70bSAndy Fleming 	default:
908272cc70bSAndy Fleming 		mmc->version = SD_VERSION_1_0;
909272cc70bSAndy Fleming 		break;
910272cc70bSAndy Fleming 	}
911272cc70bSAndy Fleming 
912b44c7083SAlagu Sankar 	if (mmc->scr[0] & SD_DATA_4BIT)
913b44c7083SAlagu Sankar 		mmc->card_caps |= MMC_MODE_4BIT;
914b44c7083SAlagu Sankar 
915272cc70bSAndy Fleming 	/* Version 1.0 doesn't support switching */
916272cc70bSAndy Fleming 	if (mmc->version == SD_VERSION_1_0)
917272cc70bSAndy Fleming 		return 0;
918272cc70bSAndy Fleming 
919272cc70bSAndy Fleming 	timeout = 4;
920272cc70bSAndy Fleming 	while (timeout--) {
921272cc70bSAndy Fleming 		err = sd_switch(mmc, SD_SWITCH_CHECK, 0, 1,
922f781dd38SAnton staaf 				(u8 *)switch_status);
923272cc70bSAndy Fleming 
924272cc70bSAndy Fleming 		if (err)
925272cc70bSAndy Fleming 			return err;
926272cc70bSAndy Fleming 
927272cc70bSAndy Fleming 		/* The high-speed function is busy.  Try again */
9284e3d89baSYauhen Kharuzhy 		if (!(__be32_to_cpu(switch_status[7]) & SD_HIGHSPEED_BUSY))
929272cc70bSAndy Fleming 			break;
930272cc70bSAndy Fleming 	}
931272cc70bSAndy Fleming 
932272cc70bSAndy Fleming 	/* If high-speed isn't supported, we return */
9334e3d89baSYauhen Kharuzhy 	if (!(__be32_to_cpu(switch_status[3]) & SD_HIGHSPEED_SUPPORTED))
934272cc70bSAndy Fleming 		return 0;
935272cc70bSAndy Fleming 
9362c3fbf4cSMacpaul Lin 	/*
9372c3fbf4cSMacpaul Lin 	 * If the host doesn't support SD_HIGHSPEED, do not switch card to
9382c3fbf4cSMacpaul Lin 	 * HIGHSPEED mode even if the card support SD_HIGHSPPED.
9392c3fbf4cSMacpaul Lin 	 * This can avoid furthur problem when the card runs in different
9402c3fbf4cSMacpaul Lin 	 * mode between the host.
9412c3fbf4cSMacpaul Lin 	 */
94293bfd616SPantelis Antoniou 	if (!((mmc->cfg->host_caps & MMC_MODE_HS_52MHz) &&
94393bfd616SPantelis Antoniou 		(mmc->cfg->host_caps & MMC_MODE_HS)))
9442c3fbf4cSMacpaul Lin 		return 0;
9452c3fbf4cSMacpaul Lin 
946f781dd38SAnton staaf 	err = sd_switch(mmc, SD_SWITCH_SWITCH, 0, 1, (u8 *)switch_status);
947272cc70bSAndy Fleming 
948272cc70bSAndy Fleming 	if (err)
949272cc70bSAndy Fleming 		return err;
950272cc70bSAndy Fleming 
9514e3d89baSYauhen Kharuzhy 	if ((__be32_to_cpu(switch_status[4]) & 0x0f000000) == 0x01000000)
952272cc70bSAndy Fleming 		mmc->card_caps |= MMC_MODE_HS;
953272cc70bSAndy Fleming 
954272cc70bSAndy Fleming 	return 0;
955272cc70bSAndy Fleming }
956272cc70bSAndy Fleming 
9573697e599SPeng Fan static int sd_read_ssr(struct mmc *mmc)
9583697e599SPeng Fan {
9593697e599SPeng Fan 	int err, i;
9603697e599SPeng Fan 	struct mmc_cmd cmd;
9613697e599SPeng Fan 	ALLOC_CACHE_ALIGN_BUFFER(uint, ssr, 16);
9623697e599SPeng Fan 	struct mmc_data data;
9633697e599SPeng Fan 	int timeout = 3;
9643697e599SPeng Fan 	unsigned int au, eo, et, es;
9653697e599SPeng Fan 
9663697e599SPeng Fan 	cmd.cmdidx = MMC_CMD_APP_CMD;
9673697e599SPeng Fan 	cmd.resp_type = MMC_RSP_R1;
9683697e599SPeng Fan 	cmd.cmdarg = mmc->rca << 16;
9693697e599SPeng Fan 
9703697e599SPeng Fan 	err = mmc_send_cmd(mmc, &cmd, NULL);
9713697e599SPeng Fan 	if (err)
9723697e599SPeng Fan 		return err;
9733697e599SPeng Fan 
9743697e599SPeng Fan 	cmd.cmdidx = SD_CMD_APP_SD_STATUS;
9753697e599SPeng Fan 	cmd.resp_type = MMC_RSP_R1;
9763697e599SPeng Fan 	cmd.cmdarg = 0;
9773697e599SPeng Fan 
9783697e599SPeng Fan retry_ssr:
9793697e599SPeng Fan 	data.dest = (char *)ssr;
9803697e599SPeng Fan 	data.blocksize = 64;
9813697e599SPeng Fan 	data.blocks = 1;
9823697e599SPeng Fan 	data.flags = MMC_DATA_READ;
9833697e599SPeng Fan 
9843697e599SPeng Fan 	err = mmc_send_cmd(mmc, &cmd, &data);
9853697e599SPeng Fan 	if (err) {
9863697e599SPeng Fan 		if (timeout--)
9873697e599SPeng Fan 			goto retry_ssr;
9883697e599SPeng Fan 
9893697e599SPeng Fan 		return err;
9903697e599SPeng Fan 	}
9913697e599SPeng Fan 
9923697e599SPeng Fan 	for (i = 0; i < 16; i++)
9933697e599SPeng Fan 		ssr[i] = be32_to_cpu(ssr[i]);
9943697e599SPeng Fan 
9953697e599SPeng Fan 	au = (ssr[2] >> 12) & 0xF;
9963697e599SPeng Fan 	if ((au <= 9) || (mmc->version == SD_VERSION_3)) {
9973697e599SPeng Fan 		mmc->ssr.au = sd_au_size[au];
9983697e599SPeng Fan 		es = (ssr[3] >> 24) & 0xFF;
9993697e599SPeng Fan 		es |= (ssr[2] & 0xFF) << 8;
10003697e599SPeng Fan 		et = (ssr[3] >> 18) & 0x3F;
10013697e599SPeng Fan 		if (es && et) {
10023697e599SPeng Fan 			eo = (ssr[3] >> 16) & 0x3;
10033697e599SPeng Fan 			mmc->ssr.erase_timeout = (et * 1000) / es;
10043697e599SPeng Fan 			mmc->ssr.erase_offset = eo * 1000;
10053697e599SPeng Fan 		}
10063697e599SPeng Fan 	} else {
10073697e599SPeng Fan 		debug("Invalid Allocation Unit Size.\n");
10083697e599SPeng Fan 	}
10093697e599SPeng Fan 
10103697e599SPeng Fan 	return 0;
10113697e599SPeng Fan }
10123697e599SPeng Fan 
1013272cc70bSAndy Fleming /* frequency bases */
1014272cc70bSAndy Fleming /* divided by 10 to be nice to platforms without floating point */
10155f837c2cSMike Frysinger static const int fbase[] = {
1016272cc70bSAndy Fleming 	10000,
1017272cc70bSAndy Fleming 	100000,
1018272cc70bSAndy Fleming 	1000000,
1019272cc70bSAndy Fleming 	10000000,
1020272cc70bSAndy Fleming };
1021272cc70bSAndy Fleming 
1022272cc70bSAndy Fleming /* Multiplier values for TRAN_SPEED.  Multiplied by 10 to be nice
1023272cc70bSAndy Fleming  * to platforms without floating point.
1024272cc70bSAndy Fleming  */
102561fe076fSSimon Glass static const u8 multipliers[] = {
1026272cc70bSAndy Fleming 	0,	/* reserved */
1027272cc70bSAndy Fleming 	10,
1028272cc70bSAndy Fleming 	12,
1029272cc70bSAndy Fleming 	13,
1030272cc70bSAndy Fleming 	15,
1031272cc70bSAndy Fleming 	20,
1032272cc70bSAndy Fleming 	25,
1033272cc70bSAndy Fleming 	30,
1034272cc70bSAndy Fleming 	35,
1035272cc70bSAndy Fleming 	40,
1036272cc70bSAndy Fleming 	45,
1037272cc70bSAndy Fleming 	50,
1038272cc70bSAndy Fleming 	55,
1039272cc70bSAndy Fleming 	60,
1040272cc70bSAndy Fleming 	70,
1041272cc70bSAndy Fleming 	80,
1042272cc70bSAndy Fleming };
1043272cc70bSAndy Fleming 
10448ca51e51SSimon Glass #ifndef CONFIG_DM_MMC_OPS
1045fdbb873eSKim Phillips static void mmc_set_ios(struct mmc *mmc)
1046272cc70bSAndy Fleming {
104793bfd616SPantelis Antoniou 	if (mmc->cfg->ops->set_ios)
104893bfd616SPantelis Antoniou 		mmc->cfg->ops->set_ios(mmc);
1049272cc70bSAndy Fleming }
10508ca51e51SSimon Glass #endif
1051272cc70bSAndy Fleming 
1052272cc70bSAndy Fleming void mmc_set_clock(struct mmc *mmc, uint clock)
1053272cc70bSAndy Fleming {
105493bfd616SPantelis Antoniou 	if (clock > mmc->cfg->f_max)
105593bfd616SPantelis Antoniou 		clock = mmc->cfg->f_max;
1056272cc70bSAndy Fleming 
105793bfd616SPantelis Antoniou 	if (clock < mmc->cfg->f_min)
105893bfd616SPantelis Antoniou 		clock = mmc->cfg->f_min;
1059272cc70bSAndy Fleming 
1060272cc70bSAndy Fleming 	mmc->clock = clock;
1061272cc70bSAndy Fleming 
1062272cc70bSAndy Fleming 	mmc_set_ios(mmc);
1063272cc70bSAndy Fleming }
1064272cc70bSAndy Fleming 
1065fdbb873eSKim Phillips static void mmc_set_bus_width(struct mmc *mmc, uint width)
1066272cc70bSAndy Fleming {
1067272cc70bSAndy Fleming 	mmc->bus_width = width;
1068272cc70bSAndy Fleming 
1069272cc70bSAndy Fleming 	mmc_set_ios(mmc);
1070272cc70bSAndy Fleming }
1071272cc70bSAndy Fleming 
1072fdbb873eSKim Phillips static int mmc_startup(struct mmc *mmc)
1073272cc70bSAndy Fleming {
1074f866a46dSStephen Warren 	int err, i;
1075272cc70bSAndy Fleming 	uint mult, freq;
1076639b7827SYoshihiro Shimoda 	u64 cmult, csize, capacity;
1077272cc70bSAndy Fleming 	struct mmc_cmd cmd;
10788bfa195eSSimon Glass 	ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
10798bfa195eSSimon Glass 	ALLOC_CACHE_ALIGN_BUFFER(u8, test_csd, MMC_MAX_BLOCK_LEN);
10805d4fc8d9SRaffaele Recalcati 	int timeout = 1000;
10810c453bb7SDiego Santa Cruz 	bool has_parts = false;
10828a0cf490SDiego Santa Cruz 	bool part_completed;
1083c40fdca6SSimon Glass 	struct blk_desc *bdesc;
1084272cc70bSAndy Fleming 
1085d52ebf10SThomas Chou #ifdef CONFIG_MMC_SPI_CRC_ON
1086d52ebf10SThomas Chou 	if (mmc_host_is_spi(mmc)) { /* enable CRC check for spi */
1087d52ebf10SThomas Chou 		cmd.cmdidx = MMC_CMD_SPI_CRC_ON_OFF;
1088d52ebf10SThomas Chou 		cmd.resp_type = MMC_RSP_R1;
1089d52ebf10SThomas Chou 		cmd.cmdarg = 1;
1090d52ebf10SThomas Chou 		err = mmc_send_cmd(mmc, &cmd, NULL);
1091d52ebf10SThomas Chou 
1092d52ebf10SThomas Chou 		if (err)
1093d52ebf10SThomas Chou 			return err;
1094d52ebf10SThomas Chou 	}
1095d52ebf10SThomas Chou #endif
1096d52ebf10SThomas Chou 
1097272cc70bSAndy Fleming 	/* Put the Card in Identify Mode */
1098d52ebf10SThomas Chou 	cmd.cmdidx = mmc_host_is_spi(mmc) ? MMC_CMD_SEND_CID :
1099d52ebf10SThomas Chou 		MMC_CMD_ALL_SEND_CID; /* cmd not supported in spi */
1100272cc70bSAndy Fleming 	cmd.resp_type = MMC_RSP_R2;
1101272cc70bSAndy Fleming 	cmd.cmdarg = 0;
1102272cc70bSAndy Fleming 
1103272cc70bSAndy Fleming 	err = mmc_send_cmd(mmc, &cmd, NULL);
1104272cc70bSAndy Fleming 
1105272cc70bSAndy Fleming 	if (err)
1106272cc70bSAndy Fleming 		return err;
1107272cc70bSAndy Fleming 
1108272cc70bSAndy Fleming 	memcpy(mmc->cid, cmd.response, 16);
1109272cc70bSAndy Fleming 
1110272cc70bSAndy Fleming 	/*
1111272cc70bSAndy Fleming 	 * For MMC cards, set the Relative Address.
1112272cc70bSAndy Fleming 	 * For SD cards, get the Relatvie Address.
1113272cc70bSAndy Fleming 	 * This also puts the cards into Standby State
1114272cc70bSAndy Fleming 	 */
1115d52ebf10SThomas Chou 	if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
1116272cc70bSAndy Fleming 		cmd.cmdidx = SD_CMD_SEND_RELATIVE_ADDR;
1117272cc70bSAndy Fleming 		cmd.cmdarg = mmc->rca << 16;
1118272cc70bSAndy Fleming 		cmd.resp_type = MMC_RSP_R6;
1119272cc70bSAndy Fleming 
1120272cc70bSAndy Fleming 		err = mmc_send_cmd(mmc, &cmd, NULL);
1121272cc70bSAndy Fleming 
1122272cc70bSAndy Fleming 		if (err)
1123272cc70bSAndy Fleming 			return err;
1124272cc70bSAndy Fleming 
1125272cc70bSAndy Fleming 		if (IS_SD(mmc))
1126998be3ddSRabin Vincent 			mmc->rca = (cmd.response[0] >> 16) & 0xffff;
1127d52ebf10SThomas Chou 	}
1128272cc70bSAndy Fleming 
1129272cc70bSAndy Fleming 	/* Get the Card-Specific Data */
1130272cc70bSAndy Fleming 	cmd.cmdidx = MMC_CMD_SEND_CSD;
1131272cc70bSAndy Fleming 	cmd.resp_type = MMC_RSP_R2;
1132272cc70bSAndy Fleming 	cmd.cmdarg = mmc->rca << 16;
1133272cc70bSAndy Fleming 
1134272cc70bSAndy Fleming 	err = mmc_send_cmd(mmc, &cmd, NULL);
1135272cc70bSAndy Fleming 
11365d4fc8d9SRaffaele Recalcati 	/* Waiting for the ready status */
11375d4fc8d9SRaffaele Recalcati 	mmc_send_status(mmc, timeout);
11385d4fc8d9SRaffaele Recalcati 
1139272cc70bSAndy Fleming 	if (err)
1140272cc70bSAndy Fleming 		return err;
1141272cc70bSAndy Fleming 
1142998be3ddSRabin Vincent 	mmc->csd[0] = cmd.response[0];
1143998be3ddSRabin Vincent 	mmc->csd[1] = cmd.response[1];
1144998be3ddSRabin Vincent 	mmc->csd[2] = cmd.response[2];
1145998be3ddSRabin Vincent 	mmc->csd[3] = cmd.response[3];
1146272cc70bSAndy Fleming 
1147272cc70bSAndy Fleming 	if (mmc->version == MMC_VERSION_UNKNOWN) {
11480b453ffeSRabin Vincent 		int version = (cmd.response[0] >> 26) & 0xf;
1149272cc70bSAndy Fleming 
1150272cc70bSAndy Fleming 		switch (version) {
1151272cc70bSAndy Fleming 		case 0:
1152272cc70bSAndy Fleming 			mmc->version = MMC_VERSION_1_2;
1153272cc70bSAndy Fleming 			break;
1154272cc70bSAndy Fleming 		case 1:
1155272cc70bSAndy Fleming 			mmc->version = MMC_VERSION_1_4;
1156272cc70bSAndy Fleming 			break;
1157272cc70bSAndy Fleming 		case 2:
1158272cc70bSAndy Fleming 			mmc->version = MMC_VERSION_2_2;
1159272cc70bSAndy Fleming 			break;
1160272cc70bSAndy Fleming 		case 3:
1161272cc70bSAndy Fleming 			mmc->version = MMC_VERSION_3;
1162272cc70bSAndy Fleming 			break;
1163272cc70bSAndy Fleming 		case 4:
1164272cc70bSAndy Fleming 			mmc->version = MMC_VERSION_4;
1165272cc70bSAndy Fleming 			break;
1166272cc70bSAndy Fleming 		default:
1167272cc70bSAndy Fleming 			mmc->version = MMC_VERSION_1_2;
1168272cc70bSAndy Fleming 			break;
1169272cc70bSAndy Fleming 		}
1170272cc70bSAndy Fleming 	}
1171272cc70bSAndy Fleming 
1172272cc70bSAndy Fleming 	/* divide frequency by 10, since the mults are 10x bigger */
11730b453ffeSRabin Vincent 	freq = fbase[(cmd.response[0] & 0x7)];
11740b453ffeSRabin Vincent 	mult = multipliers[((cmd.response[0] >> 3) & 0xf)];
1175272cc70bSAndy Fleming 
1176272cc70bSAndy Fleming 	mmc->tran_speed = freq * mult;
1177272cc70bSAndy Fleming 
1178ab71188cSMarkus Niebel 	mmc->dsr_imp = ((cmd.response[1] >> 12) & 0x1);
1179998be3ddSRabin Vincent 	mmc->read_bl_len = 1 << ((cmd.response[1] >> 16) & 0xf);
1180272cc70bSAndy Fleming 
1181272cc70bSAndy Fleming 	if (IS_SD(mmc))
1182272cc70bSAndy Fleming 		mmc->write_bl_len = mmc->read_bl_len;
1183272cc70bSAndy Fleming 	else
1184998be3ddSRabin Vincent 		mmc->write_bl_len = 1 << ((cmd.response[3] >> 22) & 0xf);
1185272cc70bSAndy Fleming 
1186272cc70bSAndy Fleming 	if (mmc->high_capacity) {
1187272cc70bSAndy Fleming 		csize = (mmc->csd[1] & 0x3f) << 16
1188272cc70bSAndy Fleming 			| (mmc->csd[2] & 0xffff0000) >> 16;
1189272cc70bSAndy Fleming 		cmult = 8;
1190272cc70bSAndy Fleming 	} else {
1191272cc70bSAndy Fleming 		csize = (mmc->csd[1] & 0x3ff) << 2
1192272cc70bSAndy Fleming 			| (mmc->csd[2] & 0xc0000000) >> 30;
1193272cc70bSAndy Fleming 		cmult = (mmc->csd[2] & 0x00038000) >> 15;
1194272cc70bSAndy Fleming 	}
1195272cc70bSAndy Fleming 
1196f866a46dSStephen Warren 	mmc->capacity_user = (csize + 1) << (cmult + 2);
1197f866a46dSStephen Warren 	mmc->capacity_user *= mmc->read_bl_len;
1198f866a46dSStephen Warren 	mmc->capacity_boot = 0;
1199f866a46dSStephen Warren 	mmc->capacity_rpmb = 0;
1200f866a46dSStephen Warren 	for (i = 0; i < 4; i++)
1201f866a46dSStephen Warren 		mmc->capacity_gp[i] = 0;
1202272cc70bSAndy Fleming 
12038bfa195eSSimon Glass 	if (mmc->read_bl_len > MMC_MAX_BLOCK_LEN)
12048bfa195eSSimon Glass 		mmc->read_bl_len = MMC_MAX_BLOCK_LEN;
1205272cc70bSAndy Fleming 
12068bfa195eSSimon Glass 	if (mmc->write_bl_len > MMC_MAX_BLOCK_LEN)
12078bfa195eSSimon Glass 		mmc->write_bl_len = MMC_MAX_BLOCK_LEN;
1208272cc70bSAndy Fleming 
1209ab71188cSMarkus Niebel 	if ((mmc->dsr_imp) && (0xffffffff != mmc->dsr)) {
1210ab71188cSMarkus Niebel 		cmd.cmdidx = MMC_CMD_SET_DSR;
1211ab71188cSMarkus Niebel 		cmd.cmdarg = (mmc->dsr & 0xffff) << 16;
1212ab71188cSMarkus Niebel 		cmd.resp_type = MMC_RSP_NONE;
1213ab71188cSMarkus Niebel 		if (mmc_send_cmd(mmc, &cmd, NULL))
1214ab71188cSMarkus Niebel 			printf("MMC: SET_DSR failed\n");
1215ab71188cSMarkus Niebel 	}
1216ab71188cSMarkus Niebel 
1217272cc70bSAndy Fleming 	/* Select the card, and put it into Transfer Mode */
1218d52ebf10SThomas Chou 	if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
1219272cc70bSAndy Fleming 		cmd.cmdidx = MMC_CMD_SELECT_CARD;
1220fe8f7066SAjay Bhargav 		cmd.resp_type = MMC_RSP_R1;
1221272cc70bSAndy Fleming 		cmd.cmdarg = mmc->rca << 16;
1222272cc70bSAndy Fleming 		err = mmc_send_cmd(mmc, &cmd, NULL);
1223272cc70bSAndy Fleming 
1224272cc70bSAndy Fleming 		if (err)
1225272cc70bSAndy Fleming 			return err;
1226d52ebf10SThomas Chou 	}
1227272cc70bSAndy Fleming 
1228e6f99a56SLei Wen 	/*
1229e6f99a56SLei Wen 	 * For SD, its erase group is always one sector
1230e6f99a56SLei Wen 	 */
1231e6f99a56SLei Wen 	mmc->erase_grp_size = 1;
1232bc897b1dSLei Wen 	mmc->part_config = MMCPART_NOAVAILABLE;
1233d23e2c09SSukumar Ghorai 	if (!IS_SD(mmc) && (mmc->version >= MMC_VERSION_4)) {
1234d23e2c09SSukumar Ghorai 		/* check  ext_csd version and capacity */
1235d23e2c09SSukumar Ghorai 		err = mmc_send_ext_csd(mmc, ext_csd);
12369cf199ebSDiego Santa Cruz 		if (err)
12379cf199ebSDiego Santa Cruz 			return err;
12389cf199ebSDiego Santa Cruz 		if (ext_csd[EXT_CSD_REV] >= 2) {
1239639b7827SYoshihiro Shimoda 			/*
1240639b7827SYoshihiro Shimoda 			 * According to the JEDEC Standard, the value of
1241639b7827SYoshihiro Shimoda 			 * ext_csd's capacity is valid if the value is more
1242639b7827SYoshihiro Shimoda 			 * than 2GB
1243639b7827SYoshihiro Shimoda 			 */
12440560db18SLei Wen 			capacity = ext_csd[EXT_CSD_SEC_CNT] << 0
12450560db18SLei Wen 					| ext_csd[EXT_CSD_SEC_CNT + 1] << 8
12460560db18SLei Wen 					| ext_csd[EXT_CSD_SEC_CNT + 2] << 16
12470560db18SLei Wen 					| ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
12488bfa195eSSimon Glass 			capacity *= MMC_MAX_BLOCK_LEN;
1249b1f1e821SŁukasz Majewski 			if ((capacity >> 20) > 2 * 1024)
1250f866a46dSStephen Warren 				mmc->capacity_user = capacity;
1251d23e2c09SSukumar Ghorai 		}
1252bc897b1dSLei Wen 
125364f4a619SJaehoon Chung 		switch (ext_csd[EXT_CSD_REV]) {
125464f4a619SJaehoon Chung 		case 1:
125564f4a619SJaehoon Chung 			mmc->version = MMC_VERSION_4_1;
125664f4a619SJaehoon Chung 			break;
125764f4a619SJaehoon Chung 		case 2:
125864f4a619SJaehoon Chung 			mmc->version = MMC_VERSION_4_2;
125964f4a619SJaehoon Chung 			break;
126064f4a619SJaehoon Chung 		case 3:
126164f4a619SJaehoon Chung 			mmc->version = MMC_VERSION_4_3;
126264f4a619SJaehoon Chung 			break;
126364f4a619SJaehoon Chung 		case 5:
126464f4a619SJaehoon Chung 			mmc->version = MMC_VERSION_4_41;
126564f4a619SJaehoon Chung 			break;
126664f4a619SJaehoon Chung 		case 6:
126764f4a619SJaehoon Chung 			mmc->version = MMC_VERSION_4_5;
126864f4a619SJaehoon Chung 			break;
1269edab723bSMarkus Niebel 		case 7:
1270edab723bSMarkus Niebel 			mmc->version = MMC_VERSION_5_0;
1271edab723bSMarkus Niebel 			break;
12721a3619cfSStefan Wahren 		case 8:
12731a3619cfSStefan Wahren 			mmc->version = MMC_VERSION_5_1;
12741a3619cfSStefan Wahren 			break;
127564f4a619SJaehoon Chung 		}
127664f4a619SJaehoon Chung 
12778a0cf490SDiego Santa Cruz 		/* The partition data may be non-zero but it is only
12788a0cf490SDiego Santa Cruz 		 * effective if PARTITION_SETTING_COMPLETED is set in
12798a0cf490SDiego Santa Cruz 		 * EXT_CSD, so ignore any data if this bit is not set,
12808a0cf490SDiego Santa Cruz 		 * except for enabling the high-capacity group size
12818a0cf490SDiego Santa Cruz 		 * definition (see below). */
12828a0cf490SDiego Santa Cruz 		part_completed = !!(ext_csd[EXT_CSD_PARTITION_SETTING] &
12838a0cf490SDiego Santa Cruz 				    EXT_CSD_PARTITION_SETTING_COMPLETED);
12848a0cf490SDiego Santa Cruz 
12850c453bb7SDiego Santa Cruz 		/* store the partition info of emmc */
12860c453bb7SDiego Santa Cruz 		mmc->part_support = ext_csd[EXT_CSD_PARTITIONING_SUPPORT];
12870c453bb7SDiego Santa Cruz 		if ((ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & PART_SUPPORT) ||
12880c453bb7SDiego Santa Cruz 		    ext_csd[EXT_CSD_BOOT_MULT])
12890c453bb7SDiego Santa Cruz 			mmc->part_config = ext_csd[EXT_CSD_PART_CONF];
12908a0cf490SDiego Santa Cruz 		if (part_completed &&
12918a0cf490SDiego Santa Cruz 		    (ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & ENHNCD_SUPPORT))
12920c453bb7SDiego Santa Cruz 			mmc->part_attr = ext_csd[EXT_CSD_PARTITIONS_ATTRIBUTE];
12930c453bb7SDiego Santa Cruz 
12940c453bb7SDiego Santa Cruz 		mmc->capacity_boot = ext_csd[EXT_CSD_BOOT_MULT] << 17;
12950c453bb7SDiego Santa Cruz 
12960c453bb7SDiego Santa Cruz 		mmc->capacity_rpmb = ext_csd[EXT_CSD_RPMB_MULT] << 17;
12970c453bb7SDiego Santa Cruz 
12980c453bb7SDiego Santa Cruz 		for (i = 0; i < 4; i++) {
12990c453bb7SDiego Santa Cruz 			int idx = EXT_CSD_GP_SIZE_MULT + i * 3;
13008a0cf490SDiego Santa Cruz 			uint mult = (ext_csd[idx + 2] << 16) +
13010c453bb7SDiego Santa Cruz 				(ext_csd[idx + 1] << 8) + ext_csd[idx];
13028a0cf490SDiego Santa Cruz 			if (mult)
13038a0cf490SDiego Santa Cruz 				has_parts = true;
13048a0cf490SDiego Santa Cruz 			if (!part_completed)
13058a0cf490SDiego Santa Cruz 				continue;
13068a0cf490SDiego Santa Cruz 			mmc->capacity_gp[i] = mult;
13070c453bb7SDiego Santa Cruz 			mmc->capacity_gp[i] *=
13080c453bb7SDiego Santa Cruz 				ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
13090c453bb7SDiego Santa Cruz 			mmc->capacity_gp[i] *= ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
1310f8e89d67SDiego Santa Cruz 			mmc->capacity_gp[i] <<= 19;
13110c453bb7SDiego Santa Cruz 		}
13120c453bb7SDiego Santa Cruz 
13138a0cf490SDiego Santa Cruz 		if (part_completed) {
1314a7f852b6SDiego Santa Cruz 			mmc->enh_user_size =
1315a7f852b6SDiego Santa Cruz 				(ext_csd[EXT_CSD_ENH_SIZE_MULT+2] << 16) +
1316a7f852b6SDiego Santa Cruz 				(ext_csd[EXT_CSD_ENH_SIZE_MULT+1] << 8) +
1317a7f852b6SDiego Santa Cruz 				ext_csd[EXT_CSD_ENH_SIZE_MULT];
1318a7f852b6SDiego Santa Cruz 			mmc->enh_user_size *= ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
1319a7f852b6SDiego Santa Cruz 			mmc->enh_user_size *= ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
1320a7f852b6SDiego Santa Cruz 			mmc->enh_user_size <<= 19;
1321a7f852b6SDiego Santa Cruz 			mmc->enh_user_start =
1322a7f852b6SDiego Santa Cruz 				(ext_csd[EXT_CSD_ENH_START_ADDR+3] << 24) +
1323a7f852b6SDiego Santa Cruz 				(ext_csd[EXT_CSD_ENH_START_ADDR+2] << 16) +
1324a7f852b6SDiego Santa Cruz 				(ext_csd[EXT_CSD_ENH_START_ADDR+1] << 8) +
1325a7f852b6SDiego Santa Cruz 				ext_csd[EXT_CSD_ENH_START_ADDR];
1326a7f852b6SDiego Santa Cruz 			if (mmc->high_capacity)
1327a7f852b6SDiego Santa Cruz 				mmc->enh_user_start <<= 9;
13288a0cf490SDiego Santa Cruz 		}
1329a7f852b6SDiego Santa Cruz 
1330e6f99a56SLei Wen 		/*
13311937e5aaSOliver Metz 		 * Host needs to enable ERASE_GRP_DEF bit if device is
13321937e5aaSOliver Metz 		 * partitioned. This bit will be lost every time after a reset
13331937e5aaSOliver Metz 		 * or power off. This will affect erase size.
1334e6f99a56SLei Wen 		 */
13358a0cf490SDiego Santa Cruz 		if (part_completed)
13360c453bb7SDiego Santa Cruz 			has_parts = true;
13371937e5aaSOliver Metz 		if ((ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & PART_SUPPORT) &&
13380c453bb7SDiego Santa Cruz 		    (ext_csd[EXT_CSD_PARTITIONS_ATTRIBUTE] & PART_ENH_ATTRIB))
13390c453bb7SDiego Santa Cruz 			has_parts = true;
13400c453bb7SDiego Santa Cruz 		if (has_parts) {
13411937e5aaSOliver Metz 			err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
13421937e5aaSOliver Metz 				EXT_CSD_ERASE_GROUP_DEF, 1);
13431937e5aaSOliver Metz 
13441937e5aaSOliver Metz 			if (err)
13451937e5aaSOliver Metz 				return err;
1346021a8055SHannes Petermaier 			else
1347021a8055SHannes Petermaier 				ext_csd[EXT_CSD_ERASE_GROUP_DEF] = 1;
1348037dc0abSDiego Santa Cruz 		}
13491937e5aaSOliver Metz 
1350037dc0abSDiego Santa Cruz 		if (ext_csd[EXT_CSD_ERASE_GROUP_DEF] & 0x01) {
13511937e5aaSOliver Metz 			/* Read out group size from ext_csd */
13520560db18SLei Wen 			mmc->erase_grp_size =
1353a4ff9f83SDiego Santa Cruz 				ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] * 1024;
1354d7b29129SMarkus Niebel 			/*
1355d7b29129SMarkus Niebel 			 * if high capacity and partition setting completed
1356d7b29129SMarkus Niebel 			 * SEC_COUNT is valid even if it is smaller than 2 GiB
1357d7b29129SMarkus Niebel 			 * JEDEC Standard JESD84-B45, 6.2.4
1358d7b29129SMarkus Niebel 			 */
13598a0cf490SDiego Santa Cruz 			if (mmc->high_capacity && part_completed) {
1360d7b29129SMarkus Niebel 				capacity = (ext_csd[EXT_CSD_SEC_CNT]) |
1361d7b29129SMarkus Niebel 					(ext_csd[EXT_CSD_SEC_CNT + 1] << 8) |
1362d7b29129SMarkus Niebel 					(ext_csd[EXT_CSD_SEC_CNT + 2] << 16) |
1363d7b29129SMarkus Niebel 					(ext_csd[EXT_CSD_SEC_CNT + 3] << 24);
1364d7b29129SMarkus Niebel 				capacity *= MMC_MAX_BLOCK_LEN;
1365d7b29129SMarkus Niebel 				mmc->capacity_user = capacity;
1366d7b29129SMarkus Niebel 			}
13678bfa195eSSimon Glass 		} else {
13681937e5aaSOliver Metz 			/* Calculate the group size from the csd value. */
1369e6f99a56SLei Wen 			int erase_gsz, erase_gmul;
1370e6f99a56SLei Wen 			erase_gsz = (mmc->csd[2] & 0x00007c00) >> 10;
1371e6f99a56SLei Wen 			erase_gmul = (mmc->csd[2] & 0x000003e0) >> 5;
1372e6f99a56SLei Wen 			mmc->erase_grp_size = (erase_gsz + 1)
1373e6f99a56SLei Wen 				* (erase_gmul + 1);
1374e6f99a56SLei Wen 		}
1375037dc0abSDiego Santa Cruz 
1376037dc0abSDiego Santa Cruz 		mmc->hc_wp_grp_size = 1024
1377037dc0abSDiego Santa Cruz 			* ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]
1378037dc0abSDiego Santa Cruz 			* ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
13799e41a00bSDiego Santa Cruz 
13809e41a00bSDiego Santa Cruz 		mmc->wr_rel_set = ext_csd[EXT_CSD_WR_REL_SET];
1381f866a46dSStephen Warren 	}
1382f866a46dSStephen Warren 
1383c40fdca6SSimon Glass 	err = mmc_set_capacity(mmc, mmc_get_blk_desc(mmc)->hwpart);
1384f866a46dSStephen Warren 	if (err)
1385f866a46dSStephen Warren 		return err;
1386d23e2c09SSukumar Ghorai 
1387272cc70bSAndy Fleming 	if (IS_SD(mmc))
1388272cc70bSAndy Fleming 		err = sd_change_freq(mmc);
1389272cc70bSAndy Fleming 	else
1390272cc70bSAndy Fleming 		err = mmc_change_freq(mmc);
1391272cc70bSAndy Fleming 
1392272cc70bSAndy Fleming 	if (err)
1393272cc70bSAndy Fleming 		return err;
1394272cc70bSAndy Fleming 
1395272cc70bSAndy Fleming 	/* Restrict card's capabilities by what the host can do */
139693bfd616SPantelis Antoniou 	mmc->card_caps &= mmc->cfg->host_caps;
1397272cc70bSAndy Fleming 
1398272cc70bSAndy Fleming 	if (IS_SD(mmc)) {
1399272cc70bSAndy Fleming 		if (mmc->card_caps & MMC_MODE_4BIT) {
1400272cc70bSAndy Fleming 			cmd.cmdidx = MMC_CMD_APP_CMD;
1401272cc70bSAndy Fleming 			cmd.resp_type = MMC_RSP_R1;
1402272cc70bSAndy Fleming 			cmd.cmdarg = mmc->rca << 16;
1403272cc70bSAndy Fleming 
1404272cc70bSAndy Fleming 			err = mmc_send_cmd(mmc, &cmd, NULL);
1405272cc70bSAndy Fleming 			if (err)
1406272cc70bSAndy Fleming 				return err;
1407272cc70bSAndy Fleming 
1408272cc70bSAndy Fleming 			cmd.cmdidx = SD_CMD_APP_SET_BUS_WIDTH;
1409272cc70bSAndy Fleming 			cmd.resp_type = MMC_RSP_R1;
1410272cc70bSAndy Fleming 			cmd.cmdarg = 2;
1411272cc70bSAndy Fleming 			err = mmc_send_cmd(mmc, &cmd, NULL);
1412272cc70bSAndy Fleming 			if (err)
1413272cc70bSAndy Fleming 				return err;
1414272cc70bSAndy Fleming 
1415272cc70bSAndy Fleming 			mmc_set_bus_width(mmc, 4);
1416272cc70bSAndy Fleming 		}
1417272cc70bSAndy Fleming 
14183697e599SPeng Fan 		err = sd_read_ssr(mmc);
14193697e599SPeng Fan 		if (err)
14203697e599SPeng Fan 			return err;
14213697e599SPeng Fan 
1422272cc70bSAndy Fleming 		if (mmc->card_caps & MMC_MODE_HS)
1423ad5fd922SJaehoon Chung 			mmc->tran_speed = 50000000;
1424272cc70bSAndy Fleming 		else
1425ad5fd922SJaehoon Chung 			mmc->tran_speed = 25000000;
1426fc5b32fbSAndrew Gabbasov 	} else if (mmc->version >= MMC_VERSION_4) {
1427fc5b32fbSAndrew Gabbasov 		/* Only version 4 of MMC supports wider bus widths */
14287798f6dbSAndy Fleming 		int idx;
14297798f6dbSAndy Fleming 
14307798f6dbSAndy Fleming 		/* An array of possible bus widths in order of preference */
14317798f6dbSAndy Fleming 		static unsigned ext_csd_bits[] = {
1432d22e3d46SJaehoon Chung 			EXT_CSD_DDR_BUS_WIDTH_8,
1433d22e3d46SJaehoon Chung 			EXT_CSD_DDR_BUS_WIDTH_4,
14347798f6dbSAndy Fleming 			EXT_CSD_BUS_WIDTH_8,
14357798f6dbSAndy Fleming 			EXT_CSD_BUS_WIDTH_4,
14367798f6dbSAndy Fleming 			EXT_CSD_BUS_WIDTH_1,
14377798f6dbSAndy Fleming 		};
14387798f6dbSAndy Fleming 
14397798f6dbSAndy Fleming 		/* An array to map CSD bus widths to host cap bits */
14407798f6dbSAndy Fleming 		static unsigned ext_to_hostcaps[] = {
1441786e8f81SAndrew Gabbasov 			[EXT_CSD_DDR_BUS_WIDTH_4] =
1442786e8f81SAndrew Gabbasov 				MMC_MODE_DDR_52MHz | MMC_MODE_4BIT,
1443786e8f81SAndrew Gabbasov 			[EXT_CSD_DDR_BUS_WIDTH_8] =
1444786e8f81SAndrew Gabbasov 				MMC_MODE_DDR_52MHz | MMC_MODE_8BIT,
14457798f6dbSAndy Fleming 			[EXT_CSD_BUS_WIDTH_4] = MMC_MODE_4BIT,
14467798f6dbSAndy Fleming 			[EXT_CSD_BUS_WIDTH_8] = MMC_MODE_8BIT,
14477798f6dbSAndy Fleming 		};
14487798f6dbSAndy Fleming 
14497798f6dbSAndy Fleming 		/* An array to map chosen bus width to an integer */
14507798f6dbSAndy Fleming 		static unsigned widths[] = {
1451d22e3d46SJaehoon Chung 			8, 4, 8, 4, 1,
14527798f6dbSAndy Fleming 		};
14537798f6dbSAndy Fleming 
14547798f6dbSAndy Fleming 		for (idx=0; idx < ARRAY_SIZE(ext_csd_bits); idx++) {
14557798f6dbSAndy Fleming 			unsigned int extw = ext_csd_bits[idx];
1456786e8f81SAndrew Gabbasov 			unsigned int caps = ext_to_hostcaps[extw];
14577798f6dbSAndy Fleming 
14587798f6dbSAndy Fleming 			/*
1459bf477073SAndrew Gabbasov 			 * If the bus width is still not changed,
1460bf477073SAndrew Gabbasov 			 * don't try to set the default again.
1461bf477073SAndrew Gabbasov 			 * Otherwise, recover from switch attempts
1462bf477073SAndrew Gabbasov 			 * by switching to 1-bit bus width.
1463bf477073SAndrew Gabbasov 			 */
1464bf477073SAndrew Gabbasov 			if (extw == EXT_CSD_BUS_WIDTH_1 &&
1465bf477073SAndrew Gabbasov 					mmc->bus_width == 1) {
1466bf477073SAndrew Gabbasov 				err = 0;
1467bf477073SAndrew Gabbasov 				break;
1468bf477073SAndrew Gabbasov 			}
1469bf477073SAndrew Gabbasov 
1470bf477073SAndrew Gabbasov 			/*
1471786e8f81SAndrew Gabbasov 			 * Check to make sure the card and controller support
1472786e8f81SAndrew Gabbasov 			 * these capabilities
14737798f6dbSAndy Fleming 			 */
1474786e8f81SAndrew Gabbasov 			if ((mmc->card_caps & caps) != caps)
14757798f6dbSAndy Fleming 				continue;
14767798f6dbSAndy Fleming 
1477272cc70bSAndy Fleming 			err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
14787798f6dbSAndy Fleming 					EXT_CSD_BUS_WIDTH, extw);
1479272cc70bSAndy Fleming 
1480272cc70bSAndy Fleming 			if (err)
14814137894eSLei Wen 				continue;
1482272cc70bSAndy Fleming 
1483786e8f81SAndrew Gabbasov 			mmc->ddr_mode = (caps & MMC_MODE_DDR_52MHz) ? 1 : 0;
14847798f6dbSAndy Fleming 			mmc_set_bus_width(mmc, widths[idx]);
1485272cc70bSAndy Fleming 
14864137894eSLei Wen 			err = mmc_send_ext_csd(mmc, test_csd);
1487272cc70bSAndy Fleming 
1488786e8f81SAndrew Gabbasov 			if (err)
1489786e8f81SAndrew Gabbasov 				continue;
1490786e8f81SAndrew Gabbasov 
1491786e8f81SAndrew Gabbasov 			/* Only compare read only fields */
1492786e8f81SAndrew Gabbasov 			if (ext_csd[EXT_CSD_PARTITIONING_SUPPORT]
1493786e8f81SAndrew Gabbasov 				== test_csd[EXT_CSD_PARTITIONING_SUPPORT] &&
1494786e8f81SAndrew Gabbasov 			    ext_csd[EXT_CSD_HC_WP_GRP_SIZE]
1495786e8f81SAndrew Gabbasov 				== test_csd[EXT_CSD_HC_WP_GRP_SIZE] &&
1496786e8f81SAndrew Gabbasov 			    ext_csd[EXT_CSD_REV]
1497786e8f81SAndrew Gabbasov 				== test_csd[EXT_CSD_REV] &&
1498786e8f81SAndrew Gabbasov 			    ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]
1499786e8f81SAndrew Gabbasov 				== test_csd[EXT_CSD_HC_ERASE_GRP_SIZE] &&
1500786e8f81SAndrew Gabbasov 			    memcmp(&ext_csd[EXT_CSD_SEC_CNT],
1501786e8f81SAndrew Gabbasov 				   &test_csd[EXT_CSD_SEC_CNT], 4) == 0)
15024137894eSLei Wen 				break;
1503786e8f81SAndrew Gabbasov 			else
1504915ffa52SJaehoon Chung 				err = -EBADMSG;
15054137894eSLei Wen 		}
1506786e8f81SAndrew Gabbasov 
1507786e8f81SAndrew Gabbasov 		if (err)
1508786e8f81SAndrew Gabbasov 			return err;
1509272cc70bSAndy Fleming 
1510272cc70bSAndy Fleming 		if (mmc->card_caps & MMC_MODE_HS) {
1511272cc70bSAndy Fleming 			if (mmc->card_caps & MMC_MODE_HS_52MHz)
1512ad5fd922SJaehoon Chung 				mmc->tran_speed = 52000000;
1513272cc70bSAndy Fleming 			else
1514ad5fd922SJaehoon Chung 				mmc->tran_speed = 26000000;
1515272cc70bSAndy Fleming 		}
1516ad5fd922SJaehoon Chung 	}
1517ad5fd922SJaehoon Chung 
1518ad5fd922SJaehoon Chung 	mmc_set_clock(mmc, mmc->tran_speed);
1519272cc70bSAndy Fleming 
15205af8f45cSAndrew Gabbasov 	/* Fix the block length for DDR mode */
15215af8f45cSAndrew Gabbasov 	if (mmc->ddr_mode) {
15225af8f45cSAndrew Gabbasov 		mmc->read_bl_len = MMC_MAX_BLOCK_LEN;
15235af8f45cSAndrew Gabbasov 		mmc->write_bl_len = MMC_MAX_BLOCK_LEN;
15245af8f45cSAndrew Gabbasov 	}
15255af8f45cSAndrew Gabbasov 
1526272cc70bSAndy Fleming 	/* fill in device description */
1527c40fdca6SSimon Glass 	bdesc = mmc_get_blk_desc(mmc);
1528c40fdca6SSimon Glass 	bdesc->lun = 0;
1529c40fdca6SSimon Glass 	bdesc->hwpart = 0;
1530c40fdca6SSimon Glass 	bdesc->type = 0;
1531c40fdca6SSimon Glass 	bdesc->blksz = mmc->read_bl_len;
1532c40fdca6SSimon Glass 	bdesc->log2blksz = LOG2(bdesc->blksz);
1533c40fdca6SSimon Glass 	bdesc->lba = lldiv(mmc->capacity, mmc->read_bl_len);
1534fc011f64SSjoerd Simons #if !defined(CONFIG_SPL_BUILD) || \
1535fc011f64SSjoerd Simons 		(defined(CONFIG_SPL_LIBCOMMON_SUPPORT) && \
1536fc011f64SSjoerd Simons 		!defined(CONFIG_USE_TINY_PRINTF))
1537c40fdca6SSimon Glass 	sprintf(bdesc->vendor, "Man %06x Snr %04x%04x",
1538babce5f6STaylor Hutt 		mmc->cid[0] >> 24, (mmc->cid[2] & 0xffff),
1539babce5f6STaylor Hutt 		(mmc->cid[3] >> 16) & 0xffff);
1540c40fdca6SSimon Glass 	sprintf(bdesc->product, "%c%c%c%c%c%c", mmc->cid[0] & 0xff,
15410b453ffeSRabin Vincent 		(mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
1542babce5f6STaylor Hutt 		(mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff,
1543babce5f6STaylor Hutt 		(mmc->cid[2] >> 24) & 0xff);
1544c40fdca6SSimon Glass 	sprintf(bdesc->revision, "%d.%d", (mmc->cid[2] >> 20) & 0xf,
1545babce5f6STaylor Hutt 		(mmc->cid[2] >> 16) & 0xf);
154656196826SPaul Burton #else
1547c40fdca6SSimon Glass 	bdesc->vendor[0] = 0;
1548c40fdca6SSimon Glass 	bdesc->product[0] = 0;
1549c40fdca6SSimon Glass 	bdesc->revision[0] = 0;
155056196826SPaul Burton #endif
1551122efd43SMikhail Kshevetskiy #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBDISK_SUPPORT)
1552c40fdca6SSimon Glass 	part_init(bdesc);
1553122efd43SMikhail Kshevetskiy #endif
1554272cc70bSAndy Fleming 
1555272cc70bSAndy Fleming 	return 0;
1556272cc70bSAndy Fleming }
1557272cc70bSAndy Fleming 
1558fdbb873eSKim Phillips static int mmc_send_if_cond(struct mmc *mmc)
1559272cc70bSAndy Fleming {
1560272cc70bSAndy Fleming 	struct mmc_cmd cmd;
1561272cc70bSAndy Fleming 	int err;
1562272cc70bSAndy Fleming 
1563272cc70bSAndy Fleming 	cmd.cmdidx = SD_CMD_SEND_IF_COND;
1564272cc70bSAndy Fleming 	/* We set the bit if the host supports voltages between 2.7 and 3.6 V */
156593bfd616SPantelis Antoniou 	cmd.cmdarg = ((mmc->cfg->voltages & 0xff8000) != 0) << 8 | 0xaa;
1566272cc70bSAndy Fleming 	cmd.resp_type = MMC_RSP_R7;
1567272cc70bSAndy Fleming 
1568272cc70bSAndy Fleming 	err = mmc_send_cmd(mmc, &cmd, NULL);
1569272cc70bSAndy Fleming 
1570272cc70bSAndy Fleming 	if (err)
1571272cc70bSAndy Fleming 		return err;
1572272cc70bSAndy Fleming 
1573998be3ddSRabin Vincent 	if ((cmd.response[0] & 0xff) != 0xaa)
1574915ffa52SJaehoon Chung 		return -EOPNOTSUPP;
1575272cc70bSAndy Fleming 	else
1576272cc70bSAndy Fleming 		mmc->version = SD_VERSION_2;
1577272cc70bSAndy Fleming 
1578272cc70bSAndy Fleming 	return 0;
1579272cc70bSAndy Fleming }
1580272cc70bSAndy Fleming 
158195de9ab2SPaul Kocialkowski /* board-specific MMC power initializations. */
158295de9ab2SPaul Kocialkowski __weak void board_mmc_power_init(void)
158395de9ab2SPaul Kocialkowski {
158495de9ab2SPaul Kocialkowski }
158595de9ab2SPaul Kocialkowski 
1586*2051aefeSPeng Fan static int mmc_power_init(struct mmc *mmc)
1587*2051aefeSPeng Fan {
1588*2051aefeSPeng Fan 	board_mmc_power_init();
1589*2051aefeSPeng Fan 
1590*2051aefeSPeng Fan #if defined(CONFIG_DM_MMC) && defined(CONFIG_DM_REGULATOR) && \
1591*2051aefeSPeng Fan 	!defined(CONFIG_SPL_BUILD)
1592*2051aefeSPeng Fan 	struct udevice *vmmc_supply;
1593*2051aefeSPeng Fan 	int ret;
1594*2051aefeSPeng Fan 
1595*2051aefeSPeng Fan 	ret = device_get_supply_regulator(mmc->dev, "vmmc-supply",
1596*2051aefeSPeng Fan 					  &vmmc_supply);
1597*2051aefeSPeng Fan 	if (ret) {
1598*2051aefeSPeng Fan 		puts("No vmmc supply\n");
1599*2051aefeSPeng Fan 		return 0;
1600*2051aefeSPeng Fan 	}
1601*2051aefeSPeng Fan 
1602*2051aefeSPeng Fan 	ret = regulator_set_enable(vmmc_supply, true);
1603*2051aefeSPeng Fan 	if (ret) {
1604*2051aefeSPeng Fan 		puts("Error enabling VMMC supply\n");
1605*2051aefeSPeng Fan 		return ret;
1606*2051aefeSPeng Fan 	}
1607*2051aefeSPeng Fan #endif
1608*2051aefeSPeng Fan 	return 0;
1609*2051aefeSPeng Fan }
1610*2051aefeSPeng Fan 
1611e9550449SChe-Liang Chiou int mmc_start_init(struct mmc *mmc)
1612272cc70bSAndy Fleming {
16138ca51e51SSimon Glass 	bool no_card;
1614afd5932bSMacpaul Lin 	int err;
1615272cc70bSAndy Fleming 
1616ab769f22SPantelis Antoniou 	/* we pretend there's no card when init is NULL */
16178ca51e51SSimon Glass 	no_card = mmc_getcd(mmc) == 0;
16188ca51e51SSimon Glass #ifndef CONFIG_DM_MMC_OPS
16198ca51e51SSimon Glass 	no_card = no_card || (mmc->cfg->ops->init == NULL);
16208ca51e51SSimon Glass #endif
16218ca51e51SSimon Glass 	if (no_card) {
162248972d90SThierry Reding 		mmc->has_init = 0;
162356196826SPaul Burton #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
162448972d90SThierry Reding 		printf("MMC: no card present\n");
162556196826SPaul Burton #endif
1626915ffa52SJaehoon Chung 		return -ENOMEDIUM;
162748972d90SThierry Reding 	}
162848972d90SThierry Reding 
1629bc897b1dSLei Wen 	if (mmc->has_init)
1630bc897b1dSLei Wen 		return 0;
1631bc897b1dSLei Wen 
16325a8dbdc6SYangbo Lu #ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT
16335a8dbdc6SYangbo Lu 	mmc_adapter_card_type_ident();
16345a8dbdc6SYangbo Lu #endif
1635*2051aefeSPeng Fan 	err = mmc_power_init(mmc);
1636*2051aefeSPeng Fan 	if (err)
1637*2051aefeSPeng Fan 		return err;
163895de9ab2SPaul Kocialkowski 
16398ca51e51SSimon Glass #ifdef CONFIG_DM_MMC_OPS
16408ca51e51SSimon Glass 	/* The device has already been probed ready for use */
16418ca51e51SSimon Glass #else
1642ab769f22SPantelis Antoniou 	/* made sure it's not NULL earlier */
164393bfd616SPantelis Antoniou 	err = mmc->cfg->ops->init(mmc);
1644272cc70bSAndy Fleming 	if (err)
1645272cc70bSAndy Fleming 		return err;
16468ca51e51SSimon Glass #endif
1647786e8f81SAndrew Gabbasov 	mmc->ddr_mode = 0;
1648b86b85e2SIlya Yanok 	mmc_set_bus_width(mmc, 1);
1649b86b85e2SIlya Yanok 	mmc_set_clock(mmc, 1);
1650b86b85e2SIlya Yanok 
1651272cc70bSAndy Fleming 	/* Reset the Card */
1652272cc70bSAndy Fleming 	err = mmc_go_idle(mmc);
1653272cc70bSAndy Fleming 
1654272cc70bSAndy Fleming 	if (err)
1655272cc70bSAndy Fleming 		return err;
1656272cc70bSAndy Fleming 
1657bc897b1dSLei Wen 	/* The internal partition reset to user partition(0) at every CMD0*/
1658c40fdca6SSimon Glass 	mmc_get_blk_desc(mmc)->hwpart = 0;
1659bc897b1dSLei Wen 
1660272cc70bSAndy Fleming 	/* Test for SD version 2 */
1661272cc70bSAndy Fleming 	err = mmc_send_if_cond(mmc);
1662272cc70bSAndy Fleming 
1663272cc70bSAndy Fleming 	/* Now try to get the SD card's operating condition */
1664272cc70bSAndy Fleming 	err = sd_send_op_cond(mmc);
1665272cc70bSAndy Fleming 
1666272cc70bSAndy Fleming 	/* If the command timed out, we check for an MMC card */
1667915ffa52SJaehoon Chung 	if (err == -ETIMEDOUT) {
1668272cc70bSAndy Fleming 		err = mmc_send_op_cond(mmc);
1669272cc70bSAndy Fleming 
1670bd47c135SAndrew Gabbasov 		if (err) {
167156196826SPaul Burton #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
1672272cc70bSAndy Fleming 			printf("Card did not respond to voltage select!\n");
167356196826SPaul Burton #endif
1674915ffa52SJaehoon Chung 			return -EOPNOTSUPP;
1675272cc70bSAndy Fleming 		}
1676272cc70bSAndy Fleming 	}
1677272cc70bSAndy Fleming 
1678bd47c135SAndrew Gabbasov 	if (!err)
1679e9550449SChe-Liang Chiou 		mmc->init_in_progress = 1;
1680e9550449SChe-Liang Chiou 
1681e9550449SChe-Liang Chiou 	return err;
1682e9550449SChe-Liang Chiou }
1683e9550449SChe-Liang Chiou 
1684e9550449SChe-Liang Chiou static int mmc_complete_init(struct mmc *mmc)
1685e9550449SChe-Liang Chiou {
1686e9550449SChe-Liang Chiou 	int err = 0;
1687e9550449SChe-Liang Chiou 
1688bd47c135SAndrew Gabbasov 	mmc->init_in_progress = 0;
1689e9550449SChe-Liang Chiou 	if (mmc->op_cond_pending)
1690e9550449SChe-Liang Chiou 		err = mmc_complete_op_cond(mmc);
1691e9550449SChe-Liang Chiou 
1692e9550449SChe-Liang Chiou 	if (!err)
1693bc897b1dSLei Wen 		err = mmc_startup(mmc);
1694bc897b1dSLei Wen 	if (err)
1695bc897b1dSLei Wen 		mmc->has_init = 0;
1696bc897b1dSLei Wen 	else
1697bc897b1dSLei Wen 		mmc->has_init = 1;
1698e9550449SChe-Liang Chiou 	return err;
1699e9550449SChe-Liang Chiou }
1700e9550449SChe-Liang Chiou 
1701e9550449SChe-Liang Chiou int mmc_init(struct mmc *mmc)
1702e9550449SChe-Liang Chiou {
1703bd47c135SAndrew Gabbasov 	int err = 0;
1704d803fea5SMateusz Zalega 	unsigned start;
170533fb211dSSimon Glass #ifdef CONFIG_DM_MMC
170633fb211dSSimon Glass 	struct mmc_uclass_priv *upriv = dev_get_uclass_priv(mmc->dev);
1707e9550449SChe-Liang Chiou 
170833fb211dSSimon Glass 	upriv->mmc = mmc;
170933fb211dSSimon Glass #endif
1710e9550449SChe-Liang Chiou 	if (mmc->has_init)
1711e9550449SChe-Liang Chiou 		return 0;
1712d803fea5SMateusz Zalega 
1713d803fea5SMateusz Zalega 	start = get_timer(0);
1714d803fea5SMateusz Zalega 
1715e9550449SChe-Liang Chiou 	if (!mmc->init_in_progress)
1716e9550449SChe-Liang Chiou 		err = mmc_start_init(mmc);
1717e9550449SChe-Liang Chiou 
1718bd47c135SAndrew Gabbasov 	if (!err)
1719e9550449SChe-Liang Chiou 		err = mmc_complete_init(mmc);
1720e9550449SChe-Liang Chiou 	debug("%s: %d, time %lu\n", __func__, err, get_timer(start));
1721bc897b1dSLei Wen 	return err;
1722272cc70bSAndy Fleming }
1723272cc70bSAndy Fleming 
1724ab71188cSMarkus Niebel int mmc_set_dsr(struct mmc *mmc, u16 val)
1725ab71188cSMarkus Niebel {
1726ab71188cSMarkus Niebel 	mmc->dsr = val;
1727ab71188cSMarkus Niebel 	return 0;
1728ab71188cSMarkus Niebel }
1729ab71188cSMarkus Niebel 
1730cee9ab7cSJeroen Hofstee /* CPU-specific MMC initializations */
1731cee9ab7cSJeroen Hofstee __weak int cpu_mmc_init(bd_t *bis)
1732272cc70bSAndy Fleming {
1733272cc70bSAndy Fleming 	return -1;
1734272cc70bSAndy Fleming }
1735272cc70bSAndy Fleming 
1736cee9ab7cSJeroen Hofstee /* board-specific MMC initializations. */
1737cee9ab7cSJeroen Hofstee __weak int board_mmc_init(bd_t *bis)
1738cee9ab7cSJeroen Hofstee {
1739cee9ab7cSJeroen Hofstee 	return -1;
1740cee9ab7cSJeroen Hofstee }
1741272cc70bSAndy Fleming 
1742e9550449SChe-Liang Chiou void mmc_set_preinit(struct mmc *mmc, int preinit)
1743e9550449SChe-Liang Chiou {
1744e9550449SChe-Liang Chiou 	mmc->preinit = preinit;
1745e9550449SChe-Liang Chiou }
1746e9550449SChe-Liang Chiou 
17478e3332e2SSjoerd Simons #if defined(CONFIG_DM_MMC) && defined(CONFIG_SPL_BUILD)
17488e3332e2SSjoerd Simons static int mmc_probe(bd_t *bis)
17498e3332e2SSjoerd Simons {
17508e3332e2SSjoerd Simons 	return 0;
17518e3332e2SSjoerd Simons }
17528e3332e2SSjoerd Simons #elif defined(CONFIG_DM_MMC)
17538e3332e2SSjoerd Simons static int mmc_probe(bd_t *bis)
17548e3332e2SSjoerd Simons {
17554a1db6d8SSimon Glass 	int ret, i;
17568e3332e2SSjoerd Simons 	struct uclass *uc;
17574a1db6d8SSimon Glass 	struct udevice *dev;
17588e3332e2SSjoerd Simons 
17598e3332e2SSjoerd Simons 	ret = uclass_get(UCLASS_MMC, &uc);
17608e3332e2SSjoerd Simons 	if (ret)
17618e3332e2SSjoerd Simons 		return ret;
17628e3332e2SSjoerd Simons 
17634a1db6d8SSimon Glass 	/*
17644a1db6d8SSimon Glass 	 * Try to add them in sequence order. Really with driver model we
17654a1db6d8SSimon Glass 	 * should allow holes, but the current MMC list does not allow that.
17664a1db6d8SSimon Glass 	 * So if we request 0, 1, 3 we will get 0, 1, 2.
17674a1db6d8SSimon Glass 	 */
17684a1db6d8SSimon Glass 	for (i = 0; ; i++) {
17694a1db6d8SSimon Glass 		ret = uclass_get_device_by_seq(UCLASS_MMC, i, &dev);
17704a1db6d8SSimon Glass 		if (ret == -ENODEV)
17714a1db6d8SSimon Glass 			break;
17724a1db6d8SSimon Glass 	}
17734a1db6d8SSimon Glass 	uclass_foreach_dev(dev, uc) {
17744a1db6d8SSimon Glass 		ret = device_probe(dev);
17758e3332e2SSjoerd Simons 		if (ret)
17764a1db6d8SSimon Glass 			printf("%s - probe failed: %d\n", dev->name, ret);
17778e3332e2SSjoerd Simons 	}
17788e3332e2SSjoerd Simons 
17798e3332e2SSjoerd Simons 	return 0;
17808e3332e2SSjoerd Simons }
17818e3332e2SSjoerd Simons #else
17828e3332e2SSjoerd Simons static int mmc_probe(bd_t *bis)
17838e3332e2SSjoerd Simons {
17848e3332e2SSjoerd Simons 	if (board_mmc_init(bis) < 0)
17858e3332e2SSjoerd Simons 		cpu_mmc_init(bis);
17868e3332e2SSjoerd Simons 
17878e3332e2SSjoerd Simons 	return 0;
17888e3332e2SSjoerd Simons }
17898e3332e2SSjoerd Simons #endif
1790e9550449SChe-Liang Chiou 
1791272cc70bSAndy Fleming int mmc_initialize(bd_t *bis)
1792272cc70bSAndy Fleming {
17931b26bab1SDaniel Kochmański 	static int initialized = 0;
17948e3332e2SSjoerd Simons 	int ret;
17951b26bab1SDaniel Kochmański 	if (initialized)	/* Avoid initializing mmc multiple times */
17961b26bab1SDaniel Kochmański 		return 0;
17971b26bab1SDaniel Kochmański 	initialized = 1;
17981b26bab1SDaniel Kochmański 
1799c40fdca6SSimon Glass #ifndef CONFIG_BLK
1800c40fdca6SSimon Glass 	mmc_list_init();
1801c40fdca6SSimon Glass #endif
18028e3332e2SSjoerd Simons 	ret = mmc_probe(bis);
18038e3332e2SSjoerd Simons 	if (ret)
18048e3332e2SSjoerd Simons 		return ret;
1805272cc70bSAndy Fleming 
1806bb0dc108SYing Zhang #ifndef CONFIG_SPL_BUILD
1807272cc70bSAndy Fleming 	print_mmc_devices(',');
1808bb0dc108SYing Zhang #endif
1809272cc70bSAndy Fleming 
1810c40fdca6SSimon Glass 	mmc_do_preinit();
1811272cc70bSAndy Fleming 	return 0;
1812272cc70bSAndy Fleming }
1813