xref: /openbmc/u-boot/drivers/mmc/mmc.c (revision 9cf199ebcff13f8d98b31ab48e3bcce37ed8925d)
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>
13d4622df3SStephen Warren #include <errno.h>
14272cc70bSAndy Fleming #include <mmc.h>
15272cc70bSAndy Fleming #include <part.h>
16272cc70bSAndy Fleming #include <malloc.h>
17272cc70bSAndy Fleming #include <linux/list.h>
189b1f942cSRabin Vincent #include <div64.h>
19da61fa5fSPaul Burton #include "mmc_private.h"
20272cc70bSAndy Fleming 
21272cc70bSAndy Fleming static struct list_head mmc_devices;
22272cc70bSAndy Fleming static int cur_dev_num = -1;
23272cc70bSAndy Fleming 
24750121c3SJeroen Hofstee __weak int board_mmc_getwp(struct mmc *mmc)
25d23d8d7eSNikita Kiryanov {
26d23d8d7eSNikita Kiryanov 	return -1;
27d23d8d7eSNikita Kiryanov }
28d23d8d7eSNikita Kiryanov 
29d23d8d7eSNikita Kiryanov int mmc_getwp(struct mmc *mmc)
30d23d8d7eSNikita Kiryanov {
31d23d8d7eSNikita Kiryanov 	int wp;
32d23d8d7eSNikita Kiryanov 
33d23d8d7eSNikita Kiryanov 	wp = board_mmc_getwp(mmc);
34d23d8d7eSNikita Kiryanov 
35d4e1da4eSPeter Korsgaard 	if (wp < 0) {
3693bfd616SPantelis Antoniou 		if (mmc->cfg->ops->getwp)
3793bfd616SPantelis Antoniou 			wp = mmc->cfg->ops->getwp(mmc);
38d4e1da4eSPeter Korsgaard 		else
39d4e1da4eSPeter Korsgaard 			wp = 0;
40d4e1da4eSPeter Korsgaard 	}
41d23d8d7eSNikita Kiryanov 
42d23d8d7eSNikita Kiryanov 	return wp;
43d23d8d7eSNikita Kiryanov }
44d23d8d7eSNikita Kiryanov 
45cee9ab7cSJeroen Hofstee __weak int board_mmc_getcd(struct mmc *mmc)
46cee9ab7cSJeroen Hofstee {
4711fdade2SStefano Babic 	return -1;
4811fdade2SStefano Babic }
4911fdade2SStefano Babic 
50da61fa5fSPaul Burton int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
51272cc70bSAndy Fleming {
525db2fe3aSRaffaele Recalcati 	int ret;
538635ff9eSMarek Vasut 
548635ff9eSMarek Vasut #ifdef CONFIG_MMC_TRACE
555db2fe3aSRaffaele Recalcati 	int i;
565db2fe3aSRaffaele Recalcati 	u8 *ptr;
575db2fe3aSRaffaele Recalcati 
585db2fe3aSRaffaele Recalcati 	printf("CMD_SEND:%d\n", cmd->cmdidx);
595db2fe3aSRaffaele Recalcati 	printf("\t\tARG\t\t\t 0x%08X\n", cmd->cmdarg);
6093bfd616SPantelis Antoniou 	ret = mmc->cfg->ops->send_cmd(mmc, cmd, data);
615db2fe3aSRaffaele Recalcati 	switch (cmd->resp_type) {
625db2fe3aSRaffaele Recalcati 		case MMC_RSP_NONE:
635db2fe3aSRaffaele Recalcati 			printf("\t\tMMC_RSP_NONE\n");
645db2fe3aSRaffaele Recalcati 			break;
655db2fe3aSRaffaele Recalcati 		case MMC_RSP_R1:
665db2fe3aSRaffaele Recalcati 			printf("\t\tMMC_RSP_R1,5,6,7 \t 0x%08X \n",
675db2fe3aSRaffaele Recalcati 				cmd->response[0]);
685db2fe3aSRaffaele Recalcati 			break;
695db2fe3aSRaffaele Recalcati 		case MMC_RSP_R1b:
705db2fe3aSRaffaele Recalcati 			printf("\t\tMMC_RSP_R1b\t\t 0x%08X \n",
715db2fe3aSRaffaele Recalcati 				cmd->response[0]);
725db2fe3aSRaffaele Recalcati 			break;
735db2fe3aSRaffaele Recalcati 		case MMC_RSP_R2:
745db2fe3aSRaffaele Recalcati 			printf("\t\tMMC_RSP_R2\t\t 0x%08X \n",
755db2fe3aSRaffaele Recalcati 				cmd->response[0]);
765db2fe3aSRaffaele Recalcati 			printf("\t\t          \t\t 0x%08X \n",
775db2fe3aSRaffaele Recalcati 				cmd->response[1]);
785db2fe3aSRaffaele Recalcati 			printf("\t\t          \t\t 0x%08X \n",
795db2fe3aSRaffaele Recalcati 				cmd->response[2]);
805db2fe3aSRaffaele Recalcati 			printf("\t\t          \t\t 0x%08X \n",
815db2fe3aSRaffaele Recalcati 				cmd->response[3]);
825db2fe3aSRaffaele Recalcati 			printf("\n");
835db2fe3aSRaffaele Recalcati 			printf("\t\t\t\t\tDUMPING DATA\n");
845db2fe3aSRaffaele Recalcati 			for (i = 0; i < 4; i++) {
855db2fe3aSRaffaele Recalcati 				int j;
865db2fe3aSRaffaele Recalcati 				printf("\t\t\t\t\t%03d - ", i*4);
87146bec79SDirk Behme 				ptr = (u8 *)&cmd->response[i];
885db2fe3aSRaffaele Recalcati 				ptr += 3;
895db2fe3aSRaffaele Recalcati 				for (j = 0; j < 4; j++)
905db2fe3aSRaffaele Recalcati 					printf("%02X ", *ptr--);
915db2fe3aSRaffaele Recalcati 				printf("\n");
925db2fe3aSRaffaele Recalcati 			}
935db2fe3aSRaffaele Recalcati 			break;
945db2fe3aSRaffaele Recalcati 		case MMC_RSP_R3:
955db2fe3aSRaffaele Recalcati 			printf("\t\tMMC_RSP_R3,4\t\t 0x%08X \n",
965db2fe3aSRaffaele Recalcati 				cmd->response[0]);
975db2fe3aSRaffaele Recalcati 			break;
985db2fe3aSRaffaele Recalcati 		default:
995db2fe3aSRaffaele Recalcati 			printf("\t\tERROR MMC rsp not supported\n");
1005db2fe3aSRaffaele Recalcati 			break;
1015db2fe3aSRaffaele Recalcati 	}
1025db2fe3aSRaffaele Recalcati #else
10393bfd616SPantelis Antoniou 	ret = mmc->cfg->ops->send_cmd(mmc, cmd, data);
1045db2fe3aSRaffaele Recalcati #endif
1058635ff9eSMarek Vasut 	return ret;
106272cc70bSAndy Fleming }
107272cc70bSAndy Fleming 
108da61fa5fSPaul Burton int mmc_send_status(struct mmc *mmc, int timeout)
1095d4fc8d9SRaffaele Recalcati {
1105d4fc8d9SRaffaele Recalcati 	struct mmc_cmd cmd;
111d617c426SJan Kloetzke 	int err, retries = 5;
1125d4fc8d9SRaffaele Recalcati #ifdef CONFIG_MMC_TRACE
1135d4fc8d9SRaffaele Recalcati 	int status;
1145d4fc8d9SRaffaele Recalcati #endif
1155d4fc8d9SRaffaele Recalcati 
1165d4fc8d9SRaffaele Recalcati 	cmd.cmdidx = MMC_CMD_SEND_STATUS;
1175d4fc8d9SRaffaele Recalcati 	cmd.resp_type = MMC_RSP_R1;
118aaf3d41aSMarek Vasut 	if (!mmc_host_is_spi(mmc))
119aaf3d41aSMarek Vasut 		cmd.cmdarg = mmc->rca << 16;
1205d4fc8d9SRaffaele Recalcati 
1215d4fc8d9SRaffaele Recalcati 	do {
1225d4fc8d9SRaffaele Recalcati 		err = mmc_send_cmd(mmc, &cmd, NULL);
123d617c426SJan Kloetzke 		if (!err) {
124d617c426SJan Kloetzke 			if ((cmd.response[0] & MMC_STATUS_RDY_FOR_DATA) &&
125d617c426SJan Kloetzke 			    (cmd.response[0] & MMC_STATUS_CURR_STATE) !=
126d617c426SJan Kloetzke 			     MMC_STATE_PRG)
1275d4fc8d9SRaffaele Recalcati 				break;
128d617c426SJan Kloetzke 			else if (cmd.response[0] & MMC_STATUS_MASK) {
12956196826SPaul Burton #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
130d617c426SJan Kloetzke 				printf("Status Error: 0x%08X\n",
131d617c426SJan Kloetzke 					cmd.response[0]);
13256196826SPaul Burton #endif
133d617c426SJan Kloetzke 				return COMM_ERR;
134d617c426SJan Kloetzke 			}
135d617c426SJan Kloetzke 		} else if (--retries < 0)
136d617c426SJan Kloetzke 			return err;
1375d4fc8d9SRaffaele Recalcati 
1385d4fc8d9SRaffaele Recalcati 		udelay(1000);
1395d4fc8d9SRaffaele Recalcati 
1405d4fc8d9SRaffaele Recalcati 	} while (timeout--);
1415d4fc8d9SRaffaele Recalcati 
1425db2fe3aSRaffaele Recalcati #ifdef CONFIG_MMC_TRACE
1435db2fe3aSRaffaele Recalcati 	status = (cmd.response[0] & MMC_STATUS_CURR_STATE) >> 9;
1445db2fe3aSRaffaele Recalcati 	printf("CURR STATE:%d\n", status);
1455db2fe3aSRaffaele Recalcati #endif
1465b0c942fSJongman Heo 	if (timeout <= 0) {
14756196826SPaul Burton #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
1485d4fc8d9SRaffaele Recalcati 		printf("Timeout waiting card ready\n");
14956196826SPaul Burton #endif
1505d4fc8d9SRaffaele Recalcati 		return TIMEOUT;
1515d4fc8d9SRaffaele Recalcati 	}
1526b2221b0SAndrew Gabbasov 	if (cmd.response[0] & MMC_STATUS_SWITCH_ERROR)
1536b2221b0SAndrew Gabbasov 		return SWITCH_ERR;
1545d4fc8d9SRaffaele Recalcati 
1555d4fc8d9SRaffaele Recalcati 	return 0;
1565d4fc8d9SRaffaele Recalcati }
1575d4fc8d9SRaffaele Recalcati 
158da61fa5fSPaul Burton int mmc_set_blocklen(struct mmc *mmc, int len)
159272cc70bSAndy Fleming {
160272cc70bSAndy Fleming 	struct mmc_cmd cmd;
161272cc70bSAndy Fleming 
162786e8f81SAndrew Gabbasov 	if (mmc->ddr_mode)
163d22e3d46SJaehoon Chung 		return 0;
164d22e3d46SJaehoon Chung 
165272cc70bSAndy Fleming 	cmd.cmdidx = MMC_CMD_SET_BLOCKLEN;
166272cc70bSAndy Fleming 	cmd.resp_type = MMC_RSP_R1;
167272cc70bSAndy Fleming 	cmd.cmdarg = len;
168272cc70bSAndy Fleming 
169272cc70bSAndy Fleming 	return mmc_send_cmd(mmc, &cmd, NULL);
170272cc70bSAndy Fleming }
171272cc70bSAndy Fleming 
172272cc70bSAndy Fleming struct mmc *find_mmc_device(int dev_num)
173272cc70bSAndy Fleming {
174272cc70bSAndy Fleming 	struct mmc *m;
175272cc70bSAndy Fleming 	struct list_head *entry;
176272cc70bSAndy Fleming 
177272cc70bSAndy Fleming 	list_for_each(entry, &mmc_devices) {
178272cc70bSAndy Fleming 		m = list_entry(entry, struct mmc, link);
179272cc70bSAndy Fleming 
180272cc70bSAndy Fleming 		if (m->block_dev.dev == dev_num)
181272cc70bSAndy Fleming 			return m;
182272cc70bSAndy Fleming 	}
183272cc70bSAndy Fleming 
18456196826SPaul Burton #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
185272cc70bSAndy Fleming 	printf("MMC Device %d not found\n", dev_num);
18656196826SPaul Burton #endif
187272cc70bSAndy Fleming 
188272cc70bSAndy Fleming 	return NULL;
189272cc70bSAndy Fleming }
190272cc70bSAndy Fleming 
191ff8fef56SSascha Silbe static int mmc_read_blocks(struct mmc *mmc, void *dst, lbaint_t start,
192fdbb873eSKim Phillips 			   lbaint_t blkcnt)
193272cc70bSAndy Fleming {
194272cc70bSAndy Fleming 	struct mmc_cmd cmd;
195272cc70bSAndy Fleming 	struct mmc_data data;
196272cc70bSAndy Fleming 
1974a1a06bcSAlagu Sankar 	if (blkcnt > 1)
1984a1a06bcSAlagu Sankar 		cmd.cmdidx = MMC_CMD_READ_MULTIPLE_BLOCK;
1994a1a06bcSAlagu Sankar 	else
200272cc70bSAndy Fleming 		cmd.cmdidx = MMC_CMD_READ_SINGLE_BLOCK;
201272cc70bSAndy Fleming 
202272cc70bSAndy Fleming 	if (mmc->high_capacity)
2034a1a06bcSAlagu Sankar 		cmd.cmdarg = start;
204272cc70bSAndy Fleming 	else
2054a1a06bcSAlagu Sankar 		cmd.cmdarg = start * mmc->read_bl_len;
206272cc70bSAndy Fleming 
207272cc70bSAndy Fleming 	cmd.resp_type = MMC_RSP_R1;
208272cc70bSAndy Fleming 
209272cc70bSAndy Fleming 	data.dest = dst;
2104a1a06bcSAlagu Sankar 	data.blocks = blkcnt;
211272cc70bSAndy Fleming 	data.blocksize = mmc->read_bl_len;
212272cc70bSAndy Fleming 	data.flags = MMC_DATA_READ;
213272cc70bSAndy Fleming 
2144a1a06bcSAlagu Sankar 	if (mmc_send_cmd(mmc, &cmd, &data))
2154a1a06bcSAlagu Sankar 		return 0;
2164a1a06bcSAlagu Sankar 
2174a1a06bcSAlagu Sankar 	if (blkcnt > 1) {
2184a1a06bcSAlagu Sankar 		cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
2194a1a06bcSAlagu Sankar 		cmd.cmdarg = 0;
2204a1a06bcSAlagu Sankar 		cmd.resp_type = MMC_RSP_R1b;
2214a1a06bcSAlagu Sankar 		if (mmc_send_cmd(mmc, &cmd, NULL)) {
22256196826SPaul Burton #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
2234a1a06bcSAlagu Sankar 			printf("mmc fail to send stop cmd\n");
22456196826SPaul Burton #endif
2254a1a06bcSAlagu Sankar 			return 0;
2264a1a06bcSAlagu Sankar 		}
227272cc70bSAndy Fleming 	}
228272cc70bSAndy Fleming 
2294a1a06bcSAlagu Sankar 	return blkcnt;
230272cc70bSAndy Fleming }
231272cc70bSAndy Fleming 
232ff8fef56SSascha Silbe static ulong mmc_bread(int dev_num, lbaint_t start, lbaint_t blkcnt, void *dst)
233272cc70bSAndy Fleming {
2344a1a06bcSAlagu Sankar 	lbaint_t cur, blocks_todo = blkcnt;
235272cc70bSAndy Fleming 
2364a1a06bcSAlagu Sankar 	if (blkcnt == 0)
2374a1a06bcSAlagu Sankar 		return 0;
2384a1a06bcSAlagu Sankar 
2394a1a06bcSAlagu Sankar 	struct mmc *mmc = find_mmc_device(dev_num);
240272cc70bSAndy Fleming 	if (!mmc)
241272cc70bSAndy Fleming 		return 0;
242272cc70bSAndy Fleming 
243d2bf29e3SLei Wen 	if ((start + blkcnt) > mmc->block_dev.lba) {
24456196826SPaul Burton #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
245ff8fef56SSascha Silbe 		printf("MMC: block number 0x" LBAF " exceeds max(0x" LBAF ")\n",
246d2bf29e3SLei Wen 			start + blkcnt, mmc->block_dev.lba);
24756196826SPaul Burton #endif
248d2bf29e3SLei Wen 		return 0;
249d2bf29e3SLei Wen 	}
250272cc70bSAndy Fleming 
2514a1a06bcSAlagu Sankar 	if (mmc_set_blocklen(mmc, mmc->read_bl_len))
252272cc70bSAndy Fleming 		return 0;
253272cc70bSAndy Fleming 
2544a1a06bcSAlagu Sankar 	do {
25593bfd616SPantelis Antoniou 		cur = (blocks_todo > mmc->cfg->b_max) ?
25693bfd616SPantelis Antoniou 			mmc->cfg->b_max : blocks_todo;
2574a1a06bcSAlagu Sankar 		if(mmc_read_blocks(mmc, dst, start, cur) != cur)
2584a1a06bcSAlagu Sankar 			return 0;
2594a1a06bcSAlagu Sankar 		blocks_todo -= cur;
2604a1a06bcSAlagu Sankar 		start += cur;
2614a1a06bcSAlagu Sankar 		dst += cur * mmc->read_bl_len;
2624a1a06bcSAlagu Sankar 	} while (blocks_todo > 0);
263272cc70bSAndy Fleming 
264272cc70bSAndy Fleming 	return blkcnt;
265272cc70bSAndy Fleming }
266272cc70bSAndy Fleming 
267fdbb873eSKim Phillips static int mmc_go_idle(struct mmc *mmc)
268272cc70bSAndy Fleming {
269272cc70bSAndy Fleming 	struct mmc_cmd cmd;
270272cc70bSAndy Fleming 	int err;
271272cc70bSAndy Fleming 
272272cc70bSAndy Fleming 	udelay(1000);
273272cc70bSAndy Fleming 
274272cc70bSAndy Fleming 	cmd.cmdidx = MMC_CMD_GO_IDLE_STATE;
275272cc70bSAndy Fleming 	cmd.cmdarg = 0;
276272cc70bSAndy Fleming 	cmd.resp_type = MMC_RSP_NONE;
277272cc70bSAndy Fleming 
278272cc70bSAndy Fleming 	err = mmc_send_cmd(mmc, &cmd, NULL);
279272cc70bSAndy Fleming 
280272cc70bSAndy Fleming 	if (err)
281272cc70bSAndy Fleming 		return err;
282272cc70bSAndy Fleming 
283272cc70bSAndy Fleming 	udelay(2000);
284272cc70bSAndy Fleming 
285272cc70bSAndy Fleming 	return 0;
286272cc70bSAndy Fleming }
287272cc70bSAndy Fleming 
288fdbb873eSKim Phillips static int sd_send_op_cond(struct mmc *mmc)
289272cc70bSAndy Fleming {
290272cc70bSAndy Fleming 	int timeout = 1000;
291272cc70bSAndy Fleming 	int err;
292272cc70bSAndy Fleming 	struct mmc_cmd cmd;
293272cc70bSAndy Fleming 
294272cc70bSAndy Fleming 	do {
295272cc70bSAndy Fleming 		cmd.cmdidx = MMC_CMD_APP_CMD;
296272cc70bSAndy Fleming 		cmd.resp_type = MMC_RSP_R1;
297272cc70bSAndy Fleming 		cmd.cmdarg = 0;
298272cc70bSAndy Fleming 
299272cc70bSAndy Fleming 		err = mmc_send_cmd(mmc, &cmd, NULL);
300272cc70bSAndy Fleming 
301272cc70bSAndy Fleming 		if (err)
302272cc70bSAndy Fleming 			return err;
303272cc70bSAndy Fleming 
304272cc70bSAndy Fleming 		cmd.cmdidx = SD_CMD_APP_SEND_OP_COND;
305272cc70bSAndy Fleming 		cmd.resp_type = MMC_RSP_R3;
306250de12bSStefano Babic 
307250de12bSStefano Babic 		/*
308250de12bSStefano Babic 		 * Most cards do not answer if some reserved bits
309250de12bSStefano Babic 		 * in the ocr are set. However, Some controller
310250de12bSStefano Babic 		 * can set bit 7 (reserved for low voltages), but
311250de12bSStefano Babic 		 * how to manage low voltages SD card is not yet
312250de12bSStefano Babic 		 * specified.
313250de12bSStefano Babic 		 */
314d52ebf10SThomas Chou 		cmd.cmdarg = mmc_host_is_spi(mmc) ? 0 :
31593bfd616SPantelis Antoniou 			(mmc->cfg->voltages & 0xff8000);
316272cc70bSAndy Fleming 
317272cc70bSAndy Fleming 		if (mmc->version == SD_VERSION_2)
318272cc70bSAndy Fleming 			cmd.cmdarg |= OCR_HCS;
319272cc70bSAndy Fleming 
320272cc70bSAndy Fleming 		err = mmc_send_cmd(mmc, &cmd, NULL);
321272cc70bSAndy Fleming 
322272cc70bSAndy Fleming 		if (err)
323272cc70bSAndy Fleming 			return err;
324272cc70bSAndy Fleming 
325272cc70bSAndy Fleming 		udelay(1000);
326272cc70bSAndy Fleming 	} while ((!(cmd.response[0] & OCR_BUSY)) && timeout--);
327272cc70bSAndy Fleming 
328272cc70bSAndy Fleming 	if (timeout <= 0)
329272cc70bSAndy Fleming 		return UNUSABLE_ERR;
330272cc70bSAndy Fleming 
331272cc70bSAndy Fleming 	if (mmc->version != SD_VERSION_2)
332272cc70bSAndy Fleming 		mmc->version = SD_VERSION_1_0;
333272cc70bSAndy Fleming 
334d52ebf10SThomas Chou 	if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
335d52ebf10SThomas Chou 		cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
336d52ebf10SThomas Chou 		cmd.resp_type = MMC_RSP_R3;
337d52ebf10SThomas Chou 		cmd.cmdarg = 0;
338d52ebf10SThomas Chou 
339d52ebf10SThomas Chou 		err = mmc_send_cmd(mmc, &cmd, NULL);
340d52ebf10SThomas Chou 
341d52ebf10SThomas Chou 		if (err)
342d52ebf10SThomas Chou 			return err;
343d52ebf10SThomas Chou 	}
344d52ebf10SThomas Chou 
345998be3ddSRabin Vincent 	mmc->ocr = cmd.response[0];
346272cc70bSAndy Fleming 
347272cc70bSAndy Fleming 	mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
348272cc70bSAndy Fleming 	mmc->rca = 0;
349272cc70bSAndy Fleming 
350272cc70bSAndy Fleming 	return 0;
351272cc70bSAndy Fleming }
352272cc70bSAndy Fleming 
353e9550449SChe-Liang Chiou /* We pass in the cmd since otherwise the init seems to fail */
354e9550449SChe-Liang Chiou static int mmc_send_op_cond_iter(struct mmc *mmc, struct mmc_cmd *cmd,
355e9550449SChe-Liang Chiou 		int use_arg)
356272cc70bSAndy Fleming {
357272cc70bSAndy Fleming 	int err;
358272cc70bSAndy Fleming 
359e9550449SChe-Liang Chiou 	cmd->cmdidx = MMC_CMD_SEND_OP_COND;
360e9550449SChe-Liang Chiou 	cmd->resp_type = MMC_RSP_R3;
361e9550449SChe-Liang Chiou 	cmd->cmdarg = 0;
362e9550449SChe-Liang Chiou 	if (use_arg && !mmc_host_is_spi(mmc)) {
363e9550449SChe-Liang Chiou 		cmd->cmdarg =
36493bfd616SPantelis Antoniou 			(mmc->cfg->voltages &
365e9550449SChe-Liang Chiou 			(mmc->op_cond_response & OCR_VOLTAGE_MASK)) |
366e9550449SChe-Liang Chiou 			(mmc->op_cond_response & OCR_ACCESS_MODE);
367e9550449SChe-Liang Chiou 
36893bfd616SPantelis Antoniou 		if (mmc->cfg->host_caps & MMC_MODE_HC)
369e9550449SChe-Liang Chiou 			cmd->cmdarg |= OCR_HCS;
370e9550449SChe-Liang Chiou 	}
371e9550449SChe-Liang Chiou 	err = mmc_send_cmd(mmc, cmd, NULL);
372e9550449SChe-Liang Chiou 	if (err)
373e9550449SChe-Liang Chiou 		return err;
374e9550449SChe-Liang Chiou 	mmc->op_cond_response = cmd->response[0];
375e9550449SChe-Liang Chiou 	return 0;
376e9550449SChe-Liang Chiou }
377e9550449SChe-Liang Chiou 
378750121c3SJeroen Hofstee static int mmc_send_op_cond(struct mmc *mmc)
379e9550449SChe-Liang Chiou {
380e9550449SChe-Liang Chiou 	struct mmc_cmd cmd;
381e9550449SChe-Liang Chiou 	int err, i;
382e9550449SChe-Liang Chiou 
383272cc70bSAndy Fleming 	/* Some cards seem to need this */
384272cc70bSAndy Fleming 	mmc_go_idle(mmc);
385272cc70bSAndy Fleming 
38631cacbabSRaffaele Recalcati  	/* Asking to the card its capabilities */
387e9550449SChe-Liang Chiou 	mmc->op_cond_pending = 1;
388e9550449SChe-Liang Chiou 	for (i = 0; i < 2; i++) {
389e9550449SChe-Liang Chiou 		err = mmc_send_op_cond_iter(mmc, &cmd, i != 0);
39031cacbabSRaffaele Recalcati 		if (err)
39131cacbabSRaffaele Recalcati 			return err;
39231cacbabSRaffaele Recalcati 
393e9550449SChe-Liang Chiou 		/* exit if not busy (flag seems to be inverted) */
394e9550449SChe-Liang Chiou 		if (mmc->op_cond_response & OCR_BUSY)
395e9550449SChe-Liang Chiou 			return 0;
396e9550449SChe-Liang Chiou 	}
397e9550449SChe-Liang Chiou 	return IN_PROGRESS;
398e9550449SChe-Liang Chiou }
39931cacbabSRaffaele Recalcati 
400750121c3SJeroen Hofstee static int mmc_complete_op_cond(struct mmc *mmc)
401e9550449SChe-Liang Chiou {
402e9550449SChe-Liang Chiou 	struct mmc_cmd cmd;
403e9550449SChe-Liang Chiou 	int timeout = 1000;
404e9550449SChe-Liang Chiou 	uint start;
405e9550449SChe-Liang Chiou 	int err;
406e9550449SChe-Liang Chiou 
407e9550449SChe-Liang Chiou 	mmc->op_cond_pending = 0;
408e9550449SChe-Liang Chiou 	start = get_timer(0);
409272cc70bSAndy Fleming 	do {
410e9550449SChe-Liang Chiou 		err = mmc_send_op_cond_iter(mmc, &cmd, 1);
411272cc70bSAndy Fleming 		if (err)
412272cc70bSAndy Fleming 			return err;
413e9550449SChe-Liang Chiou 		if (get_timer(start) > timeout)
414272cc70bSAndy Fleming 			return UNUSABLE_ERR;
415e9550449SChe-Liang Chiou 		udelay(100);
416e9550449SChe-Liang Chiou 	} while (!(mmc->op_cond_response & OCR_BUSY));
417272cc70bSAndy Fleming 
418d52ebf10SThomas Chou 	if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
419d52ebf10SThomas Chou 		cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
420d52ebf10SThomas Chou 		cmd.resp_type = MMC_RSP_R3;
421d52ebf10SThomas Chou 		cmd.cmdarg = 0;
422d52ebf10SThomas Chou 
423d52ebf10SThomas Chou 		err = mmc_send_cmd(mmc, &cmd, NULL);
424d52ebf10SThomas Chou 
425d52ebf10SThomas Chou 		if (err)
426d52ebf10SThomas Chou 			return err;
427d52ebf10SThomas Chou 	}
428d52ebf10SThomas Chou 
429272cc70bSAndy Fleming 	mmc->version = MMC_VERSION_UNKNOWN;
430998be3ddSRabin Vincent 	mmc->ocr = cmd.response[0];
431272cc70bSAndy Fleming 
432272cc70bSAndy Fleming 	mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
433def816a2SStephen Warren 	mmc->rca = 1;
434272cc70bSAndy Fleming 
435272cc70bSAndy Fleming 	return 0;
436272cc70bSAndy Fleming }
437272cc70bSAndy Fleming 
438272cc70bSAndy Fleming 
439fdbb873eSKim Phillips static int mmc_send_ext_csd(struct mmc *mmc, u8 *ext_csd)
440272cc70bSAndy Fleming {
441272cc70bSAndy Fleming 	struct mmc_cmd cmd;
442272cc70bSAndy Fleming 	struct mmc_data data;
443272cc70bSAndy Fleming 	int err;
444272cc70bSAndy Fleming 
445272cc70bSAndy Fleming 	/* Get the Card Status Register */
446272cc70bSAndy Fleming 	cmd.cmdidx = MMC_CMD_SEND_EXT_CSD;
447272cc70bSAndy Fleming 	cmd.resp_type = MMC_RSP_R1;
448272cc70bSAndy Fleming 	cmd.cmdarg = 0;
449272cc70bSAndy Fleming 
450cdfd1ac6SYoshihiro Shimoda 	data.dest = (char *)ext_csd;
451272cc70bSAndy Fleming 	data.blocks = 1;
4528bfa195eSSimon Glass 	data.blocksize = MMC_MAX_BLOCK_LEN;
453272cc70bSAndy Fleming 	data.flags = MMC_DATA_READ;
454272cc70bSAndy Fleming 
455272cc70bSAndy Fleming 	err = mmc_send_cmd(mmc, &cmd, &data);
456272cc70bSAndy Fleming 
457272cc70bSAndy Fleming 	return err;
458272cc70bSAndy Fleming }
459272cc70bSAndy Fleming 
460272cc70bSAndy Fleming 
461fdbb873eSKim Phillips static int mmc_switch(struct mmc *mmc, u8 set, u8 index, u8 value)
462272cc70bSAndy Fleming {
463272cc70bSAndy Fleming 	struct mmc_cmd cmd;
4645d4fc8d9SRaffaele Recalcati 	int timeout = 1000;
4655d4fc8d9SRaffaele Recalcati 	int ret;
466272cc70bSAndy Fleming 
467272cc70bSAndy Fleming 	cmd.cmdidx = MMC_CMD_SWITCH;
468272cc70bSAndy Fleming 	cmd.resp_type = MMC_RSP_R1b;
469272cc70bSAndy Fleming 	cmd.cmdarg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
470272cc70bSAndy Fleming 				 (index << 16) |
471272cc70bSAndy Fleming 				 (value << 8);
472272cc70bSAndy Fleming 
4735d4fc8d9SRaffaele Recalcati 	ret = mmc_send_cmd(mmc, &cmd, NULL);
4745d4fc8d9SRaffaele Recalcati 
4755d4fc8d9SRaffaele Recalcati 	/* Waiting for the ready status */
47693ad0d18SJan Kloetzke 	if (!ret)
47793ad0d18SJan Kloetzke 		ret = mmc_send_status(mmc, timeout);
4785d4fc8d9SRaffaele Recalcati 
4795d4fc8d9SRaffaele Recalcati 	return ret;
4805d4fc8d9SRaffaele Recalcati 
481272cc70bSAndy Fleming }
482272cc70bSAndy Fleming 
483fdbb873eSKim Phillips static int mmc_change_freq(struct mmc *mmc)
484272cc70bSAndy Fleming {
4858bfa195eSSimon Glass 	ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
486272cc70bSAndy Fleming 	char cardtype;
487272cc70bSAndy Fleming 	int err;
488272cc70bSAndy Fleming 
489786e8f81SAndrew Gabbasov 	mmc->card_caps = MMC_MODE_4BIT | MMC_MODE_8BIT;
490272cc70bSAndy Fleming 
491d52ebf10SThomas Chou 	if (mmc_host_is_spi(mmc))
492d52ebf10SThomas Chou 		return 0;
493d52ebf10SThomas Chou 
494272cc70bSAndy Fleming 	/* Only version 4 supports high-speed */
495272cc70bSAndy Fleming 	if (mmc->version < MMC_VERSION_4)
496272cc70bSAndy Fleming 		return 0;
497272cc70bSAndy Fleming 
498272cc70bSAndy Fleming 	err = mmc_send_ext_csd(mmc, ext_csd);
499272cc70bSAndy Fleming 
500272cc70bSAndy Fleming 	if (err)
501272cc70bSAndy Fleming 		return err;
502272cc70bSAndy Fleming 
5030560db18SLei Wen 	cardtype = ext_csd[EXT_CSD_CARD_TYPE] & 0xf;
504272cc70bSAndy Fleming 
505272cc70bSAndy Fleming 	err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING, 1);
506272cc70bSAndy Fleming 
507272cc70bSAndy Fleming 	if (err)
5086b2221b0SAndrew Gabbasov 		return err == SWITCH_ERR ? 0 : err;
509272cc70bSAndy Fleming 
510272cc70bSAndy Fleming 	/* Now check to see that it worked */
511272cc70bSAndy Fleming 	err = mmc_send_ext_csd(mmc, ext_csd);
512272cc70bSAndy Fleming 
513272cc70bSAndy Fleming 	if (err)
514272cc70bSAndy Fleming 		return err;
515272cc70bSAndy Fleming 
516272cc70bSAndy Fleming 	/* No high-speed support */
5170560db18SLei Wen 	if (!ext_csd[EXT_CSD_HS_TIMING])
518272cc70bSAndy Fleming 		return 0;
519272cc70bSAndy Fleming 
520272cc70bSAndy Fleming 	/* High Speed is set, there are two types: 52MHz and 26MHz */
521d22e3d46SJaehoon Chung 	if (cardtype & EXT_CSD_CARD_TYPE_52) {
522201d5ac4SAndrew Gabbasov 		if (cardtype & EXT_CSD_CARD_TYPE_DDR_1_8V)
523d22e3d46SJaehoon Chung 			mmc->card_caps |= MMC_MODE_DDR_52MHz;
524272cc70bSAndy Fleming 		mmc->card_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
525d22e3d46SJaehoon Chung 	} else {
526272cc70bSAndy Fleming 		mmc->card_caps |= MMC_MODE_HS;
527d22e3d46SJaehoon Chung 	}
528272cc70bSAndy Fleming 
529272cc70bSAndy Fleming 	return 0;
530272cc70bSAndy Fleming }
531272cc70bSAndy Fleming 
532f866a46dSStephen Warren static int mmc_set_capacity(struct mmc *mmc, int part_num)
533f866a46dSStephen Warren {
534f866a46dSStephen Warren 	switch (part_num) {
535f866a46dSStephen Warren 	case 0:
536f866a46dSStephen Warren 		mmc->capacity = mmc->capacity_user;
537f866a46dSStephen Warren 		break;
538f866a46dSStephen Warren 	case 1:
539f866a46dSStephen Warren 	case 2:
540f866a46dSStephen Warren 		mmc->capacity = mmc->capacity_boot;
541f866a46dSStephen Warren 		break;
542f866a46dSStephen Warren 	case 3:
543f866a46dSStephen Warren 		mmc->capacity = mmc->capacity_rpmb;
544f866a46dSStephen Warren 		break;
545f866a46dSStephen Warren 	case 4:
546f866a46dSStephen Warren 	case 5:
547f866a46dSStephen Warren 	case 6:
548f866a46dSStephen Warren 	case 7:
549f866a46dSStephen Warren 		mmc->capacity = mmc->capacity_gp[part_num - 4];
550f866a46dSStephen Warren 		break;
551f866a46dSStephen Warren 	default:
552f866a46dSStephen Warren 		return -1;
553f866a46dSStephen Warren 	}
554f866a46dSStephen Warren 
555f866a46dSStephen Warren 	mmc->block_dev.lba = lldiv(mmc->capacity, mmc->read_bl_len);
556f866a46dSStephen Warren 
557f866a46dSStephen Warren 	return 0;
558f866a46dSStephen Warren }
559f866a46dSStephen Warren 
560d2356284SStephen Warren int mmc_select_hwpart(int dev_num, int hwpart)
561d2356284SStephen Warren {
562d2356284SStephen Warren 	struct mmc *mmc = find_mmc_device(dev_num);
563d2356284SStephen Warren 	int ret;
564d2356284SStephen Warren 
565d2356284SStephen Warren 	if (!mmc)
566d4622df3SStephen Warren 		return -ENODEV;
567d2356284SStephen Warren 
568d2356284SStephen Warren 	if (mmc->part_num == hwpart)
569d2356284SStephen Warren 		return 0;
570d2356284SStephen Warren 
571d2356284SStephen Warren 	if (mmc->part_config == MMCPART_NOAVAILABLE) {
572d2356284SStephen Warren 		printf("Card doesn't support part_switch\n");
573d4622df3SStephen Warren 		return -EMEDIUMTYPE;
574d2356284SStephen Warren 	}
575d2356284SStephen Warren 
576d2356284SStephen Warren 	ret = mmc_switch_part(dev_num, hwpart);
577d2356284SStephen Warren 	if (ret)
578d4622df3SStephen Warren 		return ret;
579d2356284SStephen Warren 
580d2356284SStephen Warren 	mmc->part_num = hwpart;
581d2356284SStephen Warren 
582d2356284SStephen Warren 	return 0;
583d2356284SStephen Warren }
584d2356284SStephen Warren 
585d2356284SStephen Warren 
586bc897b1dSLei Wen int mmc_switch_part(int dev_num, unsigned int part_num)
587bc897b1dSLei Wen {
588bc897b1dSLei Wen 	struct mmc *mmc = find_mmc_device(dev_num);
589f866a46dSStephen Warren 	int ret;
590bc897b1dSLei Wen 
591bc897b1dSLei Wen 	if (!mmc)
592bc897b1dSLei Wen 		return -1;
593bc897b1dSLei Wen 
594f866a46dSStephen Warren 	ret = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONF,
595bc897b1dSLei Wen 			 (mmc->part_config & ~PART_ACCESS_MASK)
596bc897b1dSLei Wen 			 | (part_num & PART_ACCESS_MASK));
597f866a46dSStephen Warren 
5986dc93e70SPeter Bigot 	/*
5996dc93e70SPeter Bigot 	 * Set the capacity if the switch succeeded or was intended
6006dc93e70SPeter Bigot 	 * to return to representing the raw device.
6016dc93e70SPeter Bigot 	 */
6026dc93e70SPeter Bigot 	if ((ret == 0) || ((ret == -ENODEV) && (part_num == 0)))
6036dc93e70SPeter Bigot 		ret = mmc_set_capacity(mmc, part_num);
6046dc93e70SPeter Bigot 
6056dc93e70SPeter Bigot 	return ret;
606bc897b1dSLei Wen }
607bc897b1dSLei Wen 
60848972d90SThierry Reding int mmc_getcd(struct mmc *mmc)
60948972d90SThierry Reding {
61048972d90SThierry Reding 	int cd;
61148972d90SThierry Reding 
61248972d90SThierry Reding 	cd = board_mmc_getcd(mmc);
61348972d90SThierry Reding 
614d4e1da4eSPeter Korsgaard 	if (cd < 0) {
61593bfd616SPantelis Antoniou 		if (mmc->cfg->ops->getcd)
61693bfd616SPantelis Antoniou 			cd = mmc->cfg->ops->getcd(mmc);
617d4e1da4eSPeter Korsgaard 		else
618d4e1da4eSPeter Korsgaard 			cd = 1;
619d4e1da4eSPeter Korsgaard 	}
62048972d90SThierry Reding 
62148972d90SThierry Reding 	return cd;
62248972d90SThierry Reding }
62348972d90SThierry Reding 
624fdbb873eSKim Phillips static int sd_switch(struct mmc *mmc, int mode, int group, u8 value, u8 *resp)
625272cc70bSAndy Fleming {
626272cc70bSAndy Fleming 	struct mmc_cmd cmd;
627272cc70bSAndy Fleming 	struct mmc_data data;
628272cc70bSAndy Fleming 
629272cc70bSAndy Fleming 	/* Switch the frequency */
630272cc70bSAndy Fleming 	cmd.cmdidx = SD_CMD_SWITCH_FUNC;
631272cc70bSAndy Fleming 	cmd.resp_type = MMC_RSP_R1;
632272cc70bSAndy Fleming 	cmd.cmdarg = (mode << 31) | 0xffffff;
633272cc70bSAndy Fleming 	cmd.cmdarg &= ~(0xf << (group * 4));
634272cc70bSAndy Fleming 	cmd.cmdarg |= value << (group * 4);
635272cc70bSAndy Fleming 
636272cc70bSAndy Fleming 	data.dest = (char *)resp;
637272cc70bSAndy Fleming 	data.blocksize = 64;
638272cc70bSAndy Fleming 	data.blocks = 1;
639272cc70bSAndy Fleming 	data.flags = MMC_DATA_READ;
640272cc70bSAndy Fleming 
641272cc70bSAndy Fleming 	return mmc_send_cmd(mmc, &cmd, &data);
642272cc70bSAndy Fleming }
643272cc70bSAndy Fleming 
644272cc70bSAndy Fleming 
645fdbb873eSKim Phillips static int sd_change_freq(struct mmc *mmc)
646272cc70bSAndy Fleming {
647272cc70bSAndy Fleming 	int err;
648272cc70bSAndy Fleming 	struct mmc_cmd cmd;
649f781dd38SAnton staaf 	ALLOC_CACHE_ALIGN_BUFFER(uint, scr, 2);
650f781dd38SAnton staaf 	ALLOC_CACHE_ALIGN_BUFFER(uint, switch_status, 16);
651272cc70bSAndy Fleming 	struct mmc_data data;
652272cc70bSAndy Fleming 	int timeout;
653272cc70bSAndy Fleming 
654272cc70bSAndy Fleming 	mmc->card_caps = 0;
655272cc70bSAndy Fleming 
656d52ebf10SThomas Chou 	if (mmc_host_is_spi(mmc))
657d52ebf10SThomas Chou 		return 0;
658d52ebf10SThomas Chou 
659272cc70bSAndy Fleming 	/* Read the SCR to find out if this card supports higher speeds */
660272cc70bSAndy Fleming 	cmd.cmdidx = MMC_CMD_APP_CMD;
661272cc70bSAndy Fleming 	cmd.resp_type = MMC_RSP_R1;
662272cc70bSAndy Fleming 	cmd.cmdarg = mmc->rca << 16;
663272cc70bSAndy Fleming 
664272cc70bSAndy Fleming 	err = mmc_send_cmd(mmc, &cmd, NULL);
665272cc70bSAndy Fleming 
666272cc70bSAndy Fleming 	if (err)
667272cc70bSAndy Fleming 		return err;
668272cc70bSAndy Fleming 
669272cc70bSAndy Fleming 	cmd.cmdidx = SD_CMD_APP_SEND_SCR;
670272cc70bSAndy Fleming 	cmd.resp_type = MMC_RSP_R1;
671272cc70bSAndy Fleming 	cmd.cmdarg = 0;
672272cc70bSAndy Fleming 
673272cc70bSAndy Fleming 	timeout = 3;
674272cc70bSAndy Fleming 
675272cc70bSAndy Fleming retry_scr:
676f781dd38SAnton staaf 	data.dest = (char *)scr;
677272cc70bSAndy Fleming 	data.blocksize = 8;
678272cc70bSAndy Fleming 	data.blocks = 1;
679272cc70bSAndy Fleming 	data.flags = MMC_DATA_READ;
680272cc70bSAndy Fleming 
681272cc70bSAndy Fleming 	err = mmc_send_cmd(mmc, &cmd, &data);
682272cc70bSAndy Fleming 
683272cc70bSAndy Fleming 	if (err) {
684272cc70bSAndy Fleming 		if (timeout--)
685272cc70bSAndy Fleming 			goto retry_scr;
686272cc70bSAndy Fleming 
687272cc70bSAndy Fleming 		return err;
688272cc70bSAndy Fleming 	}
689272cc70bSAndy Fleming 
6904e3d89baSYauhen Kharuzhy 	mmc->scr[0] = __be32_to_cpu(scr[0]);
6914e3d89baSYauhen Kharuzhy 	mmc->scr[1] = __be32_to_cpu(scr[1]);
692272cc70bSAndy Fleming 
693272cc70bSAndy Fleming 	switch ((mmc->scr[0] >> 24) & 0xf) {
694272cc70bSAndy Fleming 		case 0:
695272cc70bSAndy Fleming 			mmc->version = SD_VERSION_1_0;
696272cc70bSAndy Fleming 			break;
697272cc70bSAndy Fleming 		case 1:
698272cc70bSAndy Fleming 			mmc->version = SD_VERSION_1_10;
699272cc70bSAndy Fleming 			break;
700272cc70bSAndy Fleming 		case 2:
701272cc70bSAndy Fleming 			mmc->version = SD_VERSION_2;
7021741c64dSJaehoon Chung 			if ((mmc->scr[0] >> 15) & 0x1)
7031741c64dSJaehoon Chung 				mmc->version = SD_VERSION_3;
704272cc70bSAndy Fleming 			break;
705272cc70bSAndy Fleming 		default:
706272cc70bSAndy Fleming 			mmc->version = SD_VERSION_1_0;
707272cc70bSAndy Fleming 			break;
708272cc70bSAndy Fleming 	}
709272cc70bSAndy Fleming 
710b44c7083SAlagu Sankar 	if (mmc->scr[0] & SD_DATA_4BIT)
711b44c7083SAlagu Sankar 		mmc->card_caps |= MMC_MODE_4BIT;
712b44c7083SAlagu Sankar 
713272cc70bSAndy Fleming 	/* Version 1.0 doesn't support switching */
714272cc70bSAndy Fleming 	if (mmc->version == SD_VERSION_1_0)
715272cc70bSAndy Fleming 		return 0;
716272cc70bSAndy Fleming 
717272cc70bSAndy Fleming 	timeout = 4;
718272cc70bSAndy Fleming 	while (timeout--) {
719272cc70bSAndy Fleming 		err = sd_switch(mmc, SD_SWITCH_CHECK, 0, 1,
720f781dd38SAnton staaf 				(u8 *)switch_status);
721272cc70bSAndy Fleming 
722272cc70bSAndy Fleming 		if (err)
723272cc70bSAndy Fleming 			return err;
724272cc70bSAndy Fleming 
725272cc70bSAndy Fleming 		/* The high-speed function is busy.  Try again */
7264e3d89baSYauhen Kharuzhy 		if (!(__be32_to_cpu(switch_status[7]) & SD_HIGHSPEED_BUSY))
727272cc70bSAndy Fleming 			break;
728272cc70bSAndy Fleming 	}
729272cc70bSAndy Fleming 
730272cc70bSAndy Fleming 	/* If high-speed isn't supported, we return */
7314e3d89baSYauhen Kharuzhy 	if (!(__be32_to_cpu(switch_status[3]) & SD_HIGHSPEED_SUPPORTED))
732272cc70bSAndy Fleming 		return 0;
733272cc70bSAndy Fleming 
7342c3fbf4cSMacpaul Lin 	/*
7352c3fbf4cSMacpaul Lin 	 * If the host doesn't support SD_HIGHSPEED, do not switch card to
7362c3fbf4cSMacpaul Lin 	 * HIGHSPEED mode even if the card support SD_HIGHSPPED.
7372c3fbf4cSMacpaul Lin 	 * This can avoid furthur problem when the card runs in different
7382c3fbf4cSMacpaul Lin 	 * mode between the host.
7392c3fbf4cSMacpaul Lin 	 */
74093bfd616SPantelis Antoniou 	if (!((mmc->cfg->host_caps & MMC_MODE_HS_52MHz) &&
74193bfd616SPantelis Antoniou 		(mmc->cfg->host_caps & MMC_MODE_HS)))
7422c3fbf4cSMacpaul Lin 		return 0;
7432c3fbf4cSMacpaul Lin 
744f781dd38SAnton staaf 	err = sd_switch(mmc, SD_SWITCH_SWITCH, 0, 1, (u8 *)switch_status);
745272cc70bSAndy Fleming 
746272cc70bSAndy Fleming 	if (err)
747272cc70bSAndy Fleming 		return err;
748272cc70bSAndy Fleming 
7494e3d89baSYauhen Kharuzhy 	if ((__be32_to_cpu(switch_status[4]) & 0x0f000000) == 0x01000000)
750272cc70bSAndy Fleming 		mmc->card_caps |= MMC_MODE_HS;
751272cc70bSAndy Fleming 
752272cc70bSAndy Fleming 	return 0;
753272cc70bSAndy Fleming }
754272cc70bSAndy Fleming 
755272cc70bSAndy Fleming /* frequency bases */
756272cc70bSAndy Fleming /* divided by 10 to be nice to platforms without floating point */
7575f837c2cSMike Frysinger static const int fbase[] = {
758272cc70bSAndy Fleming 	10000,
759272cc70bSAndy Fleming 	100000,
760272cc70bSAndy Fleming 	1000000,
761272cc70bSAndy Fleming 	10000000,
762272cc70bSAndy Fleming };
763272cc70bSAndy Fleming 
764272cc70bSAndy Fleming /* Multiplier values for TRAN_SPEED.  Multiplied by 10 to be nice
765272cc70bSAndy Fleming  * to platforms without floating point.
766272cc70bSAndy Fleming  */
7675f837c2cSMike Frysinger static const int multipliers[] = {
768272cc70bSAndy Fleming 	0,	/* reserved */
769272cc70bSAndy Fleming 	10,
770272cc70bSAndy Fleming 	12,
771272cc70bSAndy Fleming 	13,
772272cc70bSAndy Fleming 	15,
773272cc70bSAndy Fleming 	20,
774272cc70bSAndy Fleming 	25,
775272cc70bSAndy Fleming 	30,
776272cc70bSAndy Fleming 	35,
777272cc70bSAndy Fleming 	40,
778272cc70bSAndy Fleming 	45,
779272cc70bSAndy Fleming 	50,
780272cc70bSAndy Fleming 	55,
781272cc70bSAndy Fleming 	60,
782272cc70bSAndy Fleming 	70,
783272cc70bSAndy Fleming 	80,
784272cc70bSAndy Fleming };
785272cc70bSAndy Fleming 
786fdbb873eSKim Phillips static void mmc_set_ios(struct mmc *mmc)
787272cc70bSAndy Fleming {
78893bfd616SPantelis Antoniou 	if (mmc->cfg->ops->set_ios)
78993bfd616SPantelis Antoniou 		mmc->cfg->ops->set_ios(mmc);
790272cc70bSAndy Fleming }
791272cc70bSAndy Fleming 
792272cc70bSAndy Fleming void mmc_set_clock(struct mmc *mmc, uint clock)
793272cc70bSAndy Fleming {
79493bfd616SPantelis Antoniou 	if (clock > mmc->cfg->f_max)
79593bfd616SPantelis Antoniou 		clock = mmc->cfg->f_max;
796272cc70bSAndy Fleming 
79793bfd616SPantelis Antoniou 	if (clock < mmc->cfg->f_min)
79893bfd616SPantelis Antoniou 		clock = mmc->cfg->f_min;
799272cc70bSAndy Fleming 
800272cc70bSAndy Fleming 	mmc->clock = clock;
801272cc70bSAndy Fleming 
802272cc70bSAndy Fleming 	mmc_set_ios(mmc);
803272cc70bSAndy Fleming }
804272cc70bSAndy Fleming 
805fdbb873eSKim Phillips static void mmc_set_bus_width(struct mmc *mmc, uint width)
806272cc70bSAndy Fleming {
807272cc70bSAndy Fleming 	mmc->bus_width = width;
808272cc70bSAndy Fleming 
809272cc70bSAndy Fleming 	mmc_set_ios(mmc);
810272cc70bSAndy Fleming }
811272cc70bSAndy Fleming 
812fdbb873eSKim Phillips static int mmc_startup(struct mmc *mmc)
813272cc70bSAndy Fleming {
814f866a46dSStephen Warren 	int err, i;
815272cc70bSAndy Fleming 	uint mult, freq;
816639b7827SYoshihiro Shimoda 	u64 cmult, csize, capacity;
817272cc70bSAndy Fleming 	struct mmc_cmd cmd;
8188bfa195eSSimon Glass 	ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
8198bfa195eSSimon Glass 	ALLOC_CACHE_ALIGN_BUFFER(u8, test_csd, MMC_MAX_BLOCK_LEN);
8205d4fc8d9SRaffaele Recalcati 	int timeout = 1000;
8210c453bb7SDiego Santa Cruz 	bool has_parts = false;
8228a0cf490SDiego Santa Cruz 	bool part_completed;
823272cc70bSAndy Fleming 
824d52ebf10SThomas Chou #ifdef CONFIG_MMC_SPI_CRC_ON
825d52ebf10SThomas Chou 	if (mmc_host_is_spi(mmc)) { /* enable CRC check for spi */
826d52ebf10SThomas Chou 		cmd.cmdidx = MMC_CMD_SPI_CRC_ON_OFF;
827d52ebf10SThomas Chou 		cmd.resp_type = MMC_RSP_R1;
828d52ebf10SThomas Chou 		cmd.cmdarg = 1;
829d52ebf10SThomas Chou 		err = mmc_send_cmd(mmc, &cmd, NULL);
830d52ebf10SThomas Chou 
831d52ebf10SThomas Chou 		if (err)
832d52ebf10SThomas Chou 			return err;
833d52ebf10SThomas Chou 	}
834d52ebf10SThomas Chou #endif
835d52ebf10SThomas Chou 
836272cc70bSAndy Fleming 	/* Put the Card in Identify Mode */
837d52ebf10SThomas Chou 	cmd.cmdidx = mmc_host_is_spi(mmc) ? MMC_CMD_SEND_CID :
838d52ebf10SThomas Chou 		MMC_CMD_ALL_SEND_CID; /* cmd not supported in spi */
839272cc70bSAndy Fleming 	cmd.resp_type = MMC_RSP_R2;
840272cc70bSAndy Fleming 	cmd.cmdarg = 0;
841272cc70bSAndy Fleming 
842272cc70bSAndy Fleming 	err = mmc_send_cmd(mmc, &cmd, NULL);
843272cc70bSAndy Fleming 
844272cc70bSAndy Fleming 	if (err)
845272cc70bSAndy Fleming 		return err;
846272cc70bSAndy Fleming 
847272cc70bSAndy Fleming 	memcpy(mmc->cid, cmd.response, 16);
848272cc70bSAndy Fleming 
849272cc70bSAndy Fleming 	/*
850272cc70bSAndy Fleming 	 * For MMC cards, set the Relative Address.
851272cc70bSAndy Fleming 	 * For SD cards, get the Relatvie Address.
852272cc70bSAndy Fleming 	 * This also puts the cards into Standby State
853272cc70bSAndy Fleming 	 */
854d52ebf10SThomas Chou 	if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
855272cc70bSAndy Fleming 		cmd.cmdidx = SD_CMD_SEND_RELATIVE_ADDR;
856272cc70bSAndy Fleming 		cmd.cmdarg = mmc->rca << 16;
857272cc70bSAndy Fleming 		cmd.resp_type = MMC_RSP_R6;
858272cc70bSAndy Fleming 
859272cc70bSAndy Fleming 		err = mmc_send_cmd(mmc, &cmd, NULL);
860272cc70bSAndy Fleming 
861272cc70bSAndy Fleming 		if (err)
862272cc70bSAndy Fleming 			return err;
863272cc70bSAndy Fleming 
864272cc70bSAndy Fleming 		if (IS_SD(mmc))
865998be3ddSRabin Vincent 			mmc->rca = (cmd.response[0] >> 16) & 0xffff;
866d52ebf10SThomas Chou 	}
867272cc70bSAndy Fleming 
868272cc70bSAndy Fleming 	/* Get the Card-Specific Data */
869272cc70bSAndy Fleming 	cmd.cmdidx = MMC_CMD_SEND_CSD;
870272cc70bSAndy Fleming 	cmd.resp_type = MMC_RSP_R2;
871272cc70bSAndy Fleming 	cmd.cmdarg = mmc->rca << 16;
872272cc70bSAndy Fleming 
873272cc70bSAndy Fleming 	err = mmc_send_cmd(mmc, &cmd, NULL);
874272cc70bSAndy Fleming 
8755d4fc8d9SRaffaele Recalcati 	/* Waiting for the ready status */
8765d4fc8d9SRaffaele Recalcati 	mmc_send_status(mmc, timeout);
8775d4fc8d9SRaffaele Recalcati 
878272cc70bSAndy Fleming 	if (err)
879272cc70bSAndy Fleming 		return err;
880272cc70bSAndy Fleming 
881998be3ddSRabin Vincent 	mmc->csd[0] = cmd.response[0];
882998be3ddSRabin Vincent 	mmc->csd[1] = cmd.response[1];
883998be3ddSRabin Vincent 	mmc->csd[2] = cmd.response[2];
884998be3ddSRabin Vincent 	mmc->csd[3] = cmd.response[3];
885272cc70bSAndy Fleming 
886272cc70bSAndy Fleming 	if (mmc->version == MMC_VERSION_UNKNOWN) {
8870b453ffeSRabin Vincent 		int version = (cmd.response[0] >> 26) & 0xf;
888272cc70bSAndy Fleming 
889272cc70bSAndy Fleming 		switch (version) {
890272cc70bSAndy Fleming 			case 0:
891272cc70bSAndy Fleming 				mmc->version = MMC_VERSION_1_2;
892272cc70bSAndy Fleming 				break;
893272cc70bSAndy Fleming 			case 1:
894272cc70bSAndy Fleming 				mmc->version = MMC_VERSION_1_4;
895272cc70bSAndy Fleming 				break;
896272cc70bSAndy Fleming 			case 2:
897272cc70bSAndy Fleming 				mmc->version = MMC_VERSION_2_2;
898272cc70bSAndy Fleming 				break;
899272cc70bSAndy Fleming 			case 3:
900272cc70bSAndy Fleming 				mmc->version = MMC_VERSION_3;
901272cc70bSAndy Fleming 				break;
902272cc70bSAndy Fleming 			case 4:
903272cc70bSAndy Fleming 				mmc->version = MMC_VERSION_4;
904272cc70bSAndy Fleming 				break;
905272cc70bSAndy Fleming 			default:
906272cc70bSAndy Fleming 				mmc->version = MMC_VERSION_1_2;
907272cc70bSAndy Fleming 				break;
908272cc70bSAndy Fleming 		}
909272cc70bSAndy Fleming 	}
910272cc70bSAndy Fleming 
911272cc70bSAndy Fleming 	/* divide frequency by 10, since the mults are 10x bigger */
9120b453ffeSRabin Vincent 	freq = fbase[(cmd.response[0] & 0x7)];
9130b453ffeSRabin Vincent 	mult = multipliers[((cmd.response[0] >> 3) & 0xf)];
914272cc70bSAndy Fleming 
915272cc70bSAndy Fleming 	mmc->tran_speed = freq * mult;
916272cc70bSAndy Fleming 
917ab71188cSMarkus Niebel 	mmc->dsr_imp = ((cmd.response[1] >> 12) & 0x1);
918998be3ddSRabin Vincent 	mmc->read_bl_len = 1 << ((cmd.response[1] >> 16) & 0xf);
919272cc70bSAndy Fleming 
920272cc70bSAndy Fleming 	if (IS_SD(mmc))
921272cc70bSAndy Fleming 		mmc->write_bl_len = mmc->read_bl_len;
922272cc70bSAndy Fleming 	else
923998be3ddSRabin Vincent 		mmc->write_bl_len = 1 << ((cmd.response[3] >> 22) & 0xf);
924272cc70bSAndy Fleming 
925272cc70bSAndy Fleming 	if (mmc->high_capacity) {
926272cc70bSAndy Fleming 		csize = (mmc->csd[1] & 0x3f) << 16
927272cc70bSAndy Fleming 			| (mmc->csd[2] & 0xffff0000) >> 16;
928272cc70bSAndy Fleming 		cmult = 8;
929272cc70bSAndy Fleming 	} else {
930272cc70bSAndy Fleming 		csize = (mmc->csd[1] & 0x3ff) << 2
931272cc70bSAndy Fleming 			| (mmc->csd[2] & 0xc0000000) >> 30;
932272cc70bSAndy Fleming 		cmult = (mmc->csd[2] & 0x00038000) >> 15;
933272cc70bSAndy Fleming 	}
934272cc70bSAndy Fleming 
935f866a46dSStephen Warren 	mmc->capacity_user = (csize + 1) << (cmult + 2);
936f866a46dSStephen Warren 	mmc->capacity_user *= mmc->read_bl_len;
937f866a46dSStephen Warren 	mmc->capacity_boot = 0;
938f866a46dSStephen Warren 	mmc->capacity_rpmb = 0;
939f866a46dSStephen Warren 	for (i = 0; i < 4; i++)
940f866a46dSStephen Warren 		mmc->capacity_gp[i] = 0;
941272cc70bSAndy Fleming 
9428bfa195eSSimon Glass 	if (mmc->read_bl_len > MMC_MAX_BLOCK_LEN)
9438bfa195eSSimon Glass 		mmc->read_bl_len = MMC_MAX_BLOCK_LEN;
944272cc70bSAndy Fleming 
9458bfa195eSSimon Glass 	if (mmc->write_bl_len > MMC_MAX_BLOCK_LEN)
9468bfa195eSSimon Glass 		mmc->write_bl_len = MMC_MAX_BLOCK_LEN;
947272cc70bSAndy Fleming 
948ab71188cSMarkus Niebel 	if ((mmc->dsr_imp) && (0xffffffff != mmc->dsr)) {
949ab71188cSMarkus Niebel 		cmd.cmdidx = MMC_CMD_SET_DSR;
950ab71188cSMarkus Niebel 		cmd.cmdarg = (mmc->dsr & 0xffff) << 16;
951ab71188cSMarkus Niebel 		cmd.resp_type = MMC_RSP_NONE;
952ab71188cSMarkus Niebel 		if (mmc_send_cmd(mmc, &cmd, NULL))
953ab71188cSMarkus Niebel 			printf("MMC: SET_DSR failed\n");
954ab71188cSMarkus Niebel 	}
955ab71188cSMarkus Niebel 
956272cc70bSAndy Fleming 	/* Select the card, and put it into Transfer Mode */
957d52ebf10SThomas Chou 	if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
958272cc70bSAndy Fleming 		cmd.cmdidx = MMC_CMD_SELECT_CARD;
959fe8f7066SAjay Bhargav 		cmd.resp_type = MMC_RSP_R1;
960272cc70bSAndy Fleming 		cmd.cmdarg = mmc->rca << 16;
961272cc70bSAndy Fleming 		err = mmc_send_cmd(mmc, &cmd, NULL);
962272cc70bSAndy Fleming 
963272cc70bSAndy Fleming 		if (err)
964272cc70bSAndy Fleming 			return err;
965d52ebf10SThomas Chou 	}
966272cc70bSAndy Fleming 
967e6f99a56SLei Wen 	/*
968e6f99a56SLei Wen 	 * For SD, its erase group is always one sector
969e6f99a56SLei Wen 	 */
970e6f99a56SLei Wen 	mmc->erase_grp_size = 1;
971bc897b1dSLei Wen 	mmc->part_config = MMCPART_NOAVAILABLE;
972d23e2c09SSukumar Ghorai 	if (!IS_SD(mmc) && (mmc->version >= MMC_VERSION_4)) {
973d23e2c09SSukumar Ghorai 		/* check  ext_csd version and capacity */
974d23e2c09SSukumar Ghorai 		err = mmc_send_ext_csd(mmc, ext_csd);
975*9cf199ebSDiego Santa Cruz 		if (err)
976*9cf199ebSDiego Santa Cruz 			return err;
977*9cf199ebSDiego Santa Cruz 		if (ext_csd[EXT_CSD_REV] >= 2) {
978639b7827SYoshihiro Shimoda 			/*
979639b7827SYoshihiro Shimoda 			 * According to the JEDEC Standard, the value of
980639b7827SYoshihiro Shimoda 			 * ext_csd's capacity is valid if the value is more
981639b7827SYoshihiro Shimoda 			 * than 2GB
982639b7827SYoshihiro Shimoda 			 */
9830560db18SLei Wen 			capacity = ext_csd[EXT_CSD_SEC_CNT] << 0
9840560db18SLei Wen 					| ext_csd[EXT_CSD_SEC_CNT + 1] << 8
9850560db18SLei Wen 					| ext_csd[EXT_CSD_SEC_CNT + 2] << 16
9860560db18SLei Wen 					| ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
9878bfa195eSSimon Glass 			capacity *= MMC_MAX_BLOCK_LEN;
988b1f1e821SŁukasz Majewski 			if ((capacity >> 20) > 2 * 1024)
989f866a46dSStephen Warren 				mmc->capacity_user = capacity;
990d23e2c09SSukumar Ghorai 		}
991bc897b1dSLei Wen 
99264f4a619SJaehoon Chung 		switch (ext_csd[EXT_CSD_REV]) {
99364f4a619SJaehoon Chung 		case 1:
99464f4a619SJaehoon Chung 			mmc->version = MMC_VERSION_4_1;
99564f4a619SJaehoon Chung 			break;
99664f4a619SJaehoon Chung 		case 2:
99764f4a619SJaehoon Chung 			mmc->version = MMC_VERSION_4_2;
99864f4a619SJaehoon Chung 			break;
99964f4a619SJaehoon Chung 		case 3:
100064f4a619SJaehoon Chung 			mmc->version = MMC_VERSION_4_3;
100164f4a619SJaehoon Chung 			break;
100264f4a619SJaehoon Chung 		case 5:
100364f4a619SJaehoon Chung 			mmc->version = MMC_VERSION_4_41;
100464f4a619SJaehoon Chung 			break;
100564f4a619SJaehoon Chung 		case 6:
100664f4a619SJaehoon Chung 			mmc->version = MMC_VERSION_4_5;
100764f4a619SJaehoon Chung 			break;
1008edab723bSMarkus Niebel 		case 7:
1009edab723bSMarkus Niebel 			mmc->version = MMC_VERSION_5_0;
1010edab723bSMarkus Niebel 			break;
101164f4a619SJaehoon Chung 		}
101264f4a619SJaehoon Chung 
10138a0cf490SDiego Santa Cruz 		/* The partition data may be non-zero but it is only
10148a0cf490SDiego Santa Cruz 		 * effective if PARTITION_SETTING_COMPLETED is set in
10158a0cf490SDiego Santa Cruz 		 * EXT_CSD, so ignore any data if this bit is not set,
10168a0cf490SDiego Santa Cruz 		 * except for enabling the high-capacity group size
10178a0cf490SDiego Santa Cruz 		 * definition (see below). */
10188a0cf490SDiego Santa Cruz 		part_completed = !!(ext_csd[EXT_CSD_PARTITION_SETTING] &
10198a0cf490SDiego Santa Cruz 				    EXT_CSD_PARTITION_SETTING_COMPLETED);
10208a0cf490SDiego Santa Cruz 
10210c453bb7SDiego Santa Cruz 		/* store the partition info of emmc */
10220c453bb7SDiego Santa Cruz 		mmc->part_support = ext_csd[EXT_CSD_PARTITIONING_SUPPORT];
10230c453bb7SDiego Santa Cruz 		if ((ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & PART_SUPPORT) ||
10240c453bb7SDiego Santa Cruz 		    ext_csd[EXT_CSD_BOOT_MULT])
10250c453bb7SDiego Santa Cruz 			mmc->part_config = ext_csd[EXT_CSD_PART_CONF];
10268a0cf490SDiego Santa Cruz 		if (part_completed &&
10278a0cf490SDiego Santa Cruz 		    (ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & ENHNCD_SUPPORT))
10280c453bb7SDiego Santa Cruz 			mmc->part_attr = ext_csd[EXT_CSD_PARTITIONS_ATTRIBUTE];
10290c453bb7SDiego Santa Cruz 
10300c453bb7SDiego Santa Cruz 		mmc->capacity_boot = ext_csd[EXT_CSD_BOOT_MULT] << 17;
10310c453bb7SDiego Santa Cruz 
10320c453bb7SDiego Santa Cruz 		mmc->capacity_rpmb = ext_csd[EXT_CSD_RPMB_MULT] << 17;
10330c453bb7SDiego Santa Cruz 
10340c453bb7SDiego Santa Cruz 		for (i = 0; i < 4; i++) {
10350c453bb7SDiego Santa Cruz 			int idx = EXT_CSD_GP_SIZE_MULT + i * 3;
10368a0cf490SDiego Santa Cruz 			uint mult = (ext_csd[idx + 2] << 16) +
10370c453bb7SDiego Santa Cruz 				(ext_csd[idx + 1] << 8) + ext_csd[idx];
10388a0cf490SDiego Santa Cruz 			if (mult)
10398a0cf490SDiego Santa Cruz 				has_parts = true;
10408a0cf490SDiego Santa Cruz 			if (!part_completed)
10418a0cf490SDiego Santa Cruz 				continue;
10428a0cf490SDiego Santa Cruz 			mmc->capacity_gp[i] = mult;
10430c453bb7SDiego Santa Cruz 			mmc->capacity_gp[i] *=
10440c453bb7SDiego Santa Cruz 				ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
10450c453bb7SDiego Santa Cruz 			mmc->capacity_gp[i] *= ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
1046f8e89d67SDiego Santa Cruz 			mmc->capacity_gp[i] <<= 19;
10470c453bb7SDiego Santa Cruz 		}
10480c453bb7SDiego Santa Cruz 
10498a0cf490SDiego Santa Cruz 		if (part_completed) {
1050a7f852b6SDiego Santa Cruz 			mmc->enh_user_size =
1051a7f852b6SDiego Santa Cruz 				(ext_csd[EXT_CSD_ENH_SIZE_MULT+2] << 16) +
1052a7f852b6SDiego Santa Cruz 				(ext_csd[EXT_CSD_ENH_SIZE_MULT+1] << 8) +
1053a7f852b6SDiego Santa Cruz 				ext_csd[EXT_CSD_ENH_SIZE_MULT];
1054a7f852b6SDiego Santa Cruz 			mmc->enh_user_size *= ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
1055a7f852b6SDiego Santa Cruz 			mmc->enh_user_size *= ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
1056a7f852b6SDiego Santa Cruz 			mmc->enh_user_size <<= 19;
1057a7f852b6SDiego Santa Cruz 			mmc->enh_user_start =
1058a7f852b6SDiego Santa Cruz 				(ext_csd[EXT_CSD_ENH_START_ADDR+3] << 24) +
1059a7f852b6SDiego Santa Cruz 				(ext_csd[EXT_CSD_ENH_START_ADDR+2] << 16) +
1060a7f852b6SDiego Santa Cruz 				(ext_csd[EXT_CSD_ENH_START_ADDR+1] << 8) +
1061a7f852b6SDiego Santa Cruz 				ext_csd[EXT_CSD_ENH_START_ADDR];
1062a7f852b6SDiego Santa Cruz 			if (mmc->high_capacity)
1063a7f852b6SDiego Santa Cruz 				mmc->enh_user_start <<= 9;
10648a0cf490SDiego Santa Cruz 		}
1065a7f852b6SDiego Santa Cruz 
1066e6f99a56SLei Wen 		/*
10671937e5aaSOliver Metz 		 * Host needs to enable ERASE_GRP_DEF bit if device is
10681937e5aaSOliver Metz 		 * partitioned. This bit will be lost every time after a reset
10691937e5aaSOliver Metz 		 * or power off. This will affect erase size.
1070e6f99a56SLei Wen 		 */
10718a0cf490SDiego Santa Cruz 		if (part_completed)
10720c453bb7SDiego Santa Cruz 			has_parts = true;
10731937e5aaSOliver Metz 		if ((ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & PART_SUPPORT) &&
10740c453bb7SDiego Santa Cruz 		    (ext_csd[EXT_CSD_PARTITIONS_ATTRIBUTE] & PART_ENH_ATTRIB))
10750c453bb7SDiego Santa Cruz 			has_parts = true;
10760c453bb7SDiego Santa Cruz 		if (has_parts) {
10771937e5aaSOliver Metz 			err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
10781937e5aaSOliver Metz 				EXT_CSD_ERASE_GROUP_DEF, 1);
10791937e5aaSOliver Metz 
10801937e5aaSOliver Metz 			if (err)
10811937e5aaSOliver Metz 				return err;
1082021a8055SHannes Petermaier 			else
1083021a8055SHannes Petermaier 				ext_csd[EXT_CSD_ERASE_GROUP_DEF] = 1;
1084037dc0abSDiego Santa Cruz 		}
10851937e5aaSOliver Metz 
1086037dc0abSDiego Santa Cruz 		if (ext_csd[EXT_CSD_ERASE_GROUP_DEF] & 0x01) {
10871937e5aaSOliver Metz 			/* Read out group size from ext_csd */
10880560db18SLei Wen 			mmc->erase_grp_size =
1089a4ff9f83SDiego Santa Cruz 				ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] * 1024;
1090d7b29129SMarkus Niebel 			/*
1091d7b29129SMarkus Niebel 			 * if high capacity and partition setting completed
1092d7b29129SMarkus Niebel 			 * SEC_COUNT is valid even if it is smaller than 2 GiB
1093d7b29129SMarkus Niebel 			 * JEDEC Standard JESD84-B45, 6.2.4
1094d7b29129SMarkus Niebel 			 */
10958a0cf490SDiego Santa Cruz 			if (mmc->high_capacity && part_completed) {
1096d7b29129SMarkus Niebel 				capacity = (ext_csd[EXT_CSD_SEC_CNT]) |
1097d7b29129SMarkus Niebel 					(ext_csd[EXT_CSD_SEC_CNT + 1] << 8) |
1098d7b29129SMarkus Niebel 					(ext_csd[EXT_CSD_SEC_CNT + 2] << 16) |
1099d7b29129SMarkus Niebel 					(ext_csd[EXT_CSD_SEC_CNT + 3] << 24);
1100d7b29129SMarkus Niebel 				capacity *= MMC_MAX_BLOCK_LEN;
1101d7b29129SMarkus Niebel 				mmc->capacity_user = capacity;
1102d7b29129SMarkus Niebel 			}
11038bfa195eSSimon Glass 		} else {
11041937e5aaSOliver Metz 			/* Calculate the group size from the csd value. */
1105e6f99a56SLei Wen 			int erase_gsz, erase_gmul;
1106e6f99a56SLei Wen 			erase_gsz = (mmc->csd[2] & 0x00007c00) >> 10;
1107e6f99a56SLei Wen 			erase_gmul = (mmc->csd[2] & 0x000003e0) >> 5;
1108e6f99a56SLei Wen 			mmc->erase_grp_size = (erase_gsz + 1)
1109e6f99a56SLei Wen 				* (erase_gmul + 1);
1110e6f99a56SLei Wen 		}
1111037dc0abSDiego Santa Cruz 
1112037dc0abSDiego Santa Cruz 		mmc->hc_wp_grp_size = 1024
1113037dc0abSDiego Santa Cruz 			* ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]
1114037dc0abSDiego Santa Cruz 			* ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
1115f866a46dSStephen Warren 	}
1116f866a46dSStephen Warren 
1117f866a46dSStephen Warren 	err = mmc_set_capacity(mmc, mmc->part_num);
1118f866a46dSStephen Warren 	if (err)
1119f866a46dSStephen Warren 		return err;
1120d23e2c09SSukumar Ghorai 
1121272cc70bSAndy Fleming 	if (IS_SD(mmc))
1122272cc70bSAndy Fleming 		err = sd_change_freq(mmc);
1123272cc70bSAndy Fleming 	else
1124272cc70bSAndy Fleming 		err = mmc_change_freq(mmc);
1125272cc70bSAndy Fleming 
1126272cc70bSAndy Fleming 	if (err)
1127272cc70bSAndy Fleming 		return err;
1128272cc70bSAndy Fleming 
1129272cc70bSAndy Fleming 	/* Restrict card's capabilities by what the host can do */
113093bfd616SPantelis Antoniou 	mmc->card_caps &= mmc->cfg->host_caps;
1131272cc70bSAndy Fleming 
1132272cc70bSAndy Fleming 	if (IS_SD(mmc)) {
1133272cc70bSAndy Fleming 		if (mmc->card_caps & MMC_MODE_4BIT) {
1134272cc70bSAndy Fleming 			cmd.cmdidx = MMC_CMD_APP_CMD;
1135272cc70bSAndy Fleming 			cmd.resp_type = MMC_RSP_R1;
1136272cc70bSAndy Fleming 			cmd.cmdarg = mmc->rca << 16;
1137272cc70bSAndy Fleming 
1138272cc70bSAndy Fleming 			err = mmc_send_cmd(mmc, &cmd, NULL);
1139272cc70bSAndy Fleming 			if (err)
1140272cc70bSAndy Fleming 				return err;
1141272cc70bSAndy Fleming 
1142272cc70bSAndy Fleming 			cmd.cmdidx = SD_CMD_APP_SET_BUS_WIDTH;
1143272cc70bSAndy Fleming 			cmd.resp_type = MMC_RSP_R1;
1144272cc70bSAndy Fleming 			cmd.cmdarg = 2;
1145272cc70bSAndy Fleming 			err = mmc_send_cmd(mmc, &cmd, NULL);
1146272cc70bSAndy Fleming 			if (err)
1147272cc70bSAndy Fleming 				return err;
1148272cc70bSAndy Fleming 
1149272cc70bSAndy Fleming 			mmc_set_bus_width(mmc, 4);
1150272cc70bSAndy Fleming 		}
1151272cc70bSAndy Fleming 
1152272cc70bSAndy Fleming 		if (mmc->card_caps & MMC_MODE_HS)
1153ad5fd922SJaehoon Chung 			mmc->tran_speed = 50000000;
1154272cc70bSAndy Fleming 		else
1155ad5fd922SJaehoon Chung 			mmc->tran_speed = 25000000;
1156272cc70bSAndy Fleming 	} else {
11577798f6dbSAndy Fleming 		int idx;
11587798f6dbSAndy Fleming 
11597798f6dbSAndy Fleming 		/* An array of possible bus widths in order of preference */
11607798f6dbSAndy Fleming 		static unsigned ext_csd_bits[] = {
1161d22e3d46SJaehoon Chung 			EXT_CSD_DDR_BUS_WIDTH_8,
1162d22e3d46SJaehoon Chung 			EXT_CSD_DDR_BUS_WIDTH_4,
11637798f6dbSAndy Fleming 			EXT_CSD_BUS_WIDTH_8,
11647798f6dbSAndy Fleming 			EXT_CSD_BUS_WIDTH_4,
11657798f6dbSAndy Fleming 			EXT_CSD_BUS_WIDTH_1,
11667798f6dbSAndy Fleming 		};
11677798f6dbSAndy Fleming 
11687798f6dbSAndy Fleming 		/* An array to map CSD bus widths to host cap bits */
11697798f6dbSAndy Fleming 		static unsigned ext_to_hostcaps[] = {
1170786e8f81SAndrew Gabbasov 			[EXT_CSD_DDR_BUS_WIDTH_4] =
1171786e8f81SAndrew Gabbasov 				MMC_MODE_DDR_52MHz | MMC_MODE_4BIT,
1172786e8f81SAndrew Gabbasov 			[EXT_CSD_DDR_BUS_WIDTH_8] =
1173786e8f81SAndrew Gabbasov 				MMC_MODE_DDR_52MHz | MMC_MODE_8BIT,
11747798f6dbSAndy Fleming 			[EXT_CSD_BUS_WIDTH_4] = MMC_MODE_4BIT,
11757798f6dbSAndy Fleming 			[EXT_CSD_BUS_WIDTH_8] = MMC_MODE_8BIT,
11767798f6dbSAndy Fleming 		};
11777798f6dbSAndy Fleming 
11787798f6dbSAndy Fleming 		/* An array to map chosen bus width to an integer */
11797798f6dbSAndy Fleming 		static unsigned widths[] = {
1180d22e3d46SJaehoon Chung 			8, 4, 8, 4, 1,
11817798f6dbSAndy Fleming 		};
11827798f6dbSAndy Fleming 
11837798f6dbSAndy Fleming 		for (idx=0; idx < ARRAY_SIZE(ext_csd_bits); idx++) {
11847798f6dbSAndy Fleming 			unsigned int extw = ext_csd_bits[idx];
1185786e8f81SAndrew Gabbasov 			unsigned int caps = ext_to_hostcaps[extw];
11867798f6dbSAndy Fleming 
11877798f6dbSAndy Fleming 			/*
1188786e8f81SAndrew Gabbasov 			 * Check to make sure the card and controller support
1189786e8f81SAndrew Gabbasov 			 * these capabilities
11907798f6dbSAndy Fleming 			 */
1191786e8f81SAndrew Gabbasov 			if ((mmc->card_caps & caps) != caps)
11927798f6dbSAndy Fleming 				continue;
11937798f6dbSAndy Fleming 
1194272cc70bSAndy Fleming 			err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
11957798f6dbSAndy Fleming 					EXT_CSD_BUS_WIDTH, extw);
1196272cc70bSAndy Fleming 
1197272cc70bSAndy Fleming 			if (err)
11984137894eSLei Wen 				continue;
1199272cc70bSAndy Fleming 
1200786e8f81SAndrew Gabbasov 			mmc->ddr_mode = (caps & MMC_MODE_DDR_52MHz) ? 1 : 0;
12017798f6dbSAndy Fleming 			mmc_set_bus_width(mmc, widths[idx]);
1202272cc70bSAndy Fleming 
12034137894eSLei Wen 			err = mmc_send_ext_csd(mmc, test_csd);
1204272cc70bSAndy Fleming 
1205786e8f81SAndrew Gabbasov 			if (err)
1206786e8f81SAndrew Gabbasov 				continue;
1207786e8f81SAndrew Gabbasov 
1208786e8f81SAndrew Gabbasov 			/* Only compare read only fields */
1209786e8f81SAndrew Gabbasov 			if (ext_csd[EXT_CSD_PARTITIONING_SUPPORT]
1210786e8f81SAndrew Gabbasov 				== test_csd[EXT_CSD_PARTITIONING_SUPPORT] &&
1211786e8f81SAndrew Gabbasov 			    ext_csd[EXT_CSD_HC_WP_GRP_SIZE]
1212786e8f81SAndrew Gabbasov 				== test_csd[EXT_CSD_HC_WP_GRP_SIZE] &&
1213786e8f81SAndrew Gabbasov 			    ext_csd[EXT_CSD_REV]
1214786e8f81SAndrew Gabbasov 				== test_csd[EXT_CSD_REV] &&
1215786e8f81SAndrew Gabbasov 			    ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]
1216786e8f81SAndrew Gabbasov 				== test_csd[EXT_CSD_HC_ERASE_GRP_SIZE] &&
1217786e8f81SAndrew Gabbasov 			    memcmp(&ext_csd[EXT_CSD_SEC_CNT],
1218786e8f81SAndrew Gabbasov 				   &test_csd[EXT_CSD_SEC_CNT], 4) == 0)
12194137894eSLei Wen 				break;
1220786e8f81SAndrew Gabbasov 			else
1221786e8f81SAndrew Gabbasov 				err = SWITCH_ERR;
12224137894eSLei Wen 		}
1223786e8f81SAndrew Gabbasov 
1224786e8f81SAndrew Gabbasov 		if (err)
1225786e8f81SAndrew Gabbasov 			return err;
1226272cc70bSAndy Fleming 
1227272cc70bSAndy Fleming 		if (mmc->card_caps & MMC_MODE_HS) {
1228272cc70bSAndy Fleming 			if (mmc->card_caps & MMC_MODE_HS_52MHz)
1229ad5fd922SJaehoon Chung 				mmc->tran_speed = 52000000;
1230272cc70bSAndy Fleming 			else
1231ad5fd922SJaehoon Chung 				mmc->tran_speed = 26000000;
1232272cc70bSAndy Fleming 		}
1233ad5fd922SJaehoon Chung 	}
1234ad5fd922SJaehoon Chung 
1235ad5fd922SJaehoon Chung 	mmc_set_clock(mmc, mmc->tran_speed);
1236272cc70bSAndy Fleming 
12375af8f45cSAndrew Gabbasov 	/* Fix the block length for DDR mode */
12385af8f45cSAndrew Gabbasov 	if (mmc->ddr_mode) {
12395af8f45cSAndrew Gabbasov 		mmc->read_bl_len = MMC_MAX_BLOCK_LEN;
12405af8f45cSAndrew Gabbasov 		mmc->write_bl_len = MMC_MAX_BLOCK_LEN;
12415af8f45cSAndrew Gabbasov 	}
12425af8f45cSAndrew Gabbasov 
1243272cc70bSAndy Fleming 	/* fill in device description */
1244272cc70bSAndy Fleming 	mmc->block_dev.lun = 0;
1245272cc70bSAndy Fleming 	mmc->block_dev.type = 0;
1246272cc70bSAndy Fleming 	mmc->block_dev.blksz = mmc->read_bl_len;
12470472fbfdSEgbert Eich 	mmc->block_dev.log2blksz = LOG2(mmc->block_dev.blksz);
12489b1f942cSRabin Vincent 	mmc->block_dev.lba = lldiv(mmc->capacity, mmc->read_bl_len);
124956196826SPaul Burton #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
1250babce5f6STaylor Hutt 	sprintf(mmc->block_dev.vendor, "Man %06x Snr %04x%04x",
1251babce5f6STaylor Hutt 		mmc->cid[0] >> 24, (mmc->cid[2] & 0xffff),
1252babce5f6STaylor Hutt 		(mmc->cid[3] >> 16) & 0xffff);
1253babce5f6STaylor Hutt 	sprintf(mmc->block_dev.product, "%c%c%c%c%c%c", mmc->cid[0] & 0xff,
12540b453ffeSRabin Vincent 		(mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
1255babce5f6STaylor Hutt 		(mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff,
1256babce5f6STaylor Hutt 		(mmc->cid[2] >> 24) & 0xff);
1257babce5f6STaylor Hutt 	sprintf(mmc->block_dev.revision, "%d.%d", (mmc->cid[2] >> 20) & 0xf,
1258babce5f6STaylor Hutt 		(mmc->cid[2] >> 16) & 0xf);
125956196826SPaul Burton #else
126056196826SPaul Burton 	mmc->block_dev.vendor[0] = 0;
126156196826SPaul Burton 	mmc->block_dev.product[0] = 0;
126256196826SPaul Burton 	mmc->block_dev.revision[0] = 0;
126356196826SPaul Burton #endif
1264122efd43SMikhail Kshevetskiy #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBDISK_SUPPORT)
1265272cc70bSAndy Fleming 	init_part(&mmc->block_dev);
1266122efd43SMikhail Kshevetskiy #endif
1267272cc70bSAndy Fleming 
1268272cc70bSAndy Fleming 	return 0;
1269272cc70bSAndy Fleming }
1270272cc70bSAndy Fleming 
1271fdbb873eSKim Phillips static int mmc_send_if_cond(struct mmc *mmc)
1272272cc70bSAndy Fleming {
1273272cc70bSAndy Fleming 	struct mmc_cmd cmd;
1274272cc70bSAndy Fleming 	int err;
1275272cc70bSAndy Fleming 
1276272cc70bSAndy Fleming 	cmd.cmdidx = SD_CMD_SEND_IF_COND;
1277272cc70bSAndy Fleming 	/* We set the bit if the host supports voltages between 2.7 and 3.6 V */
127893bfd616SPantelis Antoniou 	cmd.cmdarg = ((mmc->cfg->voltages & 0xff8000) != 0) << 8 | 0xaa;
1279272cc70bSAndy Fleming 	cmd.resp_type = MMC_RSP_R7;
1280272cc70bSAndy Fleming 
1281272cc70bSAndy Fleming 	err = mmc_send_cmd(mmc, &cmd, NULL);
1282272cc70bSAndy Fleming 
1283272cc70bSAndy Fleming 	if (err)
1284272cc70bSAndy Fleming 		return err;
1285272cc70bSAndy Fleming 
1286998be3ddSRabin Vincent 	if ((cmd.response[0] & 0xff) != 0xaa)
1287272cc70bSAndy Fleming 		return UNUSABLE_ERR;
1288272cc70bSAndy Fleming 	else
1289272cc70bSAndy Fleming 		mmc->version = SD_VERSION_2;
1290272cc70bSAndy Fleming 
1291272cc70bSAndy Fleming 	return 0;
1292272cc70bSAndy Fleming }
1293272cc70bSAndy Fleming 
129493bfd616SPantelis Antoniou /* not used any more */
129593bfd616SPantelis Antoniou int __deprecated mmc_register(struct mmc *mmc)
1296272cc70bSAndy Fleming {
129793bfd616SPantelis Antoniou #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
129893bfd616SPantelis Antoniou 	printf("%s is deprecated! use mmc_create() instead.\n", __func__);
129993bfd616SPantelis Antoniou #endif
130093bfd616SPantelis Antoniou 	return -1;
130193bfd616SPantelis Antoniou }
130293bfd616SPantelis Antoniou 
130393bfd616SPantelis Antoniou struct mmc *mmc_create(const struct mmc_config *cfg, void *priv)
130493bfd616SPantelis Antoniou {
130593bfd616SPantelis Antoniou 	struct mmc *mmc;
130693bfd616SPantelis Antoniou 
130793bfd616SPantelis Antoniou 	/* quick validation */
130893bfd616SPantelis Antoniou 	if (cfg == NULL || cfg->ops == NULL || cfg->ops->send_cmd == NULL ||
130993bfd616SPantelis Antoniou 			cfg->f_min == 0 || cfg->f_max == 0 || cfg->b_max == 0)
131093bfd616SPantelis Antoniou 		return NULL;
131193bfd616SPantelis Antoniou 
131293bfd616SPantelis Antoniou 	mmc = calloc(1, sizeof(*mmc));
131393bfd616SPantelis Antoniou 	if (mmc == NULL)
131493bfd616SPantelis Antoniou 		return NULL;
131593bfd616SPantelis Antoniou 
131693bfd616SPantelis Antoniou 	mmc->cfg = cfg;
131793bfd616SPantelis Antoniou 	mmc->priv = priv;
131893bfd616SPantelis Antoniou 
131993bfd616SPantelis Antoniou 	/* the following chunk was mmc_register() */
132093bfd616SPantelis Antoniou 
1321ab71188cSMarkus Niebel 	/* Setup dsr related values */
1322ab71188cSMarkus Niebel 	mmc->dsr_imp = 0;
1323ab71188cSMarkus Niebel 	mmc->dsr = 0xffffffff;
1324272cc70bSAndy Fleming 	/* Setup the universal parts of the block interface just once */
1325272cc70bSAndy Fleming 	mmc->block_dev.if_type = IF_TYPE_MMC;
1326272cc70bSAndy Fleming 	mmc->block_dev.dev = cur_dev_num++;
1327272cc70bSAndy Fleming 	mmc->block_dev.removable = 1;
1328272cc70bSAndy Fleming 	mmc->block_dev.block_read = mmc_bread;
1329272cc70bSAndy Fleming 	mmc->block_dev.block_write = mmc_bwrite;
1330e6f99a56SLei Wen 	mmc->block_dev.block_erase = mmc_berase;
133193bfd616SPantelis Antoniou 
133293bfd616SPantelis Antoniou 	/* setup initial part type */
133393bfd616SPantelis Antoniou 	mmc->block_dev.part_type = mmc->cfg->part_type;
1334272cc70bSAndy Fleming 
1335272cc70bSAndy Fleming 	INIT_LIST_HEAD(&mmc->link);
1336272cc70bSAndy Fleming 
1337272cc70bSAndy Fleming 	list_add_tail(&mmc->link, &mmc_devices);
1338272cc70bSAndy Fleming 
133993bfd616SPantelis Antoniou 	return mmc;
134093bfd616SPantelis Antoniou }
134193bfd616SPantelis Antoniou 
134293bfd616SPantelis Antoniou void mmc_destroy(struct mmc *mmc)
134393bfd616SPantelis Antoniou {
134493bfd616SPantelis Antoniou 	/* only freeing memory for now */
134593bfd616SPantelis Antoniou 	free(mmc);
1346272cc70bSAndy Fleming }
1347272cc70bSAndy Fleming 
1348df3fc526SMatthew McClintock #ifdef CONFIG_PARTITIONS
1349272cc70bSAndy Fleming block_dev_desc_t *mmc_get_dev(int dev)
1350272cc70bSAndy Fleming {
1351272cc70bSAndy Fleming 	struct mmc *mmc = find_mmc_device(dev);
13526bb4b4bcSBenoît Thébaudeau 	if (!mmc || mmc_init(mmc))
135340242bc3SŁukasz Majewski 		return NULL;
1354272cc70bSAndy Fleming 
135540242bc3SŁukasz Majewski 	return &mmc->block_dev;
1356272cc70bSAndy Fleming }
1357df3fc526SMatthew McClintock #endif
1358272cc70bSAndy Fleming 
135995de9ab2SPaul Kocialkowski /* board-specific MMC power initializations. */
136095de9ab2SPaul Kocialkowski __weak void board_mmc_power_init(void)
136195de9ab2SPaul Kocialkowski {
136295de9ab2SPaul Kocialkowski }
136395de9ab2SPaul Kocialkowski 
1364e9550449SChe-Liang Chiou int mmc_start_init(struct mmc *mmc)
1365272cc70bSAndy Fleming {
1366afd5932bSMacpaul Lin 	int err;
1367272cc70bSAndy Fleming 
1368ab769f22SPantelis Antoniou 	/* we pretend there's no card when init is NULL */
136993bfd616SPantelis Antoniou 	if (mmc_getcd(mmc) == 0 || mmc->cfg->ops->init == NULL) {
137048972d90SThierry Reding 		mmc->has_init = 0;
137156196826SPaul Burton #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
137248972d90SThierry Reding 		printf("MMC: no card present\n");
137356196826SPaul Burton #endif
137448972d90SThierry Reding 		return NO_CARD_ERR;
137548972d90SThierry Reding 	}
137648972d90SThierry Reding 
1377bc897b1dSLei Wen 	if (mmc->has_init)
1378bc897b1dSLei Wen 		return 0;
1379bc897b1dSLei Wen 
138095de9ab2SPaul Kocialkowski 	board_mmc_power_init();
138195de9ab2SPaul Kocialkowski 
1382ab769f22SPantelis Antoniou 	/* made sure it's not NULL earlier */
138393bfd616SPantelis Antoniou 	err = mmc->cfg->ops->init(mmc);
1384272cc70bSAndy Fleming 
1385272cc70bSAndy Fleming 	if (err)
1386272cc70bSAndy Fleming 		return err;
1387272cc70bSAndy Fleming 
1388786e8f81SAndrew Gabbasov 	mmc->ddr_mode = 0;
1389b86b85e2SIlya Yanok 	mmc_set_bus_width(mmc, 1);
1390b86b85e2SIlya Yanok 	mmc_set_clock(mmc, 1);
1391b86b85e2SIlya Yanok 
1392272cc70bSAndy Fleming 	/* Reset the Card */
1393272cc70bSAndy Fleming 	err = mmc_go_idle(mmc);
1394272cc70bSAndy Fleming 
1395272cc70bSAndy Fleming 	if (err)
1396272cc70bSAndy Fleming 		return err;
1397272cc70bSAndy Fleming 
1398bc897b1dSLei Wen 	/* The internal partition reset to user partition(0) at every CMD0*/
1399bc897b1dSLei Wen 	mmc->part_num = 0;
1400bc897b1dSLei Wen 
1401272cc70bSAndy Fleming 	/* Test for SD version 2 */
1402272cc70bSAndy Fleming 	err = mmc_send_if_cond(mmc);
1403272cc70bSAndy Fleming 
1404272cc70bSAndy Fleming 	/* Now try to get the SD card's operating condition */
1405272cc70bSAndy Fleming 	err = sd_send_op_cond(mmc);
1406272cc70bSAndy Fleming 
1407272cc70bSAndy Fleming 	/* If the command timed out, we check for an MMC card */
1408272cc70bSAndy Fleming 	if (err == TIMEOUT) {
1409272cc70bSAndy Fleming 		err = mmc_send_op_cond(mmc);
1410272cc70bSAndy Fleming 
1411e9550449SChe-Liang Chiou 		if (err && err != IN_PROGRESS) {
141256196826SPaul Burton #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
1413272cc70bSAndy Fleming 			printf("Card did not respond to voltage select!\n");
141456196826SPaul Burton #endif
1415272cc70bSAndy Fleming 			return UNUSABLE_ERR;
1416272cc70bSAndy Fleming 		}
1417272cc70bSAndy Fleming 	}
1418272cc70bSAndy Fleming 
1419e9550449SChe-Liang Chiou 	if (err == IN_PROGRESS)
1420e9550449SChe-Liang Chiou 		mmc->init_in_progress = 1;
1421e9550449SChe-Liang Chiou 
1422e9550449SChe-Liang Chiou 	return err;
1423e9550449SChe-Liang Chiou }
1424e9550449SChe-Liang Chiou 
1425e9550449SChe-Liang Chiou static int mmc_complete_init(struct mmc *mmc)
1426e9550449SChe-Liang Chiou {
1427e9550449SChe-Liang Chiou 	int err = 0;
1428e9550449SChe-Liang Chiou 
1429e9550449SChe-Liang Chiou 	if (mmc->op_cond_pending)
1430e9550449SChe-Liang Chiou 		err = mmc_complete_op_cond(mmc);
1431e9550449SChe-Liang Chiou 
1432e9550449SChe-Liang Chiou 	if (!err)
1433bc897b1dSLei Wen 		err = mmc_startup(mmc);
1434bc897b1dSLei Wen 	if (err)
1435bc897b1dSLei Wen 		mmc->has_init = 0;
1436bc897b1dSLei Wen 	else
1437bc897b1dSLei Wen 		mmc->has_init = 1;
1438e9550449SChe-Liang Chiou 	mmc->init_in_progress = 0;
1439e9550449SChe-Liang Chiou 	return err;
1440e9550449SChe-Liang Chiou }
1441e9550449SChe-Liang Chiou 
1442e9550449SChe-Liang Chiou int mmc_init(struct mmc *mmc)
1443e9550449SChe-Liang Chiou {
1444e9550449SChe-Liang Chiou 	int err = IN_PROGRESS;
1445d803fea5SMateusz Zalega 	unsigned start;
1446e9550449SChe-Liang Chiou 
1447e9550449SChe-Liang Chiou 	if (mmc->has_init)
1448e9550449SChe-Liang Chiou 		return 0;
1449d803fea5SMateusz Zalega 
1450d803fea5SMateusz Zalega 	start = get_timer(0);
1451d803fea5SMateusz Zalega 
1452e9550449SChe-Liang Chiou 	if (!mmc->init_in_progress)
1453e9550449SChe-Liang Chiou 		err = mmc_start_init(mmc);
1454e9550449SChe-Liang Chiou 
1455e9550449SChe-Liang Chiou 	if (!err || err == IN_PROGRESS)
1456e9550449SChe-Liang Chiou 		err = mmc_complete_init(mmc);
1457e9550449SChe-Liang Chiou 	debug("%s: %d, time %lu\n", __func__, err, get_timer(start));
1458bc897b1dSLei Wen 	return err;
1459272cc70bSAndy Fleming }
1460272cc70bSAndy Fleming 
1461ab71188cSMarkus Niebel int mmc_set_dsr(struct mmc *mmc, u16 val)
1462ab71188cSMarkus Niebel {
1463ab71188cSMarkus Niebel 	mmc->dsr = val;
1464ab71188cSMarkus Niebel 	return 0;
1465ab71188cSMarkus Niebel }
1466ab71188cSMarkus Niebel 
1467cee9ab7cSJeroen Hofstee /* CPU-specific MMC initializations */
1468cee9ab7cSJeroen Hofstee __weak int cpu_mmc_init(bd_t *bis)
1469272cc70bSAndy Fleming {
1470272cc70bSAndy Fleming 	return -1;
1471272cc70bSAndy Fleming }
1472272cc70bSAndy Fleming 
1473cee9ab7cSJeroen Hofstee /* board-specific MMC initializations. */
1474cee9ab7cSJeroen Hofstee __weak int board_mmc_init(bd_t *bis)
1475cee9ab7cSJeroen Hofstee {
1476cee9ab7cSJeroen Hofstee 	return -1;
1477cee9ab7cSJeroen Hofstee }
1478272cc70bSAndy Fleming 
147956196826SPaul Burton #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
148056196826SPaul Burton 
1481272cc70bSAndy Fleming void print_mmc_devices(char separator)
1482272cc70bSAndy Fleming {
1483272cc70bSAndy Fleming 	struct mmc *m;
1484272cc70bSAndy Fleming 	struct list_head *entry;
1485272cc70bSAndy Fleming 
1486272cc70bSAndy Fleming 	list_for_each(entry, &mmc_devices) {
1487272cc70bSAndy Fleming 		m = list_entry(entry, struct mmc, link);
1488272cc70bSAndy Fleming 
148993bfd616SPantelis Antoniou 		printf("%s: %d", m->cfg->name, m->block_dev.dev);
1490272cc70bSAndy Fleming 
1491e75eaf10SLubomir Popov 		if (entry->next != &mmc_devices) {
1492272cc70bSAndy Fleming 			printf("%c", separator);
1493e75eaf10SLubomir Popov 			if (separator != '\n')
1494e75eaf10SLubomir Popov 				puts (" ");
1495e75eaf10SLubomir Popov 		}
1496272cc70bSAndy Fleming 	}
1497272cc70bSAndy Fleming 
1498272cc70bSAndy Fleming 	printf("\n");
1499272cc70bSAndy Fleming }
1500272cc70bSAndy Fleming 
150156196826SPaul Burton #else
150256196826SPaul Burton void print_mmc_devices(char separator) { }
150356196826SPaul Burton #endif
150456196826SPaul Burton 
1505ea6ebe21SLei Wen int get_mmc_num(void)
1506ea6ebe21SLei Wen {
1507ea6ebe21SLei Wen 	return cur_dev_num;
1508ea6ebe21SLei Wen }
1509ea6ebe21SLei Wen 
1510e9550449SChe-Liang Chiou void mmc_set_preinit(struct mmc *mmc, int preinit)
1511e9550449SChe-Liang Chiou {
1512e9550449SChe-Liang Chiou 	mmc->preinit = preinit;
1513e9550449SChe-Liang Chiou }
1514e9550449SChe-Liang Chiou 
1515e9550449SChe-Liang Chiou static void do_preinit(void)
1516e9550449SChe-Liang Chiou {
1517e9550449SChe-Liang Chiou 	struct mmc *m;
1518e9550449SChe-Liang Chiou 	struct list_head *entry;
1519e9550449SChe-Liang Chiou 
1520e9550449SChe-Liang Chiou 	list_for_each(entry, &mmc_devices) {
1521e9550449SChe-Liang Chiou 		m = list_entry(entry, struct mmc, link);
1522e9550449SChe-Liang Chiou 
1523e9550449SChe-Liang Chiou 		if (m->preinit)
1524e9550449SChe-Liang Chiou 			mmc_start_init(m);
1525e9550449SChe-Liang Chiou 	}
1526e9550449SChe-Liang Chiou }
1527e9550449SChe-Liang Chiou 
1528e9550449SChe-Liang Chiou 
1529272cc70bSAndy Fleming int mmc_initialize(bd_t *bis)
1530272cc70bSAndy Fleming {
1531272cc70bSAndy Fleming 	INIT_LIST_HEAD (&mmc_devices);
1532272cc70bSAndy Fleming 	cur_dev_num = 0;
1533272cc70bSAndy Fleming 
1534272cc70bSAndy Fleming 	if (board_mmc_init(bis) < 0)
1535272cc70bSAndy Fleming 		cpu_mmc_init(bis);
1536272cc70bSAndy Fleming 
1537bb0dc108SYing Zhang #ifndef CONFIG_SPL_BUILD
1538272cc70bSAndy Fleming 	print_mmc_devices(',');
1539bb0dc108SYing Zhang #endif
1540272cc70bSAndy Fleming 
1541e9550449SChe-Liang Chiou 	do_preinit();
1542272cc70bSAndy Fleming 	return 0;
1543272cc70bSAndy Fleming }
15443690d6d6SAmar 
15453690d6d6SAmar #ifdef CONFIG_SUPPORT_EMMC_BOOT
15463690d6d6SAmar /*
15473690d6d6SAmar  * This function changes the size of boot partition and the size of rpmb
15483690d6d6SAmar  * partition present on EMMC devices.
15493690d6d6SAmar  *
15503690d6d6SAmar  * Input Parameters:
15513690d6d6SAmar  * struct *mmc: pointer for the mmc device strcuture
15523690d6d6SAmar  * bootsize: size of boot partition
15533690d6d6SAmar  * rpmbsize: size of rpmb partition
15543690d6d6SAmar  *
15553690d6d6SAmar  * Returns 0 on success.
15563690d6d6SAmar  */
15573690d6d6SAmar 
15583690d6d6SAmar int mmc_boot_partition_size_change(struct mmc *mmc, unsigned long bootsize,
15593690d6d6SAmar 				unsigned long rpmbsize)
15603690d6d6SAmar {
15613690d6d6SAmar 	int err;
15623690d6d6SAmar 	struct mmc_cmd cmd;
15633690d6d6SAmar 
15643690d6d6SAmar 	/* Only use this command for raw EMMC moviNAND. Enter backdoor mode */
15653690d6d6SAmar 	cmd.cmdidx = MMC_CMD_RES_MAN;
15663690d6d6SAmar 	cmd.resp_type = MMC_RSP_R1b;
15673690d6d6SAmar 	cmd.cmdarg = MMC_CMD62_ARG1;
15683690d6d6SAmar 
15693690d6d6SAmar 	err = mmc_send_cmd(mmc, &cmd, NULL);
15703690d6d6SAmar 	if (err) {
15713690d6d6SAmar 		debug("mmc_boot_partition_size_change: Error1 = %d\n", err);
15723690d6d6SAmar 		return err;
15733690d6d6SAmar 	}
15743690d6d6SAmar 
15753690d6d6SAmar 	/* Boot partition changing mode */
15763690d6d6SAmar 	cmd.cmdidx = MMC_CMD_RES_MAN;
15773690d6d6SAmar 	cmd.resp_type = MMC_RSP_R1b;
15783690d6d6SAmar 	cmd.cmdarg = MMC_CMD62_ARG2;
15793690d6d6SAmar 
15803690d6d6SAmar 	err = mmc_send_cmd(mmc, &cmd, NULL);
15813690d6d6SAmar 	if (err) {
15823690d6d6SAmar 		debug("mmc_boot_partition_size_change: Error2 = %d\n", err);
15833690d6d6SAmar 		return err;
15843690d6d6SAmar 	}
15853690d6d6SAmar 	/* boot partition size is multiple of 128KB */
15863690d6d6SAmar 	bootsize = (bootsize * 1024) / 128;
15873690d6d6SAmar 
15883690d6d6SAmar 	/* Arg: boot partition size */
15893690d6d6SAmar 	cmd.cmdidx = MMC_CMD_RES_MAN;
15903690d6d6SAmar 	cmd.resp_type = MMC_RSP_R1b;
15913690d6d6SAmar 	cmd.cmdarg = bootsize;
15923690d6d6SAmar 
15933690d6d6SAmar 	err = mmc_send_cmd(mmc, &cmd, NULL);
15943690d6d6SAmar 	if (err) {
15953690d6d6SAmar 		debug("mmc_boot_partition_size_change: Error3 = %d\n", err);
15963690d6d6SAmar 		return err;
15973690d6d6SAmar 	}
15983690d6d6SAmar 	/* RPMB partition size is multiple of 128KB */
15993690d6d6SAmar 	rpmbsize = (rpmbsize * 1024) / 128;
16003690d6d6SAmar 	/* Arg: RPMB partition size */
16013690d6d6SAmar 	cmd.cmdidx = MMC_CMD_RES_MAN;
16023690d6d6SAmar 	cmd.resp_type = MMC_RSP_R1b;
16033690d6d6SAmar 	cmd.cmdarg = rpmbsize;
16043690d6d6SAmar 
16053690d6d6SAmar 	err = mmc_send_cmd(mmc, &cmd, NULL);
16063690d6d6SAmar 	if (err) {
16073690d6d6SAmar 		debug("mmc_boot_partition_size_change: Error4 = %d\n", err);
16083690d6d6SAmar 		return err;
16093690d6d6SAmar 	}
16103690d6d6SAmar 	return 0;
16113690d6d6SAmar }
16123690d6d6SAmar 
16133690d6d6SAmar /*
16145a99b9deSTom Rini  * Modify EXT_CSD[177] which is BOOT_BUS_WIDTH
16155a99b9deSTom Rini  * based on the passed in values for BOOT_BUS_WIDTH, RESET_BOOT_BUS_WIDTH
16165a99b9deSTom Rini  * and BOOT_MODE.
16175a99b9deSTom Rini  *
16185a99b9deSTom Rini  * Returns 0 on success.
16195a99b9deSTom Rini  */
16205a99b9deSTom Rini int mmc_set_boot_bus_width(struct mmc *mmc, u8 width, u8 reset, u8 mode)
16215a99b9deSTom Rini {
16225a99b9deSTom Rini 	int err;
16235a99b9deSTom Rini 
16245a99b9deSTom Rini 	err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_BUS_WIDTH,
16255a99b9deSTom Rini 			 EXT_CSD_BOOT_BUS_WIDTH_MODE(mode) |
16265a99b9deSTom Rini 			 EXT_CSD_BOOT_BUS_WIDTH_RESET(reset) |
16275a99b9deSTom Rini 			 EXT_CSD_BOOT_BUS_WIDTH_WIDTH(width));
16285a99b9deSTom Rini 
16295a99b9deSTom Rini 	if (err)
16305a99b9deSTom Rini 		return err;
16315a99b9deSTom Rini 	return 0;
16325a99b9deSTom Rini }
16335a99b9deSTom Rini 
16345a99b9deSTom Rini /*
1635792970b0STom Rini  * Modify EXT_CSD[179] which is PARTITION_CONFIG (formerly BOOT_CONFIG)
1636792970b0STom Rini  * based on the passed in values for BOOT_ACK, BOOT_PARTITION_ENABLE and
1637792970b0STom Rini  * PARTITION_ACCESS.
1638792970b0STom Rini  *
1639792970b0STom Rini  * Returns 0 on success.
1640792970b0STom Rini  */
1641792970b0STom Rini int mmc_set_part_conf(struct mmc *mmc, u8 ack, u8 part_num, u8 access)
1642792970b0STom Rini {
1643792970b0STom Rini 	int err;
1644792970b0STom Rini 
1645792970b0STom Rini 	err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONF,
1646792970b0STom Rini 			 EXT_CSD_BOOT_ACK(ack) |
1647792970b0STom Rini 			 EXT_CSD_BOOT_PART_NUM(part_num) |
1648792970b0STom Rini 			 EXT_CSD_PARTITION_ACCESS(access));
1649792970b0STom Rini 
1650792970b0STom Rini 	if (err)
1651792970b0STom Rini 		return err;
1652792970b0STom Rini 	return 0;
1653792970b0STom Rini }
165433ace362STom Rini 
165533ace362STom Rini /*
165633ace362STom Rini  * Modify EXT_CSD[162] which is RST_n_FUNCTION based on the given value
165733ace362STom Rini  * for enable.  Note that this is a write-once field for non-zero values.
165833ace362STom Rini  *
165933ace362STom Rini  * Returns 0 on success.
166033ace362STom Rini  */
166133ace362STom Rini int mmc_set_rst_n_function(struct mmc *mmc, u8 enable)
166233ace362STom Rini {
166333ace362STom Rini 	return mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_RST_N_FUNCTION,
166433ace362STom Rini 			  enable);
166533ace362STom Rini }
16663690d6d6SAmar #endif
1667