xref: /openbmc/u-boot/drivers/mmc/mmc.c (revision 3e01ed00)
1 /*
2  * Copyright 2008, Freescale Semiconductor, Inc
3  * Andy Fleming
4  *
5  * Based vaguely on the Linux code
6  *
7  * SPDX-License-Identifier:	GPL-2.0+
8  */
9 
10 #include <config.h>
11 #include <common.h>
12 #include <command.h>
13 #include <mmc.h>
14 #include <part.h>
15 #include <malloc.h>
16 #include <linux/list.h>
17 #include <div64.h>
18 #include "mmc_private.h"
19 
20 static struct list_head mmc_devices;
21 static int cur_dev_num = -1;
22 
23 int __weak board_mmc_getwp(struct mmc *mmc)
24 {
25 	return -1;
26 }
27 
28 int mmc_getwp(struct mmc *mmc)
29 {
30 	int wp;
31 
32 	wp = board_mmc_getwp(mmc);
33 
34 	if (wp < 0) {
35 		if (mmc->cfg->ops->getwp)
36 			wp = mmc->cfg->ops->getwp(mmc);
37 		else
38 			wp = 0;
39 	}
40 
41 	return wp;
42 }
43 
44 int __board_mmc_getcd(struct mmc *mmc) {
45 	return -1;
46 }
47 
48 int board_mmc_getcd(struct mmc *mmc)__attribute__((weak,
49 	alias("__board_mmc_getcd")));
50 
51 int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
52 {
53 	int ret;
54 
55 #ifdef CONFIG_MMC_TRACE
56 	int i;
57 	u8 *ptr;
58 
59 	printf("CMD_SEND:%d\n", cmd->cmdidx);
60 	printf("\t\tARG\t\t\t 0x%08X\n", cmd->cmdarg);
61 	ret = mmc->cfg->ops->send_cmd(mmc, cmd, data);
62 	switch (cmd->resp_type) {
63 		case MMC_RSP_NONE:
64 			printf("\t\tMMC_RSP_NONE\n");
65 			break;
66 		case MMC_RSP_R1:
67 			printf("\t\tMMC_RSP_R1,5,6,7 \t 0x%08X \n",
68 				cmd->response[0]);
69 			break;
70 		case MMC_RSP_R1b:
71 			printf("\t\tMMC_RSP_R1b\t\t 0x%08X \n",
72 				cmd->response[0]);
73 			break;
74 		case MMC_RSP_R2:
75 			printf("\t\tMMC_RSP_R2\t\t 0x%08X \n",
76 				cmd->response[0]);
77 			printf("\t\t          \t\t 0x%08X \n",
78 				cmd->response[1]);
79 			printf("\t\t          \t\t 0x%08X \n",
80 				cmd->response[2]);
81 			printf("\t\t          \t\t 0x%08X \n",
82 				cmd->response[3]);
83 			printf("\n");
84 			printf("\t\t\t\t\tDUMPING DATA\n");
85 			for (i = 0; i < 4; i++) {
86 				int j;
87 				printf("\t\t\t\t\t%03d - ", i*4);
88 				ptr = (u8 *)&cmd->response[i];
89 				ptr += 3;
90 				for (j = 0; j < 4; j++)
91 					printf("%02X ", *ptr--);
92 				printf("\n");
93 			}
94 			break;
95 		case MMC_RSP_R3:
96 			printf("\t\tMMC_RSP_R3,4\t\t 0x%08X \n",
97 				cmd->response[0]);
98 			break;
99 		default:
100 			printf("\t\tERROR MMC rsp not supported\n");
101 			break;
102 	}
103 #else
104 	ret = mmc->cfg->ops->send_cmd(mmc, cmd, data);
105 #endif
106 	return ret;
107 }
108 
109 int mmc_send_status(struct mmc *mmc, int timeout)
110 {
111 	struct mmc_cmd cmd;
112 	int err, retries = 5;
113 #ifdef CONFIG_MMC_TRACE
114 	int status;
115 #endif
116 
117 	cmd.cmdidx = MMC_CMD_SEND_STATUS;
118 	cmd.resp_type = MMC_RSP_R1;
119 	if (!mmc_host_is_spi(mmc))
120 		cmd.cmdarg = mmc->rca << 16;
121 
122 	do {
123 		err = mmc_send_cmd(mmc, &cmd, NULL);
124 		if (!err) {
125 			if ((cmd.response[0] & MMC_STATUS_RDY_FOR_DATA) &&
126 			    (cmd.response[0] & MMC_STATUS_CURR_STATE) !=
127 			     MMC_STATE_PRG)
128 				break;
129 			else if (cmd.response[0] & MMC_STATUS_MASK) {
130 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
131 				printf("Status Error: 0x%08X\n",
132 					cmd.response[0]);
133 #endif
134 				return COMM_ERR;
135 			}
136 		} else if (--retries < 0)
137 			return err;
138 
139 		udelay(1000);
140 
141 	} while (timeout--);
142 
143 #ifdef CONFIG_MMC_TRACE
144 	status = (cmd.response[0] & MMC_STATUS_CURR_STATE) >> 9;
145 	printf("CURR STATE:%d\n", status);
146 #endif
147 	if (timeout <= 0) {
148 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
149 		printf("Timeout waiting card ready\n");
150 #endif
151 		return TIMEOUT;
152 	}
153 	if (cmd.response[0] & MMC_STATUS_SWITCH_ERROR)
154 		return SWITCH_ERR;
155 
156 	return 0;
157 }
158 
159 int mmc_set_blocklen(struct mmc *mmc, int len)
160 {
161 	struct mmc_cmd cmd;
162 
163 	if (mmc->card_caps & MMC_MODE_DDR_52MHz)
164 		return 0;
165 
166 	cmd.cmdidx = MMC_CMD_SET_BLOCKLEN;
167 	cmd.resp_type = MMC_RSP_R1;
168 	cmd.cmdarg = len;
169 
170 	return mmc_send_cmd(mmc, &cmd, NULL);
171 }
172 
173 struct mmc *find_mmc_device(int dev_num)
174 {
175 	struct mmc *m;
176 	struct list_head *entry;
177 
178 	list_for_each(entry, &mmc_devices) {
179 		m = list_entry(entry, struct mmc, link);
180 
181 		if (m->block_dev.dev == dev_num)
182 			return m;
183 	}
184 
185 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
186 	printf("MMC Device %d not found\n", dev_num);
187 #endif
188 
189 	return NULL;
190 }
191 
192 static int mmc_read_blocks(struct mmc *mmc, void *dst, lbaint_t start,
193 			   lbaint_t blkcnt)
194 {
195 	struct mmc_cmd cmd;
196 	struct mmc_data data;
197 
198 	if (blkcnt > 1)
199 		cmd.cmdidx = MMC_CMD_READ_MULTIPLE_BLOCK;
200 	else
201 		cmd.cmdidx = MMC_CMD_READ_SINGLE_BLOCK;
202 
203 	if (mmc->high_capacity)
204 		cmd.cmdarg = start;
205 	else
206 		cmd.cmdarg = start * mmc->read_bl_len;
207 
208 	cmd.resp_type = MMC_RSP_R1;
209 
210 	data.dest = dst;
211 	data.blocks = blkcnt;
212 	data.blocksize = mmc->read_bl_len;
213 	data.flags = MMC_DATA_READ;
214 
215 	if (mmc_send_cmd(mmc, &cmd, &data))
216 		return 0;
217 
218 	if (blkcnt > 1) {
219 		cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
220 		cmd.cmdarg = 0;
221 		cmd.resp_type = MMC_RSP_R1b;
222 		if (mmc_send_cmd(mmc, &cmd, NULL)) {
223 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
224 			printf("mmc fail to send stop cmd\n");
225 #endif
226 			return 0;
227 		}
228 	}
229 
230 	return blkcnt;
231 }
232 
233 static ulong mmc_bread(int dev_num, lbaint_t start, lbaint_t blkcnt, void *dst)
234 {
235 	lbaint_t cur, blocks_todo = blkcnt;
236 
237 	if (blkcnt == 0)
238 		return 0;
239 
240 	struct mmc *mmc = find_mmc_device(dev_num);
241 	if (!mmc)
242 		return 0;
243 
244 	if ((start + blkcnt) > mmc->block_dev.lba) {
245 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
246 		printf("MMC: block number 0x" LBAF " exceeds max(0x" LBAF ")\n",
247 			start + blkcnt, mmc->block_dev.lba);
248 #endif
249 		return 0;
250 	}
251 
252 	if (mmc_set_blocklen(mmc, mmc->read_bl_len))
253 		return 0;
254 
255 	do {
256 		cur = (blocks_todo > mmc->cfg->b_max) ?
257 			mmc->cfg->b_max : blocks_todo;
258 		if(mmc_read_blocks(mmc, dst, start, cur) != cur)
259 			return 0;
260 		blocks_todo -= cur;
261 		start += cur;
262 		dst += cur * mmc->read_bl_len;
263 	} while (blocks_todo > 0);
264 
265 	return blkcnt;
266 }
267 
268 static int mmc_go_idle(struct mmc *mmc)
269 {
270 	struct mmc_cmd cmd;
271 	int err;
272 
273 	udelay(1000);
274 
275 	cmd.cmdidx = MMC_CMD_GO_IDLE_STATE;
276 	cmd.cmdarg = 0;
277 	cmd.resp_type = MMC_RSP_NONE;
278 
279 	err = mmc_send_cmd(mmc, &cmd, NULL);
280 
281 	if (err)
282 		return err;
283 
284 	udelay(2000);
285 
286 	return 0;
287 }
288 
289 static int sd_send_op_cond(struct mmc *mmc)
290 {
291 	int timeout = 1000;
292 	int err;
293 	struct mmc_cmd cmd;
294 
295 	do {
296 		cmd.cmdidx = MMC_CMD_APP_CMD;
297 		cmd.resp_type = MMC_RSP_R1;
298 		cmd.cmdarg = 0;
299 
300 		err = mmc_send_cmd(mmc, &cmd, NULL);
301 
302 		if (err)
303 			return err;
304 
305 		cmd.cmdidx = SD_CMD_APP_SEND_OP_COND;
306 		cmd.resp_type = MMC_RSP_R3;
307 
308 		/*
309 		 * Most cards do not answer if some reserved bits
310 		 * in the ocr are set. However, Some controller
311 		 * can set bit 7 (reserved for low voltages), but
312 		 * how to manage low voltages SD card is not yet
313 		 * specified.
314 		 */
315 		cmd.cmdarg = mmc_host_is_spi(mmc) ? 0 :
316 			(mmc->cfg->voltages & 0xff8000);
317 
318 		if (mmc->version == SD_VERSION_2)
319 			cmd.cmdarg |= OCR_HCS;
320 
321 		err = mmc_send_cmd(mmc, &cmd, NULL);
322 
323 		if (err)
324 			return err;
325 
326 		udelay(1000);
327 	} while ((!(cmd.response[0] & OCR_BUSY)) && timeout--);
328 
329 	if (timeout <= 0)
330 		return UNUSABLE_ERR;
331 
332 	if (mmc->version != SD_VERSION_2)
333 		mmc->version = SD_VERSION_1_0;
334 
335 	if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
336 		cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
337 		cmd.resp_type = MMC_RSP_R3;
338 		cmd.cmdarg = 0;
339 
340 		err = mmc_send_cmd(mmc, &cmd, NULL);
341 
342 		if (err)
343 			return err;
344 	}
345 
346 	mmc->ocr = cmd.response[0];
347 
348 	mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
349 	mmc->rca = 0;
350 
351 	return 0;
352 }
353 
354 /* We pass in the cmd since otherwise the init seems to fail */
355 static int mmc_send_op_cond_iter(struct mmc *mmc, struct mmc_cmd *cmd,
356 		int use_arg)
357 {
358 	int err;
359 
360 	cmd->cmdidx = MMC_CMD_SEND_OP_COND;
361 	cmd->resp_type = MMC_RSP_R3;
362 	cmd->cmdarg = 0;
363 	if (use_arg && !mmc_host_is_spi(mmc)) {
364 		cmd->cmdarg =
365 			(mmc->cfg->voltages &
366 			(mmc->op_cond_response & OCR_VOLTAGE_MASK)) |
367 			(mmc->op_cond_response & OCR_ACCESS_MODE);
368 
369 		if (mmc->cfg->host_caps & MMC_MODE_HC)
370 			cmd->cmdarg |= OCR_HCS;
371 	}
372 	err = mmc_send_cmd(mmc, cmd, NULL);
373 	if (err)
374 		return err;
375 	mmc->op_cond_response = cmd->response[0];
376 	return 0;
377 }
378 
379 int mmc_send_op_cond(struct mmc *mmc)
380 {
381 	struct mmc_cmd cmd;
382 	int err, i;
383 
384 	/* Some cards seem to need this */
385 	mmc_go_idle(mmc);
386 
387  	/* Asking to the card its capabilities */
388 	mmc->op_cond_pending = 1;
389 	for (i = 0; i < 2; i++) {
390 		err = mmc_send_op_cond_iter(mmc, &cmd, i != 0);
391 		if (err)
392 			return err;
393 
394 		/* exit if not busy (flag seems to be inverted) */
395 		if (mmc->op_cond_response & OCR_BUSY)
396 			return 0;
397 	}
398 	return IN_PROGRESS;
399 }
400 
401 int mmc_complete_op_cond(struct mmc *mmc)
402 {
403 	struct mmc_cmd cmd;
404 	int timeout = 1000;
405 	uint start;
406 	int err;
407 
408 	mmc->op_cond_pending = 0;
409 	start = get_timer(0);
410 	do {
411 		err = mmc_send_op_cond_iter(mmc, &cmd, 1);
412 		if (err)
413 			return err;
414 		if (get_timer(start) > timeout)
415 			return UNUSABLE_ERR;
416 		udelay(100);
417 	} while (!(mmc->op_cond_response & OCR_BUSY));
418 
419 	if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
420 		cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
421 		cmd.resp_type = MMC_RSP_R3;
422 		cmd.cmdarg = 0;
423 
424 		err = mmc_send_cmd(mmc, &cmd, NULL);
425 
426 		if (err)
427 			return err;
428 	}
429 
430 	mmc->version = MMC_VERSION_UNKNOWN;
431 	mmc->ocr = cmd.response[0];
432 
433 	mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
434 	mmc->rca = 1;
435 
436 	return 0;
437 }
438 
439 
440 static int mmc_send_ext_csd(struct mmc *mmc, u8 *ext_csd)
441 {
442 	struct mmc_cmd cmd;
443 	struct mmc_data data;
444 	int err;
445 
446 	/* Get the Card Status Register */
447 	cmd.cmdidx = MMC_CMD_SEND_EXT_CSD;
448 	cmd.resp_type = MMC_RSP_R1;
449 	cmd.cmdarg = 0;
450 
451 	data.dest = (char *)ext_csd;
452 	data.blocks = 1;
453 	data.blocksize = MMC_MAX_BLOCK_LEN;
454 	data.flags = MMC_DATA_READ;
455 
456 	err = mmc_send_cmd(mmc, &cmd, &data);
457 
458 	return err;
459 }
460 
461 
462 static int mmc_switch(struct mmc *mmc, u8 set, u8 index, u8 value)
463 {
464 	struct mmc_cmd cmd;
465 	int timeout = 1000;
466 	int ret;
467 
468 	cmd.cmdidx = MMC_CMD_SWITCH;
469 	cmd.resp_type = MMC_RSP_R1b;
470 	cmd.cmdarg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
471 				 (index << 16) |
472 				 (value << 8);
473 
474 	ret = mmc_send_cmd(mmc, &cmd, NULL);
475 
476 	/* Waiting for the ready status */
477 	if (!ret)
478 		ret = mmc_send_status(mmc, timeout);
479 
480 	return ret;
481 
482 }
483 
484 static int mmc_change_freq(struct mmc *mmc)
485 {
486 	ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
487 	char cardtype;
488 	int err;
489 
490 	mmc->card_caps = 0;
491 
492 	if (mmc_host_is_spi(mmc))
493 		return 0;
494 
495 	/* Only version 4 supports high-speed */
496 	if (mmc->version < MMC_VERSION_4)
497 		return 0;
498 
499 	err = mmc_send_ext_csd(mmc, ext_csd);
500 
501 	if (err)
502 		return err;
503 
504 	cardtype = ext_csd[EXT_CSD_CARD_TYPE] & 0xf;
505 
506 	err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING, 1);
507 
508 	if (err)
509 		return err == SWITCH_ERR ? 0 : err;
510 
511 	/* Now check to see that it worked */
512 	err = mmc_send_ext_csd(mmc, ext_csd);
513 
514 	if (err)
515 		return err;
516 
517 	/* No high-speed support */
518 	if (!ext_csd[EXT_CSD_HS_TIMING])
519 		return 0;
520 
521 	/* High Speed is set, there are two types: 52MHz and 26MHz */
522 	if (cardtype & EXT_CSD_CARD_TYPE_52) {
523 		if (cardtype & EXT_CSD_CARD_TYPE_DDR_52)
524 			mmc->card_caps |= MMC_MODE_DDR_52MHz;
525 		mmc->card_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
526 	} else {
527 		mmc->card_caps |= MMC_MODE_HS;
528 	}
529 
530 	return 0;
531 }
532 
533 static int mmc_set_capacity(struct mmc *mmc, int part_num)
534 {
535 	switch (part_num) {
536 	case 0:
537 		mmc->capacity = mmc->capacity_user;
538 		break;
539 	case 1:
540 	case 2:
541 		mmc->capacity = mmc->capacity_boot;
542 		break;
543 	case 3:
544 		mmc->capacity = mmc->capacity_rpmb;
545 		break;
546 	case 4:
547 	case 5:
548 	case 6:
549 	case 7:
550 		mmc->capacity = mmc->capacity_gp[part_num - 4];
551 		break;
552 	default:
553 		return -1;
554 	}
555 
556 	mmc->block_dev.lba = lldiv(mmc->capacity, mmc->read_bl_len);
557 
558 	return 0;
559 }
560 
561 int mmc_select_hwpart(int dev_num, int hwpart)
562 {
563 	struct mmc *mmc = find_mmc_device(dev_num);
564 	int ret;
565 
566 	if (!mmc)
567 		return -1;
568 
569 	if (mmc->part_num == hwpart)
570 		return 0;
571 
572 	if (mmc->part_config == MMCPART_NOAVAILABLE) {
573 		printf("Card doesn't support part_switch\n");
574 		return -1;
575 	}
576 
577 	ret = mmc_switch_part(dev_num, hwpart);
578 	if (ret)
579 		return -1;
580 
581 	mmc->part_num = hwpart;
582 
583 	return 0;
584 }
585 
586 
587 int mmc_switch_part(int dev_num, unsigned int part_num)
588 {
589 	struct mmc *mmc = find_mmc_device(dev_num);
590 	int ret;
591 
592 	if (!mmc)
593 		return -1;
594 
595 	ret = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONF,
596 			 (mmc->part_config & ~PART_ACCESS_MASK)
597 			 | (part_num & PART_ACCESS_MASK));
598 	if (ret)
599 		return ret;
600 
601 	return mmc_set_capacity(mmc, part_num);
602 }
603 
604 int mmc_getcd(struct mmc *mmc)
605 {
606 	int cd;
607 
608 	cd = board_mmc_getcd(mmc);
609 
610 	if (cd < 0) {
611 		if (mmc->cfg->ops->getcd)
612 			cd = mmc->cfg->ops->getcd(mmc);
613 		else
614 			cd = 1;
615 	}
616 
617 	return cd;
618 }
619 
620 static int sd_switch(struct mmc *mmc, int mode, int group, u8 value, u8 *resp)
621 {
622 	struct mmc_cmd cmd;
623 	struct mmc_data data;
624 
625 	/* Switch the frequency */
626 	cmd.cmdidx = SD_CMD_SWITCH_FUNC;
627 	cmd.resp_type = MMC_RSP_R1;
628 	cmd.cmdarg = (mode << 31) | 0xffffff;
629 	cmd.cmdarg &= ~(0xf << (group * 4));
630 	cmd.cmdarg |= value << (group * 4);
631 
632 	data.dest = (char *)resp;
633 	data.blocksize = 64;
634 	data.blocks = 1;
635 	data.flags = MMC_DATA_READ;
636 
637 	return mmc_send_cmd(mmc, &cmd, &data);
638 }
639 
640 
641 static int sd_change_freq(struct mmc *mmc)
642 {
643 	int err;
644 	struct mmc_cmd cmd;
645 	ALLOC_CACHE_ALIGN_BUFFER(uint, scr, 2);
646 	ALLOC_CACHE_ALIGN_BUFFER(uint, switch_status, 16);
647 	struct mmc_data data;
648 	int timeout;
649 
650 	mmc->card_caps = 0;
651 
652 	if (mmc_host_is_spi(mmc))
653 		return 0;
654 
655 	/* Read the SCR to find out if this card supports higher speeds */
656 	cmd.cmdidx = MMC_CMD_APP_CMD;
657 	cmd.resp_type = MMC_RSP_R1;
658 	cmd.cmdarg = mmc->rca << 16;
659 
660 	err = mmc_send_cmd(mmc, &cmd, NULL);
661 
662 	if (err)
663 		return err;
664 
665 	cmd.cmdidx = SD_CMD_APP_SEND_SCR;
666 	cmd.resp_type = MMC_RSP_R1;
667 	cmd.cmdarg = 0;
668 
669 	timeout = 3;
670 
671 retry_scr:
672 	data.dest = (char *)scr;
673 	data.blocksize = 8;
674 	data.blocks = 1;
675 	data.flags = MMC_DATA_READ;
676 
677 	err = mmc_send_cmd(mmc, &cmd, &data);
678 
679 	if (err) {
680 		if (timeout--)
681 			goto retry_scr;
682 
683 		return err;
684 	}
685 
686 	mmc->scr[0] = __be32_to_cpu(scr[0]);
687 	mmc->scr[1] = __be32_to_cpu(scr[1]);
688 
689 	switch ((mmc->scr[0] >> 24) & 0xf) {
690 		case 0:
691 			mmc->version = SD_VERSION_1_0;
692 			break;
693 		case 1:
694 			mmc->version = SD_VERSION_1_10;
695 			break;
696 		case 2:
697 			mmc->version = SD_VERSION_2;
698 			if ((mmc->scr[0] >> 15) & 0x1)
699 				mmc->version = SD_VERSION_3;
700 			break;
701 		default:
702 			mmc->version = SD_VERSION_1_0;
703 			break;
704 	}
705 
706 	if (mmc->scr[0] & SD_DATA_4BIT)
707 		mmc->card_caps |= MMC_MODE_4BIT;
708 
709 	/* Version 1.0 doesn't support switching */
710 	if (mmc->version == SD_VERSION_1_0)
711 		return 0;
712 
713 	timeout = 4;
714 	while (timeout--) {
715 		err = sd_switch(mmc, SD_SWITCH_CHECK, 0, 1,
716 				(u8 *)switch_status);
717 
718 		if (err)
719 			return err;
720 
721 		/* The high-speed function is busy.  Try again */
722 		if (!(__be32_to_cpu(switch_status[7]) & SD_HIGHSPEED_BUSY))
723 			break;
724 	}
725 
726 	/* If high-speed isn't supported, we return */
727 	if (!(__be32_to_cpu(switch_status[3]) & SD_HIGHSPEED_SUPPORTED))
728 		return 0;
729 
730 	/*
731 	 * If the host doesn't support SD_HIGHSPEED, do not switch card to
732 	 * HIGHSPEED mode even if the card support SD_HIGHSPPED.
733 	 * This can avoid furthur problem when the card runs in different
734 	 * mode between the host.
735 	 */
736 	if (!((mmc->cfg->host_caps & MMC_MODE_HS_52MHz) &&
737 		(mmc->cfg->host_caps & MMC_MODE_HS)))
738 		return 0;
739 
740 	err = sd_switch(mmc, SD_SWITCH_SWITCH, 0, 1, (u8 *)switch_status);
741 
742 	if (err)
743 		return err;
744 
745 	if ((__be32_to_cpu(switch_status[4]) & 0x0f000000) == 0x01000000)
746 		mmc->card_caps |= MMC_MODE_HS;
747 
748 	return 0;
749 }
750 
751 /* frequency bases */
752 /* divided by 10 to be nice to platforms without floating point */
753 static const int fbase[] = {
754 	10000,
755 	100000,
756 	1000000,
757 	10000000,
758 };
759 
760 /* Multiplier values for TRAN_SPEED.  Multiplied by 10 to be nice
761  * to platforms without floating point.
762  */
763 static const int multipliers[] = {
764 	0,	/* reserved */
765 	10,
766 	12,
767 	13,
768 	15,
769 	20,
770 	25,
771 	30,
772 	35,
773 	40,
774 	45,
775 	50,
776 	55,
777 	60,
778 	70,
779 	80,
780 };
781 
782 static void mmc_set_ios(struct mmc *mmc)
783 {
784 	if (mmc->cfg->ops->set_ios)
785 		mmc->cfg->ops->set_ios(mmc);
786 }
787 
788 void mmc_set_clock(struct mmc *mmc, uint clock)
789 {
790 	if (clock > mmc->cfg->f_max)
791 		clock = mmc->cfg->f_max;
792 
793 	if (clock < mmc->cfg->f_min)
794 		clock = mmc->cfg->f_min;
795 
796 	mmc->clock = clock;
797 
798 	mmc_set_ios(mmc);
799 }
800 
801 static void mmc_set_bus_width(struct mmc *mmc, uint width)
802 {
803 	mmc->bus_width = width;
804 
805 	mmc_set_ios(mmc);
806 }
807 
808 static int mmc_startup(struct mmc *mmc)
809 {
810 	int err, i;
811 	uint mult, freq;
812 	u64 cmult, csize, capacity;
813 	struct mmc_cmd cmd;
814 	ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, MMC_MAX_BLOCK_LEN);
815 	ALLOC_CACHE_ALIGN_BUFFER(u8, test_csd, MMC_MAX_BLOCK_LEN);
816 	int timeout = 1000;
817 
818 #ifdef CONFIG_MMC_SPI_CRC_ON
819 	if (mmc_host_is_spi(mmc)) { /* enable CRC check for spi */
820 		cmd.cmdidx = MMC_CMD_SPI_CRC_ON_OFF;
821 		cmd.resp_type = MMC_RSP_R1;
822 		cmd.cmdarg = 1;
823 		err = mmc_send_cmd(mmc, &cmd, NULL);
824 
825 		if (err)
826 			return err;
827 	}
828 #endif
829 
830 	/* Put the Card in Identify Mode */
831 	cmd.cmdidx = mmc_host_is_spi(mmc) ? MMC_CMD_SEND_CID :
832 		MMC_CMD_ALL_SEND_CID; /* cmd not supported in spi */
833 	cmd.resp_type = MMC_RSP_R2;
834 	cmd.cmdarg = 0;
835 
836 	err = mmc_send_cmd(mmc, &cmd, NULL);
837 
838 	if (err)
839 		return err;
840 
841 	memcpy(mmc->cid, cmd.response, 16);
842 
843 	/*
844 	 * For MMC cards, set the Relative Address.
845 	 * For SD cards, get the Relatvie Address.
846 	 * This also puts the cards into Standby State
847 	 */
848 	if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
849 		cmd.cmdidx = SD_CMD_SEND_RELATIVE_ADDR;
850 		cmd.cmdarg = mmc->rca << 16;
851 		cmd.resp_type = MMC_RSP_R6;
852 
853 		err = mmc_send_cmd(mmc, &cmd, NULL);
854 
855 		if (err)
856 			return err;
857 
858 		if (IS_SD(mmc))
859 			mmc->rca = (cmd.response[0] >> 16) & 0xffff;
860 	}
861 
862 	/* Get the Card-Specific Data */
863 	cmd.cmdidx = MMC_CMD_SEND_CSD;
864 	cmd.resp_type = MMC_RSP_R2;
865 	cmd.cmdarg = mmc->rca << 16;
866 
867 	err = mmc_send_cmd(mmc, &cmd, NULL);
868 
869 	/* Waiting for the ready status */
870 	mmc_send_status(mmc, timeout);
871 
872 	if (err)
873 		return err;
874 
875 	mmc->csd[0] = cmd.response[0];
876 	mmc->csd[1] = cmd.response[1];
877 	mmc->csd[2] = cmd.response[2];
878 	mmc->csd[3] = cmd.response[3];
879 
880 	if (mmc->version == MMC_VERSION_UNKNOWN) {
881 		int version = (cmd.response[0] >> 26) & 0xf;
882 
883 		switch (version) {
884 			case 0:
885 				mmc->version = MMC_VERSION_1_2;
886 				break;
887 			case 1:
888 				mmc->version = MMC_VERSION_1_4;
889 				break;
890 			case 2:
891 				mmc->version = MMC_VERSION_2_2;
892 				break;
893 			case 3:
894 				mmc->version = MMC_VERSION_3;
895 				break;
896 			case 4:
897 				mmc->version = MMC_VERSION_4;
898 				break;
899 			default:
900 				mmc->version = MMC_VERSION_1_2;
901 				break;
902 		}
903 	}
904 
905 	/* divide frequency by 10, since the mults are 10x bigger */
906 	freq = fbase[(cmd.response[0] & 0x7)];
907 	mult = multipliers[((cmd.response[0] >> 3) & 0xf)];
908 
909 	mmc->tran_speed = freq * mult;
910 
911 	mmc->dsr_imp = ((cmd.response[1] >> 12) & 0x1);
912 	mmc->read_bl_len = 1 << ((cmd.response[1] >> 16) & 0xf);
913 
914 	if (IS_SD(mmc))
915 		mmc->write_bl_len = mmc->read_bl_len;
916 	else
917 		mmc->write_bl_len = 1 << ((cmd.response[3] >> 22) & 0xf);
918 
919 	if (mmc->high_capacity) {
920 		csize = (mmc->csd[1] & 0x3f) << 16
921 			| (mmc->csd[2] & 0xffff0000) >> 16;
922 		cmult = 8;
923 	} else {
924 		csize = (mmc->csd[1] & 0x3ff) << 2
925 			| (mmc->csd[2] & 0xc0000000) >> 30;
926 		cmult = (mmc->csd[2] & 0x00038000) >> 15;
927 	}
928 
929 	mmc->capacity_user = (csize + 1) << (cmult + 2);
930 	mmc->capacity_user *= mmc->read_bl_len;
931 	mmc->capacity_boot = 0;
932 	mmc->capacity_rpmb = 0;
933 	for (i = 0; i < 4; i++)
934 		mmc->capacity_gp[i] = 0;
935 
936 	if (mmc->read_bl_len > MMC_MAX_BLOCK_LEN)
937 		mmc->read_bl_len = MMC_MAX_BLOCK_LEN;
938 
939 	if (mmc->write_bl_len > MMC_MAX_BLOCK_LEN)
940 		mmc->write_bl_len = MMC_MAX_BLOCK_LEN;
941 
942 	if ((mmc->dsr_imp) && (0xffffffff != mmc->dsr)) {
943 		cmd.cmdidx = MMC_CMD_SET_DSR;
944 		cmd.cmdarg = (mmc->dsr & 0xffff) << 16;
945 		cmd.resp_type = MMC_RSP_NONE;
946 		if (mmc_send_cmd(mmc, &cmd, NULL))
947 			printf("MMC: SET_DSR failed\n");
948 	}
949 
950 	/* Select the card, and put it into Transfer Mode */
951 	if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
952 		cmd.cmdidx = MMC_CMD_SELECT_CARD;
953 		cmd.resp_type = MMC_RSP_R1;
954 		cmd.cmdarg = mmc->rca << 16;
955 		err = mmc_send_cmd(mmc, &cmd, NULL);
956 
957 		if (err)
958 			return err;
959 	}
960 
961 	/*
962 	 * For SD, its erase group is always one sector
963 	 */
964 	mmc->erase_grp_size = 1;
965 	mmc->part_config = MMCPART_NOAVAILABLE;
966 	if (!IS_SD(mmc) && (mmc->version >= MMC_VERSION_4)) {
967 		/* check  ext_csd version and capacity */
968 		err = mmc_send_ext_csd(mmc, ext_csd);
969 		if (!err && (ext_csd[EXT_CSD_REV] >= 2)) {
970 			/*
971 			 * According to the JEDEC Standard, the value of
972 			 * ext_csd's capacity is valid if the value is more
973 			 * than 2GB
974 			 */
975 			capacity = ext_csd[EXT_CSD_SEC_CNT] << 0
976 					| ext_csd[EXT_CSD_SEC_CNT + 1] << 8
977 					| ext_csd[EXT_CSD_SEC_CNT + 2] << 16
978 					| ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
979 			capacity *= MMC_MAX_BLOCK_LEN;
980 			if ((capacity >> 20) > 2 * 1024)
981 				mmc->capacity_user = capacity;
982 		}
983 
984 		switch (ext_csd[EXT_CSD_REV]) {
985 		case 1:
986 			mmc->version = MMC_VERSION_4_1;
987 			break;
988 		case 2:
989 			mmc->version = MMC_VERSION_4_2;
990 			break;
991 		case 3:
992 			mmc->version = MMC_VERSION_4_3;
993 			break;
994 		case 5:
995 			mmc->version = MMC_VERSION_4_41;
996 			break;
997 		case 6:
998 			mmc->version = MMC_VERSION_4_5;
999 			break;
1000 		}
1001 
1002 		/*
1003 		 * Host needs to enable ERASE_GRP_DEF bit if device is
1004 		 * partitioned. This bit will be lost every time after a reset
1005 		 * or power off. This will affect erase size.
1006 		 */
1007 		if ((ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & PART_SUPPORT) &&
1008 		    (ext_csd[EXT_CSD_PARTITIONS_ATTRIBUTE] & PART_ENH_ATTRIB)) {
1009 			err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1010 				EXT_CSD_ERASE_GROUP_DEF, 1);
1011 
1012 			if (err)
1013 				return err;
1014 
1015 			/* Read out group size from ext_csd */
1016 			mmc->erase_grp_size =
1017 				ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] *
1018 					MMC_MAX_BLOCK_LEN * 1024;
1019 		} else {
1020 			/* Calculate the group size from the csd value. */
1021 			int erase_gsz, erase_gmul;
1022 			erase_gsz = (mmc->csd[2] & 0x00007c00) >> 10;
1023 			erase_gmul = (mmc->csd[2] & 0x000003e0) >> 5;
1024 			mmc->erase_grp_size = (erase_gsz + 1)
1025 				* (erase_gmul + 1);
1026 		}
1027 
1028 		/* store the partition info of emmc */
1029 		if ((ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & PART_SUPPORT) ||
1030 		    ext_csd[EXT_CSD_BOOT_MULT])
1031 			mmc->part_config = ext_csd[EXT_CSD_PART_CONF];
1032 
1033 		mmc->capacity_boot = ext_csd[EXT_CSD_BOOT_MULT] << 17;
1034 
1035 		mmc->capacity_rpmb = ext_csd[EXT_CSD_RPMB_MULT] << 17;
1036 
1037 		for (i = 0; i < 4; i++) {
1038 			int idx = EXT_CSD_GP_SIZE_MULT + i * 3;
1039 			mmc->capacity_gp[i] = (ext_csd[idx + 2] << 16) +
1040 				(ext_csd[idx + 1] << 8) + ext_csd[idx];
1041 			mmc->capacity_gp[i] *=
1042 				ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
1043 			mmc->capacity_gp[i] *= ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
1044 		}
1045 	}
1046 
1047 	err = mmc_set_capacity(mmc, mmc->part_num);
1048 	if (err)
1049 		return err;
1050 
1051 	if (IS_SD(mmc))
1052 		err = sd_change_freq(mmc);
1053 	else
1054 		err = mmc_change_freq(mmc);
1055 
1056 	if (err)
1057 		return err;
1058 
1059 	/* Restrict card's capabilities by what the host can do */
1060 	mmc->card_caps &= mmc->cfg->host_caps;
1061 
1062 	if (IS_SD(mmc)) {
1063 		if (mmc->card_caps & MMC_MODE_4BIT) {
1064 			cmd.cmdidx = MMC_CMD_APP_CMD;
1065 			cmd.resp_type = MMC_RSP_R1;
1066 			cmd.cmdarg = mmc->rca << 16;
1067 
1068 			err = mmc_send_cmd(mmc, &cmd, NULL);
1069 			if (err)
1070 				return err;
1071 
1072 			cmd.cmdidx = SD_CMD_APP_SET_BUS_WIDTH;
1073 			cmd.resp_type = MMC_RSP_R1;
1074 			cmd.cmdarg = 2;
1075 			err = mmc_send_cmd(mmc, &cmd, NULL);
1076 			if (err)
1077 				return err;
1078 
1079 			mmc_set_bus_width(mmc, 4);
1080 		}
1081 
1082 		if (mmc->card_caps & MMC_MODE_HS)
1083 			mmc->tran_speed = 50000000;
1084 		else
1085 			mmc->tran_speed = 25000000;
1086 	} else {
1087 		int idx;
1088 
1089 		/* An array of possible bus widths in order of preference */
1090 		static unsigned ext_csd_bits[] = {
1091 			EXT_CSD_DDR_BUS_WIDTH_8,
1092 			EXT_CSD_DDR_BUS_WIDTH_4,
1093 			EXT_CSD_BUS_WIDTH_8,
1094 			EXT_CSD_BUS_WIDTH_4,
1095 			EXT_CSD_BUS_WIDTH_1,
1096 		};
1097 
1098 		/* An array to map CSD bus widths to host cap bits */
1099 		static unsigned ext_to_hostcaps[] = {
1100 			[EXT_CSD_DDR_BUS_WIDTH_4] = MMC_MODE_DDR_52MHz,
1101 			[EXT_CSD_DDR_BUS_WIDTH_8] = MMC_MODE_DDR_52MHz,
1102 			[EXT_CSD_BUS_WIDTH_4] = MMC_MODE_4BIT,
1103 			[EXT_CSD_BUS_WIDTH_8] = MMC_MODE_8BIT,
1104 		};
1105 
1106 		/* An array to map chosen bus width to an integer */
1107 		static unsigned widths[] = {
1108 			8, 4, 8, 4, 1,
1109 		};
1110 
1111 		for (idx=0; idx < ARRAY_SIZE(ext_csd_bits); idx++) {
1112 			unsigned int extw = ext_csd_bits[idx];
1113 
1114 			/*
1115 			 * Check to make sure the controller supports
1116 			 * this bus width, if it's more than 1
1117 			 */
1118 			if (extw != EXT_CSD_BUS_WIDTH_1 &&
1119 					!(mmc->cfg->host_caps & ext_to_hostcaps[extw]))
1120 				continue;
1121 
1122 			err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1123 					EXT_CSD_BUS_WIDTH, extw);
1124 
1125 			if (err)
1126 				continue;
1127 
1128 			mmc_set_bus_width(mmc, widths[idx]);
1129 
1130 			err = mmc_send_ext_csd(mmc, test_csd);
1131 			if (!err && ext_csd[EXT_CSD_PARTITIONING_SUPPORT] \
1132 				    == test_csd[EXT_CSD_PARTITIONING_SUPPORT]
1133 				 && ext_csd[EXT_CSD_ERASE_GROUP_DEF] \
1134 				    == test_csd[EXT_CSD_ERASE_GROUP_DEF] \
1135 				 && ext_csd[EXT_CSD_REV] \
1136 				    == test_csd[EXT_CSD_REV]
1137 				 && ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] \
1138 				    == test_csd[EXT_CSD_HC_ERASE_GRP_SIZE]
1139 				 && memcmp(&ext_csd[EXT_CSD_SEC_CNT], \
1140 					&test_csd[EXT_CSD_SEC_CNT], 4) == 0) {
1141 
1142 				mmc->card_caps |= ext_to_hostcaps[extw];
1143 				break;
1144 			}
1145 		}
1146 
1147 		if (mmc->card_caps & MMC_MODE_HS) {
1148 			if (mmc->card_caps & MMC_MODE_HS_52MHz)
1149 				mmc->tran_speed = 52000000;
1150 			else
1151 				mmc->tran_speed = 26000000;
1152 		}
1153 	}
1154 
1155 	mmc_set_clock(mmc, mmc->tran_speed);
1156 
1157 	/* fill in device description */
1158 	mmc->block_dev.lun = 0;
1159 	mmc->block_dev.type = 0;
1160 	mmc->block_dev.blksz = mmc->read_bl_len;
1161 	mmc->block_dev.log2blksz = LOG2(mmc->block_dev.blksz);
1162 	mmc->block_dev.lba = lldiv(mmc->capacity, mmc->read_bl_len);
1163 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
1164 	sprintf(mmc->block_dev.vendor, "Man %06x Snr %04x%04x",
1165 		mmc->cid[0] >> 24, (mmc->cid[2] & 0xffff),
1166 		(mmc->cid[3] >> 16) & 0xffff);
1167 	sprintf(mmc->block_dev.product, "%c%c%c%c%c%c", mmc->cid[0] & 0xff,
1168 		(mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
1169 		(mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff,
1170 		(mmc->cid[2] >> 24) & 0xff);
1171 	sprintf(mmc->block_dev.revision, "%d.%d", (mmc->cid[2] >> 20) & 0xf,
1172 		(mmc->cid[2] >> 16) & 0xf);
1173 #else
1174 	mmc->block_dev.vendor[0] = 0;
1175 	mmc->block_dev.product[0] = 0;
1176 	mmc->block_dev.revision[0] = 0;
1177 #endif
1178 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBDISK_SUPPORT)
1179 	init_part(&mmc->block_dev);
1180 #endif
1181 
1182 	return 0;
1183 }
1184 
1185 static int mmc_send_if_cond(struct mmc *mmc)
1186 {
1187 	struct mmc_cmd cmd;
1188 	int err;
1189 
1190 	cmd.cmdidx = SD_CMD_SEND_IF_COND;
1191 	/* We set the bit if the host supports voltages between 2.7 and 3.6 V */
1192 	cmd.cmdarg = ((mmc->cfg->voltages & 0xff8000) != 0) << 8 | 0xaa;
1193 	cmd.resp_type = MMC_RSP_R7;
1194 
1195 	err = mmc_send_cmd(mmc, &cmd, NULL);
1196 
1197 	if (err)
1198 		return err;
1199 
1200 	if ((cmd.response[0] & 0xff) != 0xaa)
1201 		return UNUSABLE_ERR;
1202 	else
1203 		mmc->version = SD_VERSION_2;
1204 
1205 	return 0;
1206 }
1207 
1208 /* not used any more */
1209 int __deprecated mmc_register(struct mmc *mmc)
1210 {
1211 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
1212 	printf("%s is deprecated! use mmc_create() instead.\n", __func__);
1213 #endif
1214 	return -1;
1215 }
1216 
1217 struct mmc *mmc_create(const struct mmc_config *cfg, void *priv)
1218 {
1219 	struct mmc *mmc;
1220 
1221 	/* quick validation */
1222 	if (cfg == NULL || cfg->ops == NULL || cfg->ops->send_cmd == NULL ||
1223 			cfg->f_min == 0 || cfg->f_max == 0 || cfg->b_max == 0)
1224 		return NULL;
1225 
1226 	mmc = calloc(1, sizeof(*mmc));
1227 	if (mmc == NULL)
1228 		return NULL;
1229 
1230 	mmc->cfg = cfg;
1231 	mmc->priv = priv;
1232 
1233 	/* the following chunk was mmc_register() */
1234 
1235 	/* Setup dsr related values */
1236 	mmc->dsr_imp = 0;
1237 	mmc->dsr = 0xffffffff;
1238 	/* Setup the universal parts of the block interface just once */
1239 	mmc->block_dev.if_type = IF_TYPE_MMC;
1240 	mmc->block_dev.dev = cur_dev_num++;
1241 	mmc->block_dev.removable = 1;
1242 	mmc->block_dev.block_read = mmc_bread;
1243 	mmc->block_dev.block_write = mmc_bwrite;
1244 	mmc->block_dev.block_erase = mmc_berase;
1245 
1246 	/* setup initial part type */
1247 	mmc->block_dev.part_type = mmc->cfg->part_type;
1248 
1249 	INIT_LIST_HEAD(&mmc->link);
1250 
1251 	list_add_tail(&mmc->link, &mmc_devices);
1252 
1253 	return mmc;
1254 }
1255 
1256 void mmc_destroy(struct mmc *mmc)
1257 {
1258 	/* only freeing memory for now */
1259 	free(mmc);
1260 }
1261 
1262 #ifdef CONFIG_PARTITIONS
1263 block_dev_desc_t *mmc_get_dev(int dev)
1264 {
1265 	struct mmc *mmc = find_mmc_device(dev);
1266 	if (!mmc || mmc_init(mmc))
1267 		return NULL;
1268 
1269 	return &mmc->block_dev;
1270 }
1271 #endif
1272 
1273 int mmc_start_init(struct mmc *mmc)
1274 {
1275 	int err;
1276 
1277 	/* we pretend there's no card when init is NULL */
1278 	if (mmc_getcd(mmc) == 0 || mmc->cfg->ops->init == NULL) {
1279 		mmc->has_init = 0;
1280 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
1281 		printf("MMC: no card present\n");
1282 #endif
1283 		return NO_CARD_ERR;
1284 	}
1285 
1286 	if (mmc->has_init)
1287 		return 0;
1288 
1289 	/* made sure it's not NULL earlier */
1290 	err = mmc->cfg->ops->init(mmc);
1291 
1292 	if (err)
1293 		return err;
1294 
1295 	mmc_set_bus_width(mmc, 1);
1296 	mmc_set_clock(mmc, 1);
1297 
1298 	/* Reset the Card */
1299 	err = mmc_go_idle(mmc);
1300 
1301 	if (err)
1302 		return err;
1303 
1304 	/* The internal partition reset to user partition(0) at every CMD0*/
1305 	mmc->part_num = 0;
1306 
1307 	/* Test for SD version 2 */
1308 	err = mmc_send_if_cond(mmc);
1309 
1310 	/* Now try to get the SD card's operating condition */
1311 	err = sd_send_op_cond(mmc);
1312 
1313 	/* If the command timed out, we check for an MMC card */
1314 	if (err == TIMEOUT) {
1315 		err = mmc_send_op_cond(mmc);
1316 
1317 		if (err && err != IN_PROGRESS) {
1318 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
1319 			printf("Card did not respond to voltage select!\n");
1320 #endif
1321 			return UNUSABLE_ERR;
1322 		}
1323 	}
1324 
1325 	if (err == IN_PROGRESS)
1326 		mmc->init_in_progress = 1;
1327 
1328 	return err;
1329 }
1330 
1331 static int mmc_complete_init(struct mmc *mmc)
1332 {
1333 	int err = 0;
1334 
1335 	if (mmc->op_cond_pending)
1336 		err = mmc_complete_op_cond(mmc);
1337 
1338 	if (!err)
1339 		err = mmc_startup(mmc);
1340 	if (err)
1341 		mmc->has_init = 0;
1342 	else
1343 		mmc->has_init = 1;
1344 	mmc->init_in_progress = 0;
1345 	return err;
1346 }
1347 
1348 int mmc_init(struct mmc *mmc)
1349 {
1350 	int err = IN_PROGRESS;
1351 	unsigned start;
1352 
1353 	if (mmc->has_init)
1354 		return 0;
1355 
1356 	start = get_timer(0);
1357 
1358 	if (!mmc->init_in_progress)
1359 		err = mmc_start_init(mmc);
1360 
1361 	if (!err || err == IN_PROGRESS)
1362 		err = mmc_complete_init(mmc);
1363 	debug("%s: %d, time %lu\n", __func__, err, get_timer(start));
1364 	return err;
1365 }
1366 
1367 int mmc_set_dsr(struct mmc *mmc, u16 val)
1368 {
1369 	mmc->dsr = val;
1370 	return 0;
1371 }
1372 
1373 /*
1374  * CPU and board-specific MMC initializations.  Aliased function
1375  * signals caller to move on
1376  */
1377 static int __def_mmc_init(bd_t *bis)
1378 {
1379 	return -1;
1380 }
1381 
1382 int cpu_mmc_init(bd_t *bis) __attribute__((weak, alias("__def_mmc_init")));
1383 int board_mmc_init(bd_t *bis) __attribute__((weak, alias("__def_mmc_init")));
1384 
1385 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBCOMMON_SUPPORT)
1386 
1387 void print_mmc_devices(char separator)
1388 {
1389 	struct mmc *m;
1390 	struct list_head *entry;
1391 
1392 	list_for_each(entry, &mmc_devices) {
1393 		m = list_entry(entry, struct mmc, link);
1394 
1395 		printf("%s: %d", m->cfg->name, m->block_dev.dev);
1396 
1397 		if (entry->next != &mmc_devices)
1398 			printf("%c ", separator);
1399 	}
1400 
1401 	printf("\n");
1402 }
1403 
1404 #else
1405 void print_mmc_devices(char separator) { }
1406 #endif
1407 
1408 int get_mmc_num(void)
1409 {
1410 	return cur_dev_num;
1411 }
1412 
1413 void mmc_set_preinit(struct mmc *mmc, int preinit)
1414 {
1415 	mmc->preinit = preinit;
1416 }
1417 
1418 static void do_preinit(void)
1419 {
1420 	struct mmc *m;
1421 	struct list_head *entry;
1422 
1423 	list_for_each(entry, &mmc_devices) {
1424 		m = list_entry(entry, struct mmc, link);
1425 
1426 		if (m->preinit)
1427 			mmc_start_init(m);
1428 	}
1429 }
1430 
1431 
1432 int mmc_initialize(bd_t *bis)
1433 {
1434 	INIT_LIST_HEAD (&mmc_devices);
1435 	cur_dev_num = 0;
1436 
1437 	if (board_mmc_init(bis) < 0)
1438 		cpu_mmc_init(bis);
1439 
1440 #ifndef CONFIG_SPL_BUILD
1441 	print_mmc_devices(',');
1442 #endif
1443 
1444 	do_preinit();
1445 	return 0;
1446 }
1447 
1448 #ifdef CONFIG_SUPPORT_EMMC_BOOT
1449 /*
1450  * This function changes the size of boot partition and the size of rpmb
1451  * partition present on EMMC devices.
1452  *
1453  * Input Parameters:
1454  * struct *mmc: pointer for the mmc device strcuture
1455  * bootsize: size of boot partition
1456  * rpmbsize: size of rpmb partition
1457  *
1458  * Returns 0 on success.
1459  */
1460 
1461 int mmc_boot_partition_size_change(struct mmc *mmc, unsigned long bootsize,
1462 				unsigned long rpmbsize)
1463 {
1464 	int err;
1465 	struct mmc_cmd cmd;
1466 
1467 	/* Only use this command for raw EMMC moviNAND. Enter backdoor mode */
1468 	cmd.cmdidx = MMC_CMD_RES_MAN;
1469 	cmd.resp_type = MMC_RSP_R1b;
1470 	cmd.cmdarg = MMC_CMD62_ARG1;
1471 
1472 	err = mmc_send_cmd(mmc, &cmd, NULL);
1473 	if (err) {
1474 		debug("mmc_boot_partition_size_change: Error1 = %d\n", err);
1475 		return err;
1476 	}
1477 
1478 	/* Boot partition changing mode */
1479 	cmd.cmdidx = MMC_CMD_RES_MAN;
1480 	cmd.resp_type = MMC_RSP_R1b;
1481 	cmd.cmdarg = MMC_CMD62_ARG2;
1482 
1483 	err = mmc_send_cmd(mmc, &cmd, NULL);
1484 	if (err) {
1485 		debug("mmc_boot_partition_size_change: Error2 = %d\n", err);
1486 		return err;
1487 	}
1488 	/* boot partition size is multiple of 128KB */
1489 	bootsize = (bootsize * 1024) / 128;
1490 
1491 	/* Arg: boot partition size */
1492 	cmd.cmdidx = MMC_CMD_RES_MAN;
1493 	cmd.resp_type = MMC_RSP_R1b;
1494 	cmd.cmdarg = bootsize;
1495 
1496 	err = mmc_send_cmd(mmc, &cmd, NULL);
1497 	if (err) {
1498 		debug("mmc_boot_partition_size_change: Error3 = %d\n", err);
1499 		return err;
1500 	}
1501 	/* RPMB partition size is multiple of 128KB */
1502 	rpmbsize = (rpmbsize * 1024) / 128;
1503 	/* Arg: RPMB partition size */
1504 	cmd.cmdidx = MMC_CMD_RES_MAN;
1505 	cmd.resp_type = MMC_RSP_R1b;
1506 	cmd.cmdarg = rpmbsize;
1507 
1508 	err = mmc_send_cmd(mmc, &cmd, NULL);
1509 	if (err) {
1510 		debug("mmc_boot_partition_size_change: Error4 = %d\n", err);
1511 		return err;
1512 	}
1513 	return 0;
1514 }
1515 
1516 /*
1517  * Modify EXT_CSD[177] which is BOOT_BUS_WIDTH
1518  * based on the passed in values for BOOT_BUS_WIDTH, RESET_BOOT_BUS_WIDTH
1519  * and BOOT_MODE.
1520  *
1521  * Returns 0 on success.
1522  */
1523 int mmc_set_boot_bus_width(struct mmc *mmc, u8 width, u8 reset, u8 mode)
1524 {
1525 	int err;
1526 
1527 	err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_BUS_WIDTH,
1528 			 EXT_CSD_BOOT_BUS_WIDTH_MODE(mode) |
1529 			 EXT_CSD_BOOT_BUS_WIDTH_RESET(reset) |
1530 			 EXT_CSD_BOOT_BUS_WIDTH_WIDTH(width));
1531 
1532 	if (err)
1533 		return err;
1534 	return 0;
1535 }
1536 
1537 /*
1538  * Modify EXT_CSD[179] which is PARTITION_CONFIG (formerly BOOT_CONFIG)
1539  * based on the passed in values for BOOT_ACK, BOOT_PARTITION_ENABLE and
1540  * PARTITION_ACCESS.
1541  *
1542  * Returns 0 on success.
1543  */
1544 int mmc_set_part_conf(struct mmc *mmc, u8 ack, u8 part_num, u8 access)
1545 {
1546 	int err;
1547 
1548 	err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONF,
1549 			 EXT_CSD_BOOT_ACK(ack) |
1550 			 EXT_CSD_BOOT_PART_NUM(part_num) |
1551 			 EXT_CSD_PARTITION_ACCESS(access));
1552 
1553 	if (err)
1554 		return err;
1555 	return 0;
1556 }
1557 
1558 /*
1559  * Modify EXT_CSD[162] which is RST_n_FUNCTION based on the given value
1560  * for enable.  Note that this is a write-once field for non-zero values.
1561  *
1562  * Returns 0 on success.
1563  */
1564 int mmc_set_rst_n_function(struct mmc *mmc, u8 enable)
1565 {
1566 	return mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_RST_N_FUNCTION,
1567 			  enable);
1568 }
1569 #endif
1570