xref: /openbmc/u-boot/drivers/mmc/mmc.c (revision 786e8f818c25265d12d5d06e257fe2b1ba516134)
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 
162*786e8f81SAndrew 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 
489*786e8f81SAndrew 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) {
522d22e3d46SJaehoon Chung 		if (cardtype & EXT_CSD_CARD_TYPE_DDR_52)
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;
821272cc70bSAndy Fleming 
822d52ebf10SThomas Chou #ifdef CONFIG_MMC_SPI_CRC_ON
823d52ebf10SThomas Chou 	if (mmc_host_is_spi(mmc)) { /* enable CRC check for spi */
824d52ebf10SThomas Chou 		cmd.cmdidx = MMC_CMD_SPI_CRC_ON_OFF;
825d52ebf10SThomas Chou 		cmd.resp_type = MMC_RSP_R1;
826d52ebf10SThomas Chou 		cmd.cmdarg = 1;
827d52ebf10SThomas Chou 		err = mmc_send_cmd(mmc, &cmd, NULL);
828d52ebf10SThomas Chou 
829d52ebf10SThomas Chou 		if (err)
830d52ebf10SThomas Chou 			return err;
831d52ebf10SThomas Chou 	}
832d52ebf10SThomas Chou #endif
833d52ebf10SThomas Chou 
834272cc70bSAndy Fleming 	/* Put the Card in Identify Mode */
835d52ebf10SThomas Chou 	cmd.cmdidx = mmc_host_is_spi(mmc) ? MMC_CMD_SEND_CID :
836d52ebf10SThomas Chou 		MMC_CMD_ALL_SEND_CID; /* cmd not supported in spi */
837272cc70bSAndy Fleming 	cmd.resp_type = MMC_RSP_R2;
838272cc70bSAndy Fleming 	cmd.cmdarg = 0;
839272cc70bSAndy Fleming 
840272cc70bSAndy Fleming 	err = mmc_send_cmd(mmc, &cmd, NULL);
841272cc70bSAndy Fleming 
842272cc70bSAndy Fleming 	if (err)
843272cc70bSAndy Fleming 		return err;
844272cc70bSAndy Fleming 
845272cc70bSAndy Fleming 	memcpy(mmc->cid, cmd.response, 16);
846272cc70bSAndy Fleming 
847272cc70bSAndy Fleming 	/*
848272cc70bSAndy Fleming 	 * For MMC cards, set the Relative Address.
849272cc70bSAndy Fleming 	 * For SD cards, get the Relatvie Address.
850272cc70bSAndy Fleming 	 * This also puts the cards into Standby State
851272cc70bSAndy Fleming 	 */
852d52ebf10SThomas Chou 	if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
853272cc70bSAndy Fleming 		cmd.cmdidx = SD_CMD_SEND_RELATIVE_ADDR;
854272cc70bSAndy Fleming 		cmd.cmdarg = mmc->rca << 16;
855272cc70bSAndy Fleming 		cmd.resp_type = MMC_RSP_R6;
856272cc70bSAndy Fleming 
857272cc70bSAndy Fleming 		err = mmc_send_cmd(mmc, &cmd, NULL);
858272cc70bSAndy Fleming 
859272cc70bSAndy Fleming 		if (err)
860272cc70bSAndy Fleming 			return err;
861272cc70bSAndy Fleming 
862272cc70bSAndy Fleming 		if (IS_SD(mmc))
863998be3ddSRabin Vincent 			mmc->rca = (cmd.response[0] >> 16) & 0xffff;
864d52ebf10SThomas Chou 	}
865272cc70bSAndy Fleming 
866272cc70bSAndy Fleming 	/* Get the Card-Specific Data */
867272cc70bSAndy Fleming 	cmd.cmdidx = MMC_CMD_SEND_CSD;
868272cc70bSAndy Fleming 	cmd.resp_type = MMC_RSP_R2;
869272cc70bSAndy Fleming 	cmd.cmdarg = mmc->rca << 16;
870272cc70bSAndy Fleming 
871272cc70bSAndy Fleming 	err = mmc_send_cmd(mmc, &cmd, NULL);
872272cc70bSAndy Fleming 
8735d4fc8d9SRaffaele Recalcati 	/* Waiting for the ready status */
8745d4fc8d9SRaffaele Recalcati 	mmc_send_status(mmc, timeout);
8755d4fc8d9SRaffaele Recalcati 
876272cc70bSAndy Fleming 	if (err)
877272cc70bSAndy Fleming 		return err;
878272cc70bSAndy Fleming 
879998be3ddSRabin Vincent 	mmc->csd[0] = cmd.response[0];
880998be3ddSRabin Vincent 	mmc->csd[1] = cmd.response[1];
881998be3ddSRabin Vincent 	mmc->csd[2] = cmd.response[2];
882998be3ddSRabin Vincent 	mmc->csd[3] = cmd.response[3];
883272cc70bSAndy Fleming 
884272cc70bSAndy Fleming 	if (mmc->version == MMC_VERSION_UNKNOWN) {
8850b453ffeSRabin Vincent 		int version = (cmd.response[0] >> 26) & 0xf;
886272cc70bSAndy Fleming 
887272cc70bSAndy Fleming 		switch (version) {
888272cc70bSAndy Fleming 			case 0:
889272cc70bSAndy Fleming 				mmc->version = MMC_VERSION_1_2;
890272cc70bSAndy Fleming 				break;
891272cc70bSAndy Fleming 			case 1:
892272cc70bSAndy Fleming 				mmc->version = MMC_VERSION_1_4;
893272cc70bSAndy Fleming 				break;
894272cc70bSAndy Fleming 			case 2:
895272cc70bSAndy Fleming 				mmc->version = MMC_VERSION_2_2;
896272cc70bSAndy Fleming 				break;
897272cc70bSAndy Fleming 			case 3:
898272cc70bSAndy Fleming 				mmc->version = MMC_VERSION_3;
899272cc70bSAndy Fleming 				break;
900272cc70bSAndy Fleming 			case 4:
901272cc70bSAndy Fleming 				mmc->version = MMC_VERSION_4;
902272cc70bSAndy Fleming 				break;
903272cc70bSAndy Fleming 			default:
904272cc70bSAndy Fleming 				mmc->version = MMC_VERSION_1_2;
905272cc70bSAndy Fleming 				break;
906272cc70bSAndy Fleming 		}
907272cc70bSAndy Fleming 	}
908272cc70bSAndy Fleming 
909272cc70bSAndy Fleming 	/* divide frequency by 10, since the mults are 10x bigger */
9100b453ffeSRabin Vincent 	freq = fbase[(cmd.response[0] & 0x7)];
9110b453ffeSRabin Vincent 	mult = multipliers[((cmd.response[0] >> 3) & 0xf)];
912272cc70bSAndy Fleming 
913272cc70bSAndy Fleming 	mmc->tran_speed = freq * mult;
914272cc70bSAndy Fleming 
915ab71188cSMarkus Niebel 	mmc->dsr_imp = ((cmd.response[1] >> 12) & 0x1);
916998be3ddSRabin Vincent 	mmc->read_bl_len = 1 << ((cmd.response[1] >> 16) & 0xf);
917272cc70bSAndy Fleming 
918272cc70bSAndy Fleming 	if (IS_SD(mmc))
919272cc70bSAndy Fleming 		mmc->write_bl_len = mmc->read_bl_len;
920272cc70bSAndy Fleming 	else
921998be3ddSRabin Vincent 		mmc->write_bl_len = 1 << ((cmd.response[3] >> 22) & 0xf);
922272cc70bSAndy Fleming 
923272cc70bSAndy Fleming 	if (mmc->high_capacity) {
924272cc70bSAndy Fleming 		csize = (mmc->csd[1] & 0x3f) << 16
925272cc70bSAndy Fleming 			| (mmc->csd[2] & 0xffff0000) >> 16;
926272cc70bSAndy Fleming 		cmult = 8;
927272cc70bSAndy Fleming 	} else {
928272cc70bSAndy Fleming 		csize = (mmc->csd[1] & 0x3ff) << 2
929272cc70bSAndy Fleming 			| (mmc->csd[2] & 0xc0000000) >> 30;
930272cc70bSAndy Fleming 		cmult = (mmc->csd[2] & 0x00038000) >> 15;
931272cc70bSAndy Fleming 	}
932272cc70bSAndy Fleming 
933f866a46dSStephen Warren 	mmc->capacity_user = (csize + 1) << (cmult + 2);
934f866a46dSStephen Warren 	mmc->capacity_user *= mmc->read_bl_len;
935f866a46dSStephen Warren 	mmc->capacity_boot = 0;
936f866a46dSStephen Warren 	mmc->capacity_rpmb = 0;
937f866a46dSStephen Warren 	for (i = 0; i < 4; i++)
938f866a46dSStephen Warren 		mmc->capacity_gp[i] = 0;
939272cc70bSAndy Fleming 
9408bfa195eSSimon Glass 	if (mmc->read_bl_len > MMC_MAX_BLOCK_LEN)
9418bfa195eSSimon Glass 		mmc->read_bl_len = MMC_MAX_BLOCK_LEN;
942272cc70bSAndy Fleming 
9438bfa195eSSimon Glass 	if (mmc->write_bl_len > MMC_MAX_BLOCK_LEN)
9448bfa195eSSimon Glass 		mmc->write_bl_len = MMC_MAX_BLOCK_LEN;
945272cc70bSAndy Fleming 
946ab71188cSMarkus Niebel 	if ((mmc->dsr_imp) && (0xffffffff != mmc->dsr)) {
947ab71188cSMarkus Niebel 		cmd.cmdidx = MMC_CMD_SET_DSR;
948ab71188cSMarkus Niebel 		cmd.cmdarg = (mmc->dsr & 0xffff) << 16;
949ab71188cSMarkus Niebel 		cmd.resp_type = MMC_RSP_NONE;
950ab71188cSMarkus Niebel 		if (mmc_send_cmd(mmc, &cmd, NULL))
951ab71188cSMarkus Niebel 			printf("MMC: SET_DSR failed\n");
952ab71188cSMarkus Niebel 	}
953ab71188cSMarkus Niebel 
954272cc70bSAndy Fleming 	/* Select the card, and put it into Transfer Mode */
955d52ebf10SThomas Chou 	if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
956272cc70bSAndy Fleming 		cmd.cmdidx = MMC_CMD_SELECT_CARD;
957fe8f7066SAjay Bhargav 		cmd.resp_type = MMC_RSP_R1;
958272cc70bSAndy Fleming 		cmd.cmdarg = mmc->rca << 16;
959272cc70bSAndy Fleming 		err = mmc_send_cmd(mmc, &cmd, NULL);
960272cc70bSAndy Fleming 
961272cc70bSAndy Fleming 		if (err)
962272cc70bSAndy Fleming 			return err;
963d52ebf10SThomas Chou 	}
964272cc70bSAndy Fleming 
965e6f99a56SLei Wen 	/*
966e6f99a56SLei Wen 	 * For SD, its erase group is always one sector
967e6f99a56SLei Wen 	 */
968e6f99a56SLei Wen 	mmc->erase_grp_size = 1;
969bc897b1dSLei Wen 	mmc->part_config = MMCPART_NOAVAILABLE;
970d23e2c09SSukumar Ghorai 	if (!IS_SD(mmc) && (mmc->version >= MMC_VERSION_4)) {
971d23e2c09SSukumar Ghorai 		/* check  ext_csd version and capacity */
972d23e2c09SSukumar Ghorai 		err = mmc_send_ext_csd(mmc, ext_csd);
973fdbb873eSKim Phillips 		if (!err && (ext_csd[EXT_CSD_REV] >= 2)) {
974639b7827SYoshihiro Shimoda 			/*
975639b7827SYoshihiro Shimoda 			 * According to the JEDEC Standard, the value of
976639b7827SYoshihiro Shimoda 			 * ext_csd's capacity is valid if the value is more
977639b7827SYoshihiro Shimoda 			 * than 2GB
978639b7827SYoshihiro Shimoda 			 */
9790560db18SLei Wen 			capacity = ext_csd[EXT_CSD_SEC_CNT] << 0
9800560db18SLei Wen 					| ext_csd[EXT_CSD_SEC_CNT + 1] << 8
9810560db18SLei Wen 					| ext_csd[EXT_CSD_SEC_CNT + 2] << 16
9820560db18SLei Wen 					| ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
9838bfa195eSSimon Glass 			capacity *= MMC_MAX_BLOCK_LEN;
984b1f1e821SŁukasz Majewski 			if ((capacity >> 20) > 2 * 1024)
985f866a46dSStephen Warren 				mmc->capacity_user = capacity;
986d23e2c09SSukumar Ghorai 		}
987bc897b1dSLei Wen 
98864f4a619SJaehoon Chung 		switch (ext_csd[EXT_CSD_REV]) {
98964f4a619SJaehoon Chung 		case 1:
99064f4a619SJaehoon Chung 			mmc->version = MMC_VERSION_4_1;
99164f4a619SJaehoon Chung 			break;
99264f4a619SJaehoon Chung 		case 2:
99364f4a619SJaehoon Chung 			mmc->version = MMC_VERSION_4_2;
99464f4a619SJaehoon Chung 			break;
99564f4a619SJaehoon Chung 		case 3:
99664f4a619SJaehoon Chung 			mmc->version = MMC_VERSION_4_3;
99764f4a619SJaehoon Chung 			break;
99864f4a619SJaehoon Chung 		case 5:
99964f4a619SJaehoon Chung 			mmc->version = MMC_VERSION_4_41;
100064f4a619SJaehoon Chung 			break;
100164f4a619SJaehoon Chung 		case 6:
100264f4a619SJaehoon Chung 			mmc->version = MMC_VERSION_4_5;
100364f4a619SJaehoon Chung 			break;
1004edab723bSMarkus Niebel 		case 7:
1005edab723bSMarkus Niebel 			mmc->version = MMC_VERSION_5_0;
1006edab723bSMarkus Niebel 			break;
100764f4a619SJaehoon Chung 		}
100864f4a619SJaehoon Chung 
1009e6f99a56SLei Wen 		/*
10101937e5aaSOliver Metz 		 * Host needs to enable ERASE_GRP_DEF bit if device is
10111937e5aaSOliver Metz 		 * partitioned. This bit will be lost every time after a reset
10121937e5aaSOliver Metz 		 * or power off. This will affect erase size.
1013e6f99a56SLei Wen 		 */
10141937e5aaSOliver Metz 		if ((ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & PART_SUPPORT) &&
10151937e5aaSOliver Metz 		    (ext_csd[EXT_CSD_PARTITIONS_ATTRIBUTE] & PART_ENH_ATTRIB)) {
10161937e5aaSOliver Metz 			err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
10171937e5aaSOliver Metz 				EXT_CSD_ERASE_GROUP_DEF, 1);
10181937e5aaSOliver Metz 
10191937e5aaSOliver Metz 			if (err)
10201937e5aaSOliver Metz 				return err;
1021021a8055SHannes Petermaier 			else
1022021a8055SHannes Petermaier 				ext_csd[EXT_CSD_ERASE_GROUP_DEF] = 1;
10231937e5aaSOliver Metz 
10241937e5aaSOliver Metz 			/* Read out group size from ext_csd */
10250560db18SLei Wen 			mmc->erase_grp_size =
10268bfa195eSSimon Glass 				ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] *
10278bfa195eSSimon Glass 					MMC_MAX_BLOCK_LEN * 1024;
1028d7b29129SMarkus Niebel 			/*
1029d7b29129SMarkus Niebel 			 * if high capacity and partition setting completed
1030d7b29129SMarkus Niebel 			 * SEC_COUNT is valid even if it is smaller than 2 GiB
1031d7b29129SMarkus Niebel 			 * JEDEC Standard JESD84-B45, 6.2.4
1032d7b29129SMarkus Niebel 			 */
1033d7b29129SMarkus Niebel 			if (mmc->high_capacity &&
1034d7b29129SMarkus Niebel 			    (ext_csd[EXT_CSD_PARTITION_SETTING] &
1035d7b29129SMarkus Niebel 			     EXT_CSD_PARTITION_SETTING_COMPLETED)) {
1036d7b29129SMarkus Niebel 				capacity = (ext_csd[EXT_CSD_SEC_CNT]) |
1037d7b29129SMarkus Niebel 					(ext_csd[EXT_CSD_SEC_CNT + 1] << 8) |
1038d7b29129SMarkus Niebel 					(ext_csd[EXT_CSD_SEC_CNT + 2] << 16) |
1039d7b29129SMarkus Niebel 					(ext_csd[EXT_CSD_SEC_CNT + 3] << 24);
1040d7b29129SMarkus Niebel 				capacity *= MMC_MAX_BLOCK_LEN;
1041d7b29129SMarkus Niebel 				mmc->capacity_user = capacity;
1042d7b29129SMarkus Niebel 			}
10438bfa195eSSimon Glass 		} else {
10441937e5aaSOliver Metz 			/* Calculate the group size from the csd value. */
1045e6f99a56SLei Wen 			int erase_gsz, erase_gmul;
1046e6f99a56SLei Wen 			erase_gsz = (mmc->csd[2] & 0x00007c00) >> 10;
1047e6f99a56SLei Wen 			erase_gmul = (mmc->csd[2] & 0x000003e0) >> 5;
1048e6f99a56SLei Wen 			mmc->erase_grp_size = (erase_gsz + 1)
1049e6f99a56SLei Wen 				* (erase_gmul + 1);
1050e6f99a56SLei Wen 		}
1051e6f99a56SLei Wen 
1052bc897b1dSLei Wen 		/* store the partition info of emmc */
10538948ea83SStephen Warren 		if ((ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & PART_SUPPORT) ||
10548948ea83SStephen Warren 		    ext_csd[EXT_CSD_BOOT_MULT])
10550560db18SLei Wen 			mmc->part_config = ext_csd[EXT_CSD_PART_CONF];
1056f866a46dSStephen Warren 
1057f866a46dSStephen Warren 		mmc->capacity_boot = ext_csd[EXT_CSD_BOOT_MULT] << 17;
1058f866a46dSStephen Warren 
1059f866a46dSStephen Warren 		mmc->capacity_rpmb = ext_csd[EXT_CSD_RPMB_MULT] << 17;
1060f866a46dSStephen Warren 
1061f866a46dSStephen Warren 		for (i = 0; i < 4; i++) {
1062f866a46dSStephen Warren 			int idx = EXT_CSD_GP_SIZE_MULT + i * 3;
1063f866a46dSStephen Warren 			mmc->capacity_gp[i] = (ext_csd[idx + 2] << 16) +
1064f866a46dSStephen Warren 				(ext_csd[idx + 1] << 8) + ext_csd[idx];
1065f866a46dSStephen Warren 			mmc->capacity_gp[i] *=
1066f866a46dSStephen Warren 				ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
1067f866a46dSStephen Warren 			mmc->capacity_gp[i] *= ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
1068d23e2c09SSukumar Ghorai 		}
1069f866a46dSStephen Warren 	}
1070f866a46dSStephen Warren 
1071f866a46dSStephen Warren 	err = mmc_set_capacity(mmc, mmc->part_num);
1072f866a46dSStephen Warren 	if (err)
1073f866a46dSStephen Warren 		return err;
1074d23e2c09SSukumar Ghorai 
1075272cc70bSAndy Fleming 	if (IS_SD(mmc))
1076272cc70bSAndy Fleming 		err = sd_change_freq(mmc);
1077272cc70bSAndy Fleming 	else
1078272cc70bSAndy Fleming 		err = mmc_change_freq(mmc);
1079272cc70bSAndy Fleming 
1080272cc70bSAndy Fleming 	if (err)
1081272cc70bSAndy Fleming 		return err;
1082272cc70bSAndy Fleming 
1083272cc70bSAndy Fleming 	/* Restrict card's capabilities by what the host can do */
108493bfd616SPantelis Antoniou 	mmc->card_caps &= mmc->cfg->host_caps;
1085272cc70bSAndy Fleming 
1086272cc70bSAndy Fleming 	if (IS_SD(mmc)) {
1087272cc70bSAndy Fleming 		if (mmc->card_caps & MMC_MODE_4BIT) {
1088272cc70bSAndy Fleming 			cmd.cmdidx = MMC_CMD_APP_CMD;
1089272cc70bSAndy Fleming 			cmd.resp_type = MMC_RSP_R1;
1090272cc70bSAndy Fleming 			cmd.cmdarg = mmc->rca << 16;
1091272cc70bSAndy Fleming 
1092272cc70bSAndy Fleming 			err = mmc_send_cmd(mmc, &cmd, NULL);
1093272cc70bSAndy Fleming 			if (err)
1094272cc70bSAndy Fleming 				return err;
1095272cc70bSAndy Fleming 
1096272cc70bSAndy Fleming 			cmd.cmdidx = SD_CMD_APP_SET_BUS_WIDTH;
1097272cc70bSAndy Fleming 			cmd.resp_type = MMC_RSP_R1;
1098272cc70bSAndy Fleming 			cmd.cmdarg = 2;
1099272cc70bSAndy Fleming 			err = mmc_send_cmd(mmc, &cmd, NULL);
1100272cc70bSAndy Fleming 			if (err)
1101272cc70bSAndy Fleming 				return err;
1102272cc70bSAndy Fleming 
1103272cc70bSAndy Fleming 			mmc_set_bus_width(mmc, 4);
1104272cc70bSAndy Fleming 		}
1105272cc70bSAndy Fleming 
1106272cc70bSAndy Fleming 		if (mmc->card_caps & MMC_MODE_HS)
1107ad5fd922SJaehoon Chung 			mmc->tran_speed = 50000000;
1108272cc70bSAndy Fleming 		else
1109ad5fd922SJaehoon Chung 			mmc->tran_speed = 25000000;
1110272cc70bSAndy Fleming 	} else {
11117798f6dbSAndy Fleming 		int idx;
11127798f6dbSAndy Fleming 
11137798f6dbSAndy Fleming 		/* An array of possible bus widths in order of preference */
11147798f6dbSAndy Fleming 		static unsigned ext_csd_bits[] = {
1115d22e3d46SJaehoon Chung 			EXT_CSD_DDR_BUS_WIDTH_8,
1116d22e3d46SJaehoon Chung 			EXT_CSD_DDR_BUS_WIDTH_4,
11177798f6dbSAndy Fleming 			EXT_CSD_BUS_WIDTH_8,
11187798f6dbSAndy Fleming 			EXT_CSD_BUS_WIDTH_4,
11197798f6dbSAndy Fleming 			EXT_CSD_BUS_WIDTH_1,
11207798f6dbSAndy Fleming 		};
11217798f6dbSAndy Fleming 
11227798f6dbSAndy Fleming 		/* An array to map CSD bus widths to host cap bits */
11237798f6dbSAndy Fleming 		static unsigned ext_to_hostcaps[] = {
1124*786e8f81SAndrew Gabbasov 			[EXT_CSD_DDR_BUS_WIDTH_4] =
1125*786e8f81SAndrew Gabbasov 				MMC_MODE_DDR_52MHz | MMC_MODE_4BIT,
1126*786e8f81SAndrew Gabbasov 			[EXT_CSD_DDR_BUS_WIDTH_8] =
1127*786e8f81SAndrew Gabbasov 				MMC_MODE_DDR_52MHz | MMC_MODE_8BIT,
11287798f6dbSAndy Fleming 			[EXT_CSD_BUS_WIDTH_4] = MMC_MODE_4BIT,
11297798f6dbSAndy Fleming 			[EXT_CSD_BUS_WIDTH_8] = MMC_MODE_8BIT,
11307798f6dbSAndy Fleming 		};
11317798f6dbSAndy Fleming 
11327798f6dbSAndy Fleming 		/* An array to map chosen bus width to an integer */
11337798f6dbSAndy Fleming 		static unsigned widths[] = {
1134d22e3d46SJaehoon Chung 			8, 4, 8, 4, 1,
11357798f6dbSAndy Fleming 		};
11367798f6dbSAndy Fleming 
11377798f6dbSAndy Fleming 		for (idx=0; idx < ARRAY_SIZE(ext_csd_bits); idx++) {
11387798f6dbSAndy Fleming 			unsigned int extw = ext_csd_bits[idx];
1139*786e8f81SAndrew Gabbasov 			unsigned int caps = ext_to_hostcaps[extw];
11407798f6dbSAndy Fleming 
11417798f6dbSAndy Fleming 			/*
1142*786e8f81SAndrew Gabbasov 			 * Check to make sure the card and controller support
1143*786e8f81SAndrew Gabbasov 			 * these capabilities
11447798f6dbSAndy Fleming 			 */
1145*786e8f81SAndrew Gabbasov 			if ((mmc->card_caps & caps) != caps)
11467798f6dbSAndy Fleming 				continue;
11477798f6dbSAndy Fleming 
1148272cc70bSAndy Fleming 			err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
11497798f6dbSAndy Fleming 					EXT_CSD_BUS_WIDTH, extw);
1150272cc70bSAndy Fleming 
1151272cc70bSAndy Fleming 			if (err)
11524137894eSLei Wen 				continue;
1153272cc70bSAndy Fleming 
1154*786e8f81SAndrew Gabbasov 			mmc->ddr_mode = (caps & MMC_MODE_DDR_52MHz) ? 1 : 0;
11557798f6dbSAndy Fleming 			mmc_set_bus_width(mmc, widths[idx]);
1156272cc70bSAndy Fleming 
11574137894eSLei Wen 			err = mmc_send_ext_csd(mmc, test_csd);
1158272cc70bSAndy Fleming 
1159*786e8f81SAndrew Gabbasov 			if (err)
1160*786e8f81SAndrew Gabbasov 				continue;
1161*786e8f81SAndrew Gabbasov 
1162*786e8f81SAndrew Gabbasov 			/* Only compare read only fields */
1163*786e8f81SAndrew Gabbasov 			if (ext_csd[EXT_CSD_PARTITIONING_SUPPORT]
1164*786e8f81SAndrew Gabbasov 				== test_csd[EXT_CSD_PARTITIONING_SUPPORT] &&
1165*786e8f81SAndrew Gabbasov 			    ext_csd[EXT_CSD_HC_WP_GRP_SIZE]
1166*786e8f81SAndrew Gabbasov 				== test_csd[EXT_CSD_HC_WP_GRP_SIZE] &&
1167*786e8f81SAndrew Gabbasov 			    ext_csd[EXT_CSD_REV]
1168*786e8f81SAndrew Gabbasov 				== test_csd[EXT_CSD_REV] &&
1169*786e8f81SAndrew Gabbasov 			    ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]
1170*786e8f81SAndrew Gabbasov 				== test_csd[EXT_CSD_HC_ERASE_GRP_SIZE] &&
1171*786e8f81SAndrew Gabbasov 			    memcmp(&ext_csd[EXT_CSD_SEC_CNT],
1172*786e8f81SAndrew Gabbasov 				   &test_csd[EXT_CSD_SEC_CNT], 4) == 0)
11734137894eSLei Wen 				break;
1174*786e8f81SAndrew Gabbasov 			else
1175*786e8f81SAndrew Gabbasov 				err = SWITCH_ERR;
11764137894eSLei Wen 		}
1177*786e8f81SAndrew Gabbasov 
1178*786e8f81SAndrew Gabbasov 		if (err)
1179*786e8f81SAndrew Gabbasov 			return err;
1180272cc70bSAndy Fleming 
1181272cc70bSAndy Fleming 		if (mmc->card_caps & MMC_MODE_HS) {
1182272cc70bSAndy Fleming 			if (mmc->card_caps & MMC_MODE_HS_52MHz)
1183ad5fd922SJaehoon Chung 				mmc->tran_speed = 52000000;
1184272cc70bSAndy Fleming 			else
1185ad5fd922SJaehoon Chung 				mmc->tran_speed = 26000000;
1186272cc70bSAndy Fleming 		}
1187ad5fd922SJaehoon Chung 	}
1188ad5fd922SJaehoon Chung 
1189ad5fd922SJaehoon Chung 	mmc_set_clock(mmc, mmc->tran_speed);
1190272cc70bSAndy Fleming 
1191272cc70bSAndy Fleming 	/* fill in device description */
1192272cc70bSAndy Fleming 	mmc->block_dev.lun = 0;
1193272cc70bSAndy Fleming 	mmc->block_dev.type = 0;
1194272cc70bSAndy Fleming 	mmc->block_dev.blksz = mmc->read_bl_len;
11950472fbfdSEgbert Eich 	mmc->block_dev.log2blksz = LOG2(mmc->block_dev.blksz);
11969b1f942cSRabin Vincent 	mmc->block_dev.lba = lldiv(mmc->capacity, mmc->read_bl_len);
119756196826SPaul Burton #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
1198babce5f6STaylor Hutt 	sprintf(mmc->block_dev.vendor, "Man %06x Snr %04x%04x",
1199babce5f6STaylor Hutt 		mmc->cid[0] >> 24, (mmc->cid[2] & 0xffff),
1200babce5f6STaylor Hutt 		(mmc->cid[3] >> 16) & 0xffff);
1201babce5f6STaylor Hutt 	sprintf(mmc->block_dev.product, "%c%c%c%c%c%c", mmc->cid[0] & 0xff,
12020b453ffeSRabin Vincent 		(mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
1203babce5f6STaylor Hutt 		(mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff,
1204babce5f6STaylor Hutt 		(mmc->cid[2] >> 24) & 0xff);
1205babce5f6STaylor Hutt 	sprintf(mmc->block_dev.revision, "%d.%d", (mmc->cid[2] >> 20) & 0xf,
1206babce5f6STaylor Hutt 		(mmc->cid[2] >> 16) & 0xf);
120756196826SPaul Burton #else
120856196826SPaul Burton 	mmc->block_dev.vendor[0] = 0;
120956196826SPaul Burton 	mmc->block_dev.product[0] = 0;
121056196826SPaul Burton 	mmc->block_dev.revision[0] = 0;
121156196826SPaul Burton #endif
1212122efd43SMikhail Kshevetskiy #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBDISK_SUPPORT)
1213272cc70bSAndy Fleming 	init_part(&mmc->block_dev);
1214122efd43SMikhail Kshevetskiy #endif
1215272cc70bSAndy Fleming 
1216272cc70bSAndy Fleming 	return 0;
1217272cc70bSAndy Fleming }
1218272cc70bSAndy Fleming 
1219fdbb873eSKim Phillips static int mmc_send_if_cond(struct mmc *mmc)
1220272cc70bSAndy Fleming {
1221272cc70bSAndy Fleming 	struct mmc_cmd cmd;
1222272cc70bSAndy Fleming 	int err;
1223272cc70bSAndy Fleming 
1224272cc70bSAndy Fleming 	cmd.cmdidx = SD_CMD_SEND_IF_COND;
1225272cc70bSAndy Fleming 	/* We set the bit if the host supports voltages between 2.7 and 3.6 V */
122693bfd616SPantelis Antoniou 	cmd.cmdarg = ((mmc->cfg->voltages & 0xff8000) != 0) << 8 | 0xaa;
1227272cc70bSAndy Fleming 	cmd.resp_type = MMC_RSP_R7;
1228272cc70bSAndy Fleming 
1229272cc70bSAndy Fleming 	err = mmc_send_cmd(mmc, &cmd, NULL);
1230272cc70bSAndy Fleming 
1231272cc70bSAndy Fleming 	if (err)
1232272cc70bSAndy Fleming 		return err;
1233272cc70bSAndy Fleming 
1234998be3ddSRabin Vincent 	if ((cmd.response[0] & 0xff) != 0xaa)
1235272cc70bSAndy Fleming 		return UNUSABLE_ERR;
1236272cc70bSAndy Fleming 	else
1237272cc70bSAndy Fleming 		mmc->version = SD_VERSION_2;
1238272cc70bSAndy Fleming 
1239272cc70bSAndy Fleming 	return 0;
1240272cc70bSAndy Fleming }
1241272cc70bSAndy Fleming 
124293bfd616SPantelis Antoniou /* not used any more */
124393bfd616SPantelis Antoniou int __deprecated mmc_register(struct mmc *mmc)
1244272cc70bSAndy Fleming {
124593bfd616SPantelis Antoniou #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
124693bfd616SPantelis Antoniou 	printf("%s is deprecated! use mmc_create() instead.\n", __func__);
124793bfd616SPantelis Antoniou #endif
124893bfd616SPantelis Antoniou 	return -1;
124993bfd616SPantelis Antoniou }
125093bfd616SPantelis Antoniou 
125193bfd616SPantelis Antoniou struct mmc *mmc_create(const struct mmc_config *cfg, void *priv)
125293bfd616SPantelis Antoniou {
125393bfd616SPantelis Antoniou 	struct mmc *mmc;
125493bfd616SPantelis Antoniou 
125593bfd616SPantelis Antoniou 	/* quick validation */
125693bfd616SPantelis Antoniou 	if (cfg == NULL || cfg->ops == NULL || cfg->ops->send_cmd == NULL ||
125793bfd616SPantelis Antoniou 			cfg->f_min == 0 || cfg->f_max == 0 || cfg->b_max == 0)
125893bfd616SPantelis Antoniou 		return NULL;
125993bfd616SPantelis Antoniou 
126093bfd616SPantelis Antoniou 	mmc = calloc(1, sizeof(*mmc));
126193bfd616SPantelis Antoniou 	if (mmc == NULL)
126293bfd616SPantelis Antoniou 		return NULL;
126393bfd616SPantelis Antoniou 
126493bfd616SPantelis Antoniou 	mmc->cfg = cfg;
126593bfd616SPantelis Antoniou 	mmc->priv = priv;
126693bfd616SPantelis Antoniou 
126793bfd616SPantelis Antoniou 	/* the following chunk was mmc_register() */
126893bfd616SPantelis Antoniou 
1269ab71188cSMarkus Niebel 	/* Setup dsr related values */
1270ab71188cSMarkus Niebel 	mmc->dsr_imp = 0;
1271ab71188cSMarkus Niebel 	mmc->dsr = 0xffffffff;
1272272cc70bSAndy Fleming 	/* Setup the universal parts of the block interface just once */
1273272cc70bSAndy Fleming 	mmc->block_dev.if_type = IF_TYPE_MMC;
1274272cc70bSAndy Fleming 	mmc->block_dev.dev = cur_dev_num++;
1275272cc70bSAndy Fleming 	mmc->block_dev.removable = 1;
1276272cc70bSAndy Fleming 	mmc->block_dev.block_read = mmc_bread;
1277272cc70bSAndy Fleming 	mmc->block_dev.block_write = mmc_bwrite;
1278e6f99a56SLei Wen 	mmc->block_dev.block_erase = mmc_berase;
127993bfd616SPantelis Antoniou 
128093bfd616SPantelis Antoniou 	/* setup initial part type */
128193bfd616SPantelis Antoniou 	mmc->block_dev.part_type = mmc->cfg->part_type;
1282272cc70bSAndy Fleming 
1283272cc70bSAndy Fleming 	INIT_LIST_HEAD(&mmc->link);
1284272cc70bSAndy Fleming 
1285272cc70bSAndy Fleming 	list_add_tail(&mmc->link, &mmc_devices);
1286272cc70bSAndy Fleming 
128793bfd616SPantelis Antoniou 	return mmc;
128893bfd616SPantelis Antoniou }
128993bfd616SPantelis Antoniou 
129093bfd616SPantelis Antoniou void mmc_destroy(struct mmc *mmc)
129193bfd616SPantelis Antoniou {
129293bfd616SPantelis Antoniou 	/* only freeing memory for now */
129393bfd616SPantelis Antoniou 	free(mmc);
1294272cc70bSAndy Fleming }
1295272cc70bSAndy Fleming 
1296df3fc526SMatthew McClintock #ifdef CONFIG_PARTITIONS
1297272cc70bSAndy Fleming block_dev_desc_t *mmc_get_dev(int dev)
1298272cc70bSAndy Fleming {
1299272cc70bSAndy Fleming 	struct mmc *mmc = find_mmc_device(dev);
13006bb4b4bcSBenoît Thébaudeau 	if (!mmc || mmc_init(mmc))
130140242bc3SŁukasz Majewski 		return NULL;
1302272cc70bSAndy Fleming 
130340242bc3SŁukasz Majewski 	return &mmc->block_dev;
1304272cc70bSAndy Fleming }
1305df3fc526SMatthew McClintock #endif
1306272cc70bSAndy Fleming 
130795de9ab2SPaul Kocialkowski /* board-specific MMC power initializations. */
130895de9ab2SPaul Kocialkowski __weak void board_mmc_power_init(void)
130995de9ab2SPaul Kocialkowski {
131095de9ab2SPaul Kocialkowski }
131195de9ab2SPaul Kocialkowski 
1312e9550449SChe-Liang Chiou int mmc_start_init(struct mmc *mmc)
1313272cc70bSAndy Fleming {
1314afd5932bSMacpaul Lin 	int err;
1315272cc70bSAndy Fleming 
1316ab769f22SPantelis Antoniou 	/* we pretend there's no card when init is NULL */
131793bfd616SPantelis Antoniou 	if (mmc_getcd(mmc) == 0 || mmc->cfg->ops->init == NULL) {
131848972d90SThierry Reding 		mmc->has_init = 0;
131956196826SPaul Burton #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
132048972d90SThierry Reding 		printf("MMC: no card present\n");
132156196826SPaul Burton #endif
132248972d90SThierry Reding 		return NO_CARD_ERR;
132348972d90SThierry Reding 	}
132448972d90SThierry Reding 
1325bc897b1dSLei Wen 	if (mmc->has_init)
1326bc897b1dSLei Wen 		return 0;
1327bc897b1dSLei Wen 
132895de9ab2SPaul Kocialkowski 	board_mmc_power_init();
132995de9ab2SPaul Kocialkowski 
1330ab769f22SPantelis Antoniou 	/* made sure it's not NULL earlier */
133193bfd616SPantelis Antoniou 	err = mmc->cfg->ops->init(mmc);
1332272cc70bSAndy Fleming 
1333272cc70bSAndy Fleming 	if (err)
1334272cc70bSAndy Fleming 		return err;
1335272cc70bSAndy Fleming 
1336*786e8f81SAndrew Gabbasov 	mmc->ddr_mode = 0;
1337b86b85e2SIlya Yanok 	mmc_set_bus_width(mmc, 1);
1338b86b85e2SIlya Yanok 	mmc_set_clock(mmc, 1);
1339b86b85e2SIlya Yanok 
1340272cc70bSAndy Fleming 	/* Reset the Card */
1341272cc70bSAndy Fleming 	err = mmc_go_idle(mmc);
1342272cc70bSAndy Fleming 
1343272cc70bSAndy Fleming 	if (err)
1344272cc70bSAndy Fleming 		return err;
1345272cc70bSAndy Fleming 
1346bc897b1dSLei Wen 	/* The internal partition reset to user partition(0) at every CMD0*/
1347bc897b1dSLei Wen 	mmc->part_num = 0;
1348bc897b1dSLei Wen 
1349272cc70bSAndy Fleming 	/* Test for SD version 2 */
1350272cc70bSAndy Fleming 	err = mmc_send_if_cond(mmc);
1351272cc70bSAndy Fleming 
1352272cc70bSAndy Fleming 	/* Now try to get the SD card's operating condition */
1353272cc70bSAndy Fleming 	err = sd_send_op_cond(mmc);
1354272cc70bSAndy Fleming 
1355272cc70bSAndy Fleming 	/* If the command timed out, we check for an MMC card */
1356272cc70bSAndy Fleming 	if (err == TIMEOUT) {
1357272cc70bSAndy Fleming 		err = mmc_send_op_cond(mmc);
1358272cc70bSAndy Fleming 
1359e9550449SChe-Liang Chiou 		if (err && err != IN_PROGRESS) {
136056196826SPaul Burton #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
1361272cc70bSAndy Fleming 			printf("Card did not respond to voltage select!\n");
136256196826SPaul Burton #endif
1363272cc70bSAndy Fleming 			return UNUSABLE_ERR;
1364272cc70bSAndy Fleming 		}
1365272cc70bSAndy Fleming 	}
1366272cc70bSAndy Fleming 
1367e9550449SChe-Liang Chiou 	if (err == IN_PROGRESS)
1368e9550449SChe-Liang Chiou 		mmc->init_in_progress = 1;
1369e9550449SChe-Liang Chiou 
1370e9550449SChe-Liang Chiou 	return err;
1371e9550449SChe-Liang Chiou }
1372e9550449SChe-Liang Chiou 
1373e9550449SChe-Liang Chiou static int mmc_complete_init(struct mmc *mmc)
1374e9550449SChe-Liang Chiou {
1375e9550449SChe-Liang Chiou 	int err = 0;
1376e9550449SChe-Liang Chiou 
1377e9550449SChe-Liang Chiou 	if (mmc->op_cond_pending)
1378e9550449SChe-Liang Chiou 		err = mmc_complete_op_cond(mmc);
1379e9550449SChe-Liang Chiou 
1380e9550449SChe-Liang Chiou 	if (!err)
1381bc897b1dSLei Wen 		err = mmc_startup(mmc);
1382bc897b1dSLei Wen 	if (err)
1383bc897b1dSLei Wen 		mmc->has_init = 0;
1384bc897b1dSLei Wen 	else
1385bc897b1dSLei Wen 		mmc->has_init = 1;
1386e9550449SChe-Liang Chiou 	mmc->init_in_progress = 0;
1387e9550449SChe-Liang Chiou 	return err;
1388e9550449SChe-Liang Chiou }
1389e9550449SChe-Liang Chiou 
1390e9550449SChe-Liang Chiou int mmc_init(struct mmc *mmc)
1391e9550449SChe-Liang Chiou {
1392e9550449SChe-Liang Chiou 	int err = IN_PROGRESS;
1393d803fea5SMateusz Zalega 	unsigned start;
1394e9550449SChe-Liang Chiou 
1395e9550449SChe-Liang Chiou 	if (mmc->has_init)
1396e9550449SChe-Liang Chiou 		return 0;
1397d803fea5SMateusz Zalega 
1398d803fea5SMateusz Zalega 	start = get_timer(0);
1399d803fea5SMateusz Zalega 
1400e9550449SChe-Liang Chiou 	if (!mmc->init_in_progress)
1401e9550449SChe-Liang Chiou 		err = mmc_start_init(mmc);
1402e9550449SChe-Liang Chiou 
1403e9550449SChe-Liang Chiou 	if (!err || err == IN_PROGRESS)
1404e9550449SChe-Liang Chiou 		err = mmc_complete_init(mmc);
1405e9550449SChe-Liang Chiou 	debug("%s: %d, time %lu\n", __func__, err, get_timer(start));
1406bc897b1dSLei Wen 	return err;
1407272cc70bSAndy Fleming }
1408272cc70bSAndy Fleming 
1409ab71188cSMarkus Niebel int mmc_set_dsr(struct mmc *mmc, u16 val)
1410ab71188cSMarkus Niebel {
1411ab71188cSMarkus Niebel 	mmc->dsr = val;
1412ab71188cSMarkus Niebel 	return 0;
1413ab71188cSMarkus Niebel }
1414ab71188cSMarkus Niebel 
1415cee9ab7cSJeroen Hofstee /* CPU-specific MMC initializations */
1416cee9ab7cSJeroen Hofstee __weak int cpu_mmc_init(bd_t *bis)
1417272cc70bSAndy Fleming {
1418272cc70bSAndy Fleming 	return -1;
1419272cc70bSAndy Fleming }
1420272cc70bSAndy Fleming 
1421cee9ab7cSJeroen Hofstee /* board-specific MMC initializations. */
1422cee9ab7cSJeroen Hofstee __weak int board_mmc_init(bd_t *bis)
1423cee9ab7cSJeroen Hofstee {
1424cee9ab7cSJeroen Hofstee 	return -1;
1425cee9ab7cSJeroen Hofstee }
1426272cc70bSAndy Fleming 
142756196826SPaul Burton #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
142856196826SPaul Burton 
1429272cc70bSAndy Fleming void print_mmc_devices(char separator)
1430272cc70bSAndy Fleming {
1431272cc70bSAndy Fleming 	struct mmc *m;
1432272cc70bSAndy Fleming 	struct list_head *entry;
1433272cc70bSAndy Fleming 
1434272cc70bSAndy Fleming 	list_for_each(entry, &mmc_devices) {
1435272cc70bSAndy Fleming 		m = list_entry(entry, struct mmc, link);
1436272cc70bSAndy Fleming 
143793bfd616SPantelis Antoniou 		printf("%s: %d", m->cfg->name, m->block_dev.dev);
1438272cc70bSAndy Fleming 
1439e75eaf10SLubomir Popov 		if (entry->next != &mmc_devices) {
1440272cc70bSAndy Fleming 			printf("%c", separator);
1441e75eaf10SLubomir Popov 			if (separator != '\n')
1442e75eaf10SLubomir Popov 				puts (" ");
1443e75eaf10SLubomir Popov 		}
1444272cc70bSAndy Fleming 	}
1445272cc70bSAndy Fleming 
1446272cc70bSAndy Fleming 	printf("\n");
1447272cc70bSAndy Fleming }
1448272cc70bSAndy Fleming 
144956196826SPaul Burton #else
145056196826SPaul Burton void print_mmc_devices(char separator) { }
145156196826SPaul Burton #endif
145256196826SPaul Burton 
1453ea6ebe21SLei Wen int get_mmc_num(void)
1454ea6ebe21SLei Wen {
1455ea6ebe21SLei Wen 	return cur_dev_num;
1456ea6ebe21SLei Wen }
1457ea6ebe21SLei Wen 
1458e9550449SChe-Liang Chiou void mmc_set_preinit(struct mmc *mmc, int preinit)
1459e9550449SChe-Liang Chiou {
1460e9550449SChe-Liang Chiou 	mmc->preinit = preinit;
1461e9550449SChe-Liang Chiou }
1462e9550449SChe-Liang Chiou 
1463e9550449SChe-Liang Chiou static void do_preinit(void)
1464e9550449SChe-Liang Chiou {
1465e9550449SChe-Liang Chiou 	struct mmc *m;
1466e9550449SChe-Liang Chiou 	struct list_head *entry;
1467e9550449SChe-Liang Chiou 
1468e9550449SChe-Liang Chiou 	list_for_each(entry, &mmc_devices) {
1469e9550449SChe-Liang Chiou 		m = list_entry(entry, struct mmc, link);
1470e9550449SChe-Liang Chiou 
1471e9550449SChe-Liang Chiou 		if (m->preinit)
1472e9550449SChe-Liang Chiou 			mmc_start_init(m);
1473e9550449SChe-Liang Chiou 	}
1474e9550449SChe-Liang Chiou }
1475e9550449SChe-Liang Chiou 
1476e9550449SChe-Liang Chiou 
1477272cc70bSAndy Fleming int mmc_initialize(bd_t *bis)
1478272cc70bSAndy Fleming {
1479272cc70bSAndy Fleming 	INIT_LIST_HEAD (&mmc_devices);
1480272cc70bSAndy Fleming 	cur_dev_num = 0;
1481272cc70bSAndy Fleming 
1482272cc70bSAndy Fleming 	if (board_mmc_init(bis) < 0)
1483272cc70bSAndy Fleming 		cpu_mmc_init(bis);
1484272cc70bSAndy Fleming 
1485bb0dc108SYing Zhang #ifndef CONFIG_SPL_BUILD
1486272cc70bSAndy Fleming 	print_mmc_devices(',');
1487bb0dc108SYing Zhang #endif
1488272cc70bSAndy Fleming 
1489e9550449SChe-Liang Chiou 	do_preinit();
1490272cc70bSAndy Fleming 	return 0;
1491272cc70bSAndy Fleming }
14923690d6d6SAmar 
14933690d6d6SAmar #ifdef CONFIG_SUPPORT_EMMC_BOOT
14943690d6d6SAmar /*
14953690d6d6SAmar  * This function changes the size of boot partition and the size of rpmb
14963690d6d6SAmar  * partition present on EMMC devices.
14973690d6d6SAmar  *
14983690d6d6SAmar  * Input Parameters:
14993690d6d6SAmar  * struct *mmc: pointer for the mmc device strcuture
15003690d6d6SAmar  * bootsize: size of boot partition
15013690d6d6SAmar  * rpmbsize: size of rpmb partition
15023690d6d6SAmar  *
15033690d6d6SAmar  * Returns 0 on success.
15043690d6d6SAmar  */
15053690d6d6SAmar 
15063690d6d6SAmar int mmc_boot_partition_size_change(struct mmc *mmc, unsigned long bootsize,
15073690d6d6SAmar 				unsigned long rpmbsize)
15083690d6d6SAmar {
15093690d6d6SAmar 	int err;
15103690d6d6SAmar 	struct mmc_cmd cmd;
15113690d6d6SAmar 
15123690d6d6SAmar 	/* Only use this command for raw EMMC moviNAND. Enter backdoor mode */
15133690d6d6SAmar 	cmd.cmdidx = MMC_CMD_RES_MAN;
15143690d6d6SAmar 	cmd.resp_type = MMC_RSP_R1b;
15153690d6d6SAmar 	cmd.cmdarg = MMC_CMD62_ARG1;
15163690d6d6SAmar 
15173690d6d6SAmar 	err = mmc_send_cmd(mmc, &cmd, NULL);
15183690d6d6SAmar 	if (err) {
15193690d6d6SAmar 		debug("mmc_boot_partition_size_change: Error1 = %d\n", err);
15203690d6d6SAmar 		return err;
15213690d6d6SAmar 	}
15223690d6d6SAmar 
15233690d6d6SAmar 	/* Boot partition changing mode */
15243690d6d6SAmar 	cmd.cmdidx = MMC_CMD_RES_MAN;
15253690d6d6SAmar 	cmd.resp_type = MMC_RSP_R1b;
15263690d6d6SAmar 	cmd.cmdarg = MMC_CMD62_ARG2;
15273690d6d6SAmar 
15283690d6d6SAmar 	err = mmc_send_cmd(mmc, &cmd, NULL);
15293690d6d6SAmar 	if (err) {
15303690d6d6SAmar 		debug("mmc_boot_partition_size_change: Error2 = %d\n", err);
15313690d6d6SAmar 		return err;
15323690d6d6SAmar 	}
15333690d6d6SAmar 	/* boot partition size is multiple of 128KB */
15343690d6d6SAmar 	bootsize = (bootsize * 1024) / 128;
15353690d6d6SAmar 
15363690d6d6SAmar 	/* Arg: boot partition size */
15373690d6d6SAmar 	cmd.cmdidx = MMC_CMD_RES_MAN;
15383690d6d6SAmar 	cmd.resp_type = MMC_RSP_R1b;
15393690d6d6SAmar 	cmd.cmdarg = bootsize;
15403690d6d6SAmar 
15413690d6d6SAmar 	err = mmc_send_cmd(mmc, &cmd, NULL);
15423690d6d6SAmar 	if (err) {
15433690d6d6SAmar 		debug("mmc_boot_partition_size_change: Error3 = %d\n", err);
15443690d6d6SAmar 		return err;
15453690d6d6SAmar 	}
15463690d6d6SAmar 	/* RPMB partition size is multiple of 128KB */
15473690d6d6SAmar 	rpmbsize = (rpmbsize * 1024) / 128;
15483690d6d6SAmar 	/* Arg: RPMB partition size */
15493690d6d6SAmar 	cmd.cmdidx = MMC_CMD_RES_MAN;
15503690d6d6SAmar 	cmd.resp_type = MMC_RSP_R1b;
15513690d6d6SAmar 	cmd.cmdarg = rpmbsize;
15523690d6d6SAmar 
15533690d6d6SAmar 	err = mmc_send_cmd(mmc, &cmd, NULL);
15543690d6d6SAmar 	if (err) {
15553690d6d6SAmar 		debug("mmc_boot_partition_size_change: Error4 = %d\n", err);
15563690d6d6SAmar 		return err;
15573690d6d6SAmar 	}
15583690d6d6SAmar 	return 0;
15593690d6d6SAmar }
15603690d6d6SAmar 
15613690d6d6SAmar /*
15625a99b9deSTom Rini  * Modify EXT_CSD[177] which is BOOT_BUS_WIDTH
15635a99b9deSTom Rini  * based on the passed in values for BOOT_BUS_WIDTH, RESET_BOOT_BUS_WIDTH
15645a99b9deSTom Rini  * and BOOT_MODE.
15655a99b9deSTom Rini  *
15665a99b9deSTom Rini  * Returns 0 on success.
15675a99b9deSTom Rini  */
15685a99b9deSTom Rini int mmc_set_boot_bus_width(struct mmc *mmc, u8 width, u8 reset, u8 mode)
15695a99b9deSTom Rini {
15705a99b9deSTom Rini 	int err;
15715a99b9deSTom Rini 
15725a99b9deSTom Rini 	err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_BUS_WIDTH,
15735a99b9deSTom Rini 			 EXT_CSD_BOOT_BUS_WIDTH_MODE(mode) |
15745a99b9deSTom Rini 			 EXT_CSD_BOOT_BUS_WIDTH_RESET(reset) |
15755a99b9deSTom Rini 			 EXT_CSD_BOOT_BUS_WIDTH_WIDTH(width));
15765a99b9deSTom Rini 
15775a99b9deSTom Rini 	if (err)
15785a99b9deSTom Rini 		return err;
15795a99b9deSTom Rini 	return 0;
15805a99b9deSTom Rini }
15815a99b9deSTom Rini 
15825a99b9deSTom Rini /*
1583792970b0STom Rini  * Modify EXT_CSD[179] which is PARTITION_CONFIG (formerly BOOT_CONFIG)
1584792970b0STom Rini  * based on the passed in values for BOOT_ACK, BOOT_PARTITION_ENABLE and
1585792970b0STom Rini  * PARTITION_ACCESS.
1586792970b0STom Rini  *
1587792970b0STom Rini  * Returns 0 on success.
1588792970b0STom Rini  */
1589792970b0STom Rini int mmc_set_part_conf(struct mmc *mmc, u8 ack, u8 part_num, u8 access)
1590792970b0STom Rini {
1591792970b0STom Rini 	int err;
1592792970b0STom Rini 
1593792970b0STom Rini 	err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONF,
1594792970b0STom Rini 			 EXT_CSD_BOOT_ACK(ack) |
1595792970b0STom Rini 			 EXT_CSD_BOOT_PART_NUM(part_num) |
1596792970b0STom Rini 			 EXT_CSD_PARTITION_ACCESS(access));
1597792970b0STom Rini 
1598792970b0STom Rini 	if (err)
1599792970b0STom Rini 		return err;
1600792970b0STom Rini 	return 0;
1601792970b0STom Rini }
160233ace362STom Rini 
160333ace362STom Rini /*
160433ace362STom Rini  * Modify EXT_CSD[162] which is RST_n_FUNCTION based on the given value
160533ace362STom Rini  * for enable.  Note that this is a write-once field for non-zero values.
160633ace362STom Rini  *
160733ace362STom Rini  * Returns 0 on success.
160833ace362STom Rini  */
160933ace362STom Rini int mmc_set_rst_n_function(struct mmc *mmc, u8 enable)
161033ace362STom Rini {
161133ace362STom Rini 	return mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_RST_N_FUNCTION,
161233ace362STom Rini 			  enable);
161333ace362STom Rini }
16143690d6d6SAmar #endif
1615