xref: /openbmc/u-boot/drivers/mmc/mmc.c (revision 1b26bab12e85e8b0d382d6775e40d14445249574)
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 
1211677eef4SAndrew Gabbasov 	while (1) {
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 
1381677eef4SAndrew Gabbasov 		if (timeout-- <= 0)
1391677eef4SAndrew Gabbasov 			break;
1405d4fc8d9SRaffaele Recalcati 
1411677eef4SAndrew Gabbasov 		udelay(1000);
1421677eef4SAndrew Gabbasov 	}
1435d4fc8d9SRaffaele Recalcati 
1445db2fe3aSRaffaele Recalcati #ifdef CONFIG_MMC_TRACE
1455db2fe3aSRaffaele Recalcati 	status = (cmd.response[0] & MMC_STATUS_CURR_STATE) >> 9;
1465db2fe3aSRaffaele Recalcati 	printf("CURR STATE:%d\n", status);
1475db2fe3aSRaffaele Recalcati #endif
1485b0c942fSJongman Heo 	if (timeout <= 0) {
14956196826SPaul Burton #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
1505d4fc8d9SRaffaele Recalcati 		printf("Timeout waiting card ready\n");
15156196826SPaul Burton #endif
1525d4fc8d9SRaffaele Recalcati 		return TIMEOUT;
1535d4fc8d9SRaffaele Recalcati 	}
1546b2221b0SAndrew Gabbasov 	if (cmd.response[0] & MMC_STATUS_SWITCH_ERROR)
1556b2221b0SAndrew Gabbasov 		return SWITCH_ERR;
1565d4fc8d9SRaffaele Recalcati 
1575d4fc8d9SRaffaele Recalcati 	return 0;
1585d4fc8d9SRaffaele Recalcati }
1595d4fc8d9SRaffaele Recalcati 
160da61fa5fSPaul Burton int mmc_set_blocklen(struct mmc *mmc, int len)
161272cc70bSAndy Fleming {
162272cc70bSAndy Fleming 	struct mmc_cmd cmd;
163272cc70bSAndy Fleming 
164786e8f81SAndrew Gabbasov 	if (mmc->ddr_mode)
165d22e3d46SJaehoon Chung 		return 0;
166d22e3d46SJaehoon Chung 
167272cc70bSAndy Fleming 	cmd.cmdidx = MMC_CMD_SET_BLOCKLEN;
168272cc70bSAndy Fleming 	cmd.resp_type = MMC_RSP_R1;
169272cc70bSAndy Fleming 	cmd.cmdarg = len;
170272cc70bSAndy Fleming 
171272cc70bSAndy Fleming 	return mmc_send_cmd(mmc, &cmd, NULL);
172272cc70bSAndy Fleming }
173272cc70bSAndy Fleming 
174272cc70bSAndy Fleming struct mmc *find_mmc_device(int dev_num)
175272cc70bSAndy Fleming {
176272cc70bSAndy Fleming 	struct mmc *m;
177272cc70bSAndy Fleming 	struct list_head *entry;
178272cc70bSAndy Fleming 
179272cc70bSAndy Fleming 	list_for_each(entry, &mmc_devices) {
180272cc70bSAndy Fleming 		m = list_entry(entry, struct mmc, link);
181272cc70bSAndy Fleming 
182272cc70bSAndy Fleming 		if (m->block_dev.dev == dev_num)
183272cc70bSAndy Fleming 			return m;
184272cc70bSAndy Fleming 	}
185272cc70bSAndy Fleming 
18656196826SPaul Burton #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
187272cc70bSAndy Fleming 	printf("MMC Device %d not found\n", dev_num);
18856196826SPaul Burton #endif
189272cc70bSAndy Fleming 
190272cc70bSAndy Fleming 	return NULL;
191272cc70bSAndy Fleming }
192272cc70bSAndy Fleming 
193ff8fef56SSascha Silbe static int mmc_read_blocks(struct mmc *mmc, void *dst, lbaint_t start,
194fdbb873eSKim Phillips 			   lbaint_t blkcnt)
195272cc70bSAndy Fleming {
196272cc70bSAndy Fleming 	struct mmc_cmd cmd;
197272cc70bSAndy Fleming 	struct mmc_data data;
198272cc70bSAndy Fleming 
1994a1a06bcSAlagu Sankar 	if (blkcnt > 1)
2004a1a06bcSAlagu Sankar 		cmd.cmdidx = MMC_CMD_READ_MULTIPLE_BLOCK;
2014a1a06bcSAlagu Sankar 	else
202272cc70bSAndy Fleming 		cmd.cmdidx = MMC_CMD_READ_SINGLE_BLOCK;
203272cc70bSAndy Fleming 
204272cc70bSAndy Fleming 	if (mmc->high_capacity)
2054a1a06bcSAlagu Sankar 		cmd.cmdarg = start;
206272cc70bSAndy Fleming 	else
2074a1a06bcSAlagu Sankar 		cmd.cmdarg = start * mmc->read_bl_len;
208272cc70bSAndy Fleming 
209272cc70bSAndy Fleming 	cmd.resp_type = MMC_RSP_R1;
210272cc70bSAndy Fleming 
211272cc70bSAndy Fleming 	data.dest = dst;
2124a1a06bcSAlagu Sankar 	data.blocks = blkcnt;
213272cc70bSAndy Fleming 	data.blocksize = mmc->read_bl_len;
214272cc70bSAndy Fleming 	data.flags = MMC_DATA_READ;
215272cc70bSAndy Fleming 
2164a1a06bcSAlagu Sankar 	if (mmc_send_cmd(mmc, &cmd, &data))
2174a1a06bcSAlagu Sankar 		return 0;
2184a1a06bcSAlagu Sankar 
2194a1a06bcSAlagu Sankar 	if (blkcnt > 1) {
2204a1a06bcSAlagu Sankar 		cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
2214a1a06bcSAlagu Sankar 		cmd.cmdarg = 0;
2224a1a06bcSAlagu Sankar 		cmd.resp_type = MMC_RSP_R1b;
2234a1a06bcSAlagu Sankar 		if (mmc_send_cmd(mmc, &cmd, NULL)) {
22456196826SPaul Burton #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
2254a1a06bcSAlagu Sankar 			printf("mmc fail to send stop cmd\n");
22656196826SPaul Burton #endif
2274a1a06bcSAlagu Sankar 			return 0;
2284a1a06bcSAlagu Sankar 		}
229272cc70bSAndy Fleming 	}
230272cc70bSAndy Fleming 
2314a1a06bcSAlagu Sankar 	return blkcnt;
232272cc70bSAndy Fleming }
233272cc70bSAndy Fleming 
234ff8fef56SSascha Silbe static ulong mmc_bread(int dev_num, lbaint_t start, lbaint_t blkcnt, void *dst)
235272cc70bSAndy Fleming {
2364a1a06bcSAlagu Sankar 	lbaint_t cur, blocks_todo = blkcnt;
237272cc70bSAndy Fleming 
2384a1a06bcSAlagu Sankar 	if (blkcnt == 0)
2394a1a06bcSAlagu Sankar 		return 0;
2404a1a06bcSAlagu Sankar 
2414a1a06bcSAlagu Sankar 	struct mmc *mmc = find_mmc_device(dev_num);
242272cc70bSAndy Fleming 	if (!mmc)
243272cc70bSAndy Fleming 		return 0;
244272cc70bSAndy Fleming 
245d2bf29e3SLei Wen 	if ((start + blkcnt) > mmc->block_dev.lba) {
24656196826SPaul Burton #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
247ff8fef56SSascha Silbe 		printf("MMC: block number 0x" LBAF " exceeds max(0x" LBAF ")\n",
248d2bf29e3SLei Wen 			start + blkcnt, mmc->block_dev.lba);
24956196826SPaul Burton #endif
250d2bf29e3SLei Wen 		return 0;
251d2bf29e3SLei Wen 	}
252272cc70bSAndy Fleming 
25311692991SSimon Glass 	if (mmc_set_blocklen(mmc, mmc->read_bl_len)) {
25411692991SSimon Glass 		debug("%s: Failed to set blocklen\n", __func__);
255272cc70bSAndy Fleming 		return 0;
25611692991SSimon Glass 	}
257272cc70bSAndy Fleming 
2584a1a06bcSAlagu Sankar 	do {
25993bfd616SPantelis Antoniou 		cur = (blocks_todo > mmc->cfg->b_max) ?
26093bfd616SPantelis Antoniou 			mmc->cfg->b_max : blocks_todo;
26111692991SSimon Glass 		if (mmc_read_blocks(mmc, dst, start, cur) != cur) {
26211692991SSimon Glass 			debug("%s: Failed to read blocks\n", __func__);
2634a1a06bcSAlagu Sankar 			return 0;
26411692991SSimon Glass 		}
2654a1a06bcSAlagu Sankar 		blocks_todo -= cur;
2664a1a06bcSAlagu Sankar 		start += cur;
2674a1a06bcSAlagu Sankar 		dst += cur * mmc->read_bl_len;
2684a1a06bcSAlagu Sankar 	} while (blocks_todo > 0);
269272cc70bSAndy Fleming 
270272cc70bSAndy Fleming 	return blkcnt;
271272cc70bSAndy Fleming }
272272cc70bSAndy Fleming 
273fdbb873eSKim Phillips static int mmc_go_idle(struct mmc *mmc)
274272cc70bSAndy Fleming {
275272cc70bSAndy Fleming 	struct mmc_cmd cmd;
276272cc70bSAndy Fleming 	int err;
277272cc70bSAndy Fleming 
278272cc70bSAndy Fleming 	udelay(1000);
279272cc70bSAndy Fleming 
280272cc70bSAndy Fleming 	cmd.cmdidx = MMC_CMD_GO_IDLE_STATE;
281272cc70bSAndy Fleming 	cmd.cmdarg = 0;
282272cc70bSAndy Fleming 	cmd.resp_type = MMC_RSP_NONE;
283272cc70bSAndy Fleming 
284272cc70bSAndy Fleming 	err = mmc_send_cmd(mmc, &cmd, NULL);
285272cc70bSAndy Fleming 
286272cc70bSAndy Fleming 	if (err)
287272cc70bSAndy Fleming 		return err;
288272cc70bSAndy Fleming 
289272cc70bSAndy Fleming 	udelay(2000);
290272cc70bSAndy Fleming 
291272cc70bSAndy Fleming 	return 0;
292272cc70bSAndy Fleming }
293272cc70bSAndy Fleming 
294fdbb873eSKim Phillips static int sd_send_op_cond(struct mmc *mmc)
295272cc70bSAndy Fleming {
296272cc70bSAndy Fleming 	int timeout = 1000;
297272cc70bSAndy Fleming 	int err;
298272cc70bSAndy Fleming 	struct mmc_cmd cmd;
299272cc70bSAndy Fleming 
3001677eef4SAndrew Gabbasov 	while (1) {
301272cc70bSAndy Fleming 		cmd.cmdidx = MMC_CMD_APP_CMD;
302272cc70bSAndy Fleming 		cmd.resp_type = MMC_RSP_R1;
303272cc70bSAndy Fleming 		cmd.cmdarg = 0;
304272cc70bSAndy Fleming 
305272cc70bSAndy Fleming 		err = mmc_send_cmd(mmc, &cmd, NULL);
306272cc70bSAndy Fleming 
307272cc70bSAndy Fleming 		if (err)
308272cc70bSAndy Fleming 			return err;
309272cc70bSAndy Fleming 
310272cc70bSAndy Fleming 		cmd.cmdidx = SD_CMD_APP_SEND_OP_COND;
311272cc70bSAndy Fleming 		cmd.resp_type = MMC_RSP_R3;
312250de12bSStefano Babic 
313250de12bSStefano Babic 		/*
314250de12bSStefano Babic 		 * Most cards do not answer if some reserved bits
315250de12bSStefano Babic 		 * in the ocr are set. However, Some controller
316250de12bSStefano Babic 		 * can set bit 7 (reserved for low voltages), but
317250de12bSStefano Babic 		 * how to manage low voltages SD card is not yet
318250de12bSStefano Babic 		 * specified.
319250de12bSStefano Babic 		 */
320d52ebf10SThomas Chou 		cmd.cmdarg = mmc_host_is_spi(mmc) ? 0 :
32193bfd616SPantelis Antoniou 			(mmc->cfg->voltages & 0xff8000);
322272cc70bSAndy Fleming 
323272cc70bSAndy Fleming 		if (mmc->version == SD_VERSION_2)
324272cc70bSAndy Fleming 			cmd.cmdarg |= OCR_HCS;
325272cc70bSAndy Fleming 
326272cc70bSAndy Fleming 		err = mmc_send_cmd(mmc, &cmd, NULL);
327272cc70bSAndy Fleming 
328272cc70bSAndy Fleming 		if (err)
329272cc70bSAndy Fleming 			return err;
330272cc70bSAndy Fleming 
3311677eef4SAndrew Gabbasov 		if (cmd.response[0] & OCR_BUSY)
3321677eef4SAndrew Gabbasov 			break;
333272cc70bSAndy Fleming 
3341677eef4SAndrew Gabbasov 		if (timeout-- <= 0)
335272cc70bSAndy Fleming 			return UNUSABLE_ERR;
336272cc70bSAndy Fleming 
3371677eef4SAndrew Gabbasov 		udelay(1000);
3381677eef4SAndrew Gabbasov 	}
3391677eef4SAndrew Gabbasov 
340272cc70bSAndy Fleming 	if (mmc->version != SD_VERSION_2)
341272cc70bSAndy Fleming 		mmc->version = SD_VERSION_1_0;
342272cc70bSAndy Fleming 
343d52ebf10SThomas Chou 	if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
344d52ebf10SThomas Chou 		cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
345d52ebf10SThomas Chou 		cmd.resp_type = MMC_RSP_R3;
346d52ebf10SThomas Chou 		cmd.cmdarg = 0;
347d52ebf10SThomas Chou 
348d52ebf10SThomas Chou 		err = mmc_send_cmd(mmc, &cmd, NULL);
349d52ebf10SThomas Chou 
350d52ebf10SThomas Chou 		if (err)
351d52ebf10SThomas Chou 			return err;
352d52ebf10SThomas Chou 	}
353d52ebf10SThomas Chou 
354998be3ddSRabin Vincent 	mmc->ocr = cmd.response[0];
355272cc70bSAndy Fleming 
356272cc70bSAndy Fleming 	mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
357272cc70bSAndy Fleming 	mmc->rca = 0;
358272cc70bSAndy Fleming 
359272cc70bSAndy Fleming 	return 0;
360272cc70bSAndy Fleming }
361272cc70bSAndy Fleming 
3625289b535SAndrew Gabbasov static int mmc_send_op_cond_iter(struct mmc *mmc, int use_arg)
363272cc70bSAndy Fleming {
3645289b535SAndrew Gabbasov 	struct mmc_cmd cmd;
365272cc70bSAndy Fleming 	int err;
366272cc70bSAndy Fleming 
3675289b535SAndrew Gabbasov 	cmd.cmdidx = MMC_CMD_SEND_OP_COND;
3685289b535SAndrew Gabbasov 	cmd.resp_type = MMC_RSP_R3;
3695289b535SAndrew Gabbasov 	cmd.cmdarg = 0;
3705a20397bSRob Herring 	if (use_arg && !mmc_host_is_spi(mmc))
3715a20397bSRob Herring 		cmd.cmdarg = OCR_HCS |
37293bfd616SPantelis Antoniou 			(mmc->cfg->voltages &
373a626c8d4SAndrew Gabbasov 			(mmc->ocr & OCR_VOLTAGE_MASK)) |
374a626c8d4SAndrew Gabbasov 			(mmc->ocr & OCR_ACCESS_MODE);
375e9550449SChe-Liang Chiou 
3765289b535SAndrew Gabbasov 	err = mmc_send_cmd(mmc, &cmd, NULL);
377e9550449SChe-Liang Chiou 	if (err)
378e9550449SChe-Liang Chiou 		return err;
3795289b535SAndrew Gabbasov 	mmc->ocr = cmd.response[0];
380e9550449SChe-Liang Chiou 	return 0;
381e9550449SChe-Liang Chiou }
382e9550449SChe-Liang Chiou 
383750121c3SJeroen Hofstee static int mmc_send_op_cond(struct mmc *mmc)
384e9550449SChe-Liang Chiou {
385e9550449SChe-Liang Chiou 	int err, i;
386e9550449SChe-Liang Chiou 
387272cc70bSAndy Fleming 	/* Some cards seem to need this */
388272cc70bSAndy Fleming 	mmc_go_idle(mmc);
389272cc70bSAndy Fleming 
39031cacbabSRaffaele Recalcati  	/* Asking to the card its capabilities */
391e9550449SChe-Liang Chiou 	for (i = 0; i < 2; i++) {
3925289b535SAndrew Gabbasov 		err = mmc_send_op_cond_iter(mmc, i != 0);
39331cacbabSRaffaele Recalcati 		if (err)
39431cacbabSRaffaele Recalcati 			return err;
39531cacbabSRaffaele Recalcati 
396e9550449SChe-Liang Chiou 		/* exit if not busy (flag seems to be inverted) */
397a626c8d4SAndrew Gabbasov 		if (mmc->ocr & OCR_BUSY)
398bd47c135SAndrew Gabbasov 			break;
399e9550449SChe-Liang Chiou 	}
400bd47c135SAndrew Gabbasov 	mmc->op_cond_pending = 1;
401bd47c135SAndrew Gabbasov 	return 0;
402e9550449SChe-Liang Chiou }
40331cacbabSRaffaele Recalcati 
404750121c3SJeroen Hofstee static int mmc_complete_op_cond(struct mmc *mmc)
405e9550449SChe-Liang Chiou {
406e9550449SChe-Liang Chiou 	struct mmc_cmd cmd;
407e9550449SChe-Liang Chiou 	int timeout = 1000;
408e9550449SChe-Liang Chiou 	uint start;
409e9550449SChe-Liang Chiou 	int err;
410e9550449SChe-Liang Chiou 
411e9550449SChe-Liang Chiou 	mmc->op_cond_pending = 0;
412cc17c01fSAndrew Gabbasov 	if (!(mmc->ocr & OCR_BUSY)) {
413e9550449SChe-Liang Chiou 		start = get_timer(0);
4141677eef4SAndrew Gabbasov 		while (1) {
4155289b535SAndrew Gabbasov 			err = mmc_send_op_cond_iter(mmc, 1);
416272cc70bSAndy Fleming 			if (err)
417272cc70bSAndy Fleming 				return err;
4181677eef4SAndrew Gabbasov 			if (mmc->ocr & OCR_BUSY)
4191677eef4SAndrew Gabbasov 				break;
420e9550449SChe-Liang Chiou 			if (get_timer(start) > timeout)
421272cc70bSAndy Fleming 				return UNUSABLE_ERR;
422e9550449SChe-Liang Chiou 			udelay(100);
4231677eef4SAndrew Gabbasov 		}
424cc17c01fSAndrew Gabbasov 	}
425272cc70bSAndy Fleming 
426d52ebf10SThomas Chou 	if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
427d52ebf10SThomas Chou 		cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
428d52ebf10SThomas Chou 		cmd.resp_type = MMC_RSP_R3;
429d52ebf10SThomas Chou 		cmd.cmdarg = 0;
430d52ebf10SThomas Chou 
431d52ebf10SThomas Chou 		err = mmc_send_cmd(mmc, &cmd, NULL);
432d52ebf10SThomas Chou 
433d52ebf10SThomas Chou 		if (err)
434d52ebf10SThomas Chou 			return err;
435a626c8d4SAndrew Gabbasov 
436a626c8d4SAndrew Gabbasov 		mmc->ocr = cmd.response[0];
437d52ebf10SThomas Chou 	}
438d52ebf10SThomas Chou 
439272cc70bSAndy Fleming 	mmc->version = MMC_VERSION_UNKNOWN;
440272cc70bSAndy Fleming 
441272cc70bSAndy Fleming 	mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
442def816a2SStephen Warren 	mmc->rca = 1;
443272cc70bSAndy Fleming 
444272cc70bSAndy Fleming 	return 0;
445272cc70bSAndy Fleming }
446272cc70bSAndy Fleming 
447272cc70bSAndy Fleming 
448fdbb873eSKim Phillips static int mmc_send_ext_csd(struct mmc *mmc, u8 *ext_csd)
449272cc70bSAndy Fleming {
450272cc70bSAndy Fleming 	struct mmc_cmd cmd;
451272cc70bSAndy Fleming 	struct mmc_data data;
452272cc70bSAndy Fleming 	int err;
453272cc70bSAndy Fleming 
454272cc70bSAndy Fleming 	/* Get the Card Status Register */
455272cc70bSAndy Fleming 	cmd.cmdidx = MMC_CMD_SEND_EXT_CSD;
456272cc70bSAndy Fleming 	cmd.resp_type = MMC_RSP_R1;
457272cc70bSAndy Fleming 	cmd.cmdarg = 0;
458272cc70bSAndy Fleming 
459cdfd1ac6SYoshihiro Shimoda 	data.dest = (char *)ext_csd;
460272cc70bSAndy Fleming 	data.blocks = 1;
4618bfa195eSSimon Glass 	data.blocksize = MMC_MAX_BLOCK_LEN;
462272cc70bSAndy Fleming 	data.flags = MMC_DATA_READ;
463272cc70bSAndy Fleming 
464272cc70bSAndy Fleming 	err = mmc_send_cmd(mmc, &cmd, &data);
465272cc70bSAndy Fleming 
466272cc70bSAndy Fleming 	return err;
467272cc70bSAndy Fleming }
468272cc70bSAndy Fleming 
469272cc70bSAndy Fleming 
470fdbb873eSKim Phillips static int mmc_switch(struct mmc *mmc, u8 set, u8 index, u8 value)
471272cc70bSAndy Fleming {
472272cc70bSAndy Fleming 	struct mmc_cmd cmd;
4735d4fc8d9SRaffaele Recalcati 	int timeout = 1000;
4745d4fc8d9SRaffaele Recalcati 	int ret;
475272cc70bSAndy Fleming 
476272cc70bSAndy Fleming 	cmd.cmdidx = MMC_CMD_SWITCH;
477272cc70bSAndy Fleming 	cmd.resp_type = MMC_RSP_R1b;
478272cc70bSAndy Fleming 	cmd.cmdarg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
479272cc70bSAndy Fleming 				 (index << 16) |
480272cc70bSAndy Fleming 				 (value << 8);
481272cc70bSAndy Fleming 
4825d4fc8d9SRaffaele Recalcati 	ret = mmc_send_cmd(mmc, &cmd, NULL);
4835d4fc8d9SRaffaele Recalcati 
4845d4fc8d9SRaffaele Recalcati 	/* Waiting for the ready status */
48593ad0d18SJan Kloetzke 	if (!ret)
48693ad0d18SJan Kloetzke 		ret = mmc_send_status(mmc, timeout);
4875d4fc8d9SRaffaele Recalcati 
4885d4fc8d9SRaffaele Recalcati 	return ret;
4895d4fc8d9SRaffaele Recalcati 
490272cc70bSAndy Fleming }
491272cc70bSAndy Fleming 
492fdbb873eSKim Phillips static int mmc_change_freq(struct mmc *mmc)
493272cc70bSAndy Fleming {
4948bfa195eSSimon Glass 	ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
495272cc70bSAndy Fleming 	char cardtype;
496272cc70bSAndy Fleming 	int err;
497272cc70bSAndy Fleming 
498fc5b32fbSAndrew Gabbasov 	mmc->card_caps = 0;
499272cc70bSAndy Fleming 
500d52ebf10SThomas Chou 	if (mmc_host_is_spi(mmc))
501d52ebf10SThomas Chou 		return 0;
502d52ebf10SThomas Chou 
503272cc70bSAndy Fleming 	/* Only version 4 supports high-speed */
504272cc70bSAndy Fleming 	if (mmc->version < MMC_VERSION_4)
505272cc70bSAndy Fleming 		return 0;
506272cc70bSAndy Fleming 
507fc5b32fbSAndrew Gabbasov 	mmc->card_caps |= MMC_MODE_4BIT | MMC_MODE_8BIT;
508fc5b32fbSAndrew Gabbasov 
509272cc70bSAndy Fleming 	err = mmc_send_ext_csd(mmc, ext_csd);
510272cc70bSAndy Fleming 
511272cc70bSAndy Fleming 	if (err)
512272cc70bSAndy Fleming 		return err;
513272cc70bSAndy Fleming 
5140560db18SLei Wen 	cardtype = ext_csd[EXT_CSD_CARD_TYPE] & 0xf;
515272cc70bSAndy Fleming 
516272cc70bSAndy Fleming 	err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING, 1);
517272cc70bSAndy Fleming 
518272cc70bSAndy Fleming 	if (err)
5196b2221b0SAndrew Gabbasov 		return err == SWITCH_ERR ? 0 : err;
520272cc70bSAndy Fleming 
521272cc70bSAndy Fleming 	/* Now check to see that it worked */
522272cc70bSAndy Fleming 	err = mmc_send_ext_csd(mmc, ext_csd);
523272cc70bSAndy Fleming 
524272cc70bSAndy Fleming 	if (err)
525272cc70bSAndy Fleming 		return err;
526272cc70bSAndy Fleming 
527272cc70bSAndy Fleming 	/* No high-speed support */
5280560db18SLei Wen 	if (!ext_csd[EXT_CSD_HS_TIMING])
529272cc70bSAndy Fleming 		return 0;
530272cc70bSAndy Fleming 
531272cc70bSAndy Fleming 	/* High Speed is set, there are two types: 52MHz and 26MHz */
532d22e3d46SJaehoon Chung 	if (cardtype & EXT_CSD_CARD_TYPE_52) {
533201d5ac4SAndrew Gabbasov 		if (cardtype & EXT_CSD_CARD_TYPE_DDR_1_8V)
534d22e3d46SJaehoon Chung 			mmc->card_caps |= MMC_MODE_DDR_52MHz;
535272cc70bSAndy Fleming 		mmc->card_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
536d22e3d46SJaehoon Chung 	} else {
537272cc70bSAndy Fleming 		mmc->card_caps |= MMC_MODE_HS;
538d22e3d46SJaehoon Chung 	}
539272cc70bSAndy Fleming 
540272cc70bSAndy Fleming 	return 0;
541272cc70bSAndy Fleming }
542272cc70bSAndy Fleming 
543f866a46dSStephen Warren static int mmc_set_capacity(struct mmc *mmc, int part_num)
544f866a46dSStephen Warren {
545f866a46dSStephen Warren 	switch (part_num) {
546f866a46dSStephen Warren 	case 0:
547f866a46dSStephen Warren 		mmc->capacity = mmc->capacity_user;
548f866a46dSStephen Warren 		break;
549f866a46dSStephen Warren 	case 1:
550f866a46dSStephen Warren 	case 2:
551f866a46dSStephen Warren 		mmc->capacity = mmc->capacity_boot;
552f866a46dSStephen Warren 		break;
553f866a46dSStephen Warren 	case 3:
554f866a46dSStephen Warren 		mmc->capacity = mmc->capacity_rpmb;
555f866a46dSStephen Warren 		break;
556f866a46dSStephen Warren 	case 4:
557f866a46dSStephen Warren 	case 5:
558f866a46dSStephen Warren 	case 6:
559f866a46dSStephen Warren 	case 7:
560f866a46dSStephen Warren 		mmc->capacity = mmc->capacity_gp[part_num - 4];
561f866a46dSStephen Warren 		break;
562f866a46dSStephen Warren 	default:
563f866a46dSStephen Warren 		return -1;
564f866a46dSStephen Warren 	}
565f866a46dSStephen Warren 
566f866a46dSStephen Warren 	mmc->block_dev.lba = lldiv(mmc->capacity, mmc->read_bl_len);
567f866a46dSStephen Warren 
568f866a46dSStephen Warren 	return 0;
569f866a46dSStephen Warren }
570f866a46dSStephen Warren 
571d2356284SStephen Warren int mmc_select_hwpart(int dev_num, int hwpart)
572d2356284SStephen Warren {
573d2356284SStephen Warren 	struct mmc *mmc = find_mmc_device(dev_num);
574d2356284SStephen Warren 	int ret;
575d2356284SStephen Warren 
576d2356284SStephen Warren 	if (!mmc)
577d4622df3SStephen Warren 		return -ENODEV;
578d2356284SStephen Warren 
579d2356284SStephen Warren 	if (mmc->part_num == hwpart)
580d2356284SStephen Warren 		return 0;
581d2356284SStephen Warren 
582d2356284SStephen Warren 	if (mmc->part_config == MMCPART_NOAVAILABLE) {
583d2356284SStephen Warren 		printf("Card doesn't support part_switch\n");
584d4622df3SStephen Warren 		return -EMEDIUMTYPE;
585d2356284SStephen Warren 	}
586d2356284SStephen Warren 
587d2356284SStephen Warren 	ret = mmc_switch_part(dev_num, hwpart);
588d2356284SStephen Warren 	if (ret)
589d4622df3SStephen Warren 		return ret;
590d2356284SStephen Warren 
591d2356284SStephen Warren 	mmc->part_num = hwpart;
592d2356284SStephen Warren 
593d2356284SStephen Warren 	return 0;
594d2356284SStephen Warren }
595d2356284SStephen Warren 
596d2356284SStephen Warren 
597bc897b1dSLei Wen int mmc_switch_part(int dev_num, unsigned int part_num)
598bc897b1dSLei Wen {
599bc897b1dSLei Wen 	struct mmc *mmc = find_mmc_device(dev_num);
600f866a46dSStephen Warren 	int ret;
601bc897b1dSLei Wen 
602bc897b1dSLei Wen 	if (!mmc)
603bc897b1dSLei Wen 		return -1;
604bc897b1dSLei Wen 
605f866a46dSStephen Warren 	ret = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONF,
606bc897b1dSLei Wen 			 (mmc->part_config & ~PART_ACCESS_MASK)
607bc897b1dSLei Wen 			 | (part_num & PART_ACCESS_MASK));
608f866a46dSStephen Warren 
6096dc93e70SPeter Bigot 	/*
6106dc93e70SPeter Bigot 	 * Set the capacity if the switch succeeded or was intended
6116dc93e70SPeter Bigot 	 * to return to representing the raw device.
6126dc93e70SPeter Bigot 	 */
6136dc93e70SPeter Bigot 	if ((ret == 0) || ((ret == -ENODEV) && (part_num == 0)))
6146dc93e70SPeter Bigot 		ret = mmc_set_capacity(mmc, part_num);
6156dc93e70SPeter Bigot 
6166dc93e70SPeter Bigot 	return ret;
617bc897b1dSLei Wen }
618bc897b1dSLei Wen 
619ac9da0e0SDiego Santa Cruz int mmc_hwpart_config(struct mmc *mmc,
620ac9da0e0SDiego Santa Cruz 		      const struct mmc_hwpart_conf *conf,
621ac9da0e0SDiego Santa Cruz 		      enum mmc_hwpart_conf_mode mode)
622ac9da0e0SDiego Santa Cruz {
623ac9da0e0SDiego Santa Cruz 	u8 part_attrs = 0;
624ac9da0e0SDiego Santa Cruz 	u32 enh_size_mult;
625ac9da0e0SDiego Santa Cruz 	u32 enh_start_addr;
626ac9da0e0SDiego Santa Cruz 	u32 gp_size_mult[4];
627ac9da0e0SDiego Santa Cruz 	u32 max_enh_size_mult;
628ac9da0e0SDiego Santa Cruz 	u32 tot_enh_size_mult = 0;
6298dda5b0eSDiego Santa Cruz 	u8 wr_rel_set;
630ac9da0e0SDiego Santa Cruz 	int i, pidx, err;
631ac9da0e0SDiego Santa Cruz 	ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
632ac9da0e0SDiego Santa Cruz 
633ac9da0e0SDiego Santa Cruz 	if (mode < MMC_HWPART_CONF_CHECK || mode > MMC_HWPART_CONF_COMPLETE)
634ac9da0e0SDiego Santa Cruz 		return -EINVAL;
635ac9da0e0SDiego Santa Cruz 
636ac9da0e0SDiego Santa Cruz 	if (IS_SD(mmc) || (mmc->version < MMC_VERSION_4_41)) {
637ac9da0e0SDiego Santa Cruz 		printf("eMMC >= 4.4 required for enhanced user data area\n");
638ac9da0e0SDiego Santa Cruz 		return -EMEDIUMTYPE;
639ac9da0e0SDiego Santa Cruz 	}
640ac9da0e0SDiego Santa Cruz 
641ac9da0e0SDiego Santa Cruz 	if (!(mmc->part_support & PART_SUPPORT)) {
642ac9da0e0SDiego Santa Cruz 		printf("Card does not support partitioning\n");
643ac9da0e0SDiego Santa Cruz 		return -EMEDIUMTYPE;
644ac9da0e0SDiego Santa Cruz 	}
645ac9da0e0SDiego Santa Cruz 
646ac9da0e0SDiego Santa Cruz 	if (!mmc->hc_wp_grp_size) {
647ac9da0e0SDiego Santa Cruz 		printf("Card does not define HC WP group size\n");
648ac9da0e0SDiego Santa Cruz 		return -EMEDIUMTYPE;
649ac9da0e0SDiego Santa Cruz 	}
650ac9da0e0SDiego Santa Cruz 
651ac9da0e0SDiego Santa Cruz 	/* check partition alignment and total enhanced size */
652ac9da0e0SDiego Santa Cruz 	if (conf->user.enh_size) {
653ac9da0e0SDiego Santa Cruz 		if (conf->user.enh_size % mmc->hc_wp_grp_size ||
654ac9da0e0SDiego Santa Cruz 		    conf->user.enh_start % mmc->hc_wp_grp_size) {
655ac9da0e0SDiego Santa Cruz 			printf("User data enhanced area not HC WP group "
656ac9da0e0SDiego Santa Cruz 			       "size aligned\n");
657ac9da0e0SDiego Santa Cruz 			return -EINVAL;
658ac9da0e0SDiego Santa Cruz 		}
659ac9da0e0SDiego Santa Cruz 		part_attrs |= EXT_CSD_ENH_USR;
660ac9da0e0SDiego Santa Cruz 		enh_size_mult = conf->user.enh_size / mmc->hc_wp_grp_size;
661ac9da0e0SDiego Santa Cruz 		if (mmc->high_capacity) {
662ac9da0e0SDiego Santa Cruz 			enh_start_addr = conf->user.enh_start;
663ac9da0e0SDiego Santa Cruz 		} else {
664ac9da0e0SDiego Santa Cruz 			enh_start_addr = (conf->user.enh_start << 9);
665ac9da0e0SDiego Santa Cruz 		}
666ac9da0e0SDiego Santa Cruz 	} else {
667ac9da0e0SDiego Santa Cruz 		enh_size_mult = 0;
668ac9da0e0SDiego Santa Cruz 		enh_start_addr = 0;
669ac9da0e0SDiego Santa Cruz 	}
670ac9da0e0SDiego Santa Cruz 	tot_enh_size_mult += enh_size_mult;
671ac9da0e0SDiego Santa Cruz 
672ac9da0e0SDiego Santa Cruz 	for (pidx = 0; pidx < 4; pidx++) {
673ac9da0e0SDiego Santa Cruz 		if (conf->gp_part[pidx].size % mmc->hc_wp_grp_size) {
674ac9da0e0SDiego Santa Cruz 			printf("GP%i partition not HC WP group size "
675ac9da0e0SDiego Santa Cruz 			       "aligned\n", pidx+1);
676ac9da0e0SDiego Santa Cruz 			return -EINVAL;
677ac9da0e0SDiego Santa Cruz 		}
678ac9da0e0SDiego Santa Cruz 		gp_size_mult[pidx] = conf->gp_part[pidx].size / mmc->hc_wp_grp_size;
679ac9da0e0SDiego Santa Cruz 		if (conf->gp_part[pidx].size && conf->gp_part[pidx].enhanced) {
680ac9da0e0SDiego Santa Cruz 			part_attrs |= EXT_CSD_ENH_GP(pidx);
681ac9da0e0SDiego Santa Cruz 			tot_enh_size_mult += gp_size_mult[pidx];
682ac9da0e0SDiego Santa Cruz 		}
683ac9da0e0SDiego Santa Cruz 	}
684ac9da0e0SDiego Santa Cruz 
685ac9da0e0SDiego Santa Cruz 	if (part_attrs && ! (mmc->part_support & ENHNCD_SUPPORT)) {
686ac9da0e0SDiego Santa Cruz 		printf("Card does not support enhanced attribute\n");
687ac9da0e0SDiego Santa Cruz 		return -EMEDIUMTYPE;
688ac9da0e0SDiego Santa Cruz 	}
689ac9da0e0SDiego Santa Cruz 
690ac9da0e0SDiego Santa Cruz 	err = mmc_send_ext_csd(mmc, ext_csd);
691ac9da0e0SDiego Santa Cruz 	if (err)
692ac9da0e0SDiego Santa Cruz 		return err;
693ac9da0e0SDiego Santa Cruz 
694ac9da0e0SDiego Santa Cruz 	max_enh_size_mult =
695ac9da0e0SDiego Santa Cruz 		(ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT+2] << 16) +
696ac9da0e0SDiego Santa Cruz 		(ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT+1] << 8) +
697ac9da0e0SDiego Santa Cruz 		ext_csd[EXT_CSD_MAX_ENH_SIZE_MULT];
698ac9da0e0SDiego Santa Cruz 	if (tot_enh_size_mult > max_enh_size_mult) {
699ac9da0e0SDiego Santa Cruz 		printf("Total enhanced size exceeds maximum (%u > %u)\n",
700ac9da0e0SDiego Santa Cruz 		       tot_enh_size_mult, max_enh_size_mult);
701ac9da0e0SDiego Santa Cruz 		return -EMEDIUMTYPE;
702ac9da0e0SDiego Santa Cruz 	}
703ac9da0e0SDiego Santa Cruz 
7048dda5b0eSDiego Santa Cruz 	/* The default value of EXT_CSD_WR_REL_SET is device
7058dda5b0eSDiego Santa Cruz 	 * dependent, the values can only be changed if the
7068dda5b0eSDiego Santa Cruz 	 * EXT_CSD_HS_CTRL_REL bit is set. The values can be
7078dda5b0eSDiego Santa Cruz 	 * changed only once and before partitioning is completed. */
7088dda5b0eSDiego Santa Cruz 	wr_rel_set = ext_csd[EXT_CSD_WR_REL_SET];
7098dda5b0eSDiego Santa Cruz 	if (conf->user.wr_rel_change) {
7108dda5b0eSDiego Santa Cruz 		if (conf->user.wr_rel_set)
7118dda5b0eSDiego Santa Cruz 			wr_rel_set |= EXT_CSD_WR_DATA_REL_USR;
7128dda5b0eSDiego Santa Cruz 		else
7138dda5b0eSDiego Santa Cruz 			wr_rel_set &= ~EXT_CSD_WR_DATA_REL_USR;
7148dda5b0eSDiego Santa Cruz 	}
7158dda5b0eSDiego Santa Cruz 	for (pidx = 0; pidx < 4; pidx++) {
7168dda5b0eSDiego Santa Cruz 		if (conf->gp_part[pidx].wr_rel_change) {
7178dda5b0eSDiego Santa Cruz 			if (conf->gp_part[pidx].wr_rel_set)
7188dda5b0eSDiego Santa Cruz 				wr_rel_set |= EXT_CSD_WR_DATA_REL_GP(pidx);
7198dda5b0eSDiego Santa Cruz 			else
7208dda5b0eSDiego Santa Cruz 				wr_rel_set &= ~EXT_CSD_WR_DATA_REL_GP(pidx);
7218dda5b0eSDiego Santa Cruz 		}
7228dda5b0eSDiego Santa Cruz 	}
7238dda5b0eSDiego Santa Cruz 
7248dda5b0eSDiego Santa Cruz 	if (wr_rel_set != ext_csd[EXT_CSD_WR_REL_SET] &&
7258dda5b0eSDiego Santa Cruz 	    !(ext_csd[EXT_CSD_WR_REL_PARAM] & EXT_CSD_HS_CTRL_REL)) {
7268dda5b0eSDiego Santa Cruz 		puts("Card does not support host controlled partition write "
7278dda5b0eSDiego Santa Cruz 		     "reliability settings\n");
7288dda5b0eSDiego Santa Cruz 		return -EMEDIUMTYPE;
7298dda5b0eSDiego Santa Cruz 	}
7308dda5b0eSDiego Santa Cruz 
731ac9da0e0SDiego Santa Cruz 	if (ext_csd[EXT_CSD_PARTITION_SETTING] &
732ac9da0e0SDiego Santa Cruz 	    EXT_CSD_PARTITION_SETTING_COMPLETED) {
733ac9da0e0SDiego Santa Cruz 		printf("Card already partitioned\n");
734ac9da0e0SDiego Santa Cruz 		return -EPERM;
735ac9da0e0SDiego Santa Cruz 	}
736ac9da0e0SDiego Santa Cruz 
737ac9da0e0SDiego Santa Cruz 	if (mode == MMC_HWPART_CONF_CHECK)
738ac9da0e0SDiego Santa Cruz 		return 0;
739ac9da0e0SDiego Santa Cruz 
740ac9da0e0SDiego Santa Cruz 	/* Partitioning requires high-capacity size definitions */
741ac9da0e0SDiego Santa Cruz 	if (!(ext_csd[EXT_CSD_ERASE_GROUP_DEF] & 0x01)) {
742ac9da0e0SDiego Santa Cruz 		err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
743ac9da0e0SDiego Santa Cruz 				 EXT_CSD_ERASE_GROUP_DEF, 1);
744ac9da0e0SDiego Santa Cruz 
745ac9da0e0SDiego Santa Cruz 		if (err)
746ac9da0e0SDiego Santa Cruz 			return err;
747ac9da0e0SDiego Santa Cruz 
748ac9da0e0SDiego Santa Cruz 		ext_csd[EXT_CSD_ERASE_GROUP_DEF] = 1;
749ac9da0e0SDiego Santa Cruz 
750ac9da0e0SDiego Santa Cruz 		/* update erase group size to be high-capacity */
751ac9da0e0SDiego Santa Cruz 		mmc->erase_grp_size =
752ac9da0e0SDiego Santa Cruz 			ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] * 1024;
753ac9da0e0SDiego Santa Cruz 
754ac9da0e0SDiego Santa Cruz 	}
755ac9da0e0SDiego Santa Cruz 
756ac9da0e0SDiego Santa Cruz 	/* all OK, write the configuration */
757ac9da0e0SDiego Santa Cruz 	for (i = 0; i < 4; i++) {
758ac9da0e0SDiego Santa Cruz 		err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
759ac9da0e0SDiego Santa Cruz 				 EXT_CSD_ENH_START_ADDR+i,
760ac9da0e0SDiego Santa Cruz 				 (enh_start_addr >> (i*8)) & 0xFF);
761ac9da0e0SDiego Santa Cruz 		if (err)
762ac9da0e0SDiego Santa Cruz 			return err;
763ac9da0e0SDiego Santa Cruz 	}
764ac9da0e0SDiego Santa Cruz 	for (i = 0; i < 3; i++) {
765ac9da0e0SDiego Santa Cruz 		err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
766ac9da0e0SDiego Santa Cruz 				 EXT_CSD_ENH_SIZE_MULT+i,
767ac9da0e0SDiego Santa Cruz 				 (enh_size_mult >> (i*8)) & 0xFF);
768ac9da0e0SDiego Santa Cruz 		if (err)
769ac9da0e0SDiego Santa Cruz 			return err;
770ac9da0e0SDiego Santa Cruz 	}
771ac9da0e0SDiego Santa Cruz 	for (pidx = 0; pidx < 4; pidx++) {
772ac9da0e0SDiego Santa Cruz 		for (i = 0; i < 3; i++) {
773ac9da0e0SDiego Santa Cruz 			err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
774ac9da0e0SDiego Santa Cruz 					 EXT_CSD_GP_SIZE_MULT+pidx*3+i,
775ac9da0e0SDiego Santa Cruz 					 (gp_size_mult[pidx] >> (i*8)) & 0xFF);
776ac9da0e0SDiego Santa Cruz 			if (err)
777ac9da0e0SDiego Santa Cruz 				return err;
778ac9da0e0SDiego Santa Cruz 		}
779ac9da0e0SDiego Santa Cruz 	}
780ac9da0e0SDiego Santa Cruz 	err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
781ac9da0e0SDiego Santa Cruz 			 EXT_CSD_PARTITIONS_ATTRIBUTE, part_attrs);
782ac9da0e0SDiego Santa Cruz 	if (err)
783ac9da0e0SDiego Santa Cruz 		return err;
784ac9da0e0SDiego Santa Cruz 
785ac9da0e0SDiego Santa Cruz 	if (mode == MMC_HWPART_CONF_SET)
786ac9da0e0SDiego Santa Cruz 		return 0;
787ac9da0e0SDiego Santa Cruz 
7888dda5b0eSDiego Santa Cruz 	/* The WR_REL_SET is a write-once register but shall be
7898dda5b0eSDiego Santa Cruz 	 * written before setting PART_SETTING_COMPLETED. As it is
7908dda5b0eSDiego Santa Cruz 	 * write-once we can only write it when completing the
7918dda5b0eSDiego Santa Cruz 	 * partitioning. */
7928dda5b0eSDiego Santa Cruz 	if (wr_rel_set != ext_csd[EXT_CSD_WR_REL_SET]) {
7938dda5b0eSDiego Santa Cruz 		err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
7948dda5b0eSDiego Santa Cruz 				 EXT_CSD_WR_REL_SET, wr_rel_set);
7958dda5b0eSDiego Santa Cruz 		if (err)
7968dda5b0eSDiego Santa Cruz 			return err;
7978dda5b0eSDiego Santa Cruz 	}
7988dda5b0eSDiego Santa Cruz 
799ac9da0e0SDiego Santa Cruz 	/* Setting PART_SETTING_COMPLETED confirms the partition
800ac9da0e0SDiego Santa Cruz 	 * configuration but it only becomes effective after power
801ac9da0e0SDiego Santa Cruz 	 * cycle, so we do not adjust the partition related settings
802ac9da0e0SDiego Santa Cruz 	 * in the mmc struct. */
803ac9da0e0SDiego Santa Cruz 
804ac9da0e0SDiego Santa Cruz 	err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
805ac9da0e0SDiego Santa Cruz 			 EXT_CSD_PARTITION_SETTING,
806ac9da0e0SDiego Santa Cruz 			 EXT_CSD_PARTITION_SETTING_COMPLETED);
807ac9da0e0SDiego Santa Cruz 	if (err)
808ac9da0e0SDiego Santa Cruz 		return err;
809ac9da0e0SDiego Santa Cruz 
810ac9da0e0SDiego Santa Cruz 	return 0;
811ac9da0e0SDiego Santa Cruz }
812ac9da0e0SDiego Santa Cruz 
81348972d90SThierry Reding int mmc_getcd(struct mmc *mmc)
81448972d90SThierry Reding {
81548972d90SThierry Reding 	int cd;
81648972d90SThierry Reding 
81748972d90SThierry Reding 	cd = board_mmc_getcd(mmc);
81848972d90SThierry Reding 
819d4e1da4eSPeter Korsgaard 	if (cd < 0) {
82093bfd616SPantelis Antoniou 		if (mmc->cfg->ops->getcd)
82193bfd616SPantelis Antoniou 			cd = mmc->cfg->ops->getcd(mmc);
822d4e1da4eSPeter Korsgaard 		else
823d4e1da4eSPeter Korsgaard 			cd = 1;
824d4e1da4eSPeter Korsgaard 	}
82548972d90SThierry Reding 
82648972d90SThierry Reding 	return cd;
82748972d90SThierry Reding }
82848972d90SThierry Reding 
829fdbb873eSKim Phillips static int sd_switch(struct mmc *mmc, int mode, int group, u8 value, u8 *resp)
830272cc70bSAndy Fleming {
831272cc70bSAndy Fleming 	struct mmc_cmd cmd;
832272cc70bSAndy Fleming 	struct mmc_data data;
833272cc70bSAndy Fleming 
834272cc70bSAndy Fleming 	/* Switch the frequency */
835272cc70bSAndy Fleming 	cmd.cmdidx = SD_CMD_SWITCH_FUNC;
836272cc70bSAndy Fleming 	cmd.resp_type = MMC_RSP_R1;
837272cc70bSAndy Fleming 	cmd.cmdarg = (mode << 31) | 0xffffff;
838272cc70bSAndy Fleming 	cmd.cmdarg &= ~(0xf << (group * 4));
839272cc70bSAndy Fleming 	cmd.cmdarg |= value << (group * 4);
840272cc70bSAndy Fleming 
841272cc70bSAndy Fleming 	data.dest = (char *)resp;
842272cc70bSAndy Fleming 	data.blocksize = 64;
843272cc70bSAndy Fleming 	data.blocks = 1;
844272cc70bSAndy Fleming 	data.flags = MMC_DATA_READ;
845272cc70bSAndy Fleming 
846272cc70bSAndy Fleming 	return mmc_send_cmd(mmc, &cmd, &data);
847272cc70bSAndy Fleming }
848272cc70bSAndy Fleming 
849272cc70bSAndy Fleming 
850fdbb873eSKim Phillips static int sd_change_freq(struct mmc *mmc)
851272cc70bSAndy Fleming {
852272cc70bSAndy Fleming 	int err;
853272cc70bSAndy Fleming 	struct mmc_cmd cmd;
854f781dd38SAnton staaf 	ALLOC_CACHE_ALIGN_BUFFER(uint, scr, 2);
855f781dd38SAnton staaf 	ALLOC_CACHE_ALIGN_BUFFER(uint, switch_status, 16);
856272cc70bSAndy Fleming 	struct mmc_data data;
857272cc70bSAndy Fleming 	int timeout;
858272cc70bSAndy Fleming 
859272cc70bSAndy Fleming 	mmc->card_caps = 0;
860272cc70bSAndy Fleming 
861d52ebf10SThomas Chou 	if (mmc_host_is_spi(mmc))
862d52ebf10SThomas Chou 		return 0;
863d52ebf10SThomas Chou 
864272cc70bSAndy Fleming 	/* Read the SCR to find out if this card supports higher speeds */
865272cc70bSAndy Fleming 	cmd.cmdidx = MMC_CMD_APP_CMD;
866272cc70bSAndy Fleming 	cmd.resp_type = MMC_RSP_R1;
867272cc70bSAndy Fleming 	cmd.cmdarg = mmc->rca << 16;
868272cc70bSAndy Fleming 
869272cc70bSAndy Fleming 	err = mmc_send_cmd(mmc, &cmd, NULL);
870272cc70bSAndy Fleming 
871272cc70bSAndy Fleming 	if (err)
872272cc70bSAndy Fleming 		return err;
873272cc70bSAndy Fleming 
874272cc70bSAndy Fleming 	cmd.cmdidx = SD_CMD_APP_SEND_SCR;
875272cc70bSAndy Fleming 	cmd.resp_type = MMC_RSP_R1;
876272cc70bSAndy Fleming 	cmd.cmdarg = 0;
877272cc70bSAndy Fleming 
878272cc70bSAndy Fleming 	timeout = 3;
879272cc70bSAndy Fleming 
880272cc70bSAndy Fleming retry_scr:
881f781dd38SAnton staaf 	data.dest = (char *)scr;
882272cc70bSAndy Fleming 	data.blocksize = 8;
883272cc70bSAndy Fleming 	data.blocks = 1;
884272cc70bSAndy Fleming 	data.flags = MMC_DATA_READ;
885272cc70bSAndy Fleming 
886272cc70bSAndy Fleming 	err = mmc_send_cmd(mmc, &cmd, &data);
887272cc70bSAndy Fleming 
888272cc70bSAndy Fleming 	if (err) {
889272cc70bSAndy Fleming 		if (timeout--)
890272cc70bSAndy Fleming 			goto retry_scr;
891272cc70bSAndy Fleming 
892272cc70bSAndy Fleming 		return err;
893272cc70bSAndy Fleming 	}
894272cc70bSAndy Fleming 
8954e3d89baSYauhen Kharuzhy 	mmc->scr[0] = __be32_to_cpu(scr[0]);
8964e3d89baSYauhen Kharuzhy 	mmc->scr[1] = __be32_to_cpu(scr[1]);
897272cc70bSAndy Fleming 
898272cc70bSAndy Fleming 	switch ((mmc->scr[0] >> 24) & 0xf) {
899272cc70bSAndy Fleming 		case 0:
900272cc70bSAndy Fleming 			mmc->version = SD_VERSION_1_0;
901272cc70bSAndy Fleming 			break;
902272cc70bSAndy Fleming 		case 1:
903272cc70bSAndy Fleming 			mmc->version = SD_VERSION_1_10;
904272cc70bSAndy Fleming 			break;
905272cc70bSAndy Fleming 		case 2:
906272cc70bSAndy Fleming 			mmc->version = SD_VERSION_2;
9071741c64dSJaehoon Chung 			if ((mmc->scr[0] >> 15) & 0x1)
9081741c64dSJaehoon Chung 				mmc->version = SD_VERSION_3;
909272cc70bSAndy Fleming 			break;
910272cc70bSAndy Fleming 		default:
911272cc70bSAndy Fleming 			mmc->version = SD_VERSION_1_0;
912272cc70bSAndy Fleming 			break;
913272cc70bSAndy Fleming 	}
914272cc70bSAndy Fleming 
915b44c7083SAlagu Sankar 	if (mmc->scr[0] & SD_DATA_4BIT)
916b44c7083SAlagu Sankar 		mmc->card_caps |= MMC_MODE_4BIT;
917b44c7083SAlagu Sankar 
918272cc70bSAndy Fleming 	/* Version 1.0 doesn't support switching */
919272cc70bSAndy Fleming 	if (mmc->version == SD_VERSION_1_0)
920272cc70bSAndy Fleming 		return 0;
921272cc70bSAndy Fleming 
922272cc70bSAndy Fleming 	timeout = 4;
923272cc70bSAndy Fleming 	while (timeout--) {
924272cc70bSAndy Fleming 		err = sd_switch(mmc, SD_SWITCH_CHECK, 0, 1,
925f781dd38SAnton staaf 				(u8 *)switch_status);
926272cc70bSAndy Fleming 
927272cc70bSAndy Fleming 		if (err)
928272cc70bSAndy Fleming 			return err;
929272cc70bSAndy Fleming 
930272cc70bSAndy Fleming 		/* The high-speed function is busy.  Try again */
9314e3d89baSYauhen Kharuzhy 		if (!(__be32_to_cpu(switch_status[7]) & SD_HIGHSPEED_BUSY))
932272cc70bSAndy Fleming 			break;
933272cc70bSAndy Fleming 	}
934272cc70bSAndy Fleming 
935272cc70bSAndy Fleming 	/* If high-speed isn't supported, we return */
9364e3d89baSYauhen Kharuzhy 	if (!(__be32_to_cpu(switch_status[3]) & SD_HIGHSPEED_SUPPORTED))
937272cc70bSAndy Fleming 		return 0;
938272cc70bSAndy Fleming 
9392c3fbf4cSMacpaul Lin 	/*
9402c3fbf4cSMacpaul Lin 	 * If the host doesn't support SD_HIGHSPEED, do not switch card to
9412c3fbf4cSMacpaul Lin 	 * HIGHSPEED mode even if the card support SD_HIGHSPPED.
9422c3fbf4cSMacpaul Lin 	 * This can avoid furthur problem when the card runs in different
9432c3fbf4cSMacpaul Lin 	 * mode between the host.
9442c3fbf4cSMacpaul Lin 	 */
94593bfd616SPantelis Antoniou 	if (!((mmc->cfg->host_caps & MMC_MODE_HS_52MHz) &&
94693bfd616SPantelis Antoniou 		(mmc->cfg->host_caps & MMC_MODE_HS)))
9472c3fbf4cSMacpaul Lin 		return 0;
9482c3fbf4cSMacpaul Lin 
949f781dd38SAnton staaf 	err = sd_switch(mmc, SD_SWITCH_SWITCH, 0, 1, (u8 *)switch_status);
950272cc70bSAndy Fleming 
951272cc70bSAndy Fleming 	if (err)
952272cc70bSAndy Fleming 		return err;
953272cc70bSAndy Fleming 
9544e3d89baSYauhen Kharuzhy 	if ((__be32_to_cpu(switch_status[4]) & 0x0f000000) == 0x01000000)
955272cc70bSAndy Fleming 		mmc->card_caps |= MMC_MODE_HS;
956272cc70bSAndy Fleming 
957272cc70bSAndy Fleming 	return 0;
958272cc70bSAndy Fleming }
959272cc70bSAndy Fleming 
960272cc70bSAndy Fleming /* frequency bases */
961272cc70bSAndy Fleming /* divided by 10 to be nice to platforms without floating point */
9625f837c2cSMike Frysinger static const int fbase[] = {
963272cc70bSAndy Fleming 	10000,
964272cc70bSAndy Fleming 	100000,
965272cc70bSAndy Fleming 	1000000,
966272cc70bSAndy Fleming 	10000000,
967272cc70bSAndy Fleming };
968272cc70bSAndy Fleming 
969272cc70bSAndy Fleming /* Multiplier values for TRAN_SPEED.  Multiplied by 10 to be nice
970272cc70bSAndy Fleming  * to platforms without floating point.
971272cc70bSAndy Fleming  */
9725f837c2cSMike Frysinger static const int multipliers[] = {
973272cc70bSAndy Fleming 	0,	/* reserved */
974272cc70bSAndy Fleming 	10,
975272cc70bSAndy Fleming 	12,
976272cc70bSAndy Fleming 	13,
977272cc70bSAndy Fleming 	15,
978272cc70bSAndy Fleming 	20,
979272cc70bSAndy Fleming 	25,
980272cc70bSAndy Fleming 	30,
981272cc70bSAndy Fleming 	35,
982272cc70bSAndy Fleming 	40,
983272cc70bSAndy Fleming 	45,
984272cc70bSAndy Fleming 	50,
985272cc70bSAndy Fleming 	55,
986272cc70bSAndy Fleming 	60,
987272cc70bSAndy Fleming 	70,
988272cc70bSAndy Fleming 	80,
989272cc70bSAndy Fleming };
990272cc70bSAndy Fleming 
991fdbb873eSKim Phillips static void mmc_set_ios(struct mmc *mmc)
992272cc70bSAndy Fleming {
99393bfd616SPantelis Antoniou 	if (mmc->cfg->ops->set_ios)
99493bfd616SPantelis Antoniou 		mmc->cfg->ops->set_ios(mmc);
995272cc70bSAndy Fleming }
996272cc70bSAndy Fleming 
997272cc70bSAndy Fleming void mmc_set_clock(struct mmc *mmc, uint clock)
998272cc70bSAndy Fleming {
99993bfd616SPantelis Antoniou 	if (clock > mmc->cfg->f_max)
100093bfd616SPantelis Antoniou 		clock = mmc->cfg->f_max;
1001272cc70bSAndy Fleming 
100293bfd616SPantelis Antoniou 	if (clock < mmc->cfg->f_min)
100393bfd616SPantelis Antoniou 		clock = mmc->cfg->f_min;
1004272cc70bSAndy Fleming 
1005272cc70bSAndy Fleming 	mmc->clock = clock;
1006272cc70bSAndy Fleming 
1007272cc70bSAndy Fleming 	mmc_set_ios(mmc);
1008272cc70bSAndy Fleming }
1009272cc70bSAndy Fleming 
1010fdbb873eSKim Phillips static void mmc_set_bus_width(struct mmc *mmc, uint width)
1011272cc70bSAndy Fleming {
1012272cc70bSAndy Fleming 	mmc->bus_width = width;
1013272cc70bSAndy Fleming 
1014272cc70bSAndy Fleming 	mmc_set_ios(mmc);
1015272cc70bSAndy Fleming }
1016272cc70bSAndy Fleming 
1017fdbb873eSKim Phillips static int mmc_startup(struct mmc *mmc)
1018272cc70bSAndy Fleming {
1019f866a46dSStephen Warren 	int err, i;
1020272cc70bSAndy Fleming 	uint mult, freq;
1021639b7827SYoshihiro Shimoda 	u64 cmult, csize, capacity;
1022272cc70bSAndy Fleming 	struct mmc_cmd cmd;
10238bfa195eSSimon Glass 	ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
10248bfa195eSSimon Glass 	ALLOC_CACHE_ALIGN_BUFFER(u8, test_csd, MMC_MAX_BLOCK_LEN);
10255d4fc8d9SRaffaele Recalcati 	int timeout = 1000;
10260c453bb7SDiego Santa Cruz 	bool has_parts = false;
10278a0cf490SDiego Santa Cruz 	bool part_completed;
1028272cc70bSAndy Fleming 
1029d52ebf10SThomas Chou #ifdef CONFIG_MMC_SPI_CRC_ON
1030d52ebf10SThomas Chou 	if (mmc_host_is_spi(mmc)) { /* enable CRC check for spi */
1031d52ebf10SThomas Chou 		cmd.cmdidx = MMC_CMD_SPI_CRC_ON_OFF;
1032d52ebf10SThomas Chou 		cmd.resp_type = MMC_RSP_R1;
1033d52ebf10SThomas Chou 		cmd.cmdarg = 1;
1034d52ebf10SThomas Chou 		err = mmc_send_cmd(mmc, &cmd, NULL);
1035d52ebf10SThomas Chou 
1036d52ebf10SThomas Chou 		if (err)
1037d52ebf10SThomas Chou 			return err;
1038d52ebf10SThomas Chou 	}
1039d52ebf10SThomas Chou #endif
1040d52ebf10SThomas Chou 
1041272cc70bSAndy Fleming 	/* Put the Card in Identify Mode */
1042d52ebf10SThomas Chou 	cmd.cmdidx = mmc_host_is_spi(mmc) ? MMC_CMD_SEND_CID :
1043d52ebf10SThomas Chou 		MMC_CMD_ALL_SEND_CID; /* cmd not supported in spi */
1044272cc70bSAndy Fleming 	cmd.resp_type = MMC_RSP_R2;
1045272cc70bSAndy Fleming 	cmd.cmdarg = 0;
1046272cc70bSAndy Fleming 
1047272cc70bSAndy Fleming 	err = mmc_send_cmd(mmc, &cmd, NULL);
1048272cc70bSAndy Fleming 
1049272cc70bSAndy Fleming 	if (err)
1050272cc70bSAndy Fleming 		return err;
1051272cc70bSAndy Fleming 
1052272cc70bSAndy Fleming 	memcpy(mmc->cid, cmd.response, 16);
1053272cc70bSAndy Fleming 
1054272cc70bSAndy Fleming 	/*
1055272cc70bSAndy Fleming 	 * For MMC cards, set the Relative Address.
1056272cc70bSAndy Fleming 	 * For SD cards, get the Relatvie Address.
1057272cc70bSAndy Fleming 	 * This also puts the cards into Standby State
1058272cc70bSAndy Fleming 	 */
1059d52ebf10SThomas Chou 	if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
1060272cc70bSAndy Fleming 		cmd.cmdidx = SD_CMD_SEND_RELATIVE_ADDR;
1061272cc70bSAndy Fleming 		cmd.cmdarg = mmc->rca << 16;
1062272cc70bSAndy Fleming 		cmd.resp_type = MMC_RSP_R6;
1063272cc70bSAndy Fleming 
1064272cc70bSAndy Fleming 		err = mmc_send_cmd(mmc, &cmd, NULL);
1065272cc70bSAndy Fleming 
1066272cc70bSAndy Fleming 		if (err)
1067272cc70bSAndy Fleming 			return err;
1068272cc70bSAndy Fleming 
1069272cc70bSAndy Fleming 		if (IS_SD(mmc))
1070998be3ddSRabin Vincent 			mmc->rca = (cmd.response[0] >> 16) & 0xffff;
1071d52ebf10SThomas Chou 	}
1072272cc70bSAndy Fleming 
1073272cc70bSAndy Fleming 	/* Get the Card-Specific Data */
1074272cc70bSAndy Fleming 	cmd.cmdidx = MMC_CMD_SEND_CSD;
1075272cc70bSAndy Fleming 	cmd.resp_type = MMC_RSP_R2;
1076272cc70bSAndy Fleming 	cmd.cmdarg = mmc->rca << 16;
1077272cc70bSAndy Fleming 
1078272cc70bSAndy Fleming 	err = mmc_send_cmd(mmc, &cmd, NULL);
1079272cc70bSAndy Fleming 
10805d4fc8d9SRaffaele Recalcati 	/* Waiting for the ready status */
10815d4fc8d9SRaffaele Recalcati 	mmc_send_status(mmc, timeout);
10825d4fc8d9SRaffaele Recalcati 
1083272cc70bSAndy Fleming 	if (err)
1084272cc70bSAndy Fleming 		return err;
1085272cc70bSAndy Fleming 
1086998be3ddSRabin Vincent 	mmc->csd[0] = cmd.response[0];
1087998be3ddSRabin Vincent 	mmc->csd[1] = cmd.response[1];
1088998be3ddSRabin Vincent 	mmc->csd[2] = cmd.response[2];
1089998be3ddSRabin Vincent 	mmc->csd[3] = cmd.response[3];
1090272cc70bSAndy Fleming 
1091272cc70bSAndy Fleming 	if (mmc->version == MMC_VERSION_UNKNOWN) {
10920b453ffeSRabin Vincent 		int version = (cmd.response[0] >> 26) & 0xf;
1093272cc70bSAndy Fleming 
1094272cc70bSAndy Fleming 		switch (version) {
1095272cc70bSAndy Fleming 			case 0:
1096272cc70bSAndy Fleming 				mmc->version = MMC_VERSION_1_2;
1097272cc70bSAndy Fleming 				break;
1098272cc70bSAndy Fleming 			case 1:
1099272cc70bSAndy Fleming 				mmc->version = MMC_VERSION_1_4;
1100272cc70bSAndy Fleming 				break;
1101272cc70bSAndy Fleming 			case 2:
1102272cc70bSAndy Fleming 				mmc->version = MMC_VERSION_2_2;
1103272cc70bSAndy Fleming 				break;
1104272cc70bSAndy Fleming 			case 3:
1105272cc70bSAndy Fleming 				mmc->version = MMC_VERSION_3;
1106272cc70bSAndy Fleming 				break;
1107272cc70bSAndy Fleming 			case 4:
1108272cc70bSAndy Fleming 				mmc->version = MMC_VERSION_4;
1109272cc70bSAndy Fleming 				break;
1110272cc70bSAndy Fleming 			default:
1111272cc70bSAndy Fleming 				mmc->version = MMC_VERSION_1_2;
1112272cc70bSAndy Fleming 				break;
1113272cc70bSAndy Fleming 		}
1114272cc70bSAndy Fleming 	}
1115272cc70bSAndy Fleming 
1116272cc70bSAndy Fleming 	/* divide frequency by 10, since the mults are 10x bigger */
11170b453ffeSRabin Vincent 	freq = fbase[(cmd.response[0] & 0x7)];
11180b453ffeSRabin Vincent 	mult = multipliers[((cmd.response[0] >> 3) & 0xf)];
1119272cc70bSAndy Fleming 
1120272cc70bSAndy Fleming 	mmc->tran_speed = freq * mult;
1121272cc70bSAndy Fleming 
1122ab71188cSMarkus Niebel 	mmc->dsr_imp = ((cmd.response[1] >> 12) & 0x1);
1123998be3ddSRabin Vincent 	mmc->read_bl_len = 1 << ((cmd.response[1] >> 16) & 0xf);
1124272cc70bSAndy Fleming 
1125272cc70bSAndy Fleming 	if (IS_SD(mmc))
1126272cc70bSAndy Fleming 		mmc->write_bl_len = mmc->read_bl_len;
1127272cc70bSAndy Fleming 	else
1128998be3ddSRabin Vincent 		mmc->write_bl_len = 1 << ((cmd.response[3] >> 22) & 0xf);
1129272cc70bSAndy Fleming 
1130272cc70bSAndy Fleming 	if (mmc->high_capacity) {
1131272cc70bSAndy Fleming 		csize = (mmc->csd[1] & 0x3f) << 16
1132272cc70bSAndy Fleming 			| (mmc->csd[2] & 0xffff0000) >> 16;
1133272cc70bSAndy Fleming 		cmult = 8;
1134272cc70bSAndy Fleming 	} else {
1135272cc70bSAndy Fleming 		csize = (mmc->csd[1] & 0x3ff) << 2
1136272cc70bSAndy Fleming 			| (mmc->csd[2] & 0xc0000000) >> 30;
1137272cc70bSAndy Fleming 		cmult = (mmc->csd[2] & 0x00038000) >> 15;
1138272cc70bSAndy Fleming 	}
1139272cc70bSAndy Fleming 
1140f866a46dSStephen Warren 	mmc->capacity_user = (csize + 1) << (cmult + 2);
1141f866a46dSStephen Warren 	mmc->capacity_user *= mmc->read_bl_len;
1142f866a46dSStephen Warren 	mmc->capacity_boot = 0;
1143f866a46dSStephen Warren 	mmc->capacity_rpmb = 0;
1144f866a46dSStephen Warren 	for (i = 0; i < 4; i++)
1145f866a46dSStephen Warren 		mmc->capacity_gp[i] = 0;
1146272cc70bSAndy Fleming 
11478bfa195eSSimon Glass 	if (mmc->read_bl_len > MMC_MAX_BLOCK_LEN)
11488bfa195eSSimon Glass 		mmc->read_bl_len = MMC_MAX_BLOCK_LEN;
1149272cc70bSAndy Fleming 
11508bfa195eSSimon Glass 	if (mmc->write_bl_len > MMC_MAX_BLOCK_LEN)
11518bfa195eSSimon Glass 		mmc->write_bl_len = MMC_MAX_BLOCK_LEN;
1152272cc70bSAndy Fleming 
1153ab71188cSMarkus Niebel 	if ((mmc->dsr_imp) && (0xffffffff != mmc->dsr)) {
1154ab71188cSMarkus Niebel 		cmd.cmdidx = MMC_CMD_SET_DSR;
1155ab71188cSMarkus Niebel 		cmd.cmdarg = (mmc->dsr & 0xffff) << 16;
1156ab71188cSMarkus Niebel 		cmd.resp_type = MMC_RSP_NONE;
1157ab71188cSMarkus Niebel 		if (mmc_send_cmd(mmc, &cmd, NULL))
1158ab71188cSMarkus Niebel 			printf("MMC: SET_DSR failed\n");
1159ab71188cSMarkus Niebel 	}
1160ab71188cSMarkus Niebel 
1161272cc70bSAndy Fleming 	/* Select the card, and put it into Transfer Mode */
1162d52ebf10SThomas Chou 	if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
1163272cc70bSAndy Fleming 		cmd.cmdidx = MMC_CMD_SELECT_CARD;
1164fe8f7066SAjay Bhargav 		cmd.resp_type = MMC_RSP_R1;
1165272cc70bSAndy Fleming 		cmd.cmdarg = mmc->rca << 16;
1166272cc70bSAndy Fleming 		err = mmc_send_cmd(mmc, &cmd, NULL);
1167272cc70bSAndy Fleming 
1168272cc70bSAndy Fleming 		if (err)
1169272cc70bSAndy Fleming 			return err;
1170d52ebf10SThomas Chou 	}
1171272cc70bSAndy Fleming 
1172e6f99a56SLei Wen 	/*
1173e6f99a56SLei Wen 	 * For SD, its erase group is always one sector
1174e6f99a56SLei Wen 	 */
1175e6f99a56SLei Wen 	mmc->erase_grp_size = 1;
1176bc897b1dSLei Wen 	mmc->part_config = MMCPART_NOAVAILABLE;
1177d23e2c09SSukumar Ghorai 	if (!IS_SD(mmc) && (mmc->version >= MMC_VERSION_4)) {
1178d23e2c09SSukumar Ghorai 		/* check  ext_csd version and capacity */
1179d23e2c09SSukumar Ghorai 		err = mmc_send_ext_csd(mmc, ext_csd);
11809cf199ebSDiego Santa Cruz 		if (err)
11819cf199ebSDiego Santa Cruz 			return err;
11829cf199ebSDiego Santa Cruz 		if (ext_csd[EXT_CSD_REV] >= 2) {
1183639b7827SYoshihiro Shimoda 			/*
1184639b7827SYoshihiro Shimoda 			 * According to the JEDEC Standard, the value of
1185639b7827SYoshihiro Shimoda 			 * ext_csd's capacity is valid if the value is more
1186639b7827SYoshihiro Shimoda 			 * than 2GB
1187639b7827SYoshihiro Shimoda 			 */
11880560db18SLei Wen 			capacity = ext_csd[EXT_CSD_SEC_CNT] << 0
11890560db18SLei Wen 					| ext_csd[EXT_CSD_SEC_CNT + 1] << 8
11900560db18SLei Wen 					| ext_csd[EXT_CSD_SEC_CNT + 2] << 16
11910560db18SLei Wen 					| ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
11928bfa195eSSimon Glass 			capacity *= MMC_MAX_BLOCK_LEN;
1193b1f1e821SŁukasz Majewski 			if ((capacity >> 20) > 2 * 1024)
1194f866a46dSStephen Warren 				mmc->capacity_user = capacity;
1195d23e2c09SSukumar Ghorai 		}
1196bc897b1dSLei Wen 
119764f4a619SJaehoon Chung 		switch (ext_csd[EXT_CSD_REV]) {
119864f4a619SJaehoon Chung 		case 1:
119964f4a619SJaehoon Chung 			mmc->version = MMC_VERSION_4_1;
120064f4a619SJaehoon Chung 			break;
120164f4a619SJaehoon Chung 		case 2:
120264f4a619SJaehoon Chung 			mmc->version = MMC_VERSION_4_2;
120364f4a619SJaehoon Chung 			break;
120464f4a619SJaehoon Chung 		case 3:
120564f4a619SJaehoon Chung 			mmc->version = MMC_VERSION_4_3;
120664f4a619SJaehoon Chung 			break;
120764f4a619SJaehoon Chung 		case 5:
120864f4a619SJaehoon Chung 			mmc->version = MMC_VERSION_4_41;
120964f4a619SJaehoon Chung 			break;
121064f4a619SJaehoon Chung 		case 6:
121164f4a619SJaehoon Chung 			mmc->version = MMC_VERSION_4_5;
121264f4a619SJaehoon Chung 			break;
1213edab723bSMarkus Niebel 		case 7:
1214edab723bSMarkus Niebel 			mmc->version = MMC_VERSION_5_0;
1215edab723bSMarkus Niebel 			break;
121664f4a619SJaehoon Chung 		}
121764f4a619SJaehoon Chung 
12188a0cf490SDiego Santa Cruz 		/* The partition data may be non-zero but it is only
12198a0cf490SDiego Santa Cruz 		 * effective if PARTITION_SETTING_COMPLETED is set in
12208a0cf490SDiego Santa Cruz 		 * EXT_CSD, so ignore any data if this bit is not set,
12218a0cf490SDiego Santa Cruz 		 * except for enabling the high-capacity group size
12228a0cf490SDiego Santa Cruz 		 * definition (see below). */
12238a0cf490SDiego Santa Cruz 		part_completed = !!(ext_csd[EXT_CSD_PARTITION_SETTING] &
12248a0cf490SDiego Santa Cruz 				    EXT_CSD_PARTITION_SETTING_COMPLETED);
12258a0cf490SDiego Santa Cruz 
12260c453bb7SDiego Santa Cruz 		/* store the partition info of emmc */
12270c453bb7SDiego Santa Cruz 		mmc->part_support = ext_csd[EXT_CSD_PARTITIONING_SUPPORT];
12280c453bb7SDiego Santa Cruz 		if ((ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & PART_SUPPORT) ||
12290c453bb7SDiego Santa Cruz 		    ext_csd[EXT_CSD_BOOT_MULT])
12300c453bb7SDiego Santa Cruz 			mmc->part_config = ext_csd[EXT_CSD_PART_CONF];
12318a0cf490SDiego Santa Cruz 		if (part_completed &&
12328a0cf490SDiego Santa Cruz 		    (ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & ENHNCD_SUPPORT))
12330c453bb7SDiego Santa Cruz 			mmc->part_attr = ext_csd[EXT_CSD_PARTITIONS_ATTRIBUTE];
12340c453bb7SDiego Santa Cruz 
12350c453bb7SDiego Santa Cruz 		mmc->capacity_boot = ext_csd[EXT_CSD_BOOT_MULT] << 17;
12360c453bb7SDiego Santa Cruz 
12370c453bb7SDiego Santa Cruz 		mmc->capacity_rpmb = ext_csd[EXT_CSD_RPMB_MULT] << 17;
12380c453bb7SDiego Santa Cruz 
12390c453bb7SDiego Santa Cruz 		for (i = 0; i < 4; i++) {
12400c453bb7SDiego Santa Cruz 			int idx = EXT_CSD_GP_SIZE_MULT + i * 3;
12418a0cf490SDiego Santa Cruz 			uint mult = (ext_csd[idx + 2] << 16) +
12420c453bb7SDiego Santa Cruz 				(ext_csd[idx + 1] << 8) + ext_csd[idx];
12438a0cf490SDiego Santa Cruz 			if (mult)
12448a0cf490SDiego Santa Cruz 				has_parts = true;
12458a0cf490SDiego Santa Cruz 			if (!part_completed)
12468a0cf490SDiego Santa Cruz 				continue;
12478a0cf490SDiego Santa Cruz 			mmc->capacity_gp[i] = mult;
12480c453bb7SDiego Santa Cruz 			mmc->capacity_gp[i] *=
12490c453bb7SDiego Santa Cruz 				ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
12500c453bb7SDiego Santa Cruz 			mmc->capacity_gp[i] *= ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
1251f8e89d67SDiego Santa Cruz 			mmc->capacity_gp[i] <<= 19;
12520c453bb7SDiego Santa Cruz 		}
12530c453bb7SDiego Santa Cruz 
12548a0cf490SDiego Santa Cruz 		if (part_completed) {
1255a7f852b6SDiego Santa Cruz 			mmc->enh_user_size =
1256a7f852b6SDiego Santa Cruz 				(ext_csd[EXT_CSD_ENH_SIZE_MULT+2] << 16) +
1257a7f852b6SDiego Santa Cruz 				(ext_csd[EXT_CSD_ENH_SIZE_MULT+1] << 8) +
1258a7f852b6SDiego Santa Cruz 				ext_csd[EXT_CSD_ENH_SIZE_MULT];
1259a7f852b6SDiego Santa Cruz 			mmc->enh_user_size *= ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
1260a7f852b6SDiego Santa Cruz 			mmc->enh_user_size *= ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
1261a7f852b6SDiego Santa Cruz 			mmc->enh_user_size <<= 19;
1262a7f852b6SDiego Santa Cruz 			mmc->enh_user_start =
1263a7f852b6SDiego Santa Cruz 				(ext_csd[EXT_CSD_ENH_START_ADDR+3] << 24) +
1264a7f852b6SDiego Santa Cruz 				(ext_csd[EXT_CSD_ENH_START_ADDR+2] << 16) +
1265a7f852b6SDiego Santa Cruz 				(ext_csd[EXT_CSD_ENH_START_ADDR+1] << 8) +
1266a7f852b6SDiego Santa Cruz 				ext_csd[EXT_CSD_ENH_START_ADDR];
1267a7f852b6SDiego Santa Cruz 			if (mmc->high_capacity)
1268a7f852b6SDiego Santa Cruz 				mmc->enh_user_start <<= 9;
12698a0cf490SDiego Santa Cruz 		}
1270a7f852b6SDiego Santa Cruz 
1271e6f99a56SLei Wen 		/*
12721937e5aaSOliver Metz 		 * Host needs to enable ERASE_GRP_DEF bit if device is
12731937e5aaSOliver Metz 		 * partitioned. This bit will be lost every time after a reset
12741937e5aaSOliver Metz 		 * or power off. This will affect erase size.
1275e6f99a56SLei Wen 		 */
12768a0cf490SDiego Santa Cruz 		if (part_completed)
12770c453bb7SDiego Santa Cruz 			has_parts = true;
12781937e5aaSOliver Metz 		if ((ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & PART_SUPPORT) &&
12790c453bb7SDiego Santa Cruz 		    (ext_csd[EXT_CSD_PARTITIONS_ATTRIBUTE] & PART_ENH_ATTRIB))
12800c453bb7SDiego Santa Cruz 			has_parts = true;
12810c453bb7SDiego Santa Cruz 		if (has_parts) {
12821937e5aaSOliver Metz 			err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
12831937e5aaSOliver Metz 				EXT_CSD_ERASE_GROUP_DEF, 1);
12841937e5aaSOliver Metz 
12851937e5aaSOliver Metz 			if (err)
12861937e5aaSOliver Metz 				return err;
1287021a8055SHannes Petermaier 			else
1288021a8055SHannes Petermaier 				ext_csd[EXT_CSD_ERASE_GROUP_DEF] = 1;
1289037dc0abSDiego Santa Cruz 		}
12901937e5aaSOliver Metz 
1291037dc0abSDiego Santa Cruz 		if (ext_csd[EXT_CSD_ERASE_GROUP_DEF] & 0x01) {
12921937e5aaSOliver Metz 			/* Read out group size from ext_csd */
12930560db18SLei Wen 			mmc->erase_grp_size =
1294a4ff9f83SDiego Santa Cruz 				ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] * 1024;
1295d7b29129SMarkus Niebel 			/*
1296d7b29129SMarkus Niebel 			 * if high capacity and partition setting completed
1297d7b29129SMarkus Niebel 			 * SEC_COUNT is valid even if it is smaller than 2 GiB
1298d7b29129SMarkus Niebel 			 * JEDEC Standard JESD84-B45, 6.2.4
1299d7b29129SMarkus Niebel 			 */
13008a0cf490SDiego Santa Cruz 			if (mmc->high_capacity && part_completed) {
1301d7b29129SMarkus Niebel 				capacity = (ext_csd[EXT_CSD_SEC_CNT]) |
1302d7b29129SMarkus Niebel 					(ext_csd[EXT_CSD_SEC_CNT + 1] << 8) |
1303d7b29129SMarkus Niebel 					(ext_csd[EXT_CSD_SEC_CNT + 2] << 16) |
1304d7b29129SMarkus Niebel 					(ext_csd[EXT_CSD_SEC_CNT + 3] << 24);
1305d7b29129SMarkus Niebel 				capacity *= MMC_MAX_BLOCK_LEN;
1306d7b29129SMarkus Niebel 				mmc->capacity_user = capacity;
1307d7b29129SMarkus Niebel 			}
13088bfa195eSSimon Glass 		} else {
13091937e5aaSOliver Metz 			/* Calculate the group size from the csd value. */
1310e6f99a56SLei Wen 			int erase_gsz, erase_gmul;
1311e6f99a56SLei Wen 			erase_gsz = (mmc->csd[2] & 0x00007c00) >> 10;
1312e6f99a56SLei Wen 			erase_gmul = (mmc->csd[2] & 0x000003e0) >> 5;
1313e6f99a56SLei Wen 			mmc->erase_grp_size = (erase_gsz + 1)
1314e6f99a56SLei Wen 				* (erase_gmul + 1);
1315e6f99a56SLei Wen 		}
1316037dc0abSDiego Santa Cruz 
1317037dc0abSDiego Santa Cruz 		mmc->hc_wp_grp_size = 1024
1318037dc0abSDiego Santa Cruz 			* ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]
1319037dc0abSDiego Santa Cruz 			* ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
13209e41a00bSDiego Santa Cruz 
13219e41a00bSDiego Santa Cruz 		mmc->wr_rel_set = ext_csd[EXT_CSD_WR_REL_SET];
1322f866a46dSStephen Warren 	}
1323f866a46dSStephen Warren 
1324f866a46dSStephen Warren 	err = mmc_set_capacity(mmc, mmc->part_num);
1325f866a46dSStephen Warren 	if (err)
1326f866a46dSStephen Warren 		return err;
1327d23e2c09SSukumar Ghorai 
1328272cc70bSAndy Fleming 	if (IS_SD(mmc))
1329272cc70bSAndy Fleming 		err = sd_change_freq(mmc);
1330272cc70bSAndy Fleming 	else
1331272cc70bSAndy Fleming 		err = mmc_change_freq(mmc);
1332272cc70bSAndy Fleming 
1333272cc70bSAndy Fleming 	if (err)
1334272cc70bSAndy Fleming 		return err;
1335272cc70bSAndy Fleming 
1336272cc70bSAndy Fleming 	/* Restrict card's capabilities by what the host can do */
133793bfd616SPantelis Antoniou 	mmc->card_caps &= mmc->cfg->host_caps;
1338272cc70bSAndy Fleming 
1339272cc70bSAndy Fleming 	if (IS_SD(mmc)) {
1340272cc70bSAndy Fleming 		if (mmc->card_caps & MMC_MODE_4BIT) {
1341272cc70bSAndy Fleming 			cmd.cmdidx = MMC_CMD_APP_CMD;
1342272cc70bSAndy Fleming 			cmd.resp_type = MMC_RSP_R1;
1343272cc70bSAndy Fleming 			cmd.cmdarg = mmc->rca << 16;
1344272cc70bSAndy Fleming 
1345272cc70bSAndy Fleming 			err = mmc_send_cmd(mmc, &cmd, NULL);
1346272cc70bSAndy Fleming 			if (err)
1347272cc70bSAndy Fleming 				return err;
1348272cc70bSAndy Fleming 
1349272cc70bSAndy Fleming 			cmd.cmdidx = SD_CMD_APP_SET_BUS_WIDTH;
1350272cc70bSAndy Fleming 			cmd.resp_type = MMC_RSP_R1;
1351272cc70bSAndy Fleming 			cmd.cmdarg = 2;
1352272cc70bSAndy Fleming 			err = mmc_send_cmd(mmc, &cmd, NULL);
1353272cc70bSAndy Fleming 			if (err)
1354272cc70bSAndy Fleming 				return err;
1355272cc70bSAndy Fleming 
1356272cc70bSAndy Fleming 			mmc_set_bus_width(mmc, 4);
1357272cc70bSAndy Fleming 		}
1358272cc70bSAndy Fleming 
1359272cc70bSAndy Fleming 		if (mmc->card_caps & MMC_MODE_HS)
1360ad5fd922SJaehoon Chung 			mmc->tran_speed = 50000000;
1361272cc70bSAndy Fleming 		else
1362ad5fd922SJaehoon Chung 			mmc->tran_speed = 25000000;
1363fc5b32fbSAndrew Gabbasov 	} else if (mmc->version >= MMC_VERSION_4) {
1364fc5b32fbSAndrew Gabbasov 		/* Only version 4 of MMC supports wider bus widths */
13657798f6dbSAndy Fleming 		int idx;
13667798f6dbSAndy Fleming 
13677798f6dbSAndy Fleming 		/* An array of possible bus widths in order of preference */
13687798f6dbSAndy Fleming 		static unsigned ext_csd_bits[] = {
1369d22e3d46SJaehoon Chung 			EXT_CSD_DDR_BUS_WIDTH_8,
1370d22e3d46SJaehoon Chung 			EXT_CSD_DDR_BUS_WIDTH_4,
13717798f6dbSAndy Fleming 			EXT_CSD_BUS_WIDTH_8,
13727798f6dbSAndy Fleming 			EXT_CSD_BUS_WIDTH_4,
13737798f6dbSAndy Fleming 			EXT_CSD_BUS_WIDTH_1,
13747798f6dbSAndy Fleming 		};
13757798f6dbSAndy Fleming 
13767798f6dbSAndy Fleming 		/* An array to map CSD bus widths to host cap bits */
13777798f6dbSAndy Fleming 		static unsigned ext_to_hostcaps[] = {
1378786e8f81SAndrew Gabbasov 			[EXT_CSD_DDR_BUS_WIDTH_4] =
1379786e8f81SAndrew Gabbasov 				MMC_MODE_DDR_52MHz | MMC_MODE_4BIT,
1380786e8f81SAndrew Gabbasov 			[EXT_CSD_DDR_BUS_WIDTH_8] =
1381786e8f81SAndrew Gabbasov 				MMC_MODE_DDR_52MHz | MMC_MODE_8BIT,
13827798f6dbSAndy Fleming 			[EXT_CSD_BUS_WIDTH_4] = MMC_MODE_4BIT,
13837798f6dbSAndy Fleming 			[EXT_CSD_BUS_WIDTH_8] = MMC_MODE_8BIT,
13847798f6dbSAndy Fleming 		};
13857798f6dbSAndy Fleming 
13867798f6dbSAndy Fleming 		/* An array to map chosen bus width to an integer */
13877798f6dbSAndy Fleming 		static unsigned widths[] = {
1388d22e3d46SJaehoon Chung 			8, 4, 8, 4, 1,
13897798f6dbSAndy Fleming 		};
13907798f6dbSAndy Fleming 
13917798f6dbSAndy Fleming 		for (idx=0; idx < ARRAY_SIZE(ext_csd_bits); idx++) {
13927798f6dbSAndy Fleming 			unsigned int extw = ext_csd_bits[idx];
1393786e8f81SAndrew Gabbasov 			unsigned int caps = ext_to_hostcaps[extw];
13947798f6dbSAndy Fleming 
13957798f6dbSAndy Fleming 			/*
1396bf477073SAndrew Gabbasov 			 * If the bus width is still not changed,
1397bf477073SAndrew Gabbasov 			 * don't try to set the default again.
1398bf477073SAndrew Gabbasov 			 * Otherwise, recover from switch attempts
1399bf477073SAndrew Gabbasov 			 * by switching to 1-bit bus width.
1400bf477073SAndrew Gabbasov 			 */
1401bf477073SAndrew Gabbasov 			if (extw == EXT_CSD_BUS_WIDTH_1 &&
1402bf477073SAndrew Gabbasov 					mmc->bus_width == 1) {
1403bf477073SAndrew Gabbasov 				err = 0;
1404bf477073SAndrew Gabbasov 				break;
1405bf477073SAndrew Gabbasov 			}
1406bf477073SAndrew Gabbasov 
1407bf477073SAndrew Gabbasov 			/*
1408786e8f81SAndrew Gabbasov 			 * Check to make sure the card and controller support
1409786e8f81SAndrew Gabbasov 			 * these capabilities
14107798f6dbSAndy Fleming 			 */
1411786e8f81SAndrew Gabbasov 			if ((mmc->card_caps & caps) != caps)
14127798f6dbSAndy Fleming 				continue;
14137798f6dbSAndy Fleming 
1414272cc70bSAndy Fleming 			err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
14157798f6dbSAndy Fleming 					EXT_CSD_BUS_WIDTH, extw);
1416272cc70bSAndy Fleming 
1417272cc70bSAndy Fleming 			if (err)
14184137894eSLei Wen 				continue;
1419272cc70bSAndy Fleming 
1420786e8f81SAndrew Gabbasov 			mmc->ddr_mode = (caps & MMC_MODE_DDR_52MHz) ? 1 : 0;
14217798f6dbSAndy Fleming 			mmc_set_bus_width(mmc, widths[idx]);
1422272cc70bSAndy Fleming 
14234137894eSLei Wen 			err = mmc_send_ext_csd(mmc, test_csd);
1424272cc70bSAndy Fleming 
1425786e8f81SAndrew Gabbasov 			if (err)
1426786e8f81SAndrew Gabbasov 				continue;
1427786e8f81SAndrew Gabbasov 
1428786e8f81SAndrew Gabbasov 			/* Only compare read only fields */
1429786e8f81SAndrew Gabbasov 			if (ext_csd[EXT_CSD_PARTITIONING_SUPPORT]
1430786e8f81SAndrew Gabbasov 				== test_csd[EXT_CSD_PARTITIONING_SUPPORT] &&
1431786e8f81SAndrew Gabbasov 			    ext_csd[EXT_CSD_HC_WP_GRP_SIZE]
1432786e8f81SAndrew Gabbasov 				== test_csd[EXT_CSD_HC_WP_GRP_SIZE] &&
1433786e8f81SAndrew Gabbasov 			    ext_csd[EXT_CSD_REV]
1434786e8f81SAndrew Gabbasov 				== test_csd[EXT_CSD_REV] &&
1435786e8f81SAndrew Gabbasov 			    ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]
1436786e8f81SAndrew Gabbasov 				== test_csd[EXT_CSD_HC_ERASE_GRP_SIZE] &&
1437786e8f81SAndrew Gabbasov 			    memcmp(&ext_csd[EXT_CSD_SEC_CNT],
1438786e8f81SAndrew Gabbasov 				   &test_csd[EXT_CSD_SEC_CNT], 4) == 0)
14394137894eSLei Wen 				break;
1440786e8f81SAndrew Gabbasov 			else
1441786e8f81SAndrew Gabbasov 				err = SWITCH_ERR;
14424137894eSLei Wen 		}
1443786e8f81SAndrew Gabbasov 
1444786e8f81SAndrew Gabbasov 		if (err)
1445786e8f81SAndrew Gabbasov 			return err;
1446272cc70bSAndy Fleming 
1447272cc70bSAndy Fleming 		if (mmc->card_caps & MMC_MODE_HS) {
1448272cc70bSAndy Fleming 			if (mmc->card_caps & MMC_MODE_HS_52MHz)
1449ad5fd922SJaehoon Chung 				mmc->tran_speed = 52000000;
1450272cc70bSAndy Fleming 			else
1451ad5fd922SJaehoon Chung 				mmc->tran_speed = 26000000;
1452272cc70bSAndy Fleming 		}
1453ad5fd922SJaehoon Chung 	}
1454ad5fd922SJaehoon Chung 
1455ad5fd922SJaehoon Chung 	mmc_set_clock(mmc, mmc->tran_speed);
1456272cc70bSAndy Fleming 
14575af8f45cSAndrew Gabbasov 	/* Fix the block length for DDR mode */
14585af8f45cSAndrew Gabbasov 	if (mmc->ddr_mode) {
14595af8f45cSAndrew Gabbasov 		mmc->read_bl_len = MMC_MAX_BLOCK_LEN;
14605af8f45cSAndrew Gabbasov 		mmc->write_bl_len = MMC_MAX_BLOCK_LEN;
14615af8f45cSAndrew Gabbasov 	}
14625af8f45cSAndrew Gabbasov 
1463272cc70bSAndy Fleming 	/* fill in device description */
1464272cc70bSAndy Fleming 	mmc->block_dev.lun = 0;
1465272cc70bSAndy Fleming 	mmc->block_dev.type = 0;
1466272cc70bSAndy Fleming 	mmc->block_dev.blksz = mmc->read_bl_len;
14670472fbfdSEgbert Eich 	mmc->block_dev.log2blksz = LOG2(mmc->block_dev.blksz);
14689b1f942cSRabin Vincent 	mmc->block_dev.lba = lldiv(mmc->capacity, mmc->read_bl_len);
146956196826SPaul Burton #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
1470babce5f6STaylor Hutt 	sprintf(mmc->block_dev.vendor, "Man %06x Snr %04x%04x",
1471babce5f6STaylor Hutt 		mmc->cid[0] >> 24, (mmc->cid[2] & 0xffff),
1472babce5f6STaylor Hutt 		(mmc->cid[3] >> 16) & 0xffff);
1473babce5f6STaylor Hutt 	sprintf(mmc->block_dev.product, "%c%c%c%c%c%c", mmc->cid[0] & 0xff,
14740b453ffeSRabin Vincent 		(mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
1475babce5f6STaylor Hutt 		(mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff,
1476babce5f6STaylor Hutt 		(mmc->cid[2] >> 24) & 0xff);
1477babce5f6STaylor Hutt 	sprintf(mmc->block_dev.revision, "%d.%d", (mmc->cid[2] >> 20) & 0xf,
1478babce5f6STaylor Hutt 		(mmc->cid[2] >> 16) & 0xf);
147956196826SPaul Burton #else
148056196826SPaul Burton 	mmc->block_dev.vendor[0] = 0;
148156196826SPaul Burton 	mmc->block_dev.product[0] = 0;
148256196826SPaul Burton 	mmc->block_dev.revision[0] = 0;
148356196826SPaul Burton #endif
1484122efd43SMikhail Kshevetskiy #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBDISK_SUPPORT)
1485272cc70bSAndy Fleming 	init_part(&mmc->block_dev);
1486122efd43SMikhail Kshevetskiy #endif
1487272cc70bSAndy Fleming 
1488272cc70bSAndy Fleming 	return 0;
1489272cc70bSAndy Fleming }
1490272cc70bSAndy Fleming 
1491fdbb873eSKim Phillips static int mmc_send_if_cond(struct mmc *mmc)
1492272cc70bSAndy Fleming {
1493272cc70bSAndy Fleming 	struct mmc_cmd cmd;
1494272cc70bSAndy Fleming 	int err;
1495272cc70bSAndy Fleming 
1496272cc70bSAndy Fleming 	cmd.cmdidx = SD_CMD_SEND_IF_COND;
1497272cc70bSAndy Fleming 	/* We set the bit if the host supports voltages between 2.7 and 3.6 V */
149893bfd616SPantelis Antoniou 	cmd.cmdarg = ((mmc->cfg->voltages & 0xff8000) != 0) << 8 | 0xaa;
1499272cc70bSAndy Fleming 	cmd.resp_type = MMC_RSP_R7;
1500272cc70bSAndy Fleming 
1501272cc70bSAndy Fleming 	err = mmc_send_cmd(mmc, &cmd, NULL);
1502272cc70bSAndy Fleming 
1503272cc70bSAndy Fleming 	if (err)
1504272cc70bSAndy Fleming 		return err;
1505272cc70bSAndy Fleming 
1506998be3ddSRabin Vincent 	if ((cmd.response[0] & 0xff) != 0xaa)
1507272cc70bSAndy Fleming 		return UNUSABLE_ERR;
1508272cc70bSAndy Fleming 	else
1509272cc70bSAndy Fleming 		mmc->version = SD_VERSION_2;
1510272cc70bSAndy Fleming 
1511272cc70bSAndy Fleming 	return 0;
1512272cc70bSAndy Fleming }
1513272cc70bSAndy Fleming 
151493bfd616SPantelis Antoniou /* not used any more */
151593bfd616SPantelis Antoniou int __deprecated mmc_register(struct mmc *mmc)
1516272cc70bSAndy Fleming {
151793bfd616SPantelis Antoniou #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
151893bfd616SPantelis Antoniou 	printf("%s is deprecated! use mmc_create() instead.\n", __func__);
151993bfd616SPantelis Antoniou #endif
152093bfd616SPantelis Antoniou 	return -1;
152193bfd616SPantelis Antoniou }
152293bfd616SPantelis Antoniou 
152393bfd616SPantelis Antoniou struct mmc *mmc_create(const struct mmc_config *cfg, void *priv)
152493bfd616SPantelis Antoniou {
152593bfd616SPantelis Antoniou 	struct mmc *mmc;
152693bfd616SPantelis Antoniou 
152793bfd616SPantelis Antoniou 	/* quick validation */
152893bfd616SPantelis Antoniou 	if (cfg == NULL || cfg->ops == NULL || cfg->ops->send_cmd == NULL ||
152993bfd616SPantelis Antoniou 			cfg->f_min == 0 || cfg->f_max == 0 || cfg->b_max == 0)
153093bfd616SPantelis Antoniou 		return NULL;
153193bfd616SPantelis Antoniou 
153293bfd616SPantelis Antoniou 	mmc = calloc(1, sizeof(*mmc));
153393bfd616SPantelis Antoniou 	if (mmc == NULL)
153493bfd616SPantelis Antoniou 		return NULL;
153593bfd616SPantelis Antoniou 
153693bfd616SPantelis Antoniou 	mmc->cfg = cfg;
153793bfd616SPantelis Antoniou 	mmc->priv = priv;
153893bfd616SPantelis Antoniou 
153993bfd616SPantelis Antoniou 	/* the following chunk was mmc_register() */
154093bfd616SPantelis Antoniou 
1541ab71188cSMarkus Niebel 	/* Setup dsr related values */
1542ab71188cSMarkus Niebel 	mmc->dsr_imp = 0;
1543ab71188cSMarkus Niebel 	mmc->dsr = 0xffffffff;
1544272cc70bSAndy Fleming 	/* Setup the universal parts of the block interface just once */
1545272cc70bSAndy Fleming 	mmc->block_dev.if_type = IF_TYPE_MMC;
1546272cc70bSAndy Fleming 	mmc->block_dev.dev = cur_dev_num++;
1547272cc70bSAndy Fleming 	mmc->block_dev.removable = 1;
1548272cc70bSAndy Fleming 	mmc->block_dev.block_read = mmc_bread;
1549272cc70bSAndy Fleming 	mmc->block_dev.block_write = mmc_bwrite;
1550e6f99a56SLei Wen 	mmc->block_dev.block_erase = mmc_berase;
155193bfd616SPantelis Antoniou 
155293bfd616SPantelis Antoniou 	/* setup initial part type */
155393bfd616SPantelis Antoniou 	mmc->block_dev.part_type = mmc->cfg->part_type;
1554272cc70bSAndy Fleming 
1555272cc70bSAndy Fleming 	INIT_LIST_HEAD(&mmc->link);
1556272cc70bSAndy Fleming 
1557272cc70bSAndy Fleming 	list_add_tail(&mmc->link, &mmc_devices);
1558272cc70bSAndy Fleming 
155993bfd616SPantelis Antoniou 	return mmc;
156093bfd616SPantelis Antoniou }
156193bfd616SPantelis Antoniou 
156293bfd616SPantelis Antoniou void mmc_destroy(struct mmc *mmc)
156393bfd616SPantelis Antoniou {
156493bfd616SPantelis Antoniou 	/* only freeing memory for now */
156593bfd616SPantelis Antoniou 	free(mmc);
1566272cc70bSAndy Fleming }
1567272cc70bSAndy Fleming 
1568df3fc526SMatthew McClintock #ifdef CONFIG_PARTITIONS
1569272cc70bSAndy Fleming block_dev_desc_t *mmc_get_dev(int dev)
1570272cc70bSAndy Fleming {
1571272cc70bSAndy Fleming 	struct mmc *mmc = find_mmc_device(dev);
15726bb4b4bcSBenoît Thébaudeau 	if (!mmc || mmc_init(mmc))
157340242bc3SŁukasz Majewski 		return NULL;
1574272cc70bSAndy Fleming 
157540242bc3SŁukasz Majewski 	return &mmc->block_dev;
1576272cc70bSAndy Fleming }
1577df3fc526SMatthew McClintock #endif
1578272cc70bSAndy Fleming 
157995de9ab2SPaul Kocialkowski /* board-specific MMC power initializations. */
158095de9ab2SPaul Kocialkowski __weak void board_mmc_power_init(void)
158195de9ab2SPaul Kocialkowski {
158295de9ab2SPaul Kocialkowski }
158395de9ab2SPaul Kocialkowski 
1584e9550449SChe-Liang Chiou int mmc_start_init(struct mmc *mmc)
1585272cc70bSAndy Fleming {
1586afd5932bSMacpaul Lin 	int err;
1587272cc70bSAndy Fleming 
1588ab769f22SPantelis Antoniou 	/* we pretend there's no card when init is NULL */
158993bfd616SPantelis Antoniou 	if (mmc_getcd(mmc) == 0 || mmc->cfg->ops->init == NULL) {
159048972d90SThierry Reding 		mmc->has_init = 0;
159156196826SPaul Burton #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
159248972d90SThierry Reding 		printf("MMC: no card present\n");
159356196826SPaul Burton #endif
159448972d90SThierry Reding 		return NO_CARD_ERR;
159548972d90SThierry Reding 	}
159648972d90SThierry Reding 
1597bc897b1dSLei Wen 	if (mmc->has_init)
1598bc897b1dSLei Wen 		return 0;
1599bc897b1dSLei Wen 
16005a8dbdc6SYangbo Lu #ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT
16015a8dbdc6SYangbo Lu 	mmc_adapter_card_type_ident();
16025a8dbdc6SYangbo Lu #endif
160395de9ab2SPaul Kocialkowski 	board_mmc_power_init();
160495de9ab2SPaul Kocialkowski 
1605ab769f22SPantelis Antoniou 	/* made sure it's not NULL earlier */
160693bfd616SPantelis Antoniou 	err = mmc->cfg->ops->init(mmc);
1607272cc70bSAndy Fleming 
1608272cc70bSAndy Fleming 	if (err)
1609272cc70bSAndy Fleming 		return err;
1610272cc70bSAndy Fleming 
1611786e8f81SAndrew Gabbasov 	mmc->ddr_mode = 0;
1612b86b85e2SIlya Yanok 	mmc_set_bus_width(mmc, 1);
1613b86b85e2SIlya Yanok 	mmc_set_clock(mmc, 1);
1614b86b85e2SIlya Yanok 
1615272cc70bSAndy Fleming 	/* Reset the Card */
1616272cc70bSAndy Fleming 	err = mmc_go_idle(mmc);
1617272cc70bSAndy Fleming 
1618272cc70bSAndy Fleming 	if (err)
1619272cc70bSAndy Fleming 		return err;
1620272cc70bSAndy Fleming 
1621bc897b1dSLei Wen 	/* The internal partition reset to user partition(0) at every CMD0*/
1622bc897b1dSLei Wen 	mmc->part_num = 0;
1623bc897b1dSLei Wen 
1624272cc70bSAndy Fleming 	/* Test for SD version 2 */
1625272cc70bSAndy Fleming 	err = mmc_send_if_cond(mmc);
1626272cc70bSAndy Fleming 
1627272cc70bSAndy Fleming 	/* Now try to get the SD card's operating condition */
1628272cc70bSAndy Fleming 	err = sd_send_op_cond(mmc);
1629272cc70bSAndy Fleming 
1630272cc70bSAndy Fleming 	/* If the command timed out, we check for an MMC card */
1631272cc70bSAndy Fleming 	if (err == TIMEOUT) {
1632272cc70bSAndy Fleming 		err = mmc_send_op_cond(mmc);
1633272cc70bSAndy Fleming 
1634bd47c135SAndrew Gabbasov 		if (err) {
163556196826SPaul Burton #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
1636272cc70bSAndy Fleming 			printf("Card did not respond to voltage select!\n");
163756196826SPaul Burton #endif
1638272cc70bSAndy Fleming 			return UNUSABLE_ERR;
1639272cc70bSAndy Fleming 		}
1640272cc70bSAndy Fleming 	}
1641272cc70bSAndy Fleming 
1642bd47c135SAndrew Gabbasov 	if (!err)
1643e9550449SChe-Liang Chiou 		mmc->init_in_progress = 1;
1644e9550449SChe-Liang Chiou 
1645e9550449SChe-Liang Chiou 	return err;
1646e9550449SChe-Liang Chiou }
1647e9550449SChe-Liang Chiou 
1648e9550449SChe-Liang Chiou static int mmc_complete_init(struct mmc *mmc)
1649e9550449SChe-Liang Chiou {
1650e9550449SChe-Liang Chiou 	int err = 0;
1651e9550449SChe-Liang Chiou 
1652bd47c135SAndrew Gabbasov 	mmc->init_in_progress = 0;
1653e9550449SChe-Liang Chiou 	if (mmc->op_cond_pending)
1654e9550449SChe-Liang Chiou 		err = mmc_complete_op_cond(mmc);
1655e9550449SChe-Liang Chiou 
1656e9550449SChe-Liang Chiou 	if (!err)
1657bc897b1dSLei Wen 		err = mmc_startup(mmc);
1658bc897b1dSLei Wen 	if (err)
1659bc897b1dSLei Wen 		mmc->has_init = 0;
1660bc897b1dSLei Wen 	else
1661bc897b1dSLei Wen 		mmc->has_init = 1;
1662e9550449SChe-Liang Chiou 	return err;
1663e9550449SChe-Liang Chiou }
1664e9550449SChe-Liang Chiou 
1665e9550449SChe-Liang Chiou int mmc_init(struct mmc *mmc)
1666e9550449SChe-Liang Chiou {
1667bd47c135SAndrew Gabbasov 	int err = 0;
1668d803fea5SMateusz Zalega 	unsigned start;
1669e9550449SChe-Liang Chiou 
1670e9550449SChe-Liang Chiou 	if (mmc->has_init)
1671e9550449SChe-Liang Chiou 		return 0;
1672d803fea5SMateusz Zalega 
1673d803fea5SMateusz Zalega 	start = get_timer(0);
1674d803fea5SMateusz Zalega 
1675e9550449SChe-Liang Chiou 	if (!mmc->init_in_progress)
1676e9550449SChe-Liang Chiou 		err = mmc_start_init(mmc);
1677e9550449SChe-Liang Chiou 
1678bd47c135SAndrew Gabbasov 	if (!err)
1679e9550449SChe-Liang Chiou 		err = mmc_complete_init(mmc);
1680e9550449SChe-Liang Chiou 	debug("%s: %d, time %lu\n", __func__, err, get_timer(start));
1681bc897b1dSLei Wen 	return err;
1682272cc70bSAndy Fleming }
1683272cc70bSAndy Fleming 
1684ab71188cSMarkus Niebel int mmc_set_dsr(struct mmc *mmc, u16 val)
1685ab71188cSMarkus Niebel {
1686ab71188cSMarkus Niebel 	mmc->dsr = val;
1687ab71188cSMarkus Niebel 	return 0;
1688ab71188cSMarkus Niebel }
1689ab71188cSMarkus Niebel 
1690cee9ab7cSJeroen Hofstee /* CPU-specific MMC initializations */
1691cee9ab7cSJeroen Hofstee __weak int cpu_mmc_init(bd_t *bis)
1692272cc70bSAndy Fleming {
1693272cc70bSAndy Fleming 	return -1;
1694272cc70bSAndy Fleming }
1695272cc70bSAndy Fleming 
1696cee9ab7cSJeroen Hofstee /* board-specific MMC initializations. */
1697cee9ab7cSJeroen Hofstee __weak int board_mmc_init(bd_t *bis)
1698cee9ab7cSJeroen Hofstee {
1699cee9ab7cSJeroen Hofstee 	return -1;
1700cee9ab7cSJeroen Hofstee }
1701272cc70bSAndy Fleming 
170256196826SPaul Burton #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
170356196826SPaul Burton 
1704272cc70bSAndy Fleming void print_mmc_devices(char separator)
1705272cc70bSAndy Fleming {
1706272cc70bSAndy Fleming 	struct mmc *m;
1707272cc70bSAndy Fleming 	struct list_head *entry;
170834dd9284SPrzemyslaw Marczak 	char *mmc_type;
1709272cc70bSAndy Fleming 
1710272cc70bSAndy Fleming 	list_for_each(entry, &mmc_devices) {
1711272cc70bSAndy Fleming 		m = list_entry(entry, struct mmc, link);
1712272cc70bSAndy Fleming 
171334dd9284SPrzemyslaw Marczak 		if (m->has_init)
171434dd9284SPrzemyslaw Marczak 			mmc_type = IS_SD(m) ? "SD" : "eMMC";
171534dd9284SPrzemyslaw Marczak 		else
171634dd9284SPrzemyslaw Marczak 			mmc_type = NULL;
171734dd9284SPrzemyslaw Marczak 
171893bfd616SPantelis Antoniou 		printf("%s: %d", m->cfg->name, m->block_dev.dev);
171934dd9284SPrzemyslaw Marczak 		if (mmc_type)
172034dd9284SPrzemyslaw Marczak 			printf(" (%s)", mmc_type);
1721272cc70bSAndy Fleming 
1722e75eaf10SLubomir Popov 		if (entry->next != &mmc_devices) {
1723272cc70bSAndy Fleming 			printf("%c", separator);
1724e75eaf10SLubomir Popov 			if (separator != '\n')
1725e75eaf10SLubomir Popov 				puts (" ");
1726e75eaf10SLubomir Popov 		}
1727272cc70bSAndy Fleming 	}
1728272cc70bSAndy Fleming 
1729272cc70bSAndy Fleming 	printf("\n");
1730272cc70bSAndy Fleming }
1731272cc70bSAndy Fleming 
173256196826SPaul Burton #else
173356196826SPaul Burton void print_mmc_devices(char separator) { }
173456196826SPaul Burton #endif
173556196826SPaul Burton 
1736ea6ebe21SLei Wen int get_mmc_num(void)
1737ea6ebe21SLei Wen {
1738ea6ebe21SLei Wen 	return cur_dev_num;
1739ea6ebe21SLei Wen }
1740ea6ebe21SLei Wen 
1741e9550449SChe-Liang Chiou void mmc_set_preinit(struct mmc *mmc, int preinit)
1742e9550449SChe-Liang Chiou {
1743e9550449SChe-Liang Chiou 	mmc->preinit = preinit;
1744e9550449SChe-Liang Chiou }
1745e9550449SChe-Liang Chiou 
1746e9550449SChe-Liang Chiou static void do_preinit(void)
1747e9550449SChe-Liang Chiou {
1748e9550449SChe-Liang Chiou 	struct mmc *m;
1749e9550449SChe-Liang Chiou 	struct list_head *entry;
1750e9550449SChe-Liang Chiou 
1751e9550449SChe-Liang Chiou 	list_for_each(entry, &mmc_devices) {
1752e9550449SChe-Liang Chiou 		m = list_entry(entry, struct mmc, link);
1753e9550449SChe-Liang Chiou 
17545a8dbdc6SYangbo Lu #ifdef CONFIG_FSL_ESDHC_ADAPTER_IDENT
17555a8dbdc6SYangbo Lu 		mmc_set_preinit(m, 1);
17565a8dbdc6SYangbo Lu #endif
1757e9550449SChe-Liang Chiou 		if (m->preinit)
1758e9550449SChe-Liang Chiou 			mmc_start_init(m);
1759e9550449SChe-Liang Chiou 	}
1760e9550449SChe-Liang Chiou }
1761e9550449SChe-Liang Chiou 
1762e9550449SChe-Liang Chiou 
1763272cc70bSAndy Fleming int mmc_initialize(bd_t *bis)
1764272cc70bSAndy Fleming {
1765*1b26bab1SDaniel Kochmański 	static int initialized = 0;
1766*1b26bab1SDaniel Kochmański 	if (initialized)	/* Avoid initializing mmc multiple times */
1767*1b26bab1SDaniel Kochmański 		return 0;
1768*1b26bab1SDaniel Kochmański 	initialized = 1;
1769*1b26bab1SDaniel Kochmański 
1770272cc70bSAndy Fleming 	INIT_LIST_HEAD (&mmc_devices);
1771272cc70bSAndy Fleming 	cur_dev_num = 0;
1772272cc70bSAndy Fleming 
1773e7ecf7cbSSimon Glass #ifndef CONFIG_DM_MMC
1774272cc70bSAndy Fleming 	if (board_mmc_init(bis) < 0)
1775272cc70bSAndy Fleming 		cpu_mmc_init(bis);
1776e7ecf7cbSSimon Glass #endif
1777272cc70bSAndy Fleming 
1778bb0dc108SYing Zhang #ifndef CONFIG_SPL_BUILD
1779272cc70bSAndy Fleming 	print_mmc_devices(',');
1780bb0dc108SYing Zhang #endif
1781272cc70bSAndy Fleming 
1782e9550449SChe-Liang Chiou 	do_preinit();
1783272cc70bSAndy Fleming 	return 0;
1784272cc70bSAndy Fleming }
17853690d6d6SAmar 
17863690d6d6SAmar #ifdef CONFIG_SUPPORT_EMMC_BOOT
17873690d6d6SAmar /*
17883690d6d6SAmar  * This function changes the size of boot partition and the size of rpmb
17893690d6d6SAmar  * partition present on EMMC devices.
17903690d6d6SAmar  *
17913690d6d6SAmar  * Input Parameters:
17923690d6d6SAmar  * struct *mmc: pointer for the mmc device strcuture
17933690d6d6SAmar  * bootsize: size of boot partition
17943690d6d6SAmar  * rpmbsize: size of rpmb partition
17953690d6d6SAmar  *
17963690d6d6SAmar  * Returns 0 on success.
17973690d6d6SAmar  */
17983690d6d6SAmar 
17993690d6d6SAmar int mmc_boot_partition_size_change(struct mmc *mmc, unsigned long bootsize,
18003690d6d6SAmar 				unsigned long rpmbsize)
18013690d6d6SAmar {
18023690d6d6SAmar 	int err;
18033690d6d6SAmar 	struct mmc_cmd cmd;
18043690d6d6SAmar 
18053690d6d6SAmar 	/* Only use this command for raw EMMC moviNAND. Enter backdoor mode */
18063690d6d6SAmar 	cmd.cmdidx = MMC_CMD_RES_MAN;
18073690d6d6SAmar 	cmd.resp_type = MMC_RSP_R1b;
18083690d6d6SAmar 	cmd.cmdarg = MMC_CMD62_ARG1;
18093690d6d6SAmar 
18103690d6d6SAmar 	err = mmc_send_cmd(mmc, &cmd, NULL);
18113690d6d6SAmar 	if (err) {
18123690d6d6SAmar 		debug("mmc_boot_partition_size_change: Error1 = %d\n", err);
18133690d6d6SAmar 		return err;
18143690d6d6SAmar 	}
18153690d6d6SAmar 
18163690d6d6SAmar 	/* Boot partition changing mode */
18173690d6d6SAmar 	cmd.cmdidx = MMC_CMD_RES_MAN;
18183690d6d6SAmar 	cmd.resp_type = MMC_RSP_R1b;
18193690d6d6SAmar 	cmd.cmdarg = MMC_CMD62_ARG2;
18203690d6d6SAmar 
18213690d6d6SAmar 	err = mmc_send_cmd(mmc, &cmd, NULL);
18223690d6d6SAmar 	if (err) {
18233690d6d6SAmar 		debug("mmc_boot_partition_size_change: Error2 = %d\n", err);
18243690d6d6SAmar 		return err;
18253690d6d6SAmar 	}
18263690d6d6SAmar 	/* boot partition size is multiple of 128KB */
18273690d6d6SAmar 	bootsize = (bootsize * 1024) / 128;
18283690d6d6SAmar 
18293690d6d6SAmar 	/* Arg: boot partition size */
18303690d6d6SAmar 	cmd.cmdidx = MMC_CMD_RES_MAN;
18313690d6d6SAmar 	cmd.resp_type = MMC_RSP_R1b;
18323690d6d6SAmar 	cmd.cmdarg = bootsize;
18333690d6d6SAmar 
18343690d6d6SAmar 	err = mmc_send_cmd(mmc, &cmd, NULL);
18353690d6d6SAmar 	if (err) {
18363690d6d6SAmar 		debug("mmc_boot_partition_size_change: Error3 = %d\n", err);
18373690d6d6SAmar 		return err;
18383690d6d6SAmar 	}
18393690d6d6SAmar 	/* RPMB partition size is multiple of 128KB */
18403690d6d6SAmar 	rpmbsize = (rpmbsize * 1024) / 128;
18413690d6d6SAmar 	/* Arg: RPMB partition size */
18423690d6d6SAmar 	cmd.cmdidx = MMC_CMD_RES_MAN;
18433690d6d6SAmar 	cmd.resp_type = MMC_RSP_R1b;
18443690d6d6SAmar 	cmd.cmdarg = rpmbsize;
18453690d6d6SAmar 
18463690d6d6SAmar 	err = mmc_send_cmd(mmc, &cmd, NULL);
18473690d6d6SAmar 	if (err) {
18483690d6d6SAmar 		debug("mmc_boot_partition_size_change: Error4 = %d\n", err);
18493690d6d6SAmar 		return err;
18503690d6d6SAmar 	}
18513690d6d6SAmar 	return 0;
18523690d6d6SAmar }
18533690d6d6SAmar 
18543690d6d6SAmar /*
18555a99b9deSTom Rini  * Modify EXT_CSD[177] which is BOOT_BUS_WIDTH
18565a99b9deSTom Rini  * based on the passed in values for BOOT_BUS_WIDTH, RESET_BOOT_BUS_WIDTH
18575a99b9deSTom Rini  * and BOOT_MODE.
18585a99b9deSTom Rini  *
18595a99b9deSTom Rini  * Returns 0 on success.
18605a99b9deSTom Rini  */
18615a99b9deSTom Rini int mmc_set_boot_bus_width(struct mmc *mmc, u8 width, u8 reset, u8 mode)
18625a99b9deSTom Rini {
18635a99b9deSTom Rini 	int err;
18645a99b9deSTom Rini 
18655a99b9deSTom Rini 	err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_BUS_WIDTH,
18665a99b9deSTom Rini 			 EXT_CSD_BOOT_BUS_WIDTH_MODE(mode) |
18675a99b9deSTom Rini 			 EXT_CSD_BOOT_BUS_WIDTH_RESET(reset) |
18685a99b9deSTom Rini 			 EXT_CSD_BOOT_BUS_WIDTH_WIDTH(width));
18695a99b9deSTom Rini 
18705a99b9deSTom Rini 	if (err)
18715a99b9deSTom Rini 		return err;
18725a99b9deSTom Rini 	return 0;
18735a99b9deSTom Rini }
18745a99b9deSTom Rini 
18755a99b9deSTom Rini /*
1876792970b0STom Rini  * Modify EXT_CSD[179] which is PARTITION_CONFIG (formerly BOOT_CONFIG)
1877792970b0STom Rini  * based on the passed in values for BOOT_ACK, BOOT_PARTITION_ENABLE and
1878792970b0STom Rini  * PARTITION_ACCESS.
1879792970b0STom Rini  *
1880792970b0STom Rini  * Returns 0 on success.
1881792970b0STom Rini  */
1882792970b0STom Rini int mmc_set_part_conf(struct mmc *mmc, u8 ack, u8 part_num, u8 access)
1883792970b0STom Rini {
1884792970b0STom Rini 	int err;
1885792970b0STom Rini 
1886792970b0STom Rini 	err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONF,
1887792970b0STom Rini 			 EXT_CSD_BOOT_ACK(ack) |
1888792970b0STom Rini 			 EXT_CSD_BOOT_PART_NUM(part_num) |
1889792970b0STom Rini 			 EXT_CSD_PARTITION_ACCESS(access));
1890792970b0STom Rini 
1891792970b0STom Rini 	if (err)
1892792970b0STom Rini 		return err;
1893792970b0STom Rini 	return 0;
1894792970b0STom Rini }
189533ace362STom Rini 
189633ace362STom Rini /*
189733ace362STom Rini  * Modify EXT_CSD[162] which is RST_n_FUNCTION based on the given value
189833ace362STom Rini  * for enable.  Note that this is a write-once field for non-zero values.
189933ace362STom Rini  *
190033ace362STom Rini  * Returns 0 on success.
190133ace362STom Rini  */
190233ace362STom Rini int mmc_set_rst_n_function(struct mmc *mmc, u8 enable)
190333ace362STom Rini {
190433ace362STom Rini 	return mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_RST_N_FUNCTION,
190533ace362STom Rini 			  enable);
190633ace362STom Rini }
19073690d6d6SAmar #endif
1908