xref: /openbmc/u-boot/drivers/mmc/mmc.c (revision b5b838f1a726e9ab7eea505740e8169b55e90ed6)
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 
33*b5b838f1SMarek Vasut #if CONFIG_IS_ENABLED(MMC_TINY)
34*b5b838f1SMarek Vasut static struct mmc mmc_static;
35*b5b838f1SMarek Vasut struct mmc *find_mmc_device(int dev_num)
36*b5b838f1SMarek Vasut {
37*b5b838f1SMarek Vasut 	return &mmc_static;
38*b5b838f1SMarek Vasut }
39*b5b838f1SMarek Vasut 
40*b5b838f1SMarek Vasut void mmc_do_preinit(void)
41*b5b838f1SMarek Vasut {
42*b5b838f1SMarek Vasut 	struct mmc *m = &mmc_static;
43*b5b838f1SMarek Vasut #ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT
44*b5b838f1SMarek Vasut 	mmc_set_preinit(m, 1);
45*b5b838f1SMarek Vasut #endif
46*b5b838f1SMarek Vasut 	if (m->preinit)
47*b5b838f1SMarek Vasut 		mmc_start_init(m);
48*b5b838f1SMarek Vasut }
49*b5b838f1SMarek Vasut 
50*b5b838f1SMarek Vasut struct blk_desc *mmc_get_blk_desc(struct mmc *mmc)
51*b5b838f1SMarek Vasut {
52*b5b838f1SMarek Vasut 	return &mmc->block_dev;
53*b5b838f1SMarek Vasut }
54*b5b838f1SMarek Vasut #endif
55*b5b838f1SMarek Vasut 
568ca51e51SSimon Glass #ifndef CONFIG_DM_MMC_OPS
57750121c3SJeroen Hofstee __weak int board_mmc_getwp(struct mmc *mmc)
58d23d8d7eSNikita Kiryanov {
59d23d8d7eSNikita Kiryanov 	return -1;
60d23d8d7eSNikita Kiryanov }
61d23d8d7eSNikita Kiryanov 
62d23d8d7eSNikita Kiryanov int mmc_getwp(struct mmc *mmc)
63d23d8d7eSNikita Kiryanov {
64d23d8d7eSNikita Kiryanov 	int wp;
65d23d8d7eSNikita Kiryanov 
66d23d8d7eSNikita Kiryanov 	wp = board_mmc_getwp(mmc);
67d23d8d7eSNikita Kiryanov 
68d4e1da4eSPeter Korsgaard 	if (wp < 0) {
6993bfd616SPantelis Antoniou 		if (mmc->cfg->ops->getwp)
7093bfd616SPantelis Antoniou 			wp = mmc->cfg->ops->getwp(mmc);
71d4e1da4eSPeter Korsgaard 		else
72d4e1da4eSPeter Korsgaard 			wp = 0;
73d4e1da4eSPeter Korsgaard 	}
74d23d8d7eSNikita Kiryanov 
75d23d8d7eSNikita Kiryanov 	return wp;
76d23d8d7eSNikita Kiryanov }
77d23d8d7eSNikita Kiryanov 
78cee9ab7cSJeroen Hofstee __weak int board_mmc_getcd(struct mmc *mmc)
79cee9ab7cSJeroen Hofstee {
8011fdade2SStefano Babic 	return -1;
8111fdade2SStefano Babic }
828ca51e51SSimon Glass #endif
8311fdade2SStefano Babic 
848635ff9eSMarek Vasut #ifdef CONFIG_MMC_TRACE
85c0c76ebaSSimon Glass void mmmc_trace_before_send(struct mmc *mmc, struct mmc_cmd *cmd)
86c0c76ebaSSimon Glass {
87c0c76ebaSSimon Glass 	printf("CMD_SEND:%d\n", cmd->cmdidx);
88c0c76ebaSSimon Glass 	printf("\t\tARG\t\t\t 0x%08X\n", cmd->cmdarg);
89c0c76ebaSSimon Glass }
90c0c76ebaSSimon Glass 
91c0c76ebaSSimon Glass void mmmc_trace_after_send(struct mmc *mmc, struct mmc_cmd *cmd, int ret)
92c0c76ebaSSimon Glass {
935db2fe3aSRaffaele Recalcati 	int i;
945db2fe3aSRaffaele Recalcati 	u8 *ptr;
955db2fe3aSRaffaele Recalcati 
967863ce58SBin Meng 	if (ret) {
977863ce58SBin Meng 		printf("\t\tRET\t\t\t %d\n", ret);
987863ce58SBin Meng 	} else {
995db2fe3aSRaffaele Recalcati 		switch (cmd->resp_type) {
1005db2fe3aSRaffaele Recalcati 		case MMC_RSP_NONE:
1015db2fe3aSRaffaele Recalcati 			printf("\t\tMMC_RSP_NONE\n");
1025db2fe3aSRaffaele Recalcati 			break;
1035db2fe3aSRaffaele Recalcati 		case MMC_RSP_R1:
1045db2fe3aSRaffaele Recalcati 			printf("\t\tMMC_RSP_R1,5,6,7 \t 0x%08X \n",
1055db2fe3aSRaffaele Recalcati 				cmd->response[0]);
1065db2fe3aSRaffaele Recalcati 			break;
1075db2fe3aSRaffaele Recalcati 		case MMC_RSP_R1b:
1085db2fe3aSRaffaele Recalcati 			printf("\t\tMMC_RSP_R1b\t\t 0x%08X \n",
1095db2fe3aSRaffaele Recalcati 				cmd->response[0]);
1105db2fe3aSRaffaele Recalcati 			break;
1115db2fe3aSRaffaele Recalcati 		case MMC_RSP_R2:
1125db2fe3aSRaffaele Recalcati 			printf("\t\tMMC_RSP_R2\t\t 0x%08X \n",
1135db2fe3aSRaffaele Recalcati 				cmd->response[0]);
1145db2fe3aSRaffaele Recalcati 			printf("\t\t          \t\t 0x%08X \n",
1155db2fe3aSRaffaele Recalcati 				cmd->response[1]);
1165db2fe3aSRaffaele Recalcati 			printf("\t\t          \t\t 0x%08X \n",
1175db2fe3aSRaffaele Recalcati 				cmd->response[2]);
1185db2fe3aSRaffaele Recalcati 			printf("\t\t          \t\t 0x%08X \n",
1195db2fe3aSRaffaele Recalcati 				cmd->response[3]);
1205db2fe3aSRaffaele Recalcati 			printf("\n");
1215db2fe3aSRaffaele Recalcati 			printf("\t\t\t\t\tDUMPING DATA\n");
1225db2fe3aSRaffaele Recalcati 			for (i = 0; i < 4; i++) {
1235db2fe3aSRaffaele Recalcati 				int j;
1245db2fe3aSRaffaele Recalcati 				printf("\t\t\t\t\t%03d - ", i*4);
125146bec79SDirk Behme 				ptr = (u8 *)&cmd->response[i];
1265db2fe3aSRaffaele Recalcati 				ptr += 3;
1275db2fe3aSRaffaele Recalcati 				for (j = 0; j < 4; j++)
1285db2fe3aSRaffaele Recalcati 					printf("%02X ", *ptr--);
1295db2fe3aSRaffaele Recalcati 				printf("\n");
1305db2fe3aSRaffaele Recalcati 			}
1315db2fe3aSRaffaele Recalcati 			break;
1325db2fe3aSRaffaele Recalcati 		case MMC_RSP_R3:
1335db2fe3aSRaffaele Recalcati 			printf("\t\tMMC_RSP_R3,4\t\t 0x%08X \n",
1345db2fe3aSRaffaele Recalcati 				cmd->response[0]);
1355db2fe3aSRaffaele Recalcati 			break;
1365db2fe3aSRaffaele Recalcati 		default:
1375db2fe3aSRaffaele Recalcati 			printf("\t\tERROR MMC rsp not supported\n");
1385db2fe3aSRaffaele Recalcati 			break;
1395db2fe3aSRaffaele Recalcati 		}
1407863ce58SBin Meng 	}
141c0c76ebaSSimon Glass }
142c0c76ebaSSimon Glass 
143c0c76ebaSSimon Glass void mmc_trace_state(struct mmc *mmc, struct mmc_cmd *cmd)
144c0c76ebaSSimon Glass {
145c0c76ebaSSimon Glass 	int status;
146c0c76ebaSSimon Glass 
147c0c76ebaSSimon Glass 	status = (cmd->response[0] & MMC_STATUS_CURR_STATE) >> 9;
148c0c76ebaSSimon Glass 	printf("CURR STATE:%d\n", status);
149c0c76ebaSSimon Glass }
1505db2fe3aSRaffaele Recalcati #endif
151c0c76ebaSSimon Glass 
1528ca51e51SSimon Glass #ifndef CONFIG_DM_MMC_OPS
153c0c76ebaSSimon Glass int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
154c0c76ebaSSimon Glass {
155c0c76ebaSSimon Glass 	int ret;
156c0c76ebaSSimon Glass 
157c0c76ebaSSimon Glass 	mmmc_trace_before_send(mmc, cmd);
158c0c76ebaSSimon Glass 	ret = mmc->cfg->ops->send_cmd(mmc, cmd, data);
159c0c76ebaSSimon Glass 	mmmc_trace_after_send(mmc, cmd, ret);
160c0c76ebaSSimon Glass 
1618635ff9eSMarek Vasut 	return ret;
162272cc70bSAndy Fleming }
1638ca51e51SSimon Glass #endif
164272cc70bSAndy Fleming 
165da61fa5fSPaul Burton int mmc_send_status(struct mmc *mmc, int timeout)
1665d4fc8d9SRaffaele Recalcati {
1675d4fc8d9SRaffaele Recalcati 	struct mmc_cmd cmd;
168d617c426SJan Kloetzke 	int err, retries = 5;
1695d4fc8d9SRaffaele Recalcati 
1705d4fc8d9SRaffaele Recalcati 	cmd.cmdidx = MMC_CMD_SEND_STATUS;
1715d4fc8d9SRaffaele Recalcati 	cmd.resp_type = MMC_RSP_R1;
172aaf3d41aSMarek Vasut 	if (!mmc_host_is_spi(mmc))
173aaf3d41aSMarek Vasut 		cmd.cmdarg = mmc->rca << 16;
1745d4fc8d9SRaffaele Recalcati 
1751677eef4SAndrew Gabbasov 	while (1) {
1765d4fc8d9SRaffaele Recalcati 		err = mmc_send_cmd(mmc, &cmd, NULL);
177d617c426SJan Kloetzke 		if (!err) {
178d617c426SJan Kloetzke 			if ((cmd.response[0] & MMC_STATUS_RDY_FOR_DATA) &&
179d617c426SJan Kloetzke 			    (cmd.response[0] & MMC_STATUS_CURR_STATE) !=
180d617c426SJan Kloetzke 			     MMC_STATE_PRG)
1815d4fc8d9SRaffaele Recalcati 				break;
182d617c426SJan Kloetzke 			else if (cmd.response[0] & MMC_STATUS_MASK) {
18356196826SPaul Burton #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
184d617c426SJan Kloetzke 				printf("Status Error: 0x%08X\n",
185d617c426SJan Kloetzke 					cmd.response[0]);
18656196826SPaul Burton #endif
187915ffa52SJaehoon Chung 				return -ECOMM;
188d617c426SJan Kloetzke 			}
189d617c426SJan Kloetzke 		} else if (--retries < 0)
190d617c426SJan Kloetzke 			return err;
1915d4fc8d9SRaffaele Recalcati 
1921677eef4SAndrew Gabbasov 		if (timeout-- <= 0)
1931677eef4SAndrew Gabbasov 			break;
1945d4fc8d9SRaffaele Recalcati 
1951677eef4SAndrew Gabbasov 		udelay(1000);
1961677eef4SAndrew Gabbasov 	}
1975d4fc8d9SRaffaele Recalcati 
198c0c76ebaSSimon Glass 	mmc_trace_state(mmc, &cmd);
1995b0c942fSJongman Heo 	if (timeout <= 0) {
20056196826SPaul Burton #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
2015d4fc8d9SRaffaele Recalcati 		printf("Timeout waiting card ready\n");
20256196826SPaul Burton #endif
203915ffa52SJaehoon Chung 		return -ETIMEDOUT;
2045d4fc8d9SRaffaele Recalcati 	}
2055d4fc8d9SRaffaele Recalcati 
2065d4fc8d9SRaffaele Recalcati 	return 0;
2075d4fc8d9SRaffaele Recalcati }
2085d4fc8d9SRaffaele Recalcati 
209da61fa5fSPaul Burton int mmc_set_blocklen(struct mmc *mmc, int len)
210272cc70bSAndy Fleming {
211272cc70bSAndy Fleming 	struct mmc_cmd cmd;
212272cc70bSAndy Fleming 
213786e8f81SAndrew Gabbasov 	if (mmc->ddr_mode)
214d22e3d46SJaehoon Chung 		return 0;
215d22e3d46SJaehoon Chung 
216272cc70bSAndy Fleming 	cmd.cmdidx = MMC_CMD_SET_BLOCKLEN;
217272cc70bSAndy Fleming 	cmd.resp_type = MMC_RSP_R1;
218272cc70bSAndy Fleming 	cmd.cmdarg = len;
219272cc70bSAndy Fleming 
220272cc70bSAndy Fleming 	return mmc_send_cmd(mmc, &cmd, NULL);
221272cc70bSAndy Fleming }
222272cc70bSAndy Fleming 
223ff8fef56SSascha Silbe static int mmc_read_blocks(struct mmc *mmc, void *dst, lbaint_t start,
224fdbb873eSKim Phillips 			   lbaint_t blkcnt)
225272cc70bSAndy Fleming {
226272cc70bSAndy Fleming 	struct mmc_cmd cmd;
227272cc70bSAndy Fleming 	struct mmc_data data;
228272cc70bSAndy Fleming 
2294a1a06bcSAlagu Sankar 	if (blkcnt > 1)
2304a1a06bcSAlagu Sankar 		cmd.cmdidx = MMC_CMD_READ_MULTIPLE_BLOCK;
2314a1a06bcSAlagu Sankar 	else
232272cc70bSAndy Fleming 		cmd.cmdidx = MMC_CMD_READ_SINGLE_BLOCK;
233272cc70bSAndy Fleming 
234272cc70bSAndy Fleming 	if (mmc->high_capacity)
2354a1a06bcSAlagu Sankar 		cmd.cmdarg = start;
236272cc70bSAndy Fleming 	else
2374a1a06bcSAlagu Sankar 		cmd.cmdarg = start * mmc->read_bl_len;
238272cc70bSAndy Fleming 
239272cc70bSAndy Fleming 	cmd.resp_type = MMC_RSP_R1;
240272cc70bSAndy Fleming 
241272cc70bSAndy Fleming 	data.dest = dst;
2424a1a06bcSAlagu Sankar 	data.blocks = blkcnt;
243272cc70bSAndy Fleming 	data.blocksize = mmc->read_bl_len;
244272cc70bSAndy Fleming 	data.flags = MMC_DATA_READ;
245272cc70bSAndy Fleming 
2464a1a06bcSAlagu Sankar 	if (mmc_send_cmd(mmc, &cmd, &data))
2474a1a06bcSAlagu Sankar 		return 0;
2484a1a06bcSAlagu Sankar 
2494a1a06bcSAlagu Sankar 	if (blkcnt > 1) {
2504a1a06bcSAlagu Sankar 		cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
2514a1a06bcSAlagu Sankar 		cmd.cmdarg = 0;
2524a1a06bcSAlagu Sankar 		cmd.resp_type = MMC_RSP_R1b;
2534a1a06bcSAlagu Sankar 		if (mmc_send_cmd(mmc, &cmd, NULL)) {
25456196826SPaul Burton #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
2554a1a06bcSAlagu Sankar 			printf("mmc fail to send stop cmd\n");
25656196826SPaul Burton #endif
2574a1a06bcSAlagu Sankar 			return 0;
2584a1a06bcSAlagu Sankar 		}
259272cc70bSAndy Fleming 	}
260272cc70bSAndy Fleming 
2614a1a06bcSAlagu Sankar 	return blkcnt;
262272cc70bSAndy Fleming }
263272cc70bSAndy Fleming 
26433fb211dSSimon Glass #ifdef CONFIG_BLK
2657dba0b93SSimon Glass ulong mmc_bread(struct udevice *dev, lbaint_t start, lbaint_t blkcnt, void *dst)
26633fb211dSSimon Glass #else
2677dba0b93SSimon Glass ulong mmc_bread(struct blk_desc *block_dev, lbaint_t start, lbaint_t blkcnt,
2687dba0b93SSimon Glass 		void *dst)
26933fb211dSSimon Glass #endif
270272cc70bSAndy Fleming {
27133fb211dSSimon Glass #ifdef CONFIG_BLK
27233fb211dSSimon Glass 	struct blk_desc *block_dev = dev_get_uclass_platdata(dev);
27333fb211dSSimon Glass #endif
274bcce53d0SSimon Glass 	int dev_num = block_dev->devnum;
275873cc1d7SStephen Warren 	int err;
2764a1a06bcSAlagu Sankar 	lbaint_t cur, blocks_todo = blkcnt;
277272cc70bSAndy Fleming 
2784a1a06bcSAlagu Sankar 	if (blkcnt == 0)
2794a1a06bcSAlagu Sankar 		return 0;
2804a1a06bcSAlagu Sankar 
2814a1a06bcSAlagu Sankar 	struct mmc *mmc = find_mmc_device(dev_num);
282272cc70bSAndy Fleming 	if (!mmc)
283272cc70bSAndy Fleming 		return 0;
284272cc70bSAndy Fleming 
285*b5b838f1SMarek Vasut 	if (CONFIG_IS_ENABLED(MMC_TINY))
286*b5b838f1SMarek Vasut 		err = mmc_switch_part(mmc, block_dev->hwpart);
287*b5b838f1SMarek Vasut 	else
28869f45cd5SSimon Glass 		err = blk_dselect_hwpart(block_dev, block_dev->hwpart);
289*b5b838f1SMarek Vasut 
290873cc1d7SStephen Warren 	if (err < 0)
291873cc1d7SStephen Warren 		return 0;
292873cc1d7SStephen Warren 
293c40fdca6SSimon Glass 	if ((start + blkcnt) > block_dev->lba) {
29456196826SPaul Burton #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
295ff8fef56SSascha Silbe 		printf("MMC: block number 0x" LBAF " exceeds max(0x" LBAF ")\n",
296c40fdca6SSimon Glass 			start + blkcnt, block_dev->lba);
29756196826SPaul Burton #endif
298d2bf29e3SLei Wen 		return 0;
299d2bf29e3SLei Wen 	}
300272cc70bSAndy Fleming 
30111692991SSimon Glass 	if (mmc_set_blocklen(mmc, mmc->read_bl_len)) {
30211692991SSimon Glass 		debug("%s: Failed to set blocklen\n", __func__);
303272cc70bSAndy Fleming 		return 0;
30411692991SSimon Glass 	}
305272cc70bSAndy Fleming 
3064a1a06bcSAlagu Sankar 	do {
30793bfd616SPantelis Antoniou 		cur = (blocks_todo > mmc->cfg->b_max) ?
30893bfd616SPantelis Antoniou 			mmc->cfg->b_max : blocks_todo;
30911692991SSimon Glass 		if (mmc_read_blocks(mmc, dst, start, cur) != cur) {
31011692991SSimon Glass 			debug("%s: Failed to read blocks\n", __func__);
3114a1a06bcSAlagu Sankar 			return 0;
31211692991SSimon Glass 		}
3134a1a06bcSAlagu Sankar 		blocks_todo -= cur;
3144a1a06bcSAlagu Sankar 		start += cur;
3154a1a06bcSAlagu Sankar 		dst += cur * mmc->read_bl_len;
3164a1a06bcSAlagu Sankar 	} while (blocks_todo > 0);
317272cc70bSAndy Fleming 
318272cc70bSAndy Fleming 	return blkcnt;
319272cc70bSAndy Fleming }
320272cc70bSAndy Fleming 
321fdbb873eSKim Phillips static int mmc_go_idle(struct mmc *mmc)
322272cc70bSAndy Fleming {
323272cc70bSAndy Fleming 	struct mmc_cmd cmd;
324272cc70bSAndy Fleming 	int err;
325272cc70bSAndy Fleming 
326272cc70bSAndy Fleming 	udelay(1000);
327272cc70bSAndy Fleming 
328272cc70bSAndy Fleming 	cmd.cmdidx = MMC_CMD_GO_IDLE_STATE;
329272cc70bSAndy Fleming 	cmd.cmdarg = 0;
330272cc70bSAndy Fleming 	cmd.resp_type = MMC_RSP_NONE;
331272cc70bSAndy Fleming 
332272cc70bSAndy Fleming 	err = mmc_send_cmd(mmc, &cmd, NULL);
333272cc70bSAndy Fleming 
334272cc70bSAndy Fleming 	if (err)
335272cc70bSAndy Fleming 		return err;
336272cc70bSAndy Fleming 
337272cc70bSAndy Fleming 	udelay(2000);
338272cc70bSAndy Fleming 
339272cc70bSAndy Fleming 	return 0;
340272cc70bSAndy Fleming }
341272cc70bSAndy Fleming 
342fdbb873eSKim Phillips static int sd_send_op_cond(struct mmc *mmc)
343272cc70bSAndy Fleming {
344272cc70bSAndy Fleming 	int timeout = 1000;
345272cc70bSAndy Fleming 	int err;
346272cc70bSAndy Fleming 	struct mmc_cmd cmd;
347272cc70bSAndy Fleming 
3481677eef4SAndrew Gabbasov 	while (1) {
349272cc70bSAndy Fleming 		cmd.cmdidx = MMC_CMD_APP_CMD;
350272cc70bSAndy Fleming 		cmd.resp_type = MMC_RSP_R1;
351272cc70bSAndy Fleming 		cmd.cmdarg = 0;
352272cc70bSAndy Fleming 
353272cc70bSAndy Fleming 		err = mmc_send_cmd(mmc, &cmd, NULL);
354272cc70bSAndy Fleming 
355272cc70bSAndy Fleming 		if (err)
356272cc70bSAndy Fleming 			return err;
357272cc70bSAndy Fleming 
358272cc70bSAndy Fleming 		cmd.cmdidx = SD_CMD_APP_SEND_OP_COND;
359272cc70bSAndy Fleming 		cmd.resp_type = MMC_RSP_R3;
360250de12bSStefano Babic 
361250de12bSStefano Babic 		/*
362250de12bSStefano Babic 		 * Most cards do not answer if some reserved bits
363250de12bSStefano Babic 		 * in the ocr are set. However, Some controller
364250de12bSStefano Babic 		 * can set bit 7 (reserved for low voltages), but
365250de12bSStefano Babic 		 * how to manage low voltages SD card is not yet
366250de12bSStefano Babic 		 * specified.
367250de12bSStefano Babic 		 */
368d52ebf10SThomas Chou 		cmd.cmdarg = mmc_host_is_spi(mmc) ? 0 :
36993bfd616SPantelis Antoniou 			(mmc->cfg->voltages & 0xff8000);
370272cc70bSAndy Fleming 
371272cc70bSAndy Fleming 		if (mmc->version == SD_VERSION_2)
372272cc70bSAndy Fleming 			cmd.cmdarg |= OCR_HCS;
373272cc70bSAndy Fleming 
374272cc70bSAndy Fleming 		err = mmc_send_cmd(mmc, &cmd, NULL);
375272cc70bSAndy Fleming 
376272cc70bSAndy Fleming 		if (err)
377272cc70bSAndy Fleming 			return err;
378272cc70bSAndy Fleming 
3791677eef4SAndrew Gabbasov 		if (cmd.response[0] & OCR_BUSY)
3801677eef4SAndrew Gabbasov 			break;
381272cc70bSAndy Fleming 
3821677eef4SAndrew Gabbasov 		if (timeout-- <= 0)
383915ffa52SJaehoon Chung 			return -EOPNOTSUPP;
384272cc70bSAndy Fleming 
3851677eef4SAndrew Gabbasov 		udelay(1000);
3861677eef4SAndrew Gabbasov 	}
3871677eef4SAndrew Gabbasov 
388272cc70bSAndy Fleming 	if (mmc->version != SD_VERSION_2)
389272cc70bSAndy Fleming 		mmc->version = SD_VERSION_1_0;
390272cc70bSAndy Fleming 
391d52ebf10SThomas Chou 	if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
392d52ebf10SThomas Chou 		cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
393d52ebf10SThomas Chou 		cmd.resp_type = MMC_RSP_R3;
394d52ebf10SThomas Chou 		cmd.cmdarg = 0;
395d52ebf10SThomas Chou 
396d52ebf10SThomas Chou 		err = mmc_send_cmd(mmc, &cmd, NULL);
397d52ebf10SThomas Chou 
398d52ebf10SThomas Chou 		if (err)
399d52ebf10SThomas Chou 			return err;
400d52ebf10SThomas Chou 	}
401d52ebf10SThomas Chou 
402998be3ddSRabin Vincent 	mmc->ocr = cmd.response[0];
403272cc70bSAndy Fleming 
404272cc70bSAndy Fleming 	mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
405272cc70bSAndy Fleming 	mmc->rca = 0;
406272cc70bSAndy Fleming 
407272cc70bSAndy Fleming 	return 0;
408272cc70bSAndy Fleming }
409272cc70bSAndy Fleming 
4105289b535SAndrew Gabbasov static int mmc_send_op_cond_iter(struct mmc *mmc, int use_arg)
411272cc70bSAndy Fleming {
4125289b535SAndrew Gabbasov 	struct mmc_cmd cmd;
413272cc70bSAndy Fleming 	int err;
414272cc70bSAndy Fleming 
4155289b535SAndrew Gabbasov 	cmd.cmdidx = MMC_CMD_SEND_OP_COND;
4165289b535SAndrew Gabbasov 	cmd.resp_type = MMC_RSP_R3;
4175289b535SAndrew Gabbasov 	cmd.cmdarg = 0;
4185a20397bSRob Herring 	if (use_arg && !mmc_host_is_spi(mmc))
4195a20397bSRob Herring 		cmd.cmdarg = OCR_HCS |
42093bfd616SPantelis Antoniou 			(mmc->cfg->voltages &
421a626c8d4SAndrew Gabbasov 			(mmc->ocr & OCR_VOLTAGE_MASK)) |
422a626c8d4SAndrew Gabbasov 			(mmc->ocr & OCR_ACCESS_MODE);
423e9550449SChe-Liang Chiou 
4245289b535SAndrew Gabbasov 	err = mmc_send_cmd(mmc, &cmd, NULL);
425e9550449SChe-Liang Chiou 	if (err)
426e9550449SChe-Liang Chiou 		return err;
4275289b535SAndrew Gabbasov 	mmc->ocr = cmd.response[0];
428e9550449SChe-Liang Chiou 	return 0;
429e9550449SChe-Liang Chiou }
430e9550449SChe-Liang Chiou 
431750121c3SJeroen Hofstee static int mmc_send_op_cond(struct mmc *mmc)
432e9550449SChe-Liang Chiou {
433e9550449SChe-Liang Chiou 	int err, i;
434e9550449SChe-Liang Chiou 
435272cc70bSAndy Fleming 	/* Some cards seem to need this */
436272cc70bSAndy Fleming 	mmc_go_idle(mmc);
437272cc70bSAndy Fleming 
43831cacbabSRaffaele Recalcati  	/* Asking to the card its capabilities */
439e9550449SChe-Liang Chiou 	for (i = 0; i < 2; i++) {
4405289b535SAndrew Gabbasov 		err = mmc_send_op_cond_iter(mmc, i != 0);
44131cacbabSRaffaele Recalcati 		if (err)
44231cacbabSRaffaele Recalcati 			return err;
44331cacbabSRaffaele Recalcati 
444e9550449SChe-Liang Chiou 		/* exit if not busy (flag seems to be inverted) */
445a626c8d4SAndrew Gabbasov 		if (mmc->ocr & OCR_BUSY)
446bd47c135SAndrew Gabbasov 			break;
447e9550449SChe-Liang Chiou 	}
448bd47c135SAndrew Gabbasov 	mmc->op_cond_pending = 1;
449bd47c135SAndrew Gabbasov 	return 0;
450e9550449SChe-Liang Chiou }
45131cacbabSRaffaele Recalcati 
452750121c3SJeroen Hofstee static int mmc_complete_op_cond(struct mmc *mmc)
453e9550449SChe-Liang Chiou {
454e9550449SChe-Liang Chiou 	struct mmc_cmd cmd;
455e9550449SChe-Liang Chiou 	int timeout = 1000;
456e9550449SChe-Liang Chiou 	uint start;
457e9550449SChe-Liang Chiou 	int err;
458e9550449SChe-Liang Chiou 
459e9550449SChe-Liang Chiou 	mmc->op_cond_pending = 0;
460cc17c01fSAndrew Gabbasov 	if (!(mmc->ocr & OCR_BUSY)) {
461d188b113SYangbo Lu 		/* Some cards seem to need this */
462d188b113SYangbo Lu 		mmc_go_idle(mmc);
463d188b113SYangbo Lu 
464e9550449SChe-Liang Chiou 		start = get_timer(0);
4651677eef4SAndrew Gabbasov 		while (1) {
4665289b535SAndrew Gabbasov 			err = mmc_send_op_cond_iter(mmc, 1);
467272cc70bSAndy Fleming 			if (err)
468272cc70bSAndy Fleming 				return err;
4691677eef4SAndrew Gabbasov 			if (mmc->ocr & OCR_BUSY)
4701677eef4SAndrew Gabbasov 				break;
471e9550449SChe-Liang Chiou 			if (get_timer(start) > timeout)
472915ffa52SJaehoon Chung 				return -EOPNOTSUPP;
473e9550449SChe-Liang Chiou 			udelay(100);
4741677eef4SAndrew Gabbasov 		}
475cc17c01fSAndrew Gabbasov 	}
476272cc70bSAndy Fleming 
477d52ebf10SThomas Chou 	if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
478d52ebf10SThomas Chou 		cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
479d52ebf10SThomas Chou 		cmd.resp_type = MMC_RSP_R3;
480d52ebf10SThomas Chou 		cmd.cmdarg = 0;
481d52ebf10SThomas Chou 
482d52ebf10SThomas Chou 		err = mmc_send_cmd(mmc, &cmd, NULL);
483d52ebf10SThomas Chou 
484d52ebf10SThomas Chou 		if (err)
485d52ebf10SThomas Chou 			return err;
486a626c8d4SAndrew Gabbasov 
487a626c8d4SAndrew Gabbasov 		mmc->ocr = cmd.response[0];
488d52ebf10SThomas Chou 	}
489d52ebf10SThomas Chou 
490272cc70bSAndy Fleming 	mmc->version = MMC_VERSION_UNKNOWN;
491272cc70bSAndy Fleming 
492272cc70bSAndy Fleming 	mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
493def816a2SStephen Warren 	mmc->rca = 1;
494272cc70bSAndy Fleming 
495272cc70bSAndy Fleming 	return 0;
496272cc70bSAndy Fleming }
497272cc70bSAndy Fleming 
498272cc70bSAndy Fleming 
499fdbb873eSKim Phillips static int mmc_send_ext_csd(struct mmc *mmc, u8 *ext_csd)
500272cc70bSAndy Fleming {
501272cc70bSAndy Fleming 	struct mmc_cmd cmd;
502272cc70bSAndy Fleming 	struct mmc_data data;
503272cc70bSAndy Fleming 	int err;
504272cc70bSAndy Fleming 
505272cc70bSAndy Fleming 	/* Get the Card Status Register */
506272cc70bSAndy Fleming 	cmd.cmdidx = MMC_CMD_SEND_EXT_CSD;
507272cc70bSAndy Fleming 	cmd.resp_type = MMC_RSP_R1;
508272cc70bSAndy Fleming 	cmd.cmdarg = 0;
509272cc70bSAndy Fleming 
510cdfd1ac6SYoshihiro Shimoda 	data.dest = (char *)ext_csd;
511272cc70bSAndy Fleming 	data.blocks = 1;
5128bfa195eSSimon Glass 	data.blocksize = MMC_MAX_BLOCK_LEN;
513272cc70bSAndy Fleming 	data.flags = MMC_DATA_READ;
514272cc70bSAndy Fleming 
515272cc70bSAndy Fleming 	err = mmc_send_cmd(mmc, &cmd, &data);
516272cc70bSAndy Fleming 
517272cc70bSAndy Fleming 	return err;
518272cc70bSAndy Fleming }
519272cc70bSAndy Fleming 
520c40704f4SSimon Glass int mmc_switch(struct mmc *mmc, u8 set, u8 index, u8 value)
521272cc70bSAndy Fleming {
522272cc70bSAndy Fleming 	struct mmc_cmd cmd;
5235d4fc8d9SRaffaele Recalcati 	int timeout = 1000;
524a9003dc6SMaxime Ripard 	int retries = 3;
5255d4fc8d9SRaffaele Recalcati 	int ret;
526272cc70bSAndy Fleming 
527272cc70bSAndy Fleming 	cmd.cmdidx = MMC_CMD_SWITCH;
528272cc70bSAndy Fleming 	cmd.resp_type = MMC_RSP_R1b;
529272cc70bSAndy Fleming 	cmd.cmdarg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
530272cc70bSAndy Fleming 				 (index << 16) |
531272cc70bSAndy Fleming 				 (value << 8);
532272cc70bSAndy Fleming 
533a9003dc6SMaxime Ripard 	while (retries > 0) {
5345d4fc8d9SRaffaele Recalcati 		ret = mmc_send_cmd(mmc, &cmd, NULL);
5355d4fc8d9SRaffaele Recalcati 
5365d4fc8d9SRaffaele Recalcati 		/* Waiting for the ready status */
537a9003dc6SMaxime Ripard 		if (!ret) {
53893ad0d18SJan Kloetzke 			ret = mmc_send_status(mmc, timeout);
539a9003dc6SMaxime Ripard 			return ret;
540a9003dc6SMaxime Ripard 		}
541a9003dc6SMaxime Ripard 
542a9003dc6SMaxime Ripard 		retries--;
543a9003dc6SMaxime Ripard 	}
5445d4fc8d9SRaffaele Recalcati 
5455d4fc8d9SRaffaele Recalcati 	return ret;
5465d4fc8d9SRaffaele Recalcati 
547272cc70bSAndy Fleming }
548272cc70bSAndy Fleming 
549fdbb873eSKim Phillips static int mmc_change_freq(struct mmc *mmc)
550272cc70bSAndy Fleming {
5518bfa195eSSimon Glass 	ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
552272cc70bSAndy Fleming 	char cardtype;
553272cc70bSAndy Fleming 	int err;
554272cc70bSAndy Fleming 
555fc5b32fbSAndrew Gabbasov 	mmc->card_caps = 0;
556272cc70bSAndy Fleming 
557d52ebf10SThomas Chou 	if (mmc_host_is_spi(mmc))
558d52ebf10SThomas Chou 		return 0;
559d52ebf10SThomas Chou 
560272cc70bSAndy Fleming 	/* Only version 4 supports high-speed */
561272cc70bSAndy Fleming 	if (mmc->version < MMC_VERSION_4)
562272cc70bSAndy Fleming 		return 0;
563272cc70bSAndy Fleming 
564fc5b32fbSAndrew Gabbasov 	mmc->card_caps |= MMC_MODE_4BIT | MMC_MODE_8BIT;
565fc5b32fbSAndrew Gabbasov 
566272cc70bSAndy Fleming 	err = mmc_send_ext_csd(mmc, ext_csd);
567272cc70bSAndy Fleming 
568272cc70bSAndy Fleming 	if (err)
569272cc70bSAndy Fleming 		return err;
570272cc70bSAndy Fleming 
5710560db18SLei Wen 	cardtype = ext_csd[EXT_CSD_CARD_TYPE] & 0xf;
572272cc70bSAndy Fleming 
573272cc70bSAndy Fleming 	err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING, 1);
574272cc70bSAndy Fleming 
575272cc70bSAndy Fleming 	if (err)
576a5e27b41SHeiko Schocher 		return err;
577272cc70bSAndy Fleming 
578272cc70bSAndy Fleming 	/* Now check to see that it worked */
579272cc70bSAndy Fleming 	err = mmc_send_ext_csd(mmc, ext_csd);
580272cc70bSAndy Fleming 
581272cc70bSAndy Fleming 	if (err)
582272cc70bSAndy Fleming 		return err;
583272cc70bSAndy Fleming 
584272cc70bSAndy Fleming 	/* No high-speed support */
5850560db18SLei Wen 	if (!ext_csd[EXT_CSD_HS_TIMING])
586272cc70bSAndy Fleming 		return 0;
587272cc70bSAndy Fleming 
588272cc70bSAndy Fleming 	/* High Speed is set, there are two types: 52MHz and 26MHz */
589d22e3d46SJaehoon Chung 	if (cardtype & EXT_CSD_CARD_TYPE_52) {
590201d5ac4SAndrew Gabbasov 		if (cardtype & EXT_CSD_CARD_TYPE_DDR_1_8V)
591d22e3d46SJaehoon Chung 			mmc->card_caps |= MMC_MODE_DDR_52MHz;
592272cc70bSAndy Fleming 		mmc->card_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
593d22e3d46SJaehoon Chung 	} else {
594272cc70bSAndy Fleming 		mmc->card_caps |= MMC_MODE_HS;
595d22e3d46SJaehoon Chung 	}
596272cc70bSAndy Fleming 
597272cc70bSAndy Fleming 	return 0;
598272cc70bSAndy Fleming }
599272cc70bSAndy Fleming 
600f866a46dSStephen Warren static int mmc_set_capacity(struct mmc *mmc, int part_num)
601f866a46dSStephen Warren {
602f866a46dSStephen Warren 	switch (part_num) {
603f866a46dSStephen Warren 	case 0:
604f866a46dSStephen Warren 		mmc->capacity = mmc->capacity_user;
605f866a46dSStephen Warren 		break;
606f866a46dSStephen Warren 	case 1:
607f866a46dSStephen Warren 	case 2:
608f866a46dSStephen Warren 		mmc->capacity = mmc->capacity_boot;
609f866a46dSStephen Warren 		break;
610f866a46dSStephen Warren 	case 3:
611f866a46dSStephen Warren 		mmc->capacity = mmc->capacity_rpmb;
612f866a46dSStephen Warren 		break;
613f866a46dSStephen Warren 	case 4:
614f866a46dSStephen Warren 	case 5:
615f866a46dSStephen Warren 	case 6:
616f866a46dSStephen Warren 	case 7:
617f866a46dSStephen Warren 		mmc->capacity = mmc->capacity_gp[part_num - 4];
618f866a46dSStephen Warren 		break;
619f866a46dSStephen Warren 	default:
620f866a46dSStephen Warren 		return -1;
621f866a46dSStephen Warren 	}
622f866a46dSStephen Warren 
623c40fdca6SSimon Glass 	mmc_get_blk_desc(mmc)->lba = lldiv(mmc->capacity, mmc->read_bl_len);
624f866a46dSStephen Warren 
625f866a46dSStephen Warren 	return 0;
626f866a46dSStephen Warren }
627f866a46dSStephen Warren 
6287dba0b93SSimon Glass int mmc_switch_part(struct mmc *mmc, unsigned int part_num)
629bc897b1dSLei Wen {
630f866a46dSStephen Warren 	int ret;
631bc897b1dSLei Wen 
632f866a46dSStephen Warren 	ret = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONF,
633bc897b1dSLei Wen 			 (mmc->part_config & ~PART_ACCESS_MASK)
634bc897b1dSLei Wen 			 | (part_num & PART_ACCESS_MASK));
635f866a46dSStephen Warren 
6366dc93e70SPeter Bigot 	/*
6376dc93e70SPeter Bigot 	 * Set the capacity if the switch succeeded or was intended
6386dc93e70SPeter Bigot 	 * to return to representing the raw device.
6396dc93e70SPeter Bigot 	 */
640873cc1d7SStephen Warren 	if ((ret == 0) || ((ret == -ENODEV) && (part_num == 0))) {
6416dc93e70SPeter Bigot 		ret = mmc_set_capacity(mmc, part_num);
642fdbb139fSSimon Glass 		mmc_get_blk_desc(mmc)->hwpart = part_num;
643873cc1d7SStephen Warren 	}
6446dc93e70SPeter Bigot 
6456dc93e70SPeter Bigot 	return ret;
646bc897b1dSLei Wen }
647bc897b1dSLei Wen 
648ac9da0e0SDiego Santa Cruz int mmc_hwpart_config(struct mmc *mmc,
649ac9da0e0SDiego Santa Cruz 		      const struct mmc_hwpart_conf *conf,
650ac9da0e0SDiego Santa Cruz 		      enum mmc_hwpart_conf_mode mode)
651ac9da0e0SDiego Santa Cruz {
652ac9da0e0SDiego Santa Cruz 	u8 part_attrs = 0;
653ac9da0e0SDiego Santa Cruz 	u32 enh_size_mult;
654ac9da0e0SDiego Santa Cruz 	u32 enh_start_addr;
655ac9da0e0SDiego Santa Cruz 	u32 gp_size_mult[4];
656ac9da0e0SDiego Santa Cruz 	u32 max_enh_size_mult;
657ac9da0e0SDiego Santa Cruz 	u32 tot_enh_size_mult = 0;
6588dda5b0eSDiego Santa Cruz 	u8 wr_rel_set;
659ac9da0e0SDiego Santa Cruz 	int i, pidx, err;
660ac9da0e0SDiego Santa Cruz 	ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
661ac9da0e0SDiego Santa Cruz 
662ac9da0e0SDiego Santa Cruz 	if (mode < MMC_HWPART_CONF_CHECK || mode > MMC_HWPART_CONF_COMPLETE)
663ac9da0e0SDiego Santa Cruz 		return -EINVAL;
664ac9da0e0SDiego Santa Cruz 
665ac9da0e0SDiego Santa Cruz 	if (IS_SD(mmc) || (mmc->version < MMC_VERSION_4_41)) {
666ac9da0e0SDiego Santa Cruz 		printf("eMMC >= 4.4 required for enhanced user data area\n");
667ac9da0e0SDiego Santa Cruz 		return -EMEDIUMTYPE;
668ac9da0e0SDiego Santa Cruz 	}
669ac9da0e0SDiego Santa Cruz 
670ac9da0e0SDiego Santa Cruz 	if (!(mmc->part_support & PART_SUPPORT)) {
671ac9da0e0SDiego Santa Cruz 		printf("Card does not support partitioning\n");
672ac9da0e0SDiego Santa Cruz 		return -EMEDIUMTYPE;
673ac9da0e0SDiego Santa Cruz 	}
674ac9da0e0SDiego Santa Cruz 
675ac9da0e0SDiego Santa Cruz 	if (!mmc->hc_wp_grp_size) {
676ac9da0e0SDiego Santa Cruz 		printf("Card does not define HC WP group size\n");
677ac9da0e0SDiego Santa Cruz 		return -EMEDIUMTYPE;
678ac9da0e0SDiego Santa Cruz 	}
679ac9da0e0SDiego Santa Cruz 
680ac9da0e0SDiego Santa Cruz 	/* check partition alignment and total enhanced size */
681ac9da0e0SDiego Santa Cruz 	if (conf->user.enh_size) {
682ac9da0e0SDiego Santa Cruz 		if (conf->user.enh_size % mmc->hc_wp_grp_size ||
683ac9da0e0SDiego Santa Cruz 		    conf->user.enh_start % mmc->hc_wp_grp_size) {
684ac9da0e0SDiego Santa Cruz 			printf("User data enhanced area not HC WP group "
685ac9da0e0SDiego Santa Cruz 			       "size aligned\n");
686ac9da0e0SDiego Santa Cruz 			return -EINVAL;
687ac9da0e0SDiego Santa Cruz 		}
688ac9da0e0SDiego Santa Cruz 		part_attrs |= EXT_CSD_ENH_USR;
689ac9da0e0SDiego Santa Cruz 		enh_size_mult = conf->user.enh_size / mmc->hc_wp_grp_size;
690ac9da0e0SDiego Santa Cruz 		if (mmc->high_capacity) {
691ac9da0e0SDiego Santa Cruz 			enh_start_addr = conf->user.enh_start;
692ac9da0e0SDiego Santa Cruz 		} else {
693ac9da0e0SDiego Santa Cruz 			enh_start_addr = (conf->user.enh_start << 9);
694ac9da0e0SDiego Santa Cruz 		}
695ac9da0e0SDiego Santa Cruz 	} else {
696ac9da0e0SDiego Santa Cruz 		enh_size_mult = 0;
697ac9da0e0SDiego Santa Cruz 		enh_start_addr = 0;
698ac9da0e0SDiego Santa Cruz 	}
699ac9da0e0SDiego Santa Cruz 	tot_enh_size_mult += enh_size_mult;
700ac9da0e0SDiego Santa Cruz 
701ac9da0e0SDiego Santa Cruz 	for (pidx = 0; pidx < 4; pidx++) {
702ac9da0e0SDiego Santa Cruz 		if (conf->gp_part[pidx].size % mmc->hc_wp_grp_size) {
703ac9da0e0SDiego Santa Cruz 			printf("GP%i partition not HC WP group size "
704ac9da0e0SDiego Santa Cruz 			       "aligned\n", pidx+1);
705ac9da0e0SDiego Santa Cruz 			return -EINVAL;
706ac9da0e0SDiego Santa Cruz 		}
707ac9da0e0SDiego Santa Cruz 		gp_size_mult[pidx] = conf->gp_part[pidx].size / mmc->hc_wp_grp_size;
708ac9da0e0SDiego Santa Cruz 		if (conf->gp_part[pidx].size && conf->gp_part[pidx].enhanced) {
709ac9da0e0SDiego Santa Cruz 			part_attrs |= EXT_CSD_ENH_GP(pidx);
710ac9da0e0SDiego Santa Cruz 			tot_enh_size_mult += gp_size_mult[pidx];
711ac9da0e0SDiego Santa Cruz 		}
712ac9da0e0SDiego Santa Cruz 	}
713ac9da0e0SDiego Santa Cruz 
714ac9da0e0SDiego Santa Cruz 	if (part_attrs && ! (mmc->part_support & ENHNCD_SUPPORT)) {
715ac9da0e0SDiego Santa Cruz 		printf("Card does not support enhanced attribute\n");
716ac9da0e0SDiego Santa Cruz 		return -EMEDIUMTYPE;
717ac9da0e0SDiego Santa Cruz 	}
718ac9da0e0SDiego Santa Cruz 
719ac9da0e0SDiego Santa Cruz 	err = mmc_send_ext_csd(mmc, ext_csd);
720ac9da0e0SDiego Santa Cruz 	if (err)
721ac9da0e0SDiego Santa Cruz 		return err;
722ac9da0e0SDiego Santa Cruz 
723ac9da0e0SDiego Santa Cruz 	max_enh_size_mult =
724ac9da0e0SDiego Santa Cruz 		(ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT+2] << 16) +
725ac9da0e0SDiego Santa Cruz 		(ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT+1] << 8) +
726ac9da0e0SDiego Santa Cruz 		ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT];
727ac9da0e0SDiego Santa Cruz 	if (tot_enh_size_mult > max_enh_size_mult) {
728ac9da0e0SDiego Santa Cruz 		printf("Total enhanced size exceeds maximum (%u > %u)\n",
729ac9da0e0SDiego Santa Cruz 		       tot_enh_size_mult, max_enh_size_mult);
730ac9da0e0SDiego Santa Cruz 		return -EMEDIUMTYPE;
731ac9da0e0SDiego Santa Cruz 	}
732ac9da0e0SDiego Santa Cruz 
7338dda5b0eSDiego Santa Cruz 	/* The default value of EXT_CSD_WR_REL_SET is device
7348dda5b0eSDiego Santa Cruz 	 * dependent, the values can only be changed if the
7358dda5b0eSDiego Santa Cruz 	 * EXT_CSD_HS_CTRL_REL bit is set. The values can be
7368dda5b0eSDiego Santa Cruz 	 * changed only once and before partitioning is completed. */
7378dda5b0eSDiego Santa Cruz 	wr_rel_set = ext_csd[EXT_CSD_WR_REL_SET];
7388dda5b0eSDiego Santa Cruz 	if (conf->user.wr_rel_change) {
7398dda5b0eSDiego Santa Cruz 		if (conf->user.wr_rel_set)
7408dda5b0eSDiego Santa Cruz 			wr_rel_set |= EXT_CSD_WR_DATA_REL_USR;
7418dda5b0eSDiego Santa Cruz 		else
7428dda5b0eSDiego Santa Cruz 			wr_rel_set &= ~EXT_CSD_WR_DATA_REL_USR;
7438dda5b0eSDiego Santa Cruz 	}
7448dda5b0eSDiego Santa Cruz 	for (pidx = 0; pidx < 4; pidx++) {
7458dda5b0eSDiego Santa Cruz 		if (conf->gp_part[pidx].wr_rel_change) {
7468dda5b0eSDiego Santa Cruz 			if (conf->gp_part[pidx].wr_rel_set)
7478dda5b0eSDiego Santa Cruz 				wr_rel_set |= EXT_CSD_WR_DATA_REL_GP(pidx);
7488dda5b0eSDiego Santa Cruz 			else
7498dda5b0eSDiego Santa Cruz 				wr_rel_set &= ~EXT_CSD_WR_DATA_REL_GP(pidx);
7508dda5b0eSDiego Santa Cruz 		}
7518dda5b0eSDiego Santa Cruz 	}
7528dda5b0eSDiego Santa Cruz 
7538dda5b0eSDiego Santa Cruz 	if (wr_rel_set != ext_csd[EXT_CSD_WR_REL_SET] &&
7548dda5b0eSDiego Santa Cruz 	    !(ext_csd[EXT_CSD_WR_REL_PARAM] & EXT_CSD_HS_CTRL_REL)) {
7558dda5b0eSDiego Santa Cruz 		puts("Card does not support host controlled partition write "
7568dda5b0eSDiego Santa Cruz 		     "reliability settings\n");
7578dda5b0eSDiego Santa Cruz 		return -EMEDIUMTYPE;
7588dda5b0eSDiego Santa Cruz 	}
7598dda5b0eSDiego Santa Cruz 
760ac9da0e0SDiego Santa Cruz 	if (ext_csd[EXT_CSD_PARTITION_SETTING] &
761ac9da0e0SDiego Santa Cruz 	    EXT_CSD_PARTITION_SETTING_COMPLETED) {
762ac9da0e0SDiego Santa Cruz 		printf("Card already partitioned\n");
763ac9da0e0SDiego Santa Cruz 		return -EPERM;
764ac9da0e0SDiego Santa Cruz 	}
765ac9da0e0SDiego Santa Cruz 
766ac9da0e0SDiego Santa Cruz 	if (mode == MMC_HWPART_CONF_CHECK)
767ac9da0e0SDiego Santa Cruz 		return 0;
768ac9da0e0SDiego Santa Cruz 
769ac9da0e0SDiego Santa Cruz 	/* Partitioning requires high-capacity size definitions */
770ac9da0e0SDiego Santa Cruz 	if (!(ext_csd[EXT_CSD_ERASE_GROUP_DEF] & 0x01)) {
771ac9da0e0SDiego Santa Cruz 		err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
772ac9da0e0SDiego Santa Cruz 				 EXT_CSD_ERASE_GROUP_DEF, 1);
773ac9da0e0SDiego Santa Cruz 
774ac9da0e0SDiego Santa Cruz 		if (err)
775ac9da0e0SDiego Santa Cruz 			return err;
776ac9da0e0SDiego Santa Cruz 
777ac9da0e0SDiego Santa Cruz 		ext_csd[EXT_CSD_ERASE_GROUP_DEF] = 1;
778ac9da0e0SDiego Santa Cruz 
779ac9da0e0SDiego Santa Cruz 		/* update erase group size to be high-capacity */
780ac9da0e0SDiego Santa Cruz 		mmc->erase_grp_size =
781ac9da0e0SDiego Santa Cruz 			ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] * 1024;
782ac9da0e0SDiego Santa Cruz 
783ac9da0e0SDiego Santa Cruz 	}
784ac9da0e0SDiego Santa Cruz 
785ac9da0e0SDiego Santa Cruz 	/* all OK, write the configuration */
786ac9da0e0SDiego Santa Cruz 	for (i = 0; i < 4; i++) {
787ac9da0e0SDiego Santa Cruz 		err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
788ac9da0e0SDiego Santa Cruz 				 EXT_CSD_ENH_START_ADDR+i,
789ac9da0e0SDiego Santa Cruz 				 (enh_start_addr >> (i*8)) & 0xFF);
790ac9da0e0SDiego Santa Cruz 		if (err)
791ac9da0e0SDiego Santa Cruz 			return err;
792ac9da0e0SDiego Santa Cruz 	}
793ac9da0e0SDiego Santa Cruz 	for (i = 0; i < 3; i++) {
794ac9da0e0SDiego Santa Cruz 		err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
795ac9da0e0SDiego Santa Cruz 				 EXT_CSD_ENH_SIZE_MULT+i,
796ac9da0e0SDiego Santa Cruz 				 (enh_size_mult >> (i*8)) & 0xFF);
797ac9da0e0SDiego Santa Cruz 		if (err)
798ac9da0e0SDiego Santa Cruz 			return err;
799ac9da0e0SDiego Santa Cruz 	}
800ac9da0e0SDiego Santa Cruz 	for (pidx = 0; pidx < 4; pidx++) {
801ac9da0e0SDiego Santa Cruz 		for (i = 0; i < 3; i++) {
802ac9da0e0SDiego Santa Cruz 			err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
803ac9da0e0SDiego Santa Cruz 					 EXT_CSD_GP_SIZE_MULT+pidx*3+i,
804ac9da0e0SDiego Santa Cruz 					 (gp_size_mult[pidx] >> (i*8)) & 0xFF);
805ac9da0e0SDiego Santa Cruz 			if (err)
806ac9da0e0SDiego Santa Cruz 				return err;
807ac9da0e0SDiego Santa Cruz 		}
808ac9da0e0SDiego Santa Cruz 	}
809ac9da0e0SDiego Santa Cruz 	err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
810ac9da0e0SDiego Santa Cruz 			 EXT_CSD_PARTITIONS_ATTRIBUTE, part_attrs);
811ac9da0e0SDiego Santa Cruz 	if (err)
812ac9da0e0SDiego Santa Cruz 		return err;
813ac9da0e0SDiego Santa Cruz 
814ac9da0e0SDiego Santa Cruz 	if (mode == MMC_HWPART_CONF_SET)
815ac9da0e0SDiego Santa Cruz 		return 0;
816ac9da0e0SDiego Santa Cruz 
8178dda5b0eSDiego Santa Cruz 	/* The WR_REL_SET is a write-once register but shall be
8188dda5b0eSDiego Santa Cruz 	 * written before setting PART_SETTING_COMPLETED. As it is
8198dda5b0eSDiego Santa Cruz 	 * write-once we can only write it when completing the
8208dda5b0eSDiego Santa Cruz 	 * partitioning. */
8218dda5b0eSDiego Santa Cruz 	if (wr_rel_set != ext_csd[EXT_CSD_WR_REL_SET]) {
8228dda5b0eSDiego Santa Cruz 		err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
8238dda5b0eSDiego Santa Cruz 				 EXT_CSD_WR_REL_SET, wr_rel_set);
8248dda5b0eSDiego Santa Cruz 		if (err)
8258dda5b0eSDiego Santa Cruz 			return err;
8268dda5b0eSDiego Santa Cruz 	}
8278dda5b0eSDiego Santa Cruz 
828ac9da0e0SDiego Santa Cruz 	/* Setting PART_SETTING_COMPLETED confirms the partition
829ac9da0e0SDiego Santa Cruz 	 * configuration but it only becomes effective after power
830ac9da0e0SDiego Santa Cruz 	 * cycle, so we do not adjust the partition related settings
831ac9da0e0SDiego Santa Cruz 	 * in the mmc struct. */
832ac9da0e0SDiego Santa Cruz 
833ac9da0e0SDiego Santa Cruz 	err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
834ac9da0e0SDiego Santa Cruz 			 EXT_CSD_PARTITION_SETTING,
835ac9da0e0SDiego Santa Cruz 			 EXT_CSD_PARTITION_SETTING_COMPLETED);
836ac9da0e0SDiego Santa Cruz 	if (err)
837ac9da0e0SDiego Santa Cruz 		return err;
838ac9da0e0SDiego Santa Cruz 
839ac9da0e0SDiego Santa Cruz 	return 0;
840ac9da0e0SDiego Santa Cruz }
841ac9da0e0SDiego Santa Cruz 
8428ca51e51SSimon Glass #ifndef CONFIG_DM_MMC_OPS
84348972d90SThierry Reding int mmc_getcd(struct mmc *mmc)
84448972d90SThierry Reding {
84548972d90SThierry Reding 	int cd;
84648972d90SThierry Reding 
84748972d90SThierry Reding 	cd = board_mmc_getcd(mmc);
84848972d90SThierry Reding 
849d4e1da4eSPeter Korsgaard 	if (cd < 0) {
85093bfd616SPantelis Antoniou 		if (mmc->cfg->ops->getcd)
85193bfd616SPantelis Antoniou 			cd = mmc->cfg->ops->getcd(mmc);
852d4e1da4eSPeter Korsgaard 		else
853d4e1da4eSPeter Korsgaard 			cd = 1;
854d4e1da4eSPeter Korsgaard 	}
85548972d90SThierry Reding 
85648972d90SThierry Reding 	return cd;
85748972d90SThierry Reding }
8588ca51e51SSimon Glass #endif
85948972d90SThierry Reding 
860fdbb873eSKim Phillips static int sd_switch(struct mmc *mmc, int mode, int group, u8 value, u8 *resp)
861272cc70bSAndy Fleming {
862272cc70bSAndy Fleming 	struct mmc_cmd cmd;
863272cc70bSAndy Fleming 	struct mmc_data data;
864272cc70bSAndy Fleming 
865272cc70bSAndy Fleming 	/* Switch the frequency */
866272cc70bSAndy Fleming 	cmd.cmdidx = SD_CMD_SWITCH_FUNC;
867272cc70bSAndy Fleming 	cmd.resp_type = MMC_RSP_R1;
868272cc70bSAndy Fleming 	cmd.cmdarg = (mode << 31) | 0xffffff;
869272cc70bSAndy Fleming 	cmd.cmdarg &= ~(0xf << (group * 4));
870272cc70bSAndy Fleming 	cmd.cmdarg |= value << (group * 4);
871272cc70bSAndy Fleming 
872272cc70bSAndy Fleming 	data.dest = (char *)resp;
873272cc70bSAndy Fleming 	data.blocksize = 64;
874272cc70bSAndy Fleming 	data.blocks = 1;
875272cc70bSAndy Fleming 	data.flags = MMC_DATA_READ;
876272cc70bSAndy Fleming 
877272cc70bSAndy Fleming 	return mmc_send_cmd(mmc, &cmd, &data);
878272cc70bSAndy Fleming }
879272cc70bSAndy Fleming 
880272cc70bSAndy Fleming 
881fdbb873eSKim Phillips static int sd_change_freq(struct mmc *mmc)
882272cc70bSAndy Fleming {
883272cc70bSAndy Fleming 	int err;
884272cc70bSAndy Fleming 	struct mmc_cmd cmd;
885f781dd38SAnton staaf 	ALLOC_CACHE_ALIGN_BUFFER(uint, scr, 2);
886f781dd38SAnton staaf 	ALLOC_CACHE_ALIGN_BUFFER(uint, switch_status, 16);
887272cc70bSAndy Fleming 	struct mmc_data data;
888272cc70bSAndy Fleming 	int timeout;
889272cc70bSAndy Fleming 
890272cc70bSAndy Fleming 	mmc->card_caps = 0;
891272cc70bSAndy Fleming 
892d52ebf10SThomas Chou 	if (mmc_host_is_spi(mmc))
893d52ebf10SThomas Chou 		return 0;
894d52ebf10SThomas Chou 
895272cc70bSAndy Fleming 	/* Read the SCR to find out if this card supports higher speeds */
896272cc70bSAndy Fleming 	cmd.cmdidx = MMC_CMD_APP_CMD;
897272cc70bSAndy Fleming 	cmd.resp_type = MMC_RSP_R1;
898272cc70bSAndy Fleming 	cmd.cmdarg = mmc->rca << 16;
899272cc70bSAndy Fleming 
900272cc70bSAndy Fleming 	err = mmc_send_cmd(mmc, &cmd, NULL);
901272cc70bSAndy Fleming 
902272cc70bSAndy Fleming 	if (err)
903272cc70bSAndy Fleming 		return err;
904272cc70bSAndy Fleming 
905272cc70bSAndy Fleming 	cmd.cmdidx = SD_CMD_APP_SEND_SCR;
906272cc70bSAndy Fleming 	cmd.resp_type = MMC_RSP_R1;
907272cc70bSAndy Fleming 	cmd.cmdarg = 0;
908272cc70bSAndy Fleming 
909272cc70bSAndy Fleming 	timeout = 3;
910272cc70bSAndy Fleming 
911272cc70bSAndy Fleming retry_scr:
912f781dd38SAnton staaf 	data.dest = (char *)scr;
913272cc70bSAndy Fleming 	data.blocksize = 8;
914272cc70bSAndy Fleming 	data.blocks = 1;
915272cc70bSAndy Fleming 	data.flags = MMC_DATA_READ;
916272cc70bSAndy Fleming 
917272cc70bSAndy Fleming 	err = mmc_send_cmd(mmc, &cmd, &data);
918272cc70bSAndy Fleming 
919272cc70bSAndy Fleming 	if (err) {
920272cc70bSAndy Fleming 		if (timeout--)
921272cc70bSAndy Fleming 			goto retry_scr;
922272cc70bSAndy Fleming 
923272cc70bSAndy Fleming 		return err;
924272cc70bSAndy Fleming 	}
925272cc70bSAndy Fleming 
9264e3d89baSYauhen Kharuzhy 	mmc->scr[0] = __be32_to_cpu(scr[0]);
9274e3d89baSYauhen Kharuzhy 	mmc->scr[1] = __be32_to_cpu(scr[1]);
928272cc70bSAndy Fleming 
929272cc70bSAndy Fleming 	switch ((mmc->scr[0] >> 24) & 0xf) {
930272cc70bSAndy Fleming 	case 0:
931272cc70bSAndy Fleming 		mmc->version = SD_VERSION_1_0;
932272cc70bSAndy Fleming 		break;
933272cc70bSAndy Fleming 	case 1:
934272cc70bSAndy Fleming 		mmc->version = SD_VERSION_1_10;
935272cc70bSAndy Fleming 		break;
936272cc70bSAndy Fleming 	case 2:
937272cc70bSAndy Fleming 		mmc->version = SD_VERSION_2;
9381741c64dSJaehoon Chung 		if ((mmc->scr[0] >> 15) & 0x1)
9391741c64dSJaehoon Chung 			mmc->version = SD_VERSION_3;
940272cc70bSAndy Fleming 		break;
941272cc70bSAndy Fleming 	default:
942272cc70bSAndy Fleming 		mmc->version = SD_VERSION_1_0;
943272cc70bSAndy Fleming 		break;
944272cc70bSAndy Fleming 	}
945272cc70bSAndy Fleming 
946b44c7083SAlagu Sankar 	if (mmc->scr[0] & SD_DATA_4BIT)
947b44c7083SAlagu Sankar 		mmc->card_caps |= MMC_MODE_4BIT;
948b44c7083SAlagu Sankar 
949272cc70bSAndy Fleming 	/* Version 1.0 doesn't support switching */
950272cc70bSAndy Fleming 	if (mmc->version == SD_VERSION_1_0)
951272cc70bSAndy Fleming 		return 0;
952272cc70bSAndy Fleming 
953272cc70bSAndy Fleming 	timeout = 4;
954272cc70bSAndy Fleming 	while (timeout--) {
955272cc70bSAndy Fleming 		err = sd_switch(mmc, SD_SWITCH_CHECK, 0, 1,
956f781dd38SAnton staaf 				(u8 *)switch_status);
957272cc70bSAndy Fleming 
958272cc70bSAndy Fleming 		if (err)
959272cc70bSAndy Fleming 			return err;
960272cc70bSAndy Fleming 
961272cc70bSAndy Fleming 		/* The high-speed function is busy.  Try again */
9624e3d89baSYauhen Kharuzhy 		if (!(__be32_to_cpu(switch_status[7]) & SD_HIGHSPEED_BUSY))
963272cc70bSAndy Fleming 			break;
964272cc70bSAndy Fleming 	}
965272cc70bSAndy Fleming 
966272cc70bSAndy Fleming 	/* If high-speed isn't supported, we return */
9674e3d89baSYauhen Kharuzhy 	if (!(__be32_to_cpu(switch_status[3]) & SD_HIGHSPEED_SUPPORTED))
968272cc70bSAndy Fleming 		return 0;
969272cc70bSAndy Fleming 
9702c3fbf4cSMacpaul Lin 	/*
9712c3fbf4cSMacpaul Lin 	 * If the host doesn't support SD_HIGHSPEED, do not switch card to
9722c3fbf4cSMacpaul Lin 	 * HIGHSPEED mode even if the card support SD_HIGHSPPED.
9732c3fbf4cSMacpaul Lin 	 * This can avoid furthur problem when the card runs in different
9742c3fbf4cSMacpaul Lin 	 * mode between the host.
9752c3fbf4cSMacpaul Lin 	 */
97693bfd616SPantelis Antoniou 	if (!((mmc->cfg->host_caps & MMC_MODE_HS_52MHz) &&
97793bfd616SPantelis Antoniou 		(mmc->cfg->host_caps & MMC_MODE_HS)))
9782c3fbf4cSMacpaul Lin 		return 0;
9792c3fbf4cSMacpaul Lin 
980f781dd38SAnton staaf 	err = sd_switch(mmc, SD_SWITCH_SWITCH, 0, 1, (u8 *)switch_status);
981272cc70bSAndy Fleming 
982272cc70bSAndy Fleming 	if (err)
983272cc70bSAndy Fleming 		return err;
984272cc70bSAndy Fleming 
9854e3d89baSYauhen Kharuzhy 	if ((__be32_to_cpu(switch_status[4]) & 0x0f000000) == 0x01000000)
986272cc70bSAndy Fleming 		mmc->card_caps |= MMC_MODE_HS;
987272cc70bSAndy Fleming 
988272cc70bSAndy Fleming 	return 0;
989272cc70bSAndy Fleming }
990272cc70bSAndy Fleming 
9913697e599SPeng Fan static int sd_read_ssr(struct mmc *mmc)
9923697e599SPeng Fan {
9933697e599SPeng Fan 	int err, i;
9943697e599SPeng Fan 	struct mmc_cmd cmd;
9953697e599SPeng Fan 	ALLOC_CACHE_ALIGN_BUFFER(uint, ssr, 16);
9963697e599SPeng Fan 	struct mmc_data data;
9973697e599SPeng Fan 	int timeout = 3;
9983697e599SPeng Fan 	unsigned int au, eo, et, es;
9993697e599SPeng Fan 
10003697e599SPeng Fan 	cmd.cmdidx = MMC_CMD_APP_CMD;
10013697e599SPeng Fan 	cmd.resp_type = MMC_RSP_R1;
10023697e599SPeng Fan 	cmd.cmdarg = mmc->rca << 16;
10033697e599SPeng Fan 
10043697e599SPeng Fan 	err = mmc_send_cmd(mmc, &cmd, NULL);
10053697e599SPeng Fan 	if (err)
10063697e599SPeng Fan 		return err;
10073697e599SPeng Fan 
10083697e599SPeng Fan 	cmd.cmdidx = SD_CMD_APP_SD_STATUS;
10093697e599SPeng Fan 	cmd.resp_type = MMC_RSP_R1;
10103697e599SPeng Fan 	cmd.cmdarg = 0;
10113697e599SPeng Fan 
10123697e599SPeng Fan retry_ssr:
10133697e599SPeng Fan 	data.dest = (char *)ssr;
10143697e599SPeng Fan 	data.blocksize = 64;
10153697e599SPeng Fan 	data.blocks = 1;
10163697e599SPeng Fan 	data.flags = MMC_DATA_READ;
10173697e599SPeng Fan 
10183697e599SPeng Fan 	err = mmc_send_cmd(mmc, &cmd, &data);
10193697e599SPeng Fan 	if (err) {
10203697e599SPeng Fan 		if (timeout--)
10213697e599SPeng Fan 			goto retry_ssr;
10223697e599SPeng Fan 
10233697e599SPeng Fan 		return err;
10243697e599SPeng Fan 	}
10253697e599SPeng Fan 
10263697e599SPeng Fan 	for (i = 0; i < 16; i++)
10273697e599SPeng Fan 		ssr[i] = be32_to_cpu(ssr[i]);
10283697e599SPeng Fan 
10293697e599SPeng Fan 	au = (ssr[2] >> 12) & 0xF;
10303697e599SPeng Fan 	if ((au <= 9) || (mmc->version == SD_VERSION_3)) {
10313697e599SPeng Fan 		mmc->ssr.au = sd_au_size[au];
10323697e599SPeng Fan 		es = (ssr[3] >> 24) & 0xFF;
10333697e599SPeng Fan 		es |= (ssr[2] & 0xFF) << 8;
10343697e599SPeng Fan 		et = (ssr[3] >> 18) & 0x3F;
10353697e599SPeng Fan 		if (es && et) {
10363697e599SPeng Fan 			eo = (ssr[3] >> 16) & 0x3;
10373697e599SPeng Fan 			mmc->ssr.erase_timeout = (et * 1000) / es;
10383697e599SPeng Fan 			mmc->ssr.erase_offset = eo * 1000;
10393697e599SPeng Fan 		}
10403697e599SPeng Fan 	} else {
10413697e599SPeng Fan 		debug("Invalid Allocation Unit Size.\n");
10423697e599SPeng Fan 	}
10433697e599SPeng Fan 
10443697e599SPeng Fan 	return 0;
10453697e599SPeng Fan }
10463697e599SPeng Fan 
1047272cc70bSAndy Fleming /* frequency bases */
1048272cc70bSAndy Fleming /* divided by 10 to be nice to platforms without floating point */
10495f837c2cSMike Frysinger static const int fbase[] = {
1050272cc70bSAndy Fleming 	10000,
1051272cc70bSAndy Fleming 	100000,
1052272cc70bSAndy Fleming 	1000000,
1053272cc70bSAndy Fleming 	10000000,
1054272cc70bSAndy Fleming };
1055272cc70bSAndy Fleming 
1056272cc70bSAndy Fleming /* Multiplier values for TRAN_SPEED.  Multiplied by 10 to be nice
1057272cc70bSAndy Fleming  * to platforms without floating point.
1058272cc70bSAndy Fleming  */
105961fe076fSSimon Glass static const u8 multipliers[] = {
1060272cc70bSAndy Fleming 	0,	/* reserved */
1061272cc70bSAndy Fleming 	10,
1062272cc70bSAndy Fleming 	12,
1063272cc70bSAndy Fleming 	13,
1064272cc70bSAndy Fleming 	15,
1065272cc70bSAndy Fleming 	20,
1066272cc70bSAndy Fleming 	25,
1067272cc70bSAndy Fleming 	30,
1068272cc70bSAndy Fleming 	35,
1069272cc70bSAndy Fleming 	40,
1070272cc70bSAndy Fleming 	45,
1071272cc70bSAndy Fleming 	50,
1072272cc70bSAndy Fleming 	55,
1073272cc70bSAndy Fleming 	60,
1074272cc70bSAndy Fleming 	70,
1075272cc70bSAndy Fleming 	80,
1076272cc70bSAndy Fleming };
1077272cc70bSAndy Fleming 
10788ca51e51SSimon Glass #ifndef CONFIG_DM_MMC_OPS
1079fdbb873eSKim Phillips static void mmc_set_ios(struct mmc *mmc)
1080272cc70bSAndy Fleming {
108193bfd616SPantelis Antoniou 	if (mmc->cfg->ops->set_ios)
108293bfd616SPantelis Antoniou 		mmc->cfg->ops->set_ios(mmc);
1083272cc70bSAndy Fleming }
10848ca51e51SSimon Glass #endif
1085272cc70bSAndy Fleming 
1086272cc70bSAndy Fleming void mmc_set_clock(struct mmc *mmc, uint clock)
1087272cc70bSAndy Fleming {
108893bfd616SPantelis Antoniou 	if (clock > mmc->cfg->f_max)
108993bfd616SPantelis Antoniou 		clock = mmc->cfg->f_max;
1090272cc70bSAndy Fleming 
109193bfd616SPantelis Antoniou 	if (clock < mmc->cfg->f_min)
109293bfd616SPantelis Antoniou 		clock = mmc->cfg->f_min;
1093272cc70bSAndy Fleming 
1094272cc70bSAndy Fleming 	mmc->clock = clock;
1095272cc70bSAndy Fleming 
1096272cc70bSAndy Fleming 	mmc_set_ios(mmc);
1097272cc70bSAndy Fleming }
1098272cc70bSAndy Fleming 
1099fdbb873eSKim Phillips static void mmc_set_bus_width(struct mmc *mmc, uint width)
1100272cc70bSAndy Fleming {
1101272cc70bSAndy Fleming 	mmc->bus_width = width;
1102272cc70bSAndy Fleming 
1103272cc70bSAndy Fleming 	mmc_set_ios(mmc);
1104272cc70bSAndy Fleming }
1105272cc70bSAndy Fleming 
1106fdbb873eSKim Phillips static int mmc_startup(struct mmc *mmc)
1107272cc70bSAndy Fleming {
1108f866a46dSStephen Warren 	int err, i;
1109272cc70bSAndy Fleming 	uint mult, freq;
1110639b7827SYoshihiro Shimoda 	u64 cmult, csize, capacity;
1111272cc70bSAndy Fleming 	struct mmc_cmd cmd;
11128bfa195eSSimon Glass 	ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
11138bfa195eSSimon Glass 	ALLOC_CACHE_ALIGN_BUFFER(u8, test_csd, MMC_MAX_BLOCK_LEN);
11145d4fc8d9SRaffaele Recalcati 	int timeout = 1000;
11150c453bb7SDiego Santa Cruz 	bool has_parts = false;
11168a0cf490SDiego Santa Cruz 	bool part_completed;
1117c40fdca6SSimon Glass 	struct blk_desc *bdesc;
1118272cc70bSAndy Fleming 
1119d52ebf10SThomas Chou #ifdef CONFIG_MMC_SPI_CRC_ON
1120d52ebf10SThomas Chou 	if (mmc_host_is_spi(mmc)) { /* enable CRC check for spi */
1121d52ebf10SThomas Chou 		cmd.cmdidx = MMC_CMD_SPI_CRC_ON_OFF;
1122d52ebf10SThomas Chou 		cmd.resp_type = MMC_RSP_R1;
1123d52ebf10SThomas Chou 		cmd.cmdarg = 1;
1124d52ebf10SThomas Chou 		err = mmc_send_cmd(mmc, &cmd, NULL);
1125d52ebf10SThomas Chou 
1126d52ebf10SThomas Chou 		if (err)
1127d52ebf10SThomas Chou 			return err;
1128d52ebf10SThomas Chou 	}
1129d52ebf10SThomas Chou #endif
1130d52ebf10SThomas Chou 
1131272cc70bSAndy Fleming 	/* Put the Card in Identify Mode */
1132d52ebf10SThomas Chou 	cmd.cmdidx = mmc_host_is_spi(mmc) ? MMC_CMD_SEND_CID :
1133d52ebf10SThomas Chou 		MMC_CMD_ALL_SEND_CID; /* cmd not supported in spi */
1134272cc70bSAndy Fleming 	cmd.resp_type = MMC_RSP_R2;
1135272cc70bSAndy Fleming 	cmd.cmdarg = 0;
1136272cc70bSAndy Fleming 
1137272cc70bSAndy Fleming 	err = mmc_send_cmd(mmc, &cmd, NULL);
1138272cc70bSAndy Fleming 
1139272cc70bSAndy Fleming 	if (err)
1140272cc70bSAndy Fleming 		return err;
1141272cc70bSAndy Fleming 
1142272cc70bSAndy Fleming 	memcpy(mmc->cid, cmd.response, 16);
1143272cc70bSAndy Fleming 
1144272cc70bSAndy Fleming 	/*
1145272cc70bSAndy Fleming 	 * For MMC cards, set the Relative Address.
1146272cc70bSAndy Fleming 	 * For SD cards, get the Relatvie Address.
1147272cc70bSAndy Fleming 	 * This also puts the cards into Standby State
1148272cc70bSAndy Fleming 	 */
1149d52ebf10SThomas Chou 	if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
1150272cc70bSAndy Fleming 		cmd.cmdidx = SD_CMD_SEND_RELATIVE_ADDR;
1151272cc70bSAndy Fleming 		cmd.cmdarg = mmc->rca << 16;
1152272cc70bSAndy Fleming 		cmd.resp_type = MMC_RSP_R6;
1153272cc70bSAndy Fleming 
1154272cc70bSAndy Fleming 		err = mmc_send_cmd(mmc, &cmd, NULL);
1155272cc70bSAndy Fleming 
1156272cc70bSAndy Fleming 		if (err)
1157272cc70bSAndy Fleming 			return err;
1158272cc70bSAndy Fleming 
1159272cc70bSAndy Fleming 		if (IS_SD(mmc))
1160998be3ddSRabin Vincent 			mmc->rca = (cmd.response[0] >> 16) & 0xffff;
1161d52ebf10SThomas Chou 	}
1162272cc70bSAndy Fleming 
1163272cc70bSAndy Fleming 	/* Get the Card-Specific Data */
1164272cc70bSAndy Fleming 	cmd.cmdidx = MMC_CMD_SEND_CSD;
1165272cc70bSAndy Fleming 	cmd.resp_type = MMC_RSP_R2;
1166272cc70bSAndy Fleming 	cmd.cmdarg = mmc->rca << 16;
1167272cc70bSAndy Fleming 
1168272cc70bSAndy Fleming 	err = mmc_send_cmd(mmc, &cmd, NULL);
1169272cc70bSAndy Fleming 
11705d4fc8d9SRaffaele Recalcati 	/* Waiting for the ready status */
11715d4fc8d9SRaffaele Recalcati 	mmc_send_status(mmc, timeout);
11725d4fc8d9SRaffaele Recalcati 
1173272cc70bSAndy Fleming 	if (err)
1174272cc70bSAndy Fleming 		return err;
1175272cc70bSAndy Fleming 
1176998be3ddSRabin Vincent 	mmc->csd[0] = cmd.response[0];
1177998be3ddSRabin Vincent 	mmc->csd[1] = cmd.response[1];
1178998be3ddSRabin Vincent 	mmc->csd[2] = cmd.response[2];
1179998be3ddSRabin Vincent 	mmc->csd[3] = cmd.response[3];
1180272cc70bSAndy Fleming 
1181272cc70bSAndy Fleming 	if (mmc->version == MMC_VERSION_UNKNOWN) {
11820b453ffeSRabin Vincent 		int version = (cmd.response[0] >> 26) & 0xf;
1183272cc70bSAndy Fleming 
1184272cc70bSAndy Fleming 		switch (version) {
1185272cc70bSAndy Fleming 		case 0:
1186272cc70bSAndy Fleming 			mmc->version = MMC_VERSION_1_2;
1187272cc70bSAndy Fleming 			break;
1188272cc70bSAndy Fleming 		case 1:
1189272cc70bSAndy Fleming 			mmc->version = MMC_VERSION_1_4;
1190272cc70bSAndy Fleming 			break;
1191272cc70bSAndy Fleming 		case 2:
1192272cc70bSAndy Fleming 			mmc->version = MMC_VERSION_2_2;
1193272cc70bSAndy Fleming 			break;
1194272cc70bSAndy Fleming 		case 3:
1195272cc70bSAndy Fleming 			mmc->version = MMC_VERSION_3;
1196272cc70bSAndy Fleming 			break;
1197272cc70bSAndy Fleming 		case 4:
1198272cc70bSAndy Fleming 			mmc->version = MMC_VERSION_4;
1199272cc70bSAndy Fleming 			break;
1200272cc70bSAndy Fleming 		default:
1201272cc70bSAndy Fleming 			mmc->version = MMC_VERSION_1_2;
1202272cc70bSAndy Fleming 			break;
1203272cc70bSAndy Fleming 		}
1204272cc70bSAndy Fleming 	}
1205272cc70bSAndy Fleming 
1206272cc70bSAndy Fleming 	/* divide frequency by 10, since the mults are 10x bigger */
12070b453ffeSRabin Vincent 	freq = fbase[(cmd.response[0] & 0x7)];
12080b453ffeSRabin Vincent 	mult = multipliers[((cmd.response[0] >> 3) & 0xf)];
1209272cc70bSAndy Fleming 
1210272cc70bSAndy Fleming 	mmc->tran_speed = freq * mult;
1211272cc70bSAndy Fleming 
1212ab71188cSMarkus Niebel 	mmc->dsr_imp = ((cmd.response[1] >> 12) & 0x1);
1213998be3ddSRabin Vincent 	mmc->read_bl_len = 1 << ((cmd.response[1] >> 16) & 0xf);
1214272cc70bSAndy Fleming 
1215272cc70bSAndy Fleming 	if (IS_SD(mmc))
1216272cc70bSAndy Fleming 		mmc->write_bl_len = mmc->read_bl_len;
1217272cc70bSAndy Fleming 	else
1218998be3ddSRabin Vincent 		mmc->write_bl_len = 1 << ((cmd.response[3] >> 22) & 0xf);
1219272cc70bSAndy Fleming 
1220272cc70bSAndy Fleming 	if (mmc->high_capacity) {
1221272cc70bSAndy Fleming 		csize = (mmc->csd[1] & 0x3f) << 16
1222272cc70bSAndy Fleming 			| (mmc->csd[2] & 0xffff0000) >> 16;
1223272cc70bSAndy Fleming 		cmult = 8;
1224272cc70bSAndy Fleming 	} else {
1225272cc70bSAndy Fleming 		csize = (mmc->csd[1] & 0x3ff) << 2
1226272cc70bSAndy Fleming 			| (mmc->csd[2] & 0xc0000000) >> 30;
1227272cc70bSAndy Fleming 		cmult = (mmc->csd[2] & 0x00038000) >> 15;
1228272cc70bSAndy Fleming 	}
1229272cc70bSAndy Fleming 
1230f866a46dSStephen Warren 	mmc->capacity_user = (csize + 1) << (cmult + 2);
1231f866a46dSStephen Warren 	mmc->capacity_user *= mmc->read_bl_len;
1232f866a46dSStephen Warren 	mmc->capacity_boot = 0;
1233f866a46dSStephen Warren 	mmc->capacity_rpmb = 0;
1234f866a46dSStephen Warren 	for (i = 0; i < 4; i++)
1235f866a46dSStephen Warren 		mmc->capacity_gp[i] = 0;
1236272cc70bSAndy Fleming 
12378bfa195eSSimon Glass 	if (mmc->read_bl_len > MMC_MAX_BLOCK_LEN)
12388bfa195eSSimon Glass 		mmc->read_bl_len = MMC_MAX_BLOCK_LEN;
1239272cc70bSAndy Fleming 
12408bfa195eSSimon Glass 	if (mmc->write_bl_len > MMC_MAX_BLOCK_LEN)
12418bfa195eSSimon Glass 		mmc->write_bl_len = MMC_MAX_BLOCK_LEN;
1242272cc70bSAndy Fleming 
1243ab71188cSMarkus Niebel 	if ((mmc->dsr_imp) && (0xffffffff != mmc->dsr)) {
1244ab71188cSMarkus Niebel 		cmd.cmdidx = MMC_CMD_SET_DSR;
1245ab71188cSMarkus Niebel 		cmd.cmdarg = (mmc->dsr & 0xffff) << 16;
1246ab71188cSMarkus Niebel 		cmd.resp_type = MMC_RSP_NONE;
1247ab71188cSMarkus Niebel 		if (mmc_send_cmd(mmc, &cmd, NULL))
1248ab71188cSMarkus Niebel 			printf("MMC: SET_DSR failed\n");
1249ab71188cSMarkus Niebel 	}
1250ab71188cSMarkus Niebel 
1251272cc70bSAndy Fleming 	/* Select the card, and put it into Transfer Mode */
1252d52ebf10SThomas Chou 	if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
1253272cc70bSAndy Fleming 		cmd.cmdidx = MMC_CMD_SELECT_CARD;
1254fe8f7066SAjay Bhargav 		cmd.resp_type = MMC_RSP_R1;
1255272cc70bSAndy Fleming 		cmd.cmdarg = mmc->rca << 16;
1256272cc70bSAndy Fleming 		err = mmc_send_cmd(mmc, &cmd, NULL);
1257272cc70bSAndy Fleming 
1258272cc70bSAndy Fleming 		if (err)
1259272cc70bSAndy Fleming 			return err;
1260d52ebf10SThomas Chou 	}
1261272cc70bSAndy Fleming 
1262e6f99a56SLei Wen 	/*
1263e6f99a56SLei Wen 	 * For SD, its erase group is always one sector
1264e6f99a56SLei Wen 	 */
1265e6f99a56SLei Wen 	mmc->erase_grp_size = 1;
1266bc897b1dSLei Wen 	mmc->part_config = MMCPART_NOAVAILABLE;
1267d23e2c09SSukumar Ghorai 	if (!IS_SD(mmc) && (mmc->version >= MMC_VERSION_4)) {
1268d23e2c09SSukumar Ghorai 		/* check  ext_csd version and capacity */
1269d23e2c09SSukumar Ghorai 		err = mmc_send_ext_csd(mmc, ext_csd);
12709cf199ebSDiego Santa Cruz 		if (err)
12719cf199ebSDiego Santa Cruz 			return err;
12729cf199ebSDiego Santa Cruz 		if (ext_csd[EXT_CSD_REV] >= 2) {
1273639b7827SYoshihiro Shimoda 			/*
1274639b7827SYoshihiro Shimoda 			 * According to the JEDEC Standard, the value of
1275639b7827SYoshihiro Shimoda 			 * ext_csd's capacity is valid if the value is more
1276639b7827SYoshihiro Shimoda 			 * than 2GB
1277639b7827SYoshihiro Shimoda 			 */
12780560db18SLei Wen 			capacity = ext_csd[EXT_CSD_SEC_CNT] << 0
12790560db18SLei Wen 					| ext_csd[EXT_CSD_SEC_CNT + 1] << 8
12800560db18SLei Wen 					| ext_csd[EXT_CSD_SEC_CNT + 2] << 16
12810560db18SLei Wen 					| ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
12828bfa195eSSimon Glass 			capacity *= MMC_MAX_BLOCK_LEN;
1283b1f1e821SŁukasz Majewski 			if ((capacity >> 20) > 2 * 1024)
1284f866a46dSStephen Warren 				mmc->capacity_user = capacity;
1285d23e2c09SSukumar Ghorai 		}
1286bc897b1dSLei Wen 
128764f4a619SJaehoon Chung 		switch (ext_csd[EXT_CSD_REV]) {
128864f4a619SJaehoon Chung 		case 1:
128964f4a619SJaehoon Chung 			mmc->version = MMC_VERSION_4_1;
129064f4a619SJaehoon Chung 			break;
129164f4a619SJaehoon Chung 		case 2:
129264f4a619SJaehoon Chung 			mmc->version = MMC_VERSION_4_2;
129364f4a619SJaehoon Chung 			break;
129464f4a619SJaehoon Chung 		case 3:
129564f4a619SJaehoon Chung 			mmc->version = MMC_VERSION_4_3;
129664f4a619SJaehoon Chung 			break;
129764f4a619SJaehoon Chung 		case 5:
129864f4a619SJaehoon Chung 			mmc->version = MMC_VERSION_4_41;
129964f4a619SJaehoon Chung 			break;
130064f4a619SJaehoon Chung 		case 6:
130164f4a619SJaehoon Chung 			mmc->version = MMC_VERSION_4_5;
130264f4a619SJaehoon Chung 			break;
1303edab723bSMarkus Niebel 		case 7:
1304edab723bSMarkus Niebel 			mmc->version = MMC_VERSION_5_0;
1305edab723bSMarkus Niebel 			break;
13061a3619cfSStefan Wahren 		case 8:
13071a3619cfSStefan Wahren 			mmc->version = MMC_VERSION_5_1;
13081a3619cfSStefan Wahren 			break;
130964f4a619SJaehoon Chung 		}
131064f4a619SJaehoon Chung 
13118a0cf490SDiego Santa Cruz 		/* The partition data may be non-zero but it is only
13128a0cf490SDiego Santa Cruz 		 * effective if PARTITION_SETTING_COMPLETED is set in
13138a0cf490SDiego Santa Cruz 		 * EXT_CSD, so ignore any data if this bit is not set,
13148a0cf490SDiego Santa Cruz 		 * except for enabling the high-capacity group size
13158a0cf490SDiego Santa Cruz 		 * definition (see below). */
13168a0cf490SDiego Santa Cruz 		part_completed = !!(ext_csd[EXT_CSD_PARTITION_SETTING] &
13178a0cf490SDiego Santa Cruz 				    EXT_CSD_PARTITION_SETTING_COMPLETED);
13188a0cf490SDiego Santa Cruz 
13190c453bb7SDiego Santa Cruz 		/* store the partition info of emmc */
13200c453bb7SDiego Santa Cruz 		mmc->part_support = ext_csd[EXT_CSD_PARTITIONING_SUPPORT];
13210c453bb7SDiego Santa Cruz 		if ((ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & PART_SUPPORT) ||
13220c453bb7SDiego Santa Cruz 		    ext_csd[EXT_CSD_BOOT_MULT])
13230c453bb7SDiego Santa Cruz 			mmc->part_config = ext_csd[EXT_CSD_PART_CONF];
13248a0cf490SDiego Santa Cruz 		if (part_completed &&
13258a0cf490SDiego Santa Cruz 		    (ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & ENHNCD_SUPPORT))
13260c453bb7SDiego Santa Cruz 			mmc->part_attr = ext_csd[EXT_CSD_PARTITIONS_ATTRIBUTE];
13270c453bb7SDiego Santa Cruz 
13280c453bb7SDiego Santa Cruz 		mmc->capacity_boot = ext_csd[EXT_CSD_BOOT_MULT] << 17;
13290c453bb7SDiego Santa Cruz 
13300c453bb7SDiego Santa Cruz 		mmc->capacity_rpmb = ext_csd[EXT_CSD_RPMB_MULT] << 17;
13310c453bb7SDiego Santa Cruz 
13320c453bb7SDiego Santa Cruz 		for (i = 0; i < 4; i++) {
13330c453bb7SDiego Santa Cruz 			int idx = EXT_CSD_GP_SIZE_MULT + i * 3;
13348a0cf490SDiego Santa Cruz 			uint mult = (ext_csd[idx + 2] << 16) +
13350c453bb7SDiego Santa Cruz 				(ext_csd[idx + 1] << 8) + ext_csd[idx];
13368a0cf490SDiego Santa Cruz 			if (mult)
13378a0cf490SDiego Santa Cruz 				has_parts = true;
13388a0cf490SDiego Santa Cruz 			if (!part_completed)
13398a0cf490SDiego Santa Cruz 				continue;
13408a0cf490SDiego Santa Cruz 			mmc->capacity_gp[i] = mult;
13410c453bb7SDiego Santa Cruz 			mmc->capacity_gp[i] *=
13420c453bb7SDiego Santa Cruz 				ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
13430c453bb7SDiego Santa Cruz 			mmc->capacity_gp[i] *= ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
1344f8e89d67SDiego Santa Cruz 			mmc->capacity_gp[i] <<= 19;
13450c453bb7SDiego Santa Cruz 		}
13460c453bb7SDiego Santa Cruz 
13478a0cf490SDiego Santa Cruz 		if (part_completed) {
1348a7f852b6SDiego Santa Cruz 			mmc->enh_user_size =
1349a7f852b6SDiego Santa Cruz 				(ext_csd[EXT_CSD_ENH_SIZE_MULT+2] << 16) +
1350a7f852b6SDiego Santa Cruz 				(ext_csd[EXT_CSD_ENH_SIZE_MULT+1] << 8) +
1351a7f852b6SDiego Santa Cruz 				ext_csd[EXT_CSD_ENH_SIZE_MULT];
1352a7f852b6SDiego Santa Cruz 			mmc->enh_user_size *= ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
1353a7f852b6SDiego Santa Cruz 			mmc->enh_user_size *= ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
1354a7f852b6SDiego Santa Cruz 			mmc->enh_user_size <<= 19;
1355a7f852b6SDiego Santa Cruz 			mmc->enh_user_start =
1356a7f852b6SDiego Santa Cruz 				(ext_csd[EXT_CSD_ENH_START_ADDR+3] << 24) +
1357a7f852b6SDiego Santa Cruz 				(ext_csd[EXT_CSD_ENH_START_ADDR+2] << 16) +
1358a7f852b6SDiego Santa Cruz 				(ext_csd[EXT_CSD_ENH_START_ADDR+1] << 8) +
1359a7f852b6SDiego Santa Cruz 				ext_csd[EXT_CSD_ENH_START_ADDR];
1360a7f852b6SDiego Santa Cruz 			if (mmc->high_capacity)
1361a7f852b6SDiego Santa Cruz 				mmc->enh_user_start <<= 9;
13628a0cf490SDiego Santa Cruz 		}
1363a7f852b6SDiego Santa Cruz 
1364e6f99a56SLei Wen 		/*
13651937e5aaSOliver Metz 		 * Host needs to enable ERASE_GRP_DEF bit if device is
13661937e5aaSOliver Metz 		 * partitioned. This bit will be lost every time after a reset
13671937e5aaSOliver Metz 		 * or power off. This will affect erase size.
1368e6f99a56SLei Wen 		 */
13698a0cf490SDiego Santa Cruz 		if (part_completed)
13700c453bb7SDiego Santa Cruz 			has_parts = true;
13711937e5aaSOliver Metz 		if ((ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & PART_SUPPORT) &&
13720c453bb7SDiego Santa Cruz 		    (ext_csd[EXT_CSD_PARTITIONS_ATTRIBUTE] & PART_ENH_ATTRIB))
13730c453bb7SDiego Santa Cruz 			has_parts = true;
13740c453bb7SDiego Santa Cruz 		if (has_parts) {
13751937e5aaSOliver Metz 			err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
13761937e5aaSOliver Metz 				EXT_CSD_ERASE_GROUP_DEF, 1);
13771937e5aaSOliver Metz 
13781937e5aaSOliver Metz 			if (err)
13791937e5aaSOliver Metz 				return err;
1380021a8055SHannes Petermaier 			else
1381021a8055SHannes Petermaier 				ext_csd[EXT_CSD_ERASE_GROUP_DEF] = 1;
1382037dc0abSDiego Santa Cruz 		}
13831937e5aaSOliver Metz 
1384037dc0abSDiego Santa Cruz 		if (ext_csd[EXT_CSD_ERASE_GROUP_DEF] & 0x01) {
13851937e5aaSOliver Metz 			/* Read out group size from ext_csd */
13860560db18SLei Wen 			mmc->erase_grp_size =
1387a4ff9f83SDiego Santa Cruz 				ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] * 1024;
1388d7b29129SMarkus Niebel 			/*
1389d7b29129SMarkus Niebel 			 * if high capacity and partition setting completed
1390d7b29129SMarkus Niebel 			 * SEC_COUNT is valid even if it is smaller than 2 GiB
1391d7b29129SMarkus Niebel 			 * JEDEC Standard JESD84-B45, 6.2.4
1392d7b29129SMarkus Niebel 			 */
13938a0cf490SDiego Santa Cruz 			if (mmc->high_capacity && part_completed) {
1394d7b29129SMarkus Niebel 				capacity = (ext_csd[EXT_CSD_SEC_CNT]) |
1395d7b29129SMarkus Niebel 					(ext_csd[EXT_CSD_SEC_CNT + 1] << 8) |
1396d7b29129SMarkus Niebel 					(ext_csd[EXT_CSD_SEC_CNT + 2] << 16) |
1397d7b29129SMarkus Niebel 					(ext_csd[EXT_CSD_SEC_CNT + 3] << 24);
1398d7b29129SMarkus Niebel 				capacity *= MMC_MAX_BLOCK_LEN;
1399d7b29129SMarkus Niebel 				mmc->capacity_user = capacity;
1400d7b29129SMarkus Niebel 			}
14018bfa195eSSimon Glass 		} else {
14021937e5aaSOliver Metz 			/* Calculate the group size from the csd value. */
1403e6f99a56SLei Wen 			int erase_gsz, erase_gmul;
1404e6f99a56SLei Wen 			erase_gsz = (mmc->csd[2] & 0x00007c00) >> 10;
1405e6f99a56SLei Wen 			erase_gmul = (mmc->csd[2] & 0x000003e0) >> 5;
1406e6f99a56SLei Wen 			mmc->erase_grp_size = (erase_gsz + 1)
1407e6f99a56SLei Wen 				* (erase_gmul + 1);
1408e6f99a56SLei Wen 		}
1409037dc0abSDiego Santa Cruz 
1410037dc0abSDiego Santa Cruz 		mmc->hc_wp_grp_size = 1024
1411037dc0abSDiego Santa Cruz 			* ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]
1412037dc0abSDiego Santa Cruz 			* ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
14139e41a00bSDiego Santa Cruz 
14149e41a00bSDiego Santa Cruz 		mmc->wr_rel_set = ext_csd[EXT_CSD_WR_REL_SET];
1415f866a46dSStephen Warren 	}
1416f866a46dSStephen Warren 
1417c40fdca6SSimon Glass 	err = mmc_set_capacity(mmc, mmc_get_blk_desc(mmc)->hwpart);
1418f866a46dSStephen Warren 	if (err)
1419f866a46dSStephen Warren 		return err;
1420d23e2c09SSukumar Ghorai 
1421272cc70bSAndy Fleming 	if (IS_SD(mmc))
1422272cc70bSAndy Fleming 		err = sd_change_freq(mmc);
1423272cc70bSAndy Fleming 	else
1424272cc70bSAndy Fleming 		err = mmc_change_freq(mmc);
1425272cc70bSAndy Fleming 
1426272cc70bSAndy Fleming 	if (err)
1427272cc70bSAndy Fleming 		return err;
1428272cc70bSAndy Fleming 
1429272cc70bSAndy Fleming 	/* Restrict card's capabilities by what the host can do */
143093bfd616SPantelis Antoniou 	mmc->card_caps &= mmc->cfg->host_caps;
1431272cc70bSAndy Fleming 
1432272cc70bSAndy Fleming 	if (IS_SD(mmc)) {
1433272cc70bSAndy Fleming 		if (mmc->card_caps & MMC_MODE_4BIT) {
1434272cc70bSAndy Fleming 			cmd.cmdidx = MMC_CMD_APP_CMD;
1435272cc70bSAndy Fleming 			cmd.resp_type = MMC_RSP_R1;
1436272cc70bSAndy Fleming 			cmd.cmdarg = mmc->rca << 16;
1437272cc70bSAndy Fleming 
1438272cc70bSAndy Fleming 			err = mmc_send_cmd(mmc, &cmd, NULL);
1439272cc70bSAndy Fleming 			if (err)
1440272cc70bSAndy Fleming 				return err;
1441272cc70bSAndy Fleming 
1442272cc70bSAndy Fleming 			cmd.cmdidx = SD_CMD_APP_SET_BUS_WIDTH;
1443272cc70bSAndy Fleming 			cmd.resp_type = MMC_RSP_R1;
1444272cc70bSAndy Fleming 			cmd.cmdarg = 2;
1445272cc70bSAndy Fleming 			err = mmc_send_cmd(mmc, &cmd, NULL);
1446272cc70bSAndy Fleming 			if (err)
1447272cc70bSAndy Fleming 				return err;
1448272cc70bSAndy Fleming 
1449272cc70bSAndy Fleming 			mmc_set_bus_width(mmc, 4);
1450272cc70bSAndy Fleming 		}
1451272cc70bSAndy Fleming 
14523697e599SPeng Fan 		err = sd_read_ssr(mmc);
14533697e599SPeng Fan 		if (err)
14543697e599SPeng Fan 			return err;
14553697e599SPeng Fan 
1456272cc70bSAndy Fleming 		if (mmc->card_caps & MMC_MODE_HS)
1457ad5fd922SJaehoon Chung 			mmc->tran_speed = 50000000;
1458272cc70bSAndy Fleming 		else
1459ad5fd922SJaehoon Chung 			mmc->tran_speed = 25000000;
1460fc5b32fbSAndrew Gabbasov 	} else if (mmc->version >= MMC_VERSION_4) {
1461fc5b32fbSAndrew Gabbasov 		/* Only version 4 of MMC supports wider bus widths */
14627798f6dbSAndy Fleming 		int idx;
14637798f6dbSAndy Fleming 
14647798f6dbSAndy Fleming 		/* An array of possible bus widths in order of preference */
14657798f6dbSAndy Fleming 		static unsigned ext_csd_bits[] = {
1466d22e3d46SJaehoon Chung 			EXT_CSD_DDR_BUS_WIDTH_8,
1467d22e3d46SJaehoon Chung 			EXT_CSD_DDR_BUS_WIDTH_4,
14687798f6dbSAndy Fleming 			EXT_CSD_BUS_WIDTH_8,
14697798f6dbSAndy Fleming 			EXT_CSD_BUS_WIDTH_4,
14707798f6dbSAndy Fleming 			EXT_CSD_BUS_WIDTH_1,
14717798f6dbSAndy Fleming 		};
14727798f6dbSAndy Fleming 
14737798f6dbSAndy Fleming 		/* An array to map CSD bus widths to host cap bits */
14747798f6dbSAndy Fleming 		static unsigned ext_to_hostcaps[] = {
1475786e8f81SAndrew Gabbasov 			[EXT_CSD_DDR_BUS_WIDTH_4] =
1476786e8f81SAndrew Gabbasov 				MMC_MODE_DDR_52MHz | MMC_MODE_4BIT,
1477786e8f81SAndrew Gabbasov 			[EXT_CSD_DDR_BUS_WIDTH_8] =
1478786e8f81SAndrew Gabbasov 				MMC_MODE_DDR_52MHz | MMC_MODE_8BIT,
14797798f6dbSAndy Fleming 			[EXT_CSD_BUS_WIDTH_4] = MMC_MODE_4BIT,
14807798f6dbSAndy Fleming 			[EXT_CSD_BUS_WIDTH_8] = MMC_MODE_8BIT,
14817798f6dbSAndy Fleming 		};
14827798f6dbSAndy Fleming 
14837798f6dbSAndy Fleming 		/* An array to map chosen bus width to an integer */
14847798f6dbSAndy Fleming 		static unsigned widths[] = {
1485d22e3d46SJaehoon Chung 			8, 4, 8, 4, 1,
14867798f6dbSAndy Fleming 		};
14877798f6dbSAndy Fleming 
14887798f6dbSAndy Fleming 		for (idx=0; idx < ARRAY_SIZE(ext_csd_bits); idx++) {
14897798f6dbSAndy Fleming 			unsigned int extw = ext_csd_bits[idx];
1490786e8f81SAndrew Gabbasov 			unsigned int caps = ext_to_hostcaps[extw];
14917798f6dbSAndy Fleming 
14927798f6dbSAndy Fleming 			/*
1493bf477073SAndrew Gabbasov 			 * If the bus width is still not changed,
1494bf477073SAndrew Gabbasov 			 * don't try to set the default again.
1495bf477073SAndrew Gabbasov 			 * Otherwise, recover from switch attempts
1496bf477073SAndrew Gabbasov 			 * by switching to 1-bit bus width.
1497bf477073SAndrew Gabbasov 			 */
1498bf477073SAndrew Gabbasov 			if (extw == EXT_CSD_BUS_WIDTH_1 &&
1499bf477073SAndrew Gabbasov 					mmc->bus_width == 1) {
1500bf477073SAndrew Gabbasov 				err = 0;
1501bf477073SAndrew Gabbasov 				break;
1502bf477073SAndrew Gabbasov 			}
1503bf477073SAndrew Gabbasov 
1504bf477073SAndrew Gabbasov 			/*
1505786e8f81SAndrew Gabbasov 			 * Check to make sure the card and controller support
1506786e8f81SAndrew Gabbasov 			 * these capabilities
15077798f6dbSAndy Fleming 			 */
1508786e8f81SAndrew Gabbasov 			if ((mmc->card_caps & caps) != caps)
15097798f6dbSAndy Fleming 				continue;
15107798f6dbSAndy Fleming 
1511272cc70bSAndy Fleming 			err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
15127798f6dbSAndy Fleming 					EXT_CSD_BUS_WIDTH, extw);
1513272cc70bSAndy Fleming 
1514272cc70bSAndy Fleming 			if (err)
15154137894eSLei Wen 				continue;
1516272cc70bSAndy Fleming 
1517786e8f81SAndrew Gabbasov 			mmc->ddr_mode = (caps & MMC_MODE_DDR_52MHz) ? 1 : 0;
15187798f6dbSAndy Fleming 			mmc_set_bus_width(mmc, widths[idx]);
1519272cc70bSAndy Fleming 
15204137894eSLei Wen 			err = mmc_send_ext_csd(mmc, test_csd);
1521272cc70bSAndy Fleming 
1522786e8f81SAndrew Gabbasov 			if (err)
1523786e8f81SAndrew Gabbasov 				continue;
1524786e8f81SAndrew Gabbasov 
1525786e8f81SAndrew Gabbasov 			/* Only compare read only fields */
1526786e8f81SAndrew Gabbasov 			if (ext_csd[EXT_CSD_PARTITIONING_SUPPORT]
1527786e8f81SAndrew Gabbasov 				== test_csd[EXT_CSD_PARTITIONING_SUPPORT] &&
1528786e8f81SAndrew Gabbasov 			    ext_csd[EXT_CSD_HC_WP_GRP_SIZE]
1529786e8f81SAndrew Gabbasov 				== test_csd[EXT_CSD_HC_WP_GRP_SIZE] &&
1530786e8f81SAndrew Gabbasov 			    ext_csd[EXT_CSD_REV]
1531786e8f81SAndrew Gabbasov 				== test_csd[EXT_CSD_REV] &&
1532786e8f81SAndrew Gabbasov 			    ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]
1533786e8f81SAndrew Gabbasov 				== test_csd[EXT_CSD_HC_ERASE_GRP_SIZE] &&
1534786e8f81SAndrew Gabbasov 			    memcmp(&ext_csd[EXT_CSD_SEC_CNT],
1535786e8f81SAndrew Gabbasov 				   &test_csd[EXT_CSD_SEC_CNT], 4) == 0)
15364137894eSLei Wen 				break;
1537786e8f81SAndrew Gabbasov 			else
1538915ffa52SJaehoon Chung 				err = -EBADMSG;
15394137894eSLei Wen 		}
1540786e8f81SAndrew Gabbasov 
1541786e8f81SAndrew Gabbasov 		if (err)
1542786e8f81SAndrew Gabbasov 			return err;
1543272cc70bSAndy Fleming 
1544272cc70bSAndy Fleming 		if (mmc->card_caps & MMC_MODE_HS) {
1545272cc70bSAndy Fleming 			if (mmc->card_caps & MMC_MODE_HS_52MHz)
1546ad5fd922SJaehoon Chung 				mmc->tran_speed = 52000000;
1547272cc70bSAndy Fleming 			else
1548ad5fd922SJaehoon Chung 				mmc->tran_speed = 26000000;
1549272cc70bSAndy Fleming 		}
1550ad5fd922SJaehoon Chung 	}
1551ad5fd922SJaehoon Chung 
1552ad5fd922SJaehoon Chung 	mmc_set_clock(mmc, mmc->tran_speed);
1553272cc70bSAndy Fleming 
15545af8f45cSAndrew Gabbasov 	/* Fix the block length for DDR mode */
15555af8f45cSAndrew Gabbasov 	if (mmc->ddr_mode) {
15565af8f45cSAndrew Gabbasov 		mmc->read_bl_len = MMC_MAX_BLOCK_LEN;
15575af8f45cSAndrew Gabbasov 		mmc->write_bl_len = MMC_MAX_BLOCK_LEN;
15585af8f45cSAndrew Gabbasov 	}
15595af8f45cSAndrew Gabbasov 
1560272cc70bSAndy Fleming 	/* fill in device description */
1561c40fdca6SSimon Glass 	bdesc = mmc_get_blk_desc(mmc);
1562c40fdca6SSimon Glass 	bdesc->lun = 0;
1563c40fdca6SSimon Glass 	bdesc->hwpart = 0;
1564c40fdca6SSimon Glass 	bdesc->type = 0;
1565c40fdca6SSimon Glass 	bdesc->blksz = mmc->read_bl_len;
1566c40fdca6SSimon Glass 	bdesc->log2blksz = LOG2(bdesc->blksz);
1567c40fdca6SSimon Glass 	bdesc->lba = lldiv(mmc->capacity, mmc->read_bl_len);
1568fc011f64SSjoerd Simons #if !defined(CONFIG_SPL_BUILD) || \
1569fc011f64SSjoerd Simons 		(defined(CONFIG_SPL_LIBCOMMON_SUPPORT) && \
1570fc011f64SSjoerd Simons 		!defined(CONFIG_USE_TINY_PRINTF))
1571c40fdca6SSimon Glass 	sprintf(bdesc->vendor, "Man %06x Snr %04x%04x",
1572babce5f6STaylor Hutt 		mmc->cid[0] >> 24, (mmc->cid[2] & 0xffff),
1573babce5f6STaylor Hutt 		(mmc->cid[3] >> 16) & 0xffff);
1574c40fdca6SSimon Glass 	sprintf(bdesc->product, "%c%c%c%c%c%c", mmc->cid[0] & 0xff,
15750b453ffeSRabin Vincent 		(mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
1576babce5f6STaylor Hutt 		(mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff,
1577babce5f6STaylor Hutt 		(mmc->cid[2] >> 24) & 0xff);
1578c40fdca6SSimon Glass 	sprintf(bdesc->revision, "%d.%d", (mmc->cid[2] >> 20) & 0xf,
1579babce5f6STaylor Hutt 		(mmc->cid[2] >> 16) & 0xf);
158056196826SPaul Burton #else
1581c40fdca6SSimon Glass 	bdesc->vendor[0] = 0;
1582c40fdca6SSimon Glass 	bdesc->product[0] = 0;
1583c40fdca6SSimon Glass 	bdesc->revision[0] = 0;
158456196826SPaul Burton #endif
1585122efd43SMikhail Kshevetskiy #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBDISK_SUPPORT)
1586c40fdca6SSimon Glass 	part_init(bdesc);
1587122efd43SMikhail Kshevetskiy #endif
1588272cc70bSAndy Fleming 
1589272cc70bSAndy Fleming 	return 0;
1590272cc70bSAndy Fleming }
1591272cc70bSAndy Fleming 
1592fdbb873eSKim Phillips static int mmc_send_if_cond(struct mmc *mmc)
1593272cc70bSAndy Fleming {
1594272cc70bSAndy Fleming 	struct mmc_cmd cmd;
1595272cc70bSAndy Fleming 	int err;
1596272cc70bSAndy Fleming 
1597272cc70bSAndy Fleming 	cmd.cmdidx = SD_CMD_SEND_IF_COND;
1598272cc70bSAndy Fleming 	/* We set the bit if the host supports voltages between 2.7 and 3.6 V */
159993bfd616SPantelis Antoniou 	cmd.cmdarg = ((mmc->cfg->voltages & 0xff8000) != 0) << 8 | 0xaa;
1600272cc70bSAndy Fleming 	cmd.resp_type = MMC_RSP_R7;
1601272cc70bSAndy Fleming 
1602272cc70bSAndy Fleming 	err = mmc_send_cmd(mmc, &cmd, NULL);
1603272cc70bSAndy Fleming 
1604272cc70bSAndy Fleming 	if (err)
1605272cc70bSAndy Fleming 		return err;
1606272cc70bSAndy Fleming 
1607998be3ddSRabin Vincent 	if ((cmd.response[0] & 0xff) != 0xaa)
1608915ffa52SJaehoon Chung 		return -EOPNOTSUPP;
1609272cc70bSAndy Fleming 	else
1610272cc70bSAndy Fleming 		mmc->version = SD_VERSION_2;
1611272cc70bSAndy Fleming 
1612272cc70bSAndy Fleming 	return 0;
1613272cc70bSAndy Fleming }
1614272cc70bSAndy Fleming 
161595de9ab2SPaul Kocialkowski /* board-specific MMC power initializations. */
161695de9ab2SPaul Kocialkowski __weak void board_mmc_power_init(void)
161795de9ab2SPaul Kocialkowski {
161895de9ab2SPaul Kocialkowski }
161995de9ab2SPaul Kocialkowski 
16202051aefeSPeng Fan static int mmc_power_init(struct mmc *mmc)
16212051aefeSPeng Fan {
16222051aefeSPeng Fan 	board_mmc_power_init();
16232051aefeSPeng Fan 
16242051aefeSPeng Fan #if defined(CONFIG_DM_MMC) && defined(CONFIG_DM_REGULATOR) && \
16252051aefeSPeng Fan 	!defined(CONFIG_SPL_BUILD)
16262051aefeSPeng Fan 	struct udevice *vmmc_supply;
16272051aefeSPeng Fan 	int ret;
16282051aefeSPeng Fan 
16292051aefeSPeng Fan 	ret = device_get_supply_regulator(mmc->dev, "vmmc-supply",
16302051aefeSPeng Fan 					  &vmmc_supply);
16312051aefeSPeng Fan 	if (ret) {
1632288db7c7SJaehoon Chung 		debug("%s: No vmmc supply\n", mmc->dev->name);
16332051aefeSPeng Fan 		return 0;
16342051aefeSPeng Fan 	}
16352051aefeSPeng Fan 
16362051aefeSPeng Fan 	ret = regulator_set_enable(vmmc_supply, true);
16372051aefeSPeng Fan 	if (ret) {
16382051aefeSPeng Fan 		puts("Error enabling VMMC supply\n");
16392051aefeSPeng Fan 		return ret;
16402051aefeSPeng Fan 	}
16412051aefeSPeng Fan #endif
16422051aefeSPeng Fan 	return 0;
16432051aefeSPeng Fan }
16442051aefeSPeng Fan 
1645e9550449SChe-Liang Chiou int mmc_start_init(struct mmc *mmc)
1646272cc70bSAndy Fleming {
16478ca51e51SSimon Glass 	bool no_card;
1648afd5932bSMacpaul Lin 	int err;
1649272cc70bSAndy Fleming 
1650ab769f22SPantelis Antoniou 	/* we pretend there's no card when init is NULL */
16518ca51e51SSimon Glass 	no_card = mmc_getcd(mmc) == 0;
16528ca51e51SSimon Glass #ifndef CONFIG_DM_MMC_OPS
16538ca51e51SSimon Glass 	no_card = no_card || (mmc->cfg->ops->init == NULL);
16548ca51e51SSimon Glass #endif
16558ca51e51SSimon Glass 	if (no_card) {
165648972d90SThierry Reding 		mmc->has_init = 0;
165756196826SPaul Burton #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
165848972d90SThierry Reding 		printf("MMC: no card present\n");
165956196826SPaul Burton #endif
1660915ffa52SJaehoon Chung 		return -ENOMEDIUM;
166148972d90SThierry Reding 	}
166248972d90SThierry Reding 
1663bc897b1dSLei Wen 	if (mmc->has_init)
1664bc897b1dSLei Wen 		return 0;
1665bc897b1dSLei Wen 
16665a8dbdc6SYangbo Lu #ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT
16675a8dbdc6SYangbo Lu 	mmc_adapter_card_type_ident();
16685a8dbdc6SYangbo Lu #endif
16692051aefeSPeng Fan 	err = mmc_power_init(mmc);
16702051aefeSPeng Fan 	if (err)
16712051aefeSPeng Fan 		return err;
167295de9ab2SPaul Kocialkowski 
16738ca51e51SSimon Glass #ifdef CONFIG_DM_MMC_OPS
16748ca51e51SSimon Glass 	/* The device has already been probed ready for use */
16758ca51e51SSimon Glass #else
1676ab769f22SPantelis Antoniou 	/* made sure it's not NULL earlier */
167793bfd616SPantelis Antoniou 	err = mmc->cfg->ops->init(mmc);
1678272cc70bSAndy Fleming 	if (err)
1679272cc70bSAndy Fleming 		return err;
16808ca51e51SSimon Glass #endif
1681786e8f81SAndrew Gabbasov 	mmc->ddr_mode = 0;
1682b86b85e2SIlya Yanok 	mmc_set_bus_width(mmc, 1);
1683b86b85e2SIlya Yanok 	mmc_set_clock(mmc, 1);
1684b86b85e2SIlya Yanok 
1685272cc70bSAndy Fleming 	/* Reset the Card */
1686272cc70bSAndy Fleming 	err = mmc_go_idle(mmc);
1687272cc70bSAndy Fleming 
1688272cc70bSAndy Fleming 	if (err)
1689272cc70bSAndy Fleming 		return err;
1690272cc70bSAndy Fleming 
1691bc897b1dSLei Wen 	/* The internal partition reset to user partition(0) at every CMD0*/
1692c40fdca6SSimon Glass 	mmc_get_blk_desc(mmc)->hwpart = 0;
1693bc897b1dSLei Wen 
1694272cc70bSAndy Fleming 	/* Test for SD version 2 */
1695272cc70bSAndy Fleming 	err = mmc_send_if_cond(mmc);
1696272cc70bSAndy Fleming 
1697272cc70bSAndy Fleming 	/* Now try to get the SD card's operating condition */
1698272cc70bSAndy Fleming 	err = sd_send_op_cond(mmc);
1699272cc70bSAndy Fleming 
1700272cc70bSAndy Fleming 	/* If the command timed out, we check for an MMC card */
1701915ffa52SJaehoon Chung 	if (err == -ETIMEDOUT) {
1702272cc70bSAndy Fleming 		err = mmc_send_op_cond(mmc);
1703272cc70bSAndy Fleming 
1704bd47c135SAndrew Gabbasov 		if (err) {
170556196826SPaul Burton #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
1706272cc70bSAndy Fleming 			printf("Card did not respond to voltage select!\n");
170756196826SPaul Burton #endif
1708915ffa52SJaehoon Chung 			return -EOPNOTSUPP;
1709272cc70bSAndy Fleming 		}
1710272cc70bSAndy Fleming 	}
1711272cc70bSAndy Fleming 
1712bd47c135SAndrew Gabbasov 	if (!err)
1713e9550449SChe-Liang Chiou 		mmc->init_in_progress = 1;
1714e9550449SChe-Liang Chiou 
1715e9550449SChe-Liang Chiou 	return err;
1716e9550449SChe-Liang Chiou }
1717e9550449SChe-Liang Chiou 
1718e9550449SChe-Liang Chiou static int mmc_complete_init(struct mmc *mmc)
1719e9550449SChe-Liang Chiou {
1720e9550449SChe-Liang Chiou 	int err = 0;
1721e9550449SChe-Liang Chiou 
1722bd47c135SAndrew Gabbasov 	mmc->init_in_progress = 0;
1723e9550449SChe-Liang Chiou 	if (mmc->op_cond_pending)
1724e9550449SChe-Liang Chiou 		err = mmc_complete_op_cond(mmc);
1725e9550449SChe-Liang Chiou 
1726e9550449SChe-Liang Chiou 	if (!err)
1727bc897b1dSLei Wen 		err = mmc_startup(mmc);
1728bc897b1dSLei Wen 	if (err)
1729bc897b1dSLei Wen 		mmc->has_init = 0;
1730bc897b1dSLei Wen 	else
1731bc897b1dSLei Wen 		mmc->has_init = 1;
1732e9550449SChe-Liang Chiou 	return err;
1733e9550449SChe-Liang Chiou }
1734e9550449SChe-Liang Chiou 
1735e9550449SChe-Liang Chiou int mmc_init(struct mmc *mmc)
1736e9550449SChe-Liang Chiou {
1737bd47c135SAndrew Gabbasov 	int err = 0;
1738ce9eca94SMarek Vasut 	__maybe_unused unsigned start;
173933fb211dSSimon Glass #ifdef CONFIG_DM_MMC
174033fb211dSSimon Glass 	struct mmc_uclass_priv *upriv = dev_get_uclass_priv(mmc->dev);
1741e9550449SChe-Liang Chiou 
174233fb211dSSimon Glass 	upriv->mmc = mmc;
174333fb211dSSimon Glass #endif
1744e9550449SChe-Liang Chiou 	if (mmc->has_init)
1745e9550449SChe-Liang Chiou 		return 0;
1746d803fea5SMateusz Zalega 
1747d803fea5SMateusz Zalega 	start = get_timer(0);
1748d803fea5SMateusz Zalega 
1749e9550449SChe-Liang Chiou 	if (!mmc->init_in_progress)
1750e9550449SChe-Liang Chiou 		err = mmc_start_init(mmc);
1751e9550449SChe-Liang Chiou 
1752bd47c135SAndrew Gabbasov 	if (!err)
1753e9550449SChe-Liang Chiou 		err = mmc_complete_init(mmc);
1754e9550449SChe-Liang Chiou 	debug("%s: %d, time %lu\n", __func__, err, get_timer(start));
1755bc897b1dSLei Wen 	return err;
1756272cc70bSAndy Fleming }
1757272cc70bSAndy Fleming 
1758ab71188cSMarkus Niebel int mmc_set_dsr(struct mmc *mmc, u16 val)
1759ab71188cSMarkus Niebel {
1760ab71188cSMarkus Niebel 	mmc->dsr = val;
1761ab71188cSMarkus Niebel 	return 0;
1762ab71188cSMarkus Niebel }
1763ab71188cSMarkus Niebel 
1764cee9ab7cSJeroen Hofstee /* CPU-specific MMC initializations */
1765cee9ab7cSJeroen Hofstee __weak int cpu_mmc_init(bd_t *bis)
1766272cc70bSAndy Fleming {
1767272cc70bSAndy Fleming 	return -1;
1768272cc70bSAndy Fleming }
1769272cc70bSAndy Fleming 
1770cee9ab7cSJeroen Hofstee /* board-specific MMC initializations. */
1771cee9ab7cSJeroen Hofstee __weak int board_mmc_init(bd_t *bis)
1772cee9ab7cSJeroen Hofstee {
1773cee9ab7cSJeroen Hofstee 	return -1;
1774cee9ab7cSJeroen Hofstee }
1775272cc70bSAndy Fleming 
1776e9550449SChe-Liang Chiou void mmc_set_preinit(struct mmc *mmc, int preinit)
1777e9550449SChe-Liang Chiou {
1778e9550449SChe-Liang Chiou 	mmc->preinit = preinit;
1779e9550449SChe-Liang Chiou }
1780e9550449SChe-Liang Chiou 
17818e3332e2SSjoerd Simons #if defined(CONFIG_DM_MMC) && defined(CONFIG_SPL_BUILD)
17828e3332e2SSjoerd Simons static int mmc_probe(bd_t *bis)
17838e3332e2SSjoerd Simons {
17848e3332e2SSjoerd Simons 	return 0;
17858e3332e2SSjoerd Simons }
17868e3332e2SSjoerd Simons #elif defined(CONFIG_DM_MMC)
17878e3332e2SSjoerd Simons static int mmc_probe(bd_t *bis)
17888e3332e2SSjoerd Simons {
17894a1db6d8SSimon Glass 	int ret, i;
17908e3332e2SSjoerd Simons 	struct uclass *uc;
17914a1db6d8SSimon Glass 	struct udevice *dev;
17928e3332e2SSjoerd Simons 
17938e3332e2SSjoerd Simons 	ret = uclass_get(UCLASS_MMC, &uc);
17948e3332e2SSjoerd Simons 	if (ret)
17958e3332e2SSjoerd Simons 		return ret;
17968e3332e2SSjoerd Simons 
17974a1db6d8SSimon Glass 	/*
17984a1db6d8SSimon Glass 	 * Try to add them in sequence order. Really with driver model we
17994a1db6d8SSimon Glass 	 * should allow holes, but the current MMC list does not allow that.
18004a1db6d8SSimon Glass 	 * So if we request 0, 1, 3 we will get 0, 1, 2.
18014a1db6d8SSimon Glass 	 */
18024a1db6d8SSimon Glass 	for (i = 0; ; i++) {
18034a1db6d8SSimon Glass 		ret = uclass_get_device_by_seq(UCLASS_MMC, i, &dev);
18044a1db6d8SSimon Glass 		if (ret == -ENODEV)
18054a1db6d8SSimon Glass 			break;
18064a1db6d8SSimon Glass 	}
18074a1db6d8SSimon Glass 	uclass_foreach_dev(dev, uc) {
18084a1db6d8SSimon Glass 		ret = device_probe(dev);
18098e3332e2SSjoerd Simons 		if (ret)
18104a1db6d8SSimon Glass 			printf("%s - probe failed: %d\n", dev->name, ret);
18118e3332e2SSjoerd Simons 	}
18128e3332e2SSjoerd Simons 
18138e3332e2SSjoerd Simons 	return 0;
18148e3332e2SSjoerd Simons }
18158e3332e2SSjoerd Simons #else
18168e3332e2SSjoerd Simons static int mmc_probe(bd_t *bis)
18178e3332e2SSjoerd Simons {
18188e3332e2SSjoerd Simons 	if (board_mmc_init(bis) < 0)
18198e3332e2SSjoerd Simons 		cpu_mmc_init(bis);
18208e3332e2SSjoerd Simons 
18218e3332e2SSjoerd Simons 	return 0;
18228e3332e2SSjoerd Simons }
18238e3332e2SSjoerd Simons #endif
1824e9550449SChe-Liang Chiou 
1825272cc70bSAndy Fleming int mmc_initialize(bd_t *bis)
1826272cc70bSAndy Fleming {
18271b26bab1SDaniel Kochmański 	static int initialized = 0;
18288e3332e2SSjoerd Simons 	int ret;
18291b26bab1SDaniel Kochmański 	if (initialized)	/* Avoid initializing mmc multiple times */
18301b26bab1SDaniel Kochmański 		return 0;
18311b26bab1SDaniel Kochmański 	initialized = 1;
18321b26bab1SDaniel Kochmański 
1833c40fdca6SSimon Glass #ifndef CONFIG_BLK
1834*b5b838f1SMarek Vasut #if !CONFIG_IS_ENABLED(MMC_TINY)
1835c40fdca6SSimon Glass 	mmc_list_init();
1836c40fdca6SSimon Glass #endif
1837*b5b838f1SMarek Vasut #endif
18388e3332e2SSjoerd Simons 	ret = mmc_probe(bis);
18398e3332e2SSjoerd Simons 	if (ret)
18408e3332e2SSjoerd Simons 		return ret;
1841272cc70bSAndy Fleming 
1842bb0dc108SYing Zhang #ifndef CONFIG_SPL_BUILD
1843272cc70bSAndy Fleming 	print_mmc_devices(',');
1844bb0dc108SYing Zhang #endif
1845272cc70bSAndy Fleming 
1846c40fdca6SSimon Glass 	mmc_do_preinit();
1847272cc70bSAndy Fleming 	return 0;
1848272cc70bSAndy Fleming }
1849cd3d4880STomas Melin 
1850cd3d4880STomas Melin #ifdef CONFIG_CMD_BKOPS_ENABLE
1851cd3d4880STomas Melin int mmc_set_bkops_enable(struct mmc *mmc)
1852cd3d4880STomas Melin {
1853cd3d4880STomas Melin 	int err;
1854cd3d4880STomas Melin 	ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
1855cd3d4880STomas Melin 
1856cd3d4880STomas Melin 	err = mmc_send_ext_csd(mmc, ext_csd);
1857cd3d4880STomas Melin 	if (err) {
1858cd3d4880STomas Melin 		puts("Could not get ext_csd register values\n");
1859cd3d4880STomas Melin 		return err;
1860cd3d4880STomas Melin 	}
1861cd3d4880STomas Melin 
1862cd3d4880STomas Melin 	if (!(ext_csd[EXT_CSD_BKOPS_SUPPORT] & 0x1)) {
1863cd3d4880STomas Melin 		puts("Background operations not supported on device\n");
1864cd3d4880STomas Melin 		return -EMEDIUMTYPE;
1865cd3d4880STomas Melin 	}
1866cd3d4880STomas Melin 
1867cd3d4880STomas Melin 	if (ext_csd[EXT_CSD_BKOPS_EN] & 0x1) {
1868cd3d4880STomas Melin 		puts("Background operations already enabled\n");
1869cd3d4880STomas Melin 		return 0;
1870cd3d4880STomas Melin 	}
1871cd3d4880STomas Melin 
1872cd3d4880STomas Melin 	err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BKOPS_EN, 1);
1873cd3d4880STomas Melin 	if (err) {
1874cd3d4880STomas Melin 		puts("Failed to enable manual background operations\n");
1875cd3d4880STomas Melin 		return err;
1876cd3d4880STomas Melin 	}
1877cd3d4880STomas Melin 
1878cd3d4880STomas Melin 	puts("Enabled manual background operations\n");
1879cd3d4880STomas Melin 
1880cd3d4880STomas Melin 	return 0;
1881cd3d4880STomas Melin }
1882cd3d4880STomas Melin #endif
1883