xref: /openbmc/u-boot/drivers/mmc/mmc.c (revision a9003dc641cf881992897585392567811652de09)
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>
182051aefeSPeng 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;
497*a9003dc6SMaxime Ripard 	int retries = 3;
4985d4fc8d9SRaffaele Recalcati 	int ret;
499272cc70bSAndy Fleming 
500272cc70bSAndy Fleming 	cmd.cmdidx = MMC_CMD_SWITCH;
501272cc70bSAndy Fleming 	cmd.resp_type = MMC_RSP_R1b;
502272cc70bSAndy Fleming 	cmd.cmdarg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
503272cc70bSAndy Fleming 				 (index << 16) |
504272cc70bSAndy Fleming 				 (value << 8);
505272cc70bSAndy Fleming 
506*a9003dc6SMaxime Ripard 	while (retries > 0) {
5075d4fc8d9SRaffaele Recalcati 		ret = mmc_send_cmd(mmc, &cmd, NULL);
5085d4fc8d9SRaffaele Recalcati 
5095d4fc8d9SRaffaele Recalcati 		/* Waiting for the ready status */
510*a9003dc6SMaxime Ripard 		if (!ret) {
51193ad0d18SJan Kloetzke 			ret = mmc_send_status(mmc, timeout);
512*a9003dc6SMaxime Ripard 			return ret;
513*a9003dc6SMaxime Ripard 		}
514*a9003dc6SMaxime Ripard 
515*a9003dc6SMaxime Ripard 		retries--;
516*a9003dc6SMaxime Ripard 	}
5175d4fc8d9SRaffaele Recalcati 
5185d4fc8d9SRaffaele Recalcati 	return ret;
5195d4fc8d9SRaffaele Recalcati 
520272cc70bSAndy Fleming }
521272cc70bSAndy Fleming 
522fdbb873eSKim Phillips static int mmc_change_freq(struct mmc *mmc)
523272cc70bSAndy Fleming {
5248bfa195eSSimon Glass 	ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
525272cc70bSAndy Fleming 	char cardtype;
526272cc70bSAndy Fleming 	int err;
527272cc70bSAndy Fleming 
528fc5b32fbSAndrew Gabbasov 	mmc->card_caps = 0;
529272cc70bSAndy Fleming 
530d52ebf10SThomas Chou 	if (mmc_host_is_spi(mmc))
531d52ebf10SThomas Chou 		return 0;
532d52ebf10SThomas Chou 
533272cc70bSAndy Fleming 	/* Only version 4 supports high-speed */
534272cc70bSAndy Fleming 	if (mmc->version < MMC_VERSION_4)
535272cc70bSAndy Fleming 		return 0;
536272cc70bSAndy Fleming 
537fc5b32fbSAndrew Gabbasov 	mmc->card_caps |= MMC_MODE_4BIT | MMC_MODE_8BIT;
538fc5b32fbSAndrew Gabbasov 
539272cc70bSAndy Fleming 	err = mmc_send_ext_csd(mmc, ext_csd);
540272cc70bSAndy Fleming 
541272cc70bSAndy Fleming 	if (err)
542272cc70bSAndy Fleming 		return err;
543272cc70bSAndy Fleming 
5440560db18SLei Wen 	cardtype = ext_csd[EXT_CSD_CARD_TYPE] & 0xf;
545272cc70bSAndy Fleming 
546272cc70bSAndy Fleming 	err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING, 1);
547272cc70bSAndy Fleming 
548272cc70bSAndy Fleming 	if (err)
549a5e27b41SHeiko Schocher 		return err;
550272cc70bSAndy Fleming 
551272cc70bSAndy Fleming 	/* Now check to see that it worked */
552272cc70bSAndy Fleming 	err = mmc_send_ext_csd(mmc, ext_csd);
553272cc70bSAndy Fleming 
554272cc70bSAndy Fleming 	if (err)
555272cc70bSAndy Fleming 		return err;
556272cc70bSAndy Fleming 
557272cc70bSAndy Fleming 	/* No high-speed support */
5580560db18SLei Wen 	if (!ext_csd[EXT_CSD_HS_TIMING])
559272cc70bSAndy Fleming 		return 0;
560272cc70bSAndy Fleming 
561272cc70bSAndy Fleming 	/* High Speed is set, there are two types: 52MHz and 26MHz */
562d22e3d46SJaehoon Chung 	if (cardtype & EXT_CSD_CARD_TYPE_52) {
563201d5ac4SAndrew Gabbasov 		if (cardtype & EXT_CSD_CARD_TYPE_DDR_1_8V)
564d22e3d46SJaehoon Chung 			mmc->card_caps |= MMC_MODE_DDR_52MHz;
565272cc70bSAndy Fleming 		mmc->card_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
566d22e3d46SJaehoon Chung 	} else {
567272cc70bSAndy Fleming 		mmc->card_caps |= MMC_MODE_HS;
568d22e3d46SJaehoon Chung 	}
569272cc70bSAndy Fleming 
570272cc70bSAndy Fleming 	return 0;
571272cc70bSAndy Fleming }
572272cc70bSAndy Fleming 
573f866a46dSStephen Warren static int mmc_set_capacity(struct mmc *mmc, int part_num)
574f866a46dSStephen Warren {
575f866a46dSStephen Warren 	switch (part_num) {
576f866a46dSStephen Warren 	case 0:
577f866a46dSStephen Warren 		mmc->capacity = mmc->capacity_user;
578f866a46dSStephen Warren 		break;
579f866a46dSStephen Warren 	case 1:
580f866a46dSStephen Warren 	case 2:
581f866a46dSStephen Warren 		mmc->capacity = mmc->capacity_boot;
582f866a46dSStephen Warren 		break;
583f866a46dSStephen Warren 	case 3:
584f866a46dSStephen Warren 		mmc->capacity = mmc->capacity_rpmb;
585f866a46dSStephen Warren 		break;
586f866a46dSStephen Warren 	case 4:
587f866a46dSStephen Warren 	case 5:
588f866a46dSStephen Warren 	case 6:
589f866a46dSStephen Warren 	case 7:
590f866a46dSStephen Warren 		mmc->capacity = mmc->capacity_gp[part_num - 4];
591f866a46dSStephen Warren 		break;
592f866a46dSStephen Warren 	default:
593f866a46dSStephen Warren 		return -1;
594f866a46dSStephen Warren 	}
595f866a46dSStephen Warren 
596c40fdca6SSimon Glass 	mmc_get_blk_desc(mmc)->lba = lldiv(mmc->capacity, mmc->read_bl_len);
597f866a46dSStephen Warren 
598f866a46dSStephen Warren 	return 0;
599f866a46dSStephen Warren }
600f866a46dSStephen Warren 
6017dba0b93SSimon Glass int mmc_switch_part(struct mmc *mmc, unsigned int part_num)
602bc897b1dSLei Wen {
603f866a46dSStephen Warren 	int ret;
604bc897b1dSLei Wen 
605f866a46dSStephen Warren 	ret = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONF,
606bc897b1dSLei Wen 			 (mmc->part_config & ~PART_ACCESS_MASK)
607bc897b1dSLei Wen 			 | (part_num & PART_ACCESS_MASK));
608f866a46dSStephen Warren 
6096dc93e70SPeter Bigot 	/*
6106dc93e70SPeter Bigot 	 * Set the capacity if the switch succeeded or was intended
6116dc93e70SPeter Bigot 	 * to return to representing the raw device.
6126dc93e70SPeter Bigot 	 */
613873cc1d7SStephen Warren 	if ((ret == 0) || ((ret == -ENODEV) && (part_num == 0))) {
6146dc93e70SPeter Bigot 		ret = mmc_set_capacity(mmc, part_num);
615fdbb139fSSimon Glass 		mmc_get_blk_desc(mmc)->hwpart = part_num;
616873cc1d7SStephen Warren 	}
6176dc93e70SPeter Bigot 
6186dc93e70SPeter Bigot 	return ret;
619bc897b1dSLei Wen }
620bc897b1dSLei Wen 
621ac9da0e0SDiego Santa Cruz int mmc_hwpart_config(struct mmc *mmc,
622ac9da0e0SDiego Santa Cruz 		      const struct mmc_hwpart_conf *conf,
623ac9da0e0SDiego Santa Cruz 		      enum mmc_hwpart_conf_mode mode)
624ac9da0e0SDiego Santa Cruz {
625ac9da0e0SDiego Santa Cruz 	u8 part_attrs = 0;
626ac9da0e0SDiego Santa Cruz 	u32 enh_size_mult;
627ac9da0e0SDiego Santa Cruz 	u32 enh_start_addr;
628ac9da0e0SDiego Santa Cruz 	u32 gp_size_mult[4];
629ac9da0e0SDiego Santa Cruz 	u32 max_enh_size_mult;
630ac9da0e0SDiego Santa Cruz 	u32 tot_enh_size_mult = 0;
6318dda5b0eSDiego Santa Cruz 	u8 wr_rel_set;
632ac9da0e0SDiego Santa Cruz 	int i, pidx, err;
633ac9da0e0SDiego Santa Cruz 	ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
634ac9da0e0SDiego Santa Cruz 
635ac9da0e0SDiego Santa Cruz 	if (mode < MMC_HWPART_CONF_CHECK || mode > MMC_HWPART_CONF_COMPLETE)
636ac9da0e0SDiego Santa Cruz 		return -EINVAL;
637ac9da0e0SDiego Santa Cruz 
638ac9da0e0SDiego Santa Cruz 	if (IS_SD(mmc) || (mmc->version < MMC_VERSION_4_41)) {
639ac9da0e0SDiego Santa Cruz 		printf("eMMC >= 4.4 required for enhanced user data area\n");
640ac9da0e0SDiego Santa Cruz 		return -EMEDIUMTYPE;
641ac9da0e0SDiego Santa Cruz 	}
642ac9da0e0SDiego Santa Cruz 
643ac9da0e0SDiego Santa Cruz 	if (!(mmc->part_support & PART_SUPPORT)) {
644ac9da0e0SDiego Santa Cruz 		printf("Card does not support partitioning\n");
645ac9da0e0SDiego Santa Cruz 		return -EMEDIUMTYPE;
646ac9da0e0SDiego Santa Cruz 	}
647ac9da0e0SDiego Santa Cruz 
648ac9da0e0SDiego Santa Cruz 	if (!mmc->hc_wp_grp_size) {
649ac9da0e0SDiego Santa Cruz 		printf("Card does not define HC WP group size\n");
650ac9da0e0SDiego Santa Cruz 		return -EMEDIUMTYPE;
651ac9da0e0SDiego Santa Cruz 	}
652ac9da0e0SDiego Santa Cruz 
653ac9da0e0SDiego Santa Cruz 	/* check partition alignment and total enhanced size */
654ac9da0e0SDiego Santa Cruz 	if (conf->user.enh_size) {
655ac9da0e0SDiego Santa Cruz 		if (conf->user.enh_size % mmc->hc_wp_grp_size ||
656ac9da0e0SDiego Santa Cruz 		    conf->user.enh_start % mmc->hc_wp_grp_size) {
657ac9da0e0SDiego Santa Cruz 			printf("User data enhanced area not HC WP group "
658ac9da0e0SDiego Santa Cruz 			       "size aligned\n");
659ac9da0e0SDiego Santa Cruz 			return -EINVAL;
660ac9da0e0SDiego Santa Cruz 		}
661ac9da0e0SDiego Santa Cruz 		part_attrs |= EXT_CSD_ENH_USR;
662ac9da0e0SDiego Santa Cruz 		enh_size_mult = conf->user.enh_size / mmc->hc_wp_grp_size;
663ac9da0e0SDiego Santa Cruz 		if (mmc->high_capacity) {
664ac9da0e0SDiego Santa Cruz 			enh_start_addr = conf->user.enh_start;
665ac9da0e0SDiego Santa Cruz 		} else {
666ac9da0e0SDiego Santa Cruz 			enh_start_addr = (conf->user.enh_start << 9);
667ac9da0e0SDiego Santa Cruz 		}
668ac9da0e0SDiego Santa Cruz 	} else {
669ac9da0e0SDiego Santa Cruz 		enh_size_mult = 0;
670ac9da0e0SDiego Santa Cruz 		enh_start_addr = 0;
671ac9da0e0SDiego Santa Cruz 	}
672ac9da0e0SDiego Santa Cruz 	tot_enh_size_mult += enh_size_mult;
673ac9da0e0SDiego Santa Cruz 
674ac9da0e0SDiego Santa Cruz 	for (pidx = 0; pidx < 4; pidx++) {
675ac9da0e0SDiego Santa Cruz 		if (conf->gp_part[pidx].size % mmc->hc_wp_grp_size) {
676ac9da0e0SDiego Santa Cruz 			printf("GP%i partition not HC WP group size "
677ac9da0e0SDiego Santa Cruz 			       "aligned\n", pidx+1);
678ac9da0e0SDiego Santa Cruz 			return -EINVAL;
679ac9da0e0SDiego Santa Cruz 		}
680ac9da0e0SDiego Santa Cruz 		gp_size_mult[pidx] = conf->gp_part[pidx].size / mmc->hc_wp_grp_size;
681ac9da0e0SDiego Santa Cruz 		if (conf->gp_part[pidx].size && conf->gp_part[pidx].enhanced) {
682ac9da0e0SDiego Santa Cruz 			part_attrs |= EXT_CSD_ENH_GP(pidx);
683ac9da0e0SDiego Santa Cruz 			tot_enh_size_mult += gp_size_mult[pidx];
684ac9da0e0SDiego Santa Cruz 		}
685ac9da0e0SDiego Santa Cruz 	}
686ac9da0e0SDiego Santa Cruz 
687ac9da0e0SDiego Santa Cruz 	if (part_attrs && ! (mmc->part_support & ENHNCD_SUPPORT)) {
688ac9da0e0SDiego Santa Cruz 		printf("Card does not support enhanced attribute\n");
689ac9da0e0SDiego Santa Cruz 		return -EMEDIUMTYPE;
690ac9da0e0SDiego Santa Cruz 	}
691ac9da0e0SDiego Santa Cruz 
692ac9da0e0SDiego Santa Cruz 	err = mmc_send_ext_csd(mmc, ext_csd);
693ac9da0e0SDiego Santa Cruz 	if (err)
694ac9da0e0SDiego Santa Cruz 		return err;
695ac9da0e0SDiego Santa Cruz 
696ac9da0e0SDiego Santa Cruz 	max_enh_size_mult =
697ac9da0e0SDiego Santa Cruz 		(ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT+2] << 16) +
698ac9da0e0SDiego Santa Cruz 		(ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT+1] << 8) +
699ac9da0e0SDiego Santa Cruz 		ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT];
700ac9da0e0SDiego Santa Cruz 	if (tot_enh_size_mult > max_enh_size_mult) {
701ac9da0e0SDiego Santa Cruz 		printf("Total enhanced size exceeds maximum (%u > %u)\n",
702ac9da0e0SDiego Santa Cruz 		       tot_enh_size_mult, max_enh_size_mult);
703ac9da0e0SDiego Santa Cruz 		return -EMEDIUMTYPE;
704ac9da0e0SDiego Santa Cruz 	}
705ac9da0e0SDiego Santa Cruz 
7068dda5b0eSDiego Santa Cruz 	/* The default value of EXT_CSD_WR_REL_SET is device
7078dda5b0eSDiego Santa Cruz 	 * dependent, the values can only be changed if the
7088dda5b0eSDiego Santa Cruz 	 * EXT_CSD_HS_CTRL_REL bit is set. The values can be
7098dda5b0eSDiego Santa Cruz 	 * changed only once and before partitioning is completed. */
7108dda5b0eSDiego Santa Cruz 	wr_rel_set = ext_csd[EXT_CSD_WR_REL_SET];
7118dda5b0eSDiego Santa Cruz 	if (conf->user.wr_rel_change) {
7128dda5b0eSDiego Santa Cruz 		if (conf->user.wr_rel_set)
7138dda5b0eSDiego Santa Cruz 			wr_rel_set |= EXT_CSD_WR_DATA_REL_USR;
7148dda5b0eSDiego Santa Cruz 		else
7158dda5b0eSDiego Santa Cruz 			wr_rel_set &= ~EXT_CSD_WR_DATA_REL_USR;
7168dda5b0eSDiego Santa Cruz 	}
7178dda5b0eSDiego Santa Cruz 	for (pidx = 0; pidx < 4; pidx++) {
7188dda5b0eSDiego Santa Cruz 		if (conf->gp_part[pidx].wr_rel_change) {
7198dda5b0eSDiego Santa Cruz 			if (conf->gp_part[pidx].wr_rel_set)
7208dda5b0eSDiego Santa Cruz 				wr_rel_set |= EXT_CSD_WR_DATA_REL_GP(pidx);
7218dda5b0eSDiego Santa Cruz 			else
7228dda5b0eSDiego Santa Cruz 				wr_rel_set &= ~EXT_CSD_WR_DATA_REL_GP(pidx);
7238dda5b0eSDiego Santa Cruz 		}
7248dda5b0eSDiego Santa Cruz 	}
7258dda5b0eSDiego Santa Cruz 
7268dda5b0eSDiego Santa Cruz 	if (wr_rel_set != ext_csd[EXT_CSD_WR_REL_SET] &&
7278dda5b0eSDiego Santa Cruz 	    !(ext_csd[EXT_CSD_WR_REL_PARAM] & EXT_CSD_HS_CTRL_REL)) {
7288dda5b0eSDiego Santa Cruz 		puts("Card does not support host controlled partition write "
7298dda5b0eSDiego Santa Cruz 		     "reliability settings\n");
7308dda5b0eSDiego Santa Cruz 		return -EMEDIUMTYPE;
7318dda5b0eSDiego Santa Cruz 	}
7328dda5b0eSDiego Santa Cruz 
733ac9da0e0SDiego Santa Cruz 	if (ext_csd[EXT_CSD_PARTITION_SETTING] &
734ac9da0e0SDiego Santa Cruz 	    EXT_CSD_PARTITION_SETTING_COMPLETED) {
735ac9da0e0SDiego Santa Cruz 		printf("Card already partitioned\n");
736ac9da0e0SDiego Santa Cruz 		return -EPERM;
737ac9da0e0SDiego Santa Cruz 	}
738ac9da0e0SDiego Santa Cruz 
739ac9da0e0SDiego Santa Cruz 	if (mode == MMC_HWPART_CONF_CHECK)
740ac9da0e0SDiego Santa Cruz 		return 0;
741ac9da0e0SDiego Santa Cruz 
742ac9da0e0SDiego Santa Cruz 	/* Partitioning requires high-capacity size definitions */
743ac9da0e0SDiego Santa Cruz 	if (!(ext_csd[EXT_CSD_ERASE_GROUP_DEF] & 0x01)) {
744ac9da0e0SDiego Santa Cruz 		err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
745ac9da0e0SDiego Santa Cruz 				 EXT_CSD_ERASE_GROUP_DEF, 1);
746ac9da0e0SDiego Santa Cruz 
747ac9da0e0SDiego Santa Cruz 		if (err)
748ac9da0e0SDiego Santa Cruz 			return err;
749ac9da0e0SDiego Santa Cruz 
750ac9da0e0SDiego Santa Cruz 		ext_csd[EXT_CSD_ERASE_GROUP_DEF] = 1;
751ac9da0e0SDiego Santa Cruz 
752ac9da0e0SDiego Santa Cruz 		/* update erase group size to be high-capacity */
753ac9da0e0SDiego Santa Cruz 		mmc->erase_grp_size =
754ac9da0e0SDiego Santa Cruz 			ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] * 1024;
755ac9da0e0SDiego Santa Cruz 
756ac9da0e0SDiego Santa Cruz 	}
757ac9da0e0SDiego Santa Cruz 
758ac9da0e0SDiego Santa Cruz 	/* all OK, write the configuration */
759ac9da0e0SDiego Santa Cruz 	for (i = 0; i < 4; i++) {
760ac9da0e0SDiego Santa Cruz 		err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
761ac9da0e0SDiego Santa Cruz 				 EXT_CSD_ENH_START_ADDR+i,
762ac9da0e0SDiego Santa Cruz 				 (enh_start_addr >> (i*8)) & 0xFF);
763ac9da0e0SDiego Santa Cruz 		if (err)
764ac9da0e0SDiego Santa Cruz 			return err;
765ac9da0e0SDiego Santa Cruz 	}
766ac9da0e0SDiego Santa Cruz 	for (i = 0; i < 3; i++) {
767ac9da0e0SDiego Santa Cruz 		err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
768ac9da0e0SDiego Santa Cruz 				 EXT_CSD_ENH_SIZE_MULT+i,
769ac9da0e0SDiego Santa Cruz 				 (enh_size_mult >> (i*8)) & 0xFF);
770ac9da0e0SDiego Santa Cruz 		if (err)
771ac9da0e0SDiego Santa Cruz 			return err;
772ac9da0e0SDiego Santa Cruz 	}
773ac9da0e0SDiego Santa Cruz 	for (pidx = 0; pidx < 4; pidx++) {
774ac9da0e0SDiego Santa Cruz 		for (i = 0; i < 3; i++) {
775ac9da0e0SDiego Santa Cruz 			err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
776ac9da0e0SDiego Santa Cruz 					 EXT_CSD_GP_SIZE_MULT+pidx*3+i,
777ac9da0e0SDiego Santa Cruz 					 (gp_size_mult[pidx] >> (i*8)) & 0xFF);
778ac9da0e0SDiego Santa Cruz 			if (err)
779ac9da0e0SDiego Santa Cruz 				return err;
780ac9da0e0SDiego Santa Cruz 		}
781ac9da0e0SDiego Santa Cruz 	}
782ac9da0e0SDiego Santa Cruz 	err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
783ac9da0e0SDiego Santa Cruz 			 EXT_CSD_PARTITIONS_ATTRIBUTE, part_attrs);
784ac9da0e0SDiego Santa Cruz 	if (err)
785ac9da0e0SDiego Santa Cruz 		return err;
786ac9da0e0SDiego Santa Cruz 
787ac9da0e0SDiego Santa Cruz 	if (mode == MMC_HWPART_CONF_SET)
788ac9da0e0SDiego Santa Cruz 		return 0;
789ac9da0e0SDiego Santa Cruz 
7908dda5b0eSDiego Santa Cruz 	/* The WR_REL_SET is a write-once register but shall be
7918dda5b0eSDiego Santa Cruz 	 * written before setting PART_SETTING_COMPLETED. As it is
7928dda5b0eSDiego Santa Cruz 	 * write-once we can only write it when completing the
7938dda5b0eSDiego Santa Cruz 	 * partitioning. */
7948dda5b0eSDiego Santa Cruz 	if (wr_rel_set != ext_csd[EXT_CSD_WR_REL_SET]) {
7958dda5b0eSDiego Santa Cruz 		err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
7968dda5b0eSDiego Santa Cruz 				 EXT_CSD_WR_REL_SET, wr_rel_set);
7978dda5b0eSDiego Santa Cruz 		if (err)
7988dda5b0eSDiego Santa Cruz 			return err;
7998dda5b0eSDiego Santa Cruz 	}
8008dda5b0eSDiego Santa Cruz 
801ac9da0e0SDiego Santa Cruz 	/* Setting PART_SETTING_COMPLETED confirms the partition
802ac9da0e0SDiego Santa Cruz 	 * configuration but it only becomes effective after power
803ac9da0e0SDiego Santa Cruz 	 * cycle, so we do not adjust the partition related settings
804ac9da0e0SDiego Santa Cruz 	 * in the mmc struct. */
805ac9da0e0SDiego Santa Cruz 
806ac9da0e0SDiego Santa Cruz 	err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
807ac9da0e0SDiego Santa Cruz 			 EXT_CSD_PARTITION_SETTING,
808ac9da0e0SDiego Santa Cruz 			 EXT_CSD_PARTITION_SETTING_COMPLETED);
809ac9da0e0SDiego Santa Cruz 	if (err)
810ac9da0e0SDiego Santa Cruz 		return err;
811ac9da0e0SDiego Santa Cruz 
812ac9da0e0SDiego Santa Cruz 	return 0;
813ac9da0e0SDiego Santa Cruz }
814ac9da0e0SDiego Santa Cruz 
8158ca51e51SSimon Glass #ifndef CONFIG_DM_MMC_OPS
81648972d90SThierry Reding int mmc_getcd(struct mmc *mmc)
81748972d90SThierry Reding {
81848972d90SThierry Reding 	int cd;
81948972d90SThierry Reding 
82048972d90SThierry Reding 	cd = board_mmc_getcd(mmc);
82148972d90SThierry Reding 
822d4e1da4eSPeter Korsgaard 	if (cd < 0) {
82393bfd616SPantelis Antoniou 		if (mmc->cfg->ops->getcd)
82493bfd616SPantelis Antoniou 			cd = mmc->cfg->ops->getcd(mmc);
825d4e1da4eSPeter Korsgaard 		else
826d4e1da4eSPeter Korsgaard 			cd = 1;
827d4e1da4eSPeter Korsgaard 	}
82848972d90SThierry Reding 
82948972d90SThierry Reding 	return cd;
83048972d90SThierry Reding }
8318ca51e51SSimon Glass #endif
83248972d90SThierry Reding 
833fdbb873eSKim Phillips static int sd_switch(struct mmc *mmc, int mode, int group, u8 value, u8 *resp)
834272cc70bSAndy Fleming {
835272cc70bSAndy Fleming 	struct mmc_cmd cmd;
836272cc70bSAndy Fleming 	struct mmc_data data;
837272cc70bSAndy Fleming 
838272cc70bSAndy Fleming 	/* Switch the frequency */
839272cc70bSAndy Fleming 	cmd.cmdidx = SD_CMD_SWITCH_FUNC;
840272cc70bSAndy Fleming 	cmd.resp_type = MMC_RSP_R1;
841272cc70bSAndy Fleming 	cmd.cmdarg = (mode << 31) | 0xffffff;
842272cc70bSAndy Fleming 	cmd.cmdarg &= ~(0xf << (group * 4));
843272cc70bSAndy Fleming 	cmd.cmdarg |= value << (group * 4);
844272cc70bSAndy Fleming 
845272cc70bSAndy Fleming 	data.dest = (char *)resp;
846272cc70bSAndy Fleming 	data.blocksize = 64;
847272cc70bSAndy Fleming 	data.blocks = 1;
848272cc70bSAndy Fleming 	data.flags = MMC_DATA_READ;
849272cc70bSAndy Fleming 
850272cc70bSAndy Fleming 	return mmc_send_cmd(mmc, &cmd, &data);
851272cc70bSAndy Fleming }
852272cc70bSAndy Fleming 
853272cc70bSAndy Fleming 
854fdbb873eSKim Phillips static int sd_change_freq(struct mmc *mmc)
855272cc70bSAndy Fleming {
856272cc70bSAndy Fleming 	int err;
857272cc70bSAndy Fleming 	struct mmc_cmd cmd;
858f781dd38SAnton staaf 	ALLOC_CACHE_ALIGN_BUFFER(uint, scr, 2);
859f781dd38SAnton staaf 	ALLOC_CACHE_ALIGN_BUFFER(uint, switch_status, 16);
860272cc70bSAndy Fleming 	struct mmc_data data;
861272cc70bSAndy Fleming 	int timeout;
862272cc70bSAndy Fleming 
863272cc70bSAndy Fleming 	mmc->card_caps = 0;
864272cc70bSAndy Fleming 
865d52ebf10SThomas Chou 	if (mmc_host_is_spi(mmc))
866d52ebf10SThomas Chou 		return 0;
867d52ebf10SThomas Chou 
868272cc70bSAndy Fleming 	/* Read the SCR to find out if this card supports higher speeds */
869272cc70bSAndy Fleming 	cmd.cmdidx = MMC_CMD_APP_CMD;
870272cc70bSAndy Fleming 	cmd.resp_type = MMC_RSP_R1;
871272cc70bSAndy Fleming 	cmd.cmdarg = mmc->rca << 16;
872272cc70bSAndy Fleming 
873272cc70bSAndy Fleming 	err = mmc_send_cmd(mmc, &cmd, NULL);
874272cc70bSAndy Fleming 
875272cc70bSAndy Fleming 	if (err)
876272cc70bSAndy Fleming 		return err;
877272cc70bSAndy Fleming 
878272cc70bSAndy Fleming 	cmd.cmdidx = SD_CMD_APP_SEND_SCR;
879272cc70bSAndy Fleming 	cmd.resp_type = MMC_RSP_R1;
880272cc70bSAndy Fleming 	cmd.cmdarg = 0;
881272cc70bSAndy Fleming 
882272cc70bSAndy Fleming 	timeout = 3;
883272cc70bSAndy Fleming 
884272cc70bSAndy Fleming retry_scr:
885f781dd38SAnton staaf 	data.dest = (char *)scr;
886272cc70bSAndy Fleming 	data.blocksize = 8;
887272cc70bSAndy Fleming 	data.blocks = 1;
888272cc70bSAndy Fleming 	data.flags = MMC_DATA_READ;
889272cc70bSAndy Fleming 
890272cc70bSAndy Fleming 	err = mmc_send_cmd(mmc, &cmd, &data);
891272cc70bSAndy Fleming 
892272cc70bSAndy Fleming 	if (err) {
893272cc70bSAndy Fleming 		if (timeout--)
894272cc70bSAndy Fleming 			goto retry_scr;
895272cc70bSAndy Fleming 
896272cc70bSAndy Fleming 		return err;
897272cc70bSAndy Fleming 	}
898272cc70bSAndy Fleming 
8994e3d89baSYauhen Kharuzhy 	mmc->scr[0] = __be32_to_cpu(scr[0]);
9004e3d89baSYauhen Kharuzhy 	mmc->scr[1] = __be32_to_cpu(scr[1]);
901272cc70bSAndy Fleming 
902272cc70bSAndy Fleming 	switch ((mmc->scr[0] >> 24) & 0xf) {
903272cc70bSAndy Fleming 	case 0:
904272cc70bSAndy Fleming 		mmc->version = SD_VERSION_1_0;
905272cc70bSAndy Fleming 		break;
906272cc70bSAndy Fleming 	case 1:
907272cc70bSAndy Fleming 		mmc->version = SD_VERSION_1_10;
908272cc70bSAndy Fleming 		break;
909272cc70bSAndy Fleming 	case 2:
910272cc70bSAndy Fleming 		mmc->version = SD_VERSION_2;
9111741c64dSJaehoon Chung 		if ((mmc->scr[0] >> 15) & 0x1)
9121741c64dSJaehoon Chung 			mmc->version = SD_VERSION_3;
913272cc70bSAndy Fleming 		break;
914272cc70bSAndy Fleming 	default:
915272cc70bSAndy Fleming 		mmc->version = SD_VERSION_1_0;
916272cc70bSAndy Fleming 		break;
917272cc70bSAndy Fleming 	}
918272cc70bSAndy Fleming 
919b44c7083SAlagu Sankar 	if (mmc->scr[0] & SD_DATA_4BIT)
920b44c7083SAlagu Sankar 		mmc->card_caps |= MMC_MODE_4BIT;
921b44c7083SAlagu Sankar 
922272cc70bSAndy Fleming 	/* Version 1.0 doesn't support switching */
923272cc70bSAndy Fleming 	if (mmc->version == SD_VERSION_1_0)
924272cc70bSAndy Fleming 		return 0;
925272cc70bSAndy Fleming 
926272cc70bSAndy Fleming 	timeout = 4;
927272cc70bSAndy Fleming 	while (timeout--) {
928272cc70bSAndy Fleming 		err = sd_switch(mmc, SD_SWITCH_CHECK, 0, 1,
929f781dd38SAnton staaf 				(u8 *)switch_status);
930272cc70bSAndy Fleming 
931272cc70bSAndy Fleming 		if (err)
932272cc70bSAndy Fleming 			return err;
933272cc70bSAndy Fleming 
934272cc70bSAndy Fleming 		/* The high-speed function is busy.  Try again */
9354e3d89baSYauhen Kharuzhy 		if (!(__be32_to_cpu(switch_status[7]) & SD_HIGHSPEED_BUSY))
936272cc70bSAndy Fleming 			break;
937272cc70bSAndy Fleming 	}
938272cc70bSAndy Fleming 
939272cc70bSAndy Fleming 	/* If high-speed isn't supported, we return */
9404e3d89baSYauhen Kharuzhy 	if (!(__be32_to_cpu(switch_status[3]) & SD_HIGHSPEED_SUPPORTED))
941272cc70bSAndy Fleming 		return 0;
942272cc70bSAndy Fleming 
9432c3fbf4cSMacpaul Lin 	/*
9442c3fbf4cSMacpaul Lin 	 * If the host doesn't support SD_HIGHSPEED, do not switch card to
9452c3fbf4cSMacpaul Lin 	 * HIGHSPEED mode even if the card support SD_HIGHSPPED.
9462c3fbf4cSMacpaul Lin 	 * This can avoid furthur problem when the card runs in different
9472c3fbf4cSMacpaul Lin 	 * mode between the host.
9482c3fbf4cSMacpaul Lin 	 */
94993bfd616SPantelis Antoniou 	if (!((mmc->cfg->host_caps & MMC_MODE_HS_52MHz) &&
95093bfd616SPantelis Antoniou 		(mmc->cfg->host_caps & MMC_MODE_HS)))
9512c3fbf4cSMacpaul Lin 		return 0;
9522c3fbf4cSMacpaul Lin 
953f781dd38SAnton staaf 	err = sd_switch(mmc, SD_SWITCH_SWITCH, 0, 1, (u8 *)switch_status);
954272cc70bSAndy Fleming 
955272cc70bSAndy Fleming 	if (err)
956272cc70bSAndy Fleming 		return err;
957272cc70bSAndy Fleming 
9584e3d89baSYauhen Kharuzhy 	if ((__be32_to_cpu(switch_status[4]) & 0x0f000000) == 0x01000000)
959272cc70bSAndy Fleming 		mmc->card_caps |= MMC_MODE_HS;
960272cc70bSAndy Fleming 
961272cc70bSAndy Fleming 	return 0;
962272cc70bSAndy Fleming }
963272cc70bSAndy Fleming 
9643697e599SPeng Fan static int sd_read_ssr(struct mmc *mmc)
9653697e599SPeng Fan {
9663697e599SPeng Fan 	int err, i;
9673697e599SPeng Fan 	struct mmc_cmd cmd;
9683697e599SPeng Fan 	ALLOC_CACHE_ALIGN_BUFFER(uint, ssr, 16);
9693697e599SPeng Fan 	struct mmc_data data;
9703697e599SPeng Fan 	int timeout = 3;
9713697e599SPeng Fan 	unsigned int au, eo, et, es;
9723697e599SPeng Fan 
9733697e599SPeng Fan 	cmd.cmdidx = MMC_CMD_APP_CMD;
9743697e599SPeng Fan 	cmd.resp_type = MMC_RSP_R1;
9753697e599SPeng Fan 	cmd.cmdarg = mmc->rca << 16;
9763697e599SPeng Fan 
9773697e599SPeng Fan 	err = mmc_send_cmd(mmc, &cmd, NULL);
9783697e599SPeng Fan 	if (err)
9793697e599SPeng Fan 		return err;
9803697e599SPeng Fan 
9813697e599SPeng Fan 	cmd.cmdidx = SD_CMD_APP_SD_STATUS;
9823697e599SPeng Fan 	cmd.resp_type = MMC_RSP_R1;
9833697e599SPeng Fan 	cmd.cmdarg = 0;
9843697e599SPeng Fan 
9853697e599SPeng Fan retry_ssr:
9863697e599SPeng Fan 	data.dest = (char *)ssr;
9873697e599SPeng Fan 	data.blocksize = 64;
9883697e599SPeng Fan 	data.blocks = 1;
9893697e599SPeng Fan 	data.flags = MMC_DATA_READ;
9903697e599SPeng Fan 
9913697e599SPeng Fan 	err = mmc_send_cmd(mmc, &cmd, &data);
9923697e599SPeng Fan 	if (err) {
9933697e599SPeng Fan 		if (timeout--)
9943697e599SPeng Fan 			goto retry_ssr;
9953697e599SPeng Fan 
9963697e599SPeng Fan 		return err;
9973697e599SPeng Fan 	}
9983697e599SPeng Fan 
9993697e599SPeng Fan 	for (i = 0; i < 16; i++)
10003697e599SPeng Fan 		ssr[i] = be32_to_cpu(ssr[i]);
10013697e599SPeng Fan 
10023697e599SPeng Fan 	au = (ssr[2] >> 12) & 0xF;
10033697e599SPeng Fan 	if ((au <= 9) || (mmc->version == SD_VERSION_3)) {
10043697e599SPeng Fan 		mmc->ssr.au = sd_au_size[au];
10053697e599SPeng Fan 		es = (ssr[3] >> 24) & 0xFF;
10063697e599SPeng Fan 		es |= (ssr[2] & 0xFF) << 8;
10073697e599SPeng Fan 		et = (ssr[3] >> 18) & 0x3F;
10083697e599SPeng Fan 		if (es && et) {
10093697e599SPeng Fan 			eo = (ssr[3] >> 16) & 0x3;
10103697e599SPeng Fan 			mmc->ssr.erase_timeout = (et * 1000) / es;
10113697e599SPeng Fan 			mmc->ssr.erase_offset = eo * 1000;
10123697e599SPeng Fan 		}
10133697e599SPeng Fan 	} else {
10143697e599SPeng Fan 		debug("Invalid Allocation Unit Size.\n");
10153697e599SPeng Fan 	}
10163697e599SPeng Fan 
10173697e599SPeng Fan 	return 0;
10183697e599SPeng Fan }
10193697e599SPeng Fan 
1020272cc70bSAndy Fleming /* frequency bases */
1021272cc70bSAndy Fleming /* divided by 10 to be nice to platforms without floating point */
10225f837c2cSMike Frysinger static const int fbase[] = {
1023272cc70bSAndy Fleming 	10000,
1024272cc70bSAndy Fleming 	100000,
1025272cc70bSAndy Fleming 	1000000,
1026272cc70bSAndy Fleming 	10000000,
1027272cc70bSAndy Fleming };
1028272cc70bSAndy Fleming 
1029272cc70bSAndy Fleming /* Multiplier values for TRAN_SPEED.  Multiplied by 10 to be nice
1030272cc70bSAndy Fleming  * to platforms without floating point.
1031272cc70bSAndy Fleming  */
103261fe076fSSimon Glass static const u8 multipliers[] = {
1033272cc70bSAndy Fleming 	0,	/* reserved */
1034272cc70bSAndy Fleming 	10,
1035272cc70bSAndy Fleming 	12,
1036272cc70bSAndy Fleming 	13,
1037272cc70bSAndy Fleming 	15,
1038272cc70bSAndy Fleming 	20,
1039272cc70bSAndy Fleming 	25,
1040272cc70bSAndy Fleming 	30,
1041272cc70bSAndy Fleming 	35,
1042272cc70bSAndy Fleming 	40,
1043272cc70bSAndy Fleming 	45,
1044272cc70bSAndy Fleming 	50,
1045272cc70bSAndy Fleming 	55,
1046272cc70bSAndy Fleming 	60,
1047272cc70bSAndy Fleming 	70,
1048272cc70bSAndy Fleming 	80,
1049272cc70bSAndy Fleming };
1050272cc70bSAndy Fleming 
10518ca51e51SSimon Glass #ifndef CONFIG_DM_MMC_OPS
1052fdbb873eSKim Phillips static void mmc_set_ios(struct mmc *mmc)
1053272cc70bSAndy Fleming {
105493bfd616SPantelis Antoniou 	if (mmc->cfg->ops->set_ios)
105593bfd616SPantelis Antoniou 		mmc->cfg->ops->set_ios(mmc);
1056272cc70bSAndy Fleming }
10578ca51e51SSimon Glass #endif
1058272cc70bSAndy Fleming 
1059272cc70bSAndy Fleming void mmc_set_clock(struct mmc *mmc, uint clock)
1060272cc70bSAndy Fleming {
106193bfd616SPantelis Antoniou 	if (clock > mmc->cfg->f_max)
106293bfd616SPantelis Antoniou 		clock = mmc->cfg->f_max;
1063272cc70bSAndy Fleming 
106493bfd616SPantelis Antoniou 	if (clock < mmc->cfg->f_min)
106593bfd616SPantelis Antoniou 		clock = mmc->cfg->f_min;
1066272cc70bSAndy Fleming 
1067272cc70bSAndy Fleming 	mmc->clock = clock;
1068272cc70bSAndy Fleming 
1069272cc70bSAndy Fleming 	mmc_set_ios(mmc);
1070272cc70bSAndy Fleming }
1071272cc70bSAndy Fleming 
1072fdbb873eSKim Phillips static void mmc_set_bus_width(struct mmc *mmc, uint width)
1073272cc70bSAndy Fleming {
1074272cc70bSAndy Fleming 	mmc->bus_width = width;
1075272cc70bSAndy Fleming 
1076272cc70bSAndy Fleming 	mmc_set_ios(mmc);
1077272cc70bSAndy Fleming }
1078272cc70bSAndy Fleming 
1079fdbb873eSKim Phillips static int mmc_startup(struct mmc *mmc)
1080272cc70bSAndy Fleming {
1081f866a46dSStephen Warren 	int err, i;
1082272cc70bSAndy Fleming 	uint mult, freq;
1083639b7827SYoshihiro Shimoda 	u64 cmult, csize, capacity;
1084272cc70bSAndy Fleming 	struct mmc_cmd cmd;
10858bfa195eSSimon Glass 	ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
10868bfa195eSSimon Glass 	ALLOC_CACHE_ALIGN_BUFFER(u8, test_csd, MMC_MAX_BLOCK_LEN);
10875d4fc8d9SRaffaele Recalcati 	int timeout = 1000;
10880c453bb7SDiego Santa Cruz 	bool has_parts = false;
10898a0cf490SDiego Santa Cruz 	bool part_completed;
1090c40fdca6SSimon Glass 	struct blk_desc *bdesc;
1091272cc70bSAndy Fleming 
1092d52ebf10SThomas Chou #ifdef CONFIG_MMC_SPI_CRC_ON
1093d52ebf10SThomas Chou 	if (mmc_host_is_spi(mmc)) { /* enable CRC check for spi */
1094d52ebf10SThomas Chou 		cmd.cmdidx = MMC_CMD_SPI_CRC_ON_OFF;
1095d52ebf10SThomas Chou 		cmd.resp_type = MMC_RSP_R1;
1096d52ebf10SThomas Chou 		cmd.cmdarg = 1;
1097d52ebf10SThomas Chou 		err = mmc_send_cmd(mmc, &cmd, NULL);
1098d52ebf10SThomas Chou 
1099d52ebf10SThomas Chou 		if (err)
1100d52ebf10SThomas Chou 			return err;
1101d52ebf10SThomas Chou 	}
1102d52ebf10SThomas Chou #endif
1103d52ebf10SThomas Chou 
1104272cc70bSAndy Fleming 	/* Put the Card in Identify Mode */
1105d52ebf10SThomas Chou 	cmd.cmdidx = mmc_host_is_spi(mmc) ? MMC_CMD_SEND_CID :
1106d52ebf10SThomas Chou 		MMC_CMD_ALL_SEND_CID; /* cmd not supported in spi */
1107272cc70bSAndy Fleming 	cmd.resp_type = MMC_RSP_R2;
1108272cc70bSAndy Fleming 	cmd.cmdarg = 0;
1109272cc70bSAndy Fleming 
1110272cc70bSAndy Fleming 	err = mmc_send_cmd(mmc, &cmd, NULL);
1111272cc70bSAndy Fleming 
1112272cc70bSAndy Fleming 	if (err)
1113272cc70bSAndy Fleming 		return err;
1114272cc70bSAndy Fleming 
1115272cc70bSAndy Fleming 	memcpy(mmc->cid, cmd.response, 16);
1116272cc70bSAndy Fleming 
1117272cc70bSAndy Fleming 	/*
1118272cc70bSAndy Fleming 	 * For MMC cards, set the Relative Address.
1119272cc70bSAndy Fleming 	 * For SD cards, get the Relatvie Address.
1120272cc70bSAndy Fleming 	 * This also puts the cards into Standby State
1121272cc70bSAndy Fleming 	 */
1122d52ebf10SThomas Chou 	if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
1123272cc70bSAndy Fleming 		cmd.cmdidx = SD_CMD_SEND_RELATIVE_ADDR;
1124272cc70bSAndy Fleming 		cmd.cmdarg = mmc->rca << 16;
1125272cc70bSAndy Fleming 		cmd.resp_type = MMC_RSP_R6;
1126272cc70bSAndy Fleming 
1127272cc70bSAndy Fleming 		err = mmc_send_cmd(mmc, &cmd, NULL);
1128272cc70bSAndy Fleming 
1129272cc70bSAndy Fleming 		if (err)
1130272cc70bSAndy Fleming 			return err;
1131272cc70bSAndy Fleming 
1132272cc70bSAndy Fleming 		if (IS_SD(mmc))
1133998be3ddSRabin Vincent 			mmc->rca = (cmd.response[0] >> 16) & 0xffff;
1134d52ebf10SThomas Chou 	}
1135272cc70bSAndy Fleming 
1136272cc70bSAndy Fleming 	/* Get the Card-Specific Data */
1137272cc70bSAndy Fleming 	cmd.cmdidx = MMC_CMD_SEND_CSD;
1138272cc70bSAndy Fleming 	cmd.resp_type = MMC_RSP_R2;
1139272cc70bSAndy Fleming 	cmd.cmdarg = mmc->rca << 16;
1140272cc70bSAndy Fleming 
1141272cc70bSAndy Fleming 	err = mmc_send_cmd(mmc, &cmd, NULL);
1142272cc70bSAndy Fleming 
11435d4fc8d9SRaffaele Recalcati 	/* Waiting for the ready status */
11445d4fc8d9SRaffaele Recalcati 	mmc_send_status(mmc, timeout);
11455d4fc8d9SRaffaele Recalcati 
1146272cc70bSAndy Fleming 	if (err)
1147272cc70bSAndy Fleming 		return err;
1148272cc70bSAndy Fleming 
1149998be3ddSRabin Vincent 	mmc->csd[0] = cmd.response[0];
1150998be3ddSRabin Vincent 	mmc->csd[1] = cmd.response[1];
1151998be3ddSRabin Vincent 	mmc->csd[2] = cmd.response[2];
1152998be3ddSRabin Vincent 	mmc->csd[3] = cmd.response[3];
1153272cc70bSAndy Fleming 
1154272cc70bSAndy Fleming 	if (mmc->version == MMC_VERSION_UNKNOWN) {
11550b453ffeSRabin Vincent 		int version = (cmd.response[0] >> 26) & 0xf;
1156272cc70bSAndy Fleming 
1157272cc70bSAndy Fleming 		switch (version) {
1158272cc70bSAndy Fleming 		case 0:
1159272cc70bSAndy Fleming 			mmc->version = MMC_VERSION_1_2;
1160272cc70bSAndy Fleming 			break;
1161272cc70bSAndy Fleming 		case 1:
1162272cc70bSAndy Fleming 			mmc->version = MMC_VERSION_1_4;
1163272cc70bSAndy Fleming 			break;
1164272cc70bSAndy Fleming 		case 2:
1165272cc70bSAndy Fleming 			mmc->version = MMC_VERSION_2_2;
1166272cc70bSAndy Fleming 			break;
1167272cc70bSAndy Fleming 		case 3:
1168272cc70bSAndy Fleming 			mmc->version = MMC_VERSION_3;
1169272cc70bSAndy Fleming 			break;
1170272cc70bSAndy Fleming 		case 4:
1171272cc70bSAndy Fleming 			mmc->version = MMC_VERSION_4;
1172272cc70bSAndy Fleming 			break;
1173272cc70bSAndy Fleming 		default:
1174272cc70bSAndy Fleming 			mmc->version = MMC_VERSION_1_2;
1175272cc70bSAndy Fleming 			break;
1176272cc70bSAndy Fleming 		}
1177272cc70bSAndy Fleming 	}
1178272cc70bSAndy Fleming 
1179272cc70bSAndy Fleming 	/* divide frequency by 10, since the mults are 10x bigger */
11800b453ffeSRabin Vincent 	freq = fbase[(cmd.response[0] & 0x7)];
11810b453ffeSRabin Vincent 	mult = multipliers[((cmd.response[0] >> 3) & 0xf)];
1182272cc70bSAndy Fleming 
1183272cc70bSAndy Fleming 	mmc->tran_speed = freq * mult;
1184272cc70bSAndy Fleming 
1185ab71188cSMarkus Niebel 	mmc->dsr_imp = ((cmd.response[1] >> 12) & 0x1);
1186998be3ddSRabin Vincent 	mmc->read_bl_len = 1 << ((cmd.response[1] >> 16) & 0xf);
1187272cc70bSAndy Fleming 
1188272cc70bSAndy Fleming 	if (IS_SD(mmc))
1189272cc70bSAndy Fleming 		mmc->write_bl_len = mmc->read_bl_len;
1190272cc70bSAndy Fleming 	else
1191998be3ddSRabin Vincent 		mmc->write_bl_len = 1 << ((cmd.response[3] >> 22) & 0xf);
1192272cc70bSAndy Fleming 
1193272cc70bSAndy Fleming 	if (mmc->high_capacity) {
1194272cc70bSAndy Fleming 		csize = (mmc->csd[1] & 0x3f) << 16
1195272cc70bSAndy Fleming 			| (mmc->csd[2] & 0xffff0000) >> 16;
1196272cc70bSAndy Fleming 		cmult = 8;
1197272cc70bSAndy Fleming 	} else {
1198272cc70bSAndy Fleming 		csize = (mmc->csd[1] & 0x3ff) << 2
1199272cc70bSAndy Fleming 			| (mmc->csd[2] & 0xc0000000) >> 30;
1200272cc70bSAndy Fleming 		cmult = (mmc->csd[2] & 0x00038000) >> 15;
1201272cc70bSAndy Fleming 	}
1202272cc70bSAndy Fleming 
1203f866a46dSStephen Warren 	mmc->capacity_user = (csize + 1) << (cmult + 2);
1204f866a46dSStephen Warren 	mmc->capacity_user *= mmc->read_bl_len;
1205f866a46dSStephen Warren 	mmc->capacity_boot = 0;
1206f866a46dSStephen Warren 	mmc->capacity_rpmb = 0;
1207f866a46dSStephen Warren 	for (i = 0; i < 4; i++)
1208f866a46dSStephen Warren 		mmc->capacity_gp[i] = 0;
1209272cc70bSAndy Fleming 
12108bfa195eSSimon Glass 	if (mmc->read_bl_len > MMC_MAX_BLOCK_LEN)
12118bfa195eSSimon Glass 		mmc->read_bl_len = MMC_MAX_BLOCK_LEN;
1212272cc70bSAndy Fleming 
12138bfa195eSSimon Glass 	if (mmc->write_bl_len > MMC_MAX_BLOCK_LEN)
12148bfa195eSSimon Glass 		mmc->write_bl_len = MMC_MAX_BLOCK_LEN;
1215272cc70bSAndy Fleming 
1216ab71188cSMarkus Niebel 	if ((mmc->dsr_imp) && (0xffffffff != mmc->dsr)) {
1217ab71188cSMarkus Niebel 		cmd.cmdidx = MMC_CMD_SET_DSR;
1218ab71188cSMarkus Niebel 		cmd.cmdarg = (mmc->dsr & 0xffff) << 16;
1219ab71188cSMarkus Niebel 		cmd.resp_type = MMC_RSP_NONE;
1220ab71188cSMarkus Niebel 		if (mmc_send_cmd(mmc, &cmd, NULL))
1221ab71188cSMarkus Niebel 			printf("MMC: SET_DSR failed\n");
1222ab71188cSMarkus Niebel 	}
1223ab71188cSMarkus Niebel 
1224272cc70bSAndy Fleming 	/* Select the card, and put it into Transfer Mode */
1225d52ebf10SThomas Chou 	if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
1226272cc70bSAndy Fleming 		cmd.cmdidx = MMC_CMD_SELECT_CARD;
1227fe8f7066SAjay Bhargav 		cmd.resp_type = MMC_RSP_R1;
1228272cc70bSAndy Fleming 		cmd.cmdarg = mmc->rca << 16;
1229272cc70bSAndy Fleming 		err = mmc_send_cmd(mmc, &cmd, NULL);
1230272cc70bSAndy Fleming 
1231272cc70bSAndy Fleming 		if (err)
1232272cc70bSAndy Fleming 			return err;
1233d52ebf10SThomas Chou 	}
1234272cc70bSAndy Fleming 
1235e6f99a56SLei Wen 	/*
1236e6f99a56SLei Wen 	 * For SD, its erase group is always one sector
1237e6f99a56SLei Wen 	 */
1238e6f99a56SLei Wen 	mmc->erase_grp_size = 1;
1239bc897b1dSLei Wen 	mmc->part_config = MMCPART_NOAVAILABLE;
1240d23e2c09SSukumar Ghorai 	if (!IS_SD(mmc) && (mmc->version >= MMC_VERSION_4)) {
1241d23e2c09SSukumar Ghorai 		/* check  ext_csd version and capacity */
1242d23e2c09SSukumar Ghorai 		err = mmc_send_ext_csd(mmc, ext_csd);
12439cf199ebSDiego Santa Cruz 		if (err)
12449cf199ebSDiego Santa Cruz 			return err;
12459cf199ebSDiego Santa Cruz 		if (ext_csd[EXT_CSD_REV] >= 2) {
1246639b7827SYoshihiro Shimoda 			/*
1247639b7827SYoshihiro Shimoda 			 * According to the JEDEC Standard, the value of
1248639b7827SYoshihiro Shimoda 			 * ext_csd's capacity is valid if the value is more
1249639b7827SYoshihiro Shimoda 			 * than 2GB
1250639b7827SYoshihiro Shimoda 			 */
12510560db18SLei Wen 			capacity = ext_csd[EXT_CSD_SEC_CNT] << 0
12520560db18SLei Wen 					| ext_csd[EXT_CSD_SEC_CNT + 1] << 8
12530560db18SLei Wen 					| ext_csd[EXT_CSD_SEC_CNT + 2] << 16
12540560db18SLei Wen 					| ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
12558bfa195eSSimon Glass 			capacity *= MMC_MAX_BLOCK_LEN;
1256b1f1e821SŁukasz Majewski 			if ((capacity >> 20) > 2 * 1024)
1257f866a46dSStephen Warren 				mmc->capacity_user = capacity;
1258d23e2c09SSukumar Ghorai 		}
1259bc897b1dSLei Wen 
126064f4a619SJaehoon Chung 		switch (ext_csd[EXT_CSD_REV]) {
126164f4a619SJaehoon Chung 		case 1:
126264f4a619SJaehoon Chung 			mmc->version = MMC_VERSION_4_1;
126364f4a619SJaehoon Chung 			break;
126464f4a619SJaehoon Chung 		case 2:
126564f4a619SJaehoon Chung 			mmc->version = MMC_VERSION_4_2;
126664f4a619SJaehoon Chung 			break;
126764f4a619SJaehoon Chung 		case 3:
126864f4a619SJaehoon Chung 			mmc->version = MMC_VERSION_4_3;
126964f4a619SJaehoon Chung 			break;
127064f4a619SJaehoon Chung 		case 5:
127164f4a619SJaehoon Chung 			mmc->version = MMC_VERSION_4_41;
127264f4a619SJaehoon Chung 			break;
127364f4a619SJaehoon Chung 		case 6:
127464f4a619SJaehoon Chung 			mmc->version = MMC_VERSION_4_5;
127564f4a619SJaehoon Chung 			break;
1276edab723bSMarkus Niebel 		case 7:
1277edab723bSMarkus Niebel 			mmc->version = MMC_VERSION_5_0;
1278edab723bSMarkus Niebel 			break;
12791a3619cfSStefan Wahren 		case 8:
12801a3619cfSStefan Wahren 			mmc->version = MMC_VERSION_5_1;
12811a3619cfSStefan Wahren 			break;
128264f4a619SJaehoon Chung 		}
128364f4a619SJaehoon Chung 
12848a0cf490SDiego Santa Cruz 		/* The partition data may be non-zero but it is only
12858a0cf490SDiego Santa Cruz 		 * effective if PARTITION_SETTING_COMPLETED is set in
12868a0cf490SDiego Santa Cruz 		 * EXT_CSD, so ignore any data if this bit is not set,
12878a0cf490SDiego Santa Cruz 		 * except for enabling the high-capacity group size
12888a0cf490SDiego Santa Cruz 		 * definition (see below). */
12898a0cf490SDiego Santa Cruz 		part_completed = !!(ext_csd[EXT_CSD_PARTITION_SETTING] &
12908a0cf490SDiego Santa Cruz 				    EXT_CSD_PARTITION_SETTING_COMPLETED);
12918a0cf490SDiego Santa Cruz 
12920c453bb7SDiego Santa Cruz 		/* store the partition info of emmc */
12930c453bb7SDiego Santa Cruz 		mmc->part_support = ext_csd[EXT_CSD_PARTITIONING_SUPPORT];
12940c453bb7SDiego Santa Cruz 		if ((ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & PART_SUPPORT) ||
12950c453bb7SDiego Santa Cruz 		    ext_csd[EXT_CSD_BOOT_MULT])
12960c453bb7SDiego Santa Cruz 			mmc->part_config = ext_csd[EXT_CSD_PART_CONF];
12978a0cf490SDiego Santa Cruz 		if (part_completed &&
12988a0cf490SDiego Santa Cruz 		    (ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & ENHNCD_SUPPORT))
12990c453bb7SDiego Santa Cruz 			mmc->part_attr = ext_csd[EXT_CSD_PARTITIONS_ATTRIBUTE];
13000c453bb7SDiego Santa Cruz 
13010c453bb7SDiego Santa Cruz 		mmc->capacity_boot = ext_csd[EXT_CSD_BOOT_MULT] << 17;
13020c453bb7SDiego Santa Cruz 
13030c453bb7SDiego Santa Cruz 		mmc->capacity_rpmb = ext_csd[EXT_CSD_RPMB_MULT] << 17;
13040c453bb7SDiego Santa Cruz 
13050c453bb7SDiego Santa Cruz 		for (i = 0; i < 4; i++) {
13060c453bb7SDiego Santa Cruz 			int idx = EXT_CSD_GP_SIZE_MULT + i * 3;
13078a0cf490SDiego Santa Cruz 			uint mult = (ext_csd[idx + 2] << 16) +
13080c453bb7SDiego Santa Cruz 				(ext_csd[idx + 1] << 8) + ext_csd[idx];
13098a0cf490SDiego Santa Cruz 			if (mult)
13108a0cf490SDiego Santa Cruz 				has_parts = true;
13118a0cf490SDiego Santa Cruz 			if (!part_completed)
13128a0cf490SDiego Santa Cruz 				continue;
13138a0cf490SDiego Santa Cruz 			mmc->capacity_gp[i] = mult;
13140c453bb7SDiego Santa Cruz 			mmc->capacity_gp[i] *=
13150c453bb7SDiego Santa Cruz 				ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
13160c453bb7SDiego Santa Cruz 			mmc->capacity_gp[i] *= ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
1317f8e89d67SDiego Santa Cruz 			mmc->capacity_gp[i] <<= 19;
13180c453bb7SDiego Santa Cruz 		}
13190c453bb7SDiego Santa Cruz 
13208a0cf490SDiego Santa Cruz 		if (part_completed) {
1321a7f852b6SDiego Santa Cruz 			mmc->enh_user_size =
1322a7f852b6SDiego Santa Cruz 				(ext_csd[EXT_CSD_ENH_SIZE_MULT+2] << 16) +
1323a7f852b6SDiego Santa Cruz 				(ext_csd[EXT_CSD_ENH_SIZE_MULT+1] << 8) +
1324a7f852b6SDiego Santa Cruz 				ext_csd[EXT_CSD_ENH_SIZE_MULT];
1325a7f852b6SDiego Santa Cruz 			mmc->enh_user_size *= ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
1326a7f852b6SDiego Santa Cruz 			mmc->enh_user_size *= ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
1327a7f852b6SDiego Santa Cruz 			mmc->enh_user_size <<= 19;
1328a7f852b6SDiego Santa Cruz 			mmc->enh_user_start =
1329a7f852b6SDiego Santa Cruz 				(ext_csd[EXT_CSD_ENH_START_ADDR+3] << 24) +
1330a7f852b6SDiego Santa Cruz 				(ext_csd[EXT_CSD_ENH_START_ADDR+2] << 16) +
1331a7f852b6SDiego Santa Cruz 				(ext_csd[EXT_CSD_ENH_START_ADDR+1] << 8) +
1332a7f852b6SDiego Santa Cruz 				ext_csd[EXT_CSD_ENH_START_ADDR];
1333a7f852b6SDiego Santa Cruz 			if (mmc->high_capacity)
1334a7f852b6SDiego Santa Cruz 				mmc->enh_user_start <<= 9;
13358a0cf490SDiego Santa Cruz 		}
1336a7f852b6SDiego Santa Cruz 
1337e6f99a56SLei Wen 		/*
13381937e5aaSOliver Metz 		 * Host needs to enable ERASE_GRP_DEF bit if device is
13391937e5aaSOliver Metz 		 * partitioned. This bit will be lost every time after a reset
13401937e5aaSOliver Metz 		 * or power off. This will affect erase size.
1341e6f99a56SLei Wen 		 */
13428a0cf490SDiego Santa Cruz 		if (part_completed)
13430c453bb7SDiego Santa Cruz 			has_parts = true;
13441937e5aaSOliver Metz 		if ((ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & PART_SUPPORT) &&
13450c453bb7SDiego Santa Cruz 		    (ext_csd[EXT_CSD_PARTITIONS_ATTRIBUTE] & PART_ENH_ATTRIB))
13460c453bb7SDiego Santa Cruz 			has_parts = true;
13470c453bb7SDiego Santa Cruz 		if (has_parts) {
13481937e5aaSOliver Metz 			err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
13491937e5aaSOliver Metz 				EXT_CSD_ERASE_GROUP_DEF, 1);
13501937e5aaSOliver Metz 
13511937e5aaSOliver Metz 			if (err)
13521937e5aaSOliver Metz 				return err;
1353021a8055SHannes Petermaier 			else
1354021a8055SHannes Petermaier 				ext_csd[EXT_CSD_ERASE_GROUP_DEF] = 1;
1355037dc0abSDiego Santa Cruz 		}
13561937e5aaSOliver Metz 
1357037dc0abSDiego Santa Cruz 		if (ext_csd[EXT_CSD_ERASE_GROUP_DEF] & 0x01) {
13581937e5aaSOliver Metz 			/* Read out group size from ext_csd */
13590560db18SLei Wen 			mmc->erase_grp_size =
1360a4ff9f83SDiego Santa Cruz 				ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] * 1024;
1361d7b29129SMarkus Niebel 			/*
1362d7b29129SMarkus Niebel 			 * if high capacity and partition setting completed
1363d7b29129SMarkus Niebel 			 * SEC_COUNT is valid even if it is smaller than 2 GiB
1364d7b29129SMarkus Niebel 			 * JEDEC Standard JESD84-B45, 6.2.4
1365d7b29129SMarkus Niebel 			 */
13668a0cf490SDiego Santa Cruz 			if (mmc->high_capacity && part_completed) {
1367d7b29129SMarkus Niebel 				capacity = (ext_csd[EXT_CSD_SEC_CNT]) |
1368d7b29129SMarkus Niebel 					(ext_csd[EXT_CSD_SEC_CNT + 1] << 8) |
1369d7b29129SMarkus Niebel 					(ext_csd[EXT_CSD_SEC_CNT + 2] << 16) |
1370d7b29129SMarkus Niebel 					(ext_csd[EXT_CSD_SEC_CNT + 3] << 24);
1371d7b29129SMarkus Niebel 				capacity *= MMC_MAX_BLOCK_LEN;
1372d7b29129SMarkus Niebel 				mmc->capacity_user = capacity;
1373d7b29129SMarkus Niebel 			}
13748bfa195eSSimon Glass 		} else {
13751937e5aaSOliver Metz 			/* Calculate the group size from the csd value. */
1376e6f99a56SLei Wen 			int erase_gsz, erase_gmul;
1377e6f99a56SLei Wen 			erase_gsz = (mmc->csd[2] & 0x00007c00) >> 10;
1378e6f99a56SLei Wen 			erase_gmul = (mmc->csd[2] & 0x000003e0) >> 5;
1379e6f99a56SLei Wen 			mmc->erase_grp_size = (erase_gsz + 1)
1380e6f99a56SLei Wen 				* (erase_gmul + 1);
1381e6f99a56SLei Wen 		}
1382037dc0abSDiego Santa Cruz 
1383037dc0abSDiego Santa Cruz 		mmc->hc_wp_grp_size = 1024
1384037dc0abSDiego Santa Cruz 			* ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]
1385037dc0abSDiego Santa Cruz 			* ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
13869e41a00bSDiego Santa Cruz 
13879e41a00bSDiego Santa Cruz 		mmc->wr_rel_set = ext_csd[EXT_CSD_WR_REL_SET];
1388f866a46dSStephen Warren 	}
1389f866a46dSStephen Warren 
1390c40fdca6SSimon Glass 	err = mmc_set_capacity(mmc, mmc_get_blk_desc(mmc)->hwpart);
1391f866a46dSStephen Warren 	if (err)
1392f866a46dSStephen Warren 		return err;
1393d23e2c09SSukumar Ghorai 
1394272cc70bSAndy Fleming 	if (IS_SD(mmc))
1395272cc70bSAndy Fleming 		err = sd_change_freq(mmc);
1396272cc70bSAndy Fleming 	else
1397272cc70bSAndy Fleming 		err = mmc_change_freq(mmc);
1398272cc70bSAndy Fleming 
1399272cc70bSAndy Fleming 	if (err)
1400272cc70bSAndy Fleming 		return err;
1401272cc70bSAndy Fleming 
1402272cc70bSAndy Fleming 	/* Restrict card's capabilities by what the host can do */
140393bfd616SPantelis Antoniou 	mmc->card_caps &= mmc->cfg->host_caps;
1404272cc70bSAndy Fleming 
1405272cc70bSAndy Fleming 	if (IS_SD(mmc)) {
1406272cc70bSAndy Fleming 		if (mmc->card_caps & MMC_MODE_4BIT) {
1407272cc70bSAndy Fleming 			cmd.cmdidx = MMC_CMD_APP_CMD;
1408272cc70bSAndy Fleming 			cmd.resp_type = MMC_RSP_R1;
1409272cc70bSAndy Fleming 			cmd.cmdarg = mmc->rca << 16;
1410272cc70bSAndy Fleming 
1411272cc70bSAndy Fleming 			err = mmc_send_cmd(mmc, &cmd, NULL);
1412272cc70bSAndy Fleming 			if (err)
1413272cc70bSAndy Fleming 				return err;
1414272cc70bSAndy Fleming 
1415272cc70bSAndy Fleming 			cmd.cmdidx = SD_CMD_APP_SET_BUS_WIDTH;
1416272cc70bSAndy Fleming 			cmd.resp_type = MMC_RSP_R1;
1417272cc70bSAndy Fleming 			cmd.cmdarg = 2;
1418272cc70bSAndy Fleming 			err = mmc_send_cmd(mmc, &cmd, NULL);
1419272cc70bSAndy Fleming 			if (err)
1420272cc70bSAndy Fleming 				return err;
1421272cc70bSAndy Fleming 
1422272cc70bSAndy Fleming 			mmc_set_bus_width(mmc, 4);
1423272cc70bSAndy Fleming 		}
1424272cc70bSAndy Fleming 
14253697e599SPeng Fan 		err = sd_read_ssr(mmc);
14263697e599SPeng Fan 		if (err)
14273697e599SPeng Fan 			return err;
14283697e599SPeng Fan 
1429272cc70bSAndy Fleming 		if (mmc->card_caps & MMC_MODE_HS)
1430ad5fd922SJaehoon Chung 			mmc->tran_speed = 50000000;
1431272cc70bSAndy Fleming 		else
1432ad5fd922SJaehoon Chung 			mmc->tran_speed = 25000000;
1433fc5b32fbSAndrew Gabbasov 	} else if (mmc->version >= MMC_VERSION_4) {
1434fc5b32fbSAndrew Gabbasov 		/* Only version 4 of MMC supports wider bus widths */
14357798f6dbSAndy Fleming 		int idx;
14367798f6dbSAndy Fleming 
14377798f6dbSAndy Fleming 		/* An array of possible bus widths in order of preference */
14387798f6dbSAndy Fleming 		static unsigned ext_csd_bits[] = {
1439d22e3d46SJaehoon Chung 			EXT_CSD_DDR_BUS_WIDTH_8,
1440d22e3d46SJaehoon Chung 			EXT_CSD_DDR_BUS_WIDTH_4,
14417798f6dbSAndy Fleming 			EXT_CSD_BUS_WIDTH_8,
14427798f6dbSAndy Fleming 			EXT_CSD_BUS_WIDTH_4,
14437798f6dbSAndy Fleming 			EXT_CSD_BUS_WIDTH_1,
14447798f6dbSAndy Fleming 		};
14457798f6dbSAndy Fleming 
14467798f6dbSAndy Fleming 		/* An array to map CSD bus widths to host cap bits */
14477798f6dbSAndy Fleming 		static unsigned ext_to_hostcaps[] = {
1448786e8f81SAndrew Gabbasov 			[EXT_CSD_DDR_BUS_WIDTH_4] =
1449786e8f81SAndrew Gabbasov 				MMC_MODE_DDR_52MHz | MMC_MODE_4BIT,
1450786e8f81SAndrew Gabbasov 			[EXT_CSD_DDR_BUS_WIDTH_8] =
1451786e8f81SAndrew Gabbasov 				MMC_MODE_DDR_52MHz | MMC_MODE_8BIT,
14527798f6dbSAndy Fleming 			[EXT_CSD_BUS_WIDTH_4] = MMC_MODE_4BIT,
14537798f6dbSAndy Fleming 			[EXT_CSD_BUS_WIDTH_8] = MMC_MODE_8BIT,
14547798f6dbSAndy Fleming 		};
14557798f6dbSAndy Fleming 
14567798f6dbSAndy Fleming 		/* An array to map chosen bus width to an integer */
14577798f6dbSAndy Fleming 		static unsigned widths[] = {
1458d22e3d46SJaehoon Chung 			8, 4, 8, 4, 1,
14597798f6dbSAndy Fleming 		};
14607798f6dbSAndy Fleming 
14617798f6dbSAndy Fleming 		for (idx=0; idx < ARRAY_SIZE(ext_csd_bits); idx++) {
14627798f6dbSAndy Fleming 			unsigned int extw = ext_csd_bits[idx];
1463786e8f81SAndrew Gabbasov 			unsigned int caps = ext_to_hostcaps[extw];
14647798f6dbSAndy Fleming 
14657798f6dbSAndy Fleming 			/*
1466bf477073SAndrew Gabbasov 			 * If the bus width is still not changed,
1467bf477073SAndrew Gabbasov 			 * don't try to set the default again.
1468bf477073SAndrew Gabbasov 			 * Otherwise, recover from switch attempts
1469bf477073SAndrew Gabbasov 			 * by switching to 1-bit bus width.
1470bf477073SAndrew Gabbasov 			 */
1471bf477073SAndrew Gabbasov 			if (extw == EXT_CSD_BUS_WIDTH_1 &&
1472bf477073SAndrew Gabbasov 					mmc->bus_width == 1) {
1473bf477073SAndrew Gabbasov 				err = 0;
1474bf477073SAndrew Gabbasov 				break;
1475bf477073SAndrew Gabbasov 			}
1476bf477073SAndrew Gabbasov 
1477bf477073SAndrew Gabbasov 			/*
1478786e8f81SAndrew Gabbasov 			 * Check to make sure the card and controller support
1479786e8f81SAndrew Gabbasov 			 * these capabilities
14807798f6dbSAndy Fleming 			 */
1481786e8f81SAndrew Gabbasov 			if ((mmc->card_caps & caps) != caps)
14827798f6dbSAndy Fleming 				continue;
14837798f6dbSAndy Fleming 
1484272cc70bSAndy Fleming 			err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
14857798f6dbSAndy Fleming 					EXT_CSD_BUS_WIDTH, extw);
1486272cc70bSAndy Fleming 
1487272cc70bSAndy Fleming 			if (err)
14884137894eSLei Wen 				continue;
1489272cc70bSAndy Fleming 
1490786e8f81SAndrew Gabbasov 			mmc->ddr_mode = (caps & MMC_MODE_DDR_52MHz) ? 1 : 0;
14917798f6dbSAndy Fleming 			mmc_set_bus_width(mmc, widths[idx]);
1492272cc70bSAndy Fleming 
14934137894eSLei Wen 			err = mmc_send_ext_csd(mmc, test_csd);
1494272cc70bSAndy Fleming 
1495786e8f81SAndrew Gabbasov 			if (err)
1496786e8f81SAndrew Gabbasov 				continue;
1497786e8f81SAndrew Gabbasov 
1498786e8f81SAndrew Gabbasov 			/* Only compare read only fields */
1499786e8f81SAndrew Gabbasov 			if (ext_csd[EXT_CSD_PARTITIONING_SUPPORT]
1500786e8f81SAndrew Gabbasov 				== test_csd[EXT_CSD_PARTITIONING_SUPPORT] &&
1501786e8f81SAndrew Gabbasov 			    ext_csd[EXT_CSD_HC_WP_GRP_SIZE]
1502786e8f81SAndrew Gabbasov 				== test_csd[EXT_CSD_HC_WP_GRP_SIZE] &&
1503786e8f81SAndrew Gabbasov 			    ext_csd[EXT_CSD_REV]
1504786e8f81SAndrew Gabbasov 				== test_csd[EXT_CSD_REV] &&
1505786e8f81SAndrew Gabbasov 			    ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]
1506786e8f81SAndrew Gabbasov 				== test_csd[EXT_CSD_HC_ERASE_GRP_SIZE] &&
1507786e8f81SAndrew Gabbasov 			    memcmp(&ext_csd[EXT_CSD_SEC_CNT],
1508786e8f81SAndrew Gabbasov 				   &test_csd[EXT_CSD_SEC_CNT], 4) == 0)
15094137894eSLei Wen 				break;
1510786e8f81SAndrew Gabbasov 			else
1511915ffa52SJaehoon Chung 				err = -EBADMSG;
15124137894eSLei Wen 		}
1513786e8f81SAndrew Gabbasov 
1514786e8f81SAndrew Gabbasov 		if (err)
1515786e8f81SAndrew Gabbasov 			return err;
1516272cc70bSAndy Fleming 
1517272cc70bSAndy Fleming 		if (mmc->card_caps & MMC_MODE_HS) {
1518272cc70bSAndy Fleming 			if (mmc->card_caps & MMC_MODE_HS_52MHz)
1519ad5fd922SJaehoon Chung 				mmc->tran_speed = 52000000;
1520272cc70bSAndy Fleming 			else
1521ad5fd922SJaehoon Chung 				mmc->tran_speed = 26000000;
1522272cc70bSAndy Fleming 		}
1523ad5fd922SJaehoon Chung 	}
1524ad5fd922SJaehoon Chung 
1525ad5fd922SJaehoon Chung 	mmc_set_clock(mmc, mmc->tran_speed);
1526272cc70bSAndy Fleming 
15275af8f45cSAndrew Gabbasov 	/* Fix the block length for DDR mode */
15285af8f45cSAndrew Gabbasov 	if (mmc->ddr_mode) {
15295af8f45cSAndrew Gabbasov 		mmc->read_bl_len = MMC_MAX_BLOCK_LEN;
15305af8f45cSAndrew Gabbasov 		mmc->write_bl_len = MMC_MAX_BLOCK_LEN;
15315af8f45cSAndrew Gabbasov 	}
15325af8f45cSAndrew Gabbasov 
1533272cc70bSAndy Fleming 	/* fill in device description */
1534c40fdca6SSimon Glass 	bdesc = mmc_get_blk_desc(mmc);
1535c40fdca6SSimon Glass 	bdesc->lun = 0;
1536c40fdca6SSimon Glass 	bdesc->hwpart = 0;
1537c40fdca6SSimon Glass 	bdesc->type = 0;
1538c40fdca6SSimon Glass 	bdesc->blksz = mmc->read_bl_len;
1539c40fdca6SSimon Glass 	bdesc->log2blksz = LOG2(bdesc->blksz);
1540c40fdca6SSimon Glass 	bdesc->lba = lldiv(mmc->capacity, mmc->read_bl_len);
1541fc011f64SSjoerd Simons #if !defined(CONFIG_SPL_BUILD) || \
1542fc011f64SSjoerd Simons 		(defined(CONFIG_SPL_LIBCOMMON_SUPPORT) && \
1543fc011f64SSjoerd Simons 		!defined(CONFIG_USE_TINY_PRINTF))
1544c40fdca6SSimon Glass 	sprintf(bdesc->vendor, "Man %06x Snr %04x%04x",
1545babce5f6STaylor Hutt 		mmc->cid[0] >> 24, (mmc->cid[2] & 0xffff),
1546babce5f6STaylor Hutt 		(mmc->cid[3] >> 16) & 0xffff);
1547c40fdca6SSimon Glass 	sprintf(bdesc->product, "%c%c%c%c%c%c", mmc->cid[0] & 0xff,
15480b453ffeSRabin Vincent 		(mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
1549babce5f6STaylor Hutt 		(mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff,
1550babce5f6STaylor Hutt 		(mmc->cid[2] >> 24) & 0xff);
1551c40fdca6SSimon Glass 	sprintf(bdesc->revision, "%d.%d", (mmc->cid[2] >> 20) & 0xf,
1552babce5f6STaylor Hutt 		(mmc->cid[2] >> 16) & 0xf);
155356196826SPaul Burton #else
1554c40fdca6SSimon Glass 	bdesc->vendor[0] = 0;
1555c40fdca6SSimon Glass 	bdesc->product[0] = 0;
1556c40fdca6SSimon Glass 	bdesc->revision[0] = 0;
155756196826SPaul Burton #endif
1558122efd43SMikhail Kshevetskiy #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBDISK_SUPPORT)
1559c40fdca6SSimon Glass 	part_init(bdesc);
1560122efd43SMikhail Kshevetskiy #endif
1561272cc70bSAndy Fleming 
1562272cc70bSAndy Fleming 	return 0;
1563272cc70bSAndy Fleming }
1564272cc70bSAndy Fleming 
1565fdbb873eSKim Phillips static int mmc_send_if_cond(struct mmc *mmc)
1566272cc70bSAndy Fleming {
1567272cc70bSAndy Fleming 	struct mmc_cmd cmd;
1568272cc70bSAndy Fleming 	int err;
1569272cc70bSAndy Fleming 
1570272cc70bSAndy Fleming 	cmd.cmdidx = SD_CMD_SEND_IF_COND;
1571272cc70bSAndy Fleming 	/* We set the bit if the host supports voltages between 2.7 and 3.6 V */
157293bfd616SPantelis Antoniou 	cmd.cmdarg = ((mmc->cfg->voltages & 0xff8000) != 0) << 8 | 0xaa;
1573272cc70bSAndy Fleming 	cmd.resp_type = MMC_RSP_R7;
1574272cc70bSAndy Fleming 
1575272cc70bSAndy Fleming 	err = mmc_send_cmd(mmc, &cmd, NULL);
1576272cc70bSAndy Fleming 
1577272cc70bSAndy Fleming 	if (err)
1578272cc70bSAndy Fleming 		return err;
1579272cc70bSAndy Fleming 
1580998be3ddSRabin Vincent 	if ((cmd.response[0] & 0xff) != 0xaa)
1581915ffa52SJaehoon Chung 		return -EOPNOTSUPP;
1582272cc70bSAndy Fleming 	else
1583272cc70bSAndy Fleming 		mmc->version = SD_VERSION_2;
1584272cc70bSAndy Fleming 
1585272cc70bSAndy Fleming 	return 0;
1586272cc70bSAndy Fleming }
1587272cc70bSAndy Fleming 
158895de9ab2SPaul Kocialkowski /* board-specific MMC power initializations. */
158995de9ab2SPaul Kocialkowski __weak void board_mmc_power_init(void)
159095de9ab2SPaul Kocialkowski {
159195de9ab2SPaul Kocialkowski }
159295de9ab2SPaul Kocialkowski 
15932051aefeSPeng Fan static int mmc_power_init(struct mmc *mmc)
15942051aefeSPeng Fan {
15952051aefeSPeng Fan 	board_mmc_power_init();
15962051aefeSPeng Fan 
15972051aefeSPeng Fan #if defined(CONFIG_DM_MMC) && defined(CONFIG_DM_REGULATOR) && \
15982051aefeSPeng Fan 	!defined(CONFIG_SPL_BUILD)
15992051aefeSPeng Fan 	struct udevice *vmmc_supply;
16002051aefeSPeng Fan 	int ret;
16012051aefeSPeng Fan 
16022051aefeSPeng Fan 	ret = device_get_supply_regulator(mmc->dev, "vmmc-supply",
16032051aefeSPeng Fan 					  &vmmc_supply);
16042051aefeSPeng Fan 	if (ret) {
1605288db7c7SJaehoon Chung 		debug("%s: No vmmc supply\n", mmc->dev->name);
16062051aefeSPeng Fan 		return 0;
16072051aefeSPeng Fan 	}
16082051aefeSPeng Fan 
16092051aefeSPeng Fan 	ret = regulator_set_enable(vmmc_supply, true);
16102051aefeSPeng Fan 	if (ret) {
16112051aefeSPeng Fan 		puts("Error enabling VMMC supply\n");
16122051aefeSPeng Fan 		return ret;
16132051aefeSPeng Fan 	}
16142051aefeSPeng Fan #endif
16152051aefeSPeng Fan 	return 0;
16162051aefeSPeng Fan }
16172051aefeSPeng Fan 
1618e9550449SChe-Liang Chiou int mmc_start_init(struct mmc *mmc)
1619272cc70bSAndy Fleming {
16208ca51e51SSimon Glass 	bool no_card;
1621afd5932bSMacpaul Lin 	int err;
1622272cc70bSAndy Fleming 
1623ab769f22SPantelis Antoniou 	/* we pretend there's no card when init is NULL */
16248ca51e51SSimon Glass 	no_card = mmc_getcd(mmc) == 0;
16258ca51e51SSimon Glass #ifndef CONFIG_DM_MMC_OPS
16268ca51e51SSimon Glass 	no_card = no_card || (mmc->cfg->ops->init == NULL);
16278ca51e51SSimon Glass #endif
16288ca51e51SSimon Glass 	if (no_card) {
162948972d90SThierry Reding 		mmc->has_init = 0;
163056196826SPaul Burton #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
163148972d90SThierry Reding 		printf("MMC: no card present\n");
163256196826SPaul Burton #endif
1633915ffa52SJaehoon Chung 		return -ENOMEDIUM;
163448972d90SThierry Reding 	}
163548972d90SThierry Reding 
1636bc897b1dSLei Wen 	if (mmc->has_init)
1637bc897b1dSLei Wen 		return 0;
1638bc897b1dSLei Wen 
16395a8dbdc6SYangbo Lu #ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT
16405a8dbdc6SYangbo Lu 	mmc_adapter_card_type_ident();
16415a8dbdc6SYangbo Lu #endif
16422051aefeSPeng Fan 	err = mmc_power_init(mmc);
16432051aefeSPeng Fan 	if (err)
16442051aefeSPeng Fan 		return err;
164595de9ab2SPaul Kocialkowski 
16468ca51e51SSimon Glass #ifdef CONFIG_DM_MMC_OPS
16478ca51e51SSimon Glass 	/* The device has already been probed ready for use */
16488ca51e51SSimon Glass #else
1649ab769f22SPantelis Antoniou 	/* made sure it's not NULL earlier */
165093bfd616SPantelis Antoniou 	err = mmc->cfg->ops->init(mmc);
1651272cc70bSAndy Fleming 	if (err)
1652272cc70bSAndy Fleming 		return err;
16538ca51e51SSimon Glass #endif
1654786e8f81SAndrew Gabbasov 	mmc->ddr_mode = 0;
1655b86b85e2SIlya Yanok 	mmc_set_bus_width(mmc, 1);
1656b86b85e2SIlya Yanok 	mmc_set_clock(mmc, 1);
1657b86b85e2SIlya Yanok 
1658272cc70bSAndy Fleming 	/* Reset the Card */
1659272cc70bSAndy Fleming 	err = mmc_go_idle(mmc);
1660272cc70bSAndy Fleming 
1661272cc70bSAndy Fleming 	if (err)
1662272cc70bSAndy Fleming 		return err;
1663272cc70bSAndy Fleming 
1664bc897b1dSLei Wen 	/* The internal partition reset to user partition(0) at every CMD0*/
1665c40fdca6SSimon Glass 	mmc_get_blk_desc(mmc)->hwpart = 0;
1666bc897b1dSLei Wen 
1667272cc70bSAndy Fleming 	/* Test for SD version 2 */
1668272cc70bSAndy Fleming 	err = mmc_send_if_cond(mmc);
1669272cc70bSAndy Fleming 
1670272cc70bSAndy Fleming 	/* Now try to get the SD card's operating condition */
1671272cc70bSAndy Fleming 	err = sd_send_op_cond(mmc);
1672272cc70bSAndy Fleming 
1673272cc70bSAndy Fleming 	/* If the command timed out, we check for an MMC card */
1674915ffa52SJaehoon Chung 	if (err == -ETIMEDOUT) {
1675272cc70bSAndy Fleming 		err = mmc_send_op_cond(mmc);
1676272cc70bSAndy Fleming 
1677bd47c135SAndrew Gabbasov 		if (err) {
167856196826SPaul Burton #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
1679272cc70bSAndy Fleming 			printf("Card did not respond to voltage select!\n");
168056196826SPaul Burton #endif
1681915ffa52SJaehoon Chung 			return -EOPNOTSUPP;
1682272cc70bSAndy Fleming 		}
1683272cc70bSAndy Fleming 	}
1684272cc70bSAndy Fleming 
1685bd47c135SAndrew Gabbasov 	if (!err)
1686e9550449SChe-Liang Chiou 		mmc->init_in_progress = 1;
1687e9550449SChe-Liang Chiou 
1688e9550449SChe-Liang Chiou 	return err;
1689e9550449SChe-Liang Chiou }
1690e9550449SChe-Liang Chiou 
1691e9550449SChe-Liang Chiou static int mmc_complete_init(struct mmc *mmc)
1692e9550449SChe-Liang Chiou {
1693e9550449SChe-Liang Chiou 	int err = 0;
1694e9550449SChe-Liang Chiou 
1695bd47c135SAndrew Gabbasov 	mmc->init_in_progress = 0;
1696e9550449SChe-Liang Chiou 	if (mmc->op_cond_pending)
1697e9550449SChe-Liang Chiou 		err = mmc_complete_op_cond(mmc);
1698e9550449SChe-Liang Chiou 
1699e9550449SChe-Liang Chiou 	if (!err)
1700bc897b1dSLei Wen 		err = mmc_startup(mmc);
1701bc897b1dSLei Wen 	if (err)
1702bc897b1dSLei Wen 		mmc->has_init = 0;
1703bc897b1dSLei Wen 	else
1704bc897b1dSLei Wen 		mmc->has_init = 1;
1705e9550449SChe-Liang Chiou 	return err;
1706e9550449SChe-Liang Chiou }
1707e9550449SChe-Liang Chiou 
1708e9550449SChe-Liang Chiou int mmc_init(struct mmc *mmc)
1709e9550449SChe-Liang Chiou {
1710bd47c135SAndrew Gabbasov 	int err = 0;
1711d803fea5SMateusz Zalega 	unsigned start;
171233fb211dSSimon Glass #ifdef CONFIG_DM_MMC
171333fb211dSSimon Glass 	struct mmc_uclass_priv *upriv = dev_get_uclass_priv(mmc->dev);
1714e9550449SChe-Liang Chiou 
171533fb211dSSimon Glass 	upriv->mmc = mmc;
171633fb211dSSimon Glass #endif
1717e9550449SChe-Liang Chiou 	if (mmc->has_init)
1718e9550449SChe-Liang Chiou 		return 0;
1719d803fea5SMateusz Zalega 
1720d803fea5SMateusz Zalega 	start = get_timer(0);
1721d803fea5SMateusz Zalega 
1722e9550449SChe-Liang Chiou 	if (!mmc->init_in_progress)
1723e9550449SChe-Liang Chiou 		err = mmc_start_init(mmc);
1724e9550449SChe-Liang Chiou 
1725bd47c135SAndrew Gabbasov 	if (!err)
1726e9550449SChe-Liang Chiou 		err = mmc_complete_init(mmc);
1727e9550449SChe-Liang Chiou 	debug("%s: %d, time %lu\n", __func__, err, get_timer(start));
1728bc897b1dSLei Wen 	return err;
1729272cc70bSAndy Fleming }
1730272cc70bSAndy Fleming 
1731ab71188cSMarkus Niebel int mmc_set_dsr(struct mmc *mmc, u16 val)
1732ab71188cSMarkus Niebel {
1733ab71188cSMarkus Niebel 	mmc->dsr = val;
1734ab71188cSMarkus Niebel 	return 0;
1735ab71188cSMarkus Niebel }
1736ab71188cSMarkus Niebel 
1737cee9ab7cSJeroen Hofstee /* CPU-specific MMC initializations */
1738cee9ab7cSJeroen Hofstee __weak int cpu_mmc_init(bd_t *bis)
1739272cc70bSAndy Fleming {
1740272cc70bSAndy Fleming 	return -1;
1741272cc70bSAndy Fleming }
1742272cc70bSAndy Fleming 
1743cee9ab7cSJeroen Hofstee /* board-specific MMC initializations. */
1744cee9ab7cSJeroen Hofstee __weak int board_mmc_init(bd_t *bis)
1745cee9ab7cSJeroen Hofstee {
1746cee9ab7cSJeroen Hofstee 	return -1;
1747cee9ab7cSJeroen Hofstee }
1748272cc70bSAndy Fleming 
1749e9550449SChe-Liang Chiou void mmc_set_preinit(struct mmc *mmc, int preinit)
1750e9550449SChe-Liang Chiou {
1751e9550449SChe-Liang Chiou 	mmc->preinit = preinit;
1752e9550449SChe-Liang Chiou }
1753e9550449SChe-Liang Chiou 
17548e3332e2SSjoerd Simons #if defined(CONFIG_DM_MMC) && defined(CONFIG_SPL_BUILD)
17558e3332e2SSjoerd Simons static int mmc_probe(bd_t *bis)
17568e3332e2SSjoerd Simons {
17578e3332e2SSjoerd Simons 	return 0;
17588e3332e2SSjoerd Simons }
17598e3332e2SSjoerd Simons #elif defined(CONFIG_DM_MMC)
17608e3332e2SSjoerd Simons static int mmc_probe(bd_t *bis)
17618e3332e2SSjoerd Simons {
17624a1db6d8SSimon Glass 	int ret, i;
17638e3332e2SSjoerd Simons 	struct uclass *uc;
17644a1db6d8SSimon Glass 	struct udevice *dev;
17658e3332e2SSjoerd Simons 
17668e3332e2SSjoerd Simons 	ret = uclass_get(UCLASS_MMC, &uc);
17678e3332e2SSjoerd Simons 	if (ret)
17688e3332e2SSjoerd Simons 		return ret;
17698e3332e2SSjoerd Simons 
17704a1db6d8SSimon Glass 	/*
17714a1db6d8SSimon Glass 	 * Try to add them in sequence order. Really with driver model we
17724a1db6d8SSimon Glass 	 * should allow holes, but the current MMC list does not allow that.
17734a1db6d8SSimon Glass 	 * So if we request 0, 1, 3 we will get 0, 1, 2.
17744a1db6d8SSimon Glass 	 */
17754a1db6d8SSimon Glass 	for (i = 0; ; i++) {
17764a1db6d8SSimon Glass 		ret = uclass_get_device_by_seq(UCLASS_MMC, i, &dev);
17774a1db6d8SSimon Glass 		if (ret == -ENODEV)
17784a1db6d8SSimon Glass 			break;
17794a1db6d8SSimon Glass 	}
17804a1db6d8SSimon Glass 	uclass_foreach_dev(dev, uc) {
17814a1db6d8SSimon Glass 		ret = device_probe(dev);
17828e3332e2SSjoerd Simons 		if (ret)
17834a1db6d8SSimon Glass 			printf("%s - probe failed: %d\n", dev->name, ret);
17848e3332e2SSjoerd Simons 	}
17858e3332e2SSjoerd Simons 
17868e3332e2SSjoerd Simons 	return 0;
17878e3332e2SSjoerd Simons }
17888e3332e2SSjoerd Simons #else
17898e3332e2SSjoerd Simons static int mmc_probe(bd_t *bis)
17908e3332e2SSjoerd Simons {
17918e3332e2SSjoerd Simons 	if (board_mmc_init(bis) < 0)
17928e3332e2SSjoerd Simons 		cpu_mmc_init(bis);
17938e3332e2SSjoerd Simons 
17948e3332e2SSjoerd Simons 	return 0;
17958e3332e2SSjoerd Simons }
17968e3332e2SSjoerd Simons #endif
1797e9550449SChe-Liang Chiou 
1798272cc70bSAndy Fleming int mmc_initialize(bd_t *bis)
1799272cc70bSAndy Fleming {
18001b26bab1SDaniel Kochmański 	static int initialized = 0;
18018e3332e2SSjoerd Simons 	int ret;
18021b26bab1SDaniel Kochmański 	if (initialized)	/* Avoid initializing mmc multiple times */
18031b26bab1SDaniel Kochmański 		return 0;
18041b26bab1SDaniel Kochmański 	initialized = 1;
18051b26bab1SDaniel Kochmański 
1806c40fdca6SSimon Glass #ifndef CONFIG_BLK
1807c40fdca6SSimon Glass 	mmc_list_init();
1808c40fdca6SSimon Glass #endif
18098e3332e2SSjoerd Simons 	ret = mmc_probe(bis);
18108e3332e2SSjoerd Simons 	if (ret)
18118e3332e2SSjoerd Simons 		return ret;
1812272cc70bSAndy Fleming 
1813bb0dc108SYing Zhang #ifndef CONFIG_SPL_BUILD
1814272cc70bSAndy Fleming 	print_mmc_devices(',');
1815bb0dc108SYing Zhang #endif
1816272cc70bSAndy Fleming 
1817c40fdca6SSimon Glass 	mmc_do_preinit();
1818272cc70bSAndy Fleming 	return 0;
1819272cc70bSAndy Fleming }
1820