xref: /openbmc/u-boot/drivers/mmc/mmc.c (revision 73286d87)
1 /*
2  * Copyright 2008, Freescale Semiconductor, Inc
3  * Andy Fleming
4  *
5  * Based vaguely on the Linux code
6  *
7  * See file CREDITS for list of people who contributed to this
8  * project.
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License as
12  * published by the Free Software Foundation; either version 2 of
13  * the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
23  * MA 02111-1307 USA
24  */
25 
26 #include <config.h>
27 #include <common.h>
28 #include <command.h>
29 #include <mmc.h>
30 #include <part.h>
31 #include <malloc.h>
32 #include <linux/list.h>
33 #include <div64.h>
34 
35 /* Set block count limit because of 16 bit register limit on some hardware*/
36 #ifndef CONFIG_SYS_MMC_MAX_BLK_COUNT
37 #define CONFIG_SYS_MMC_MAX_BLK_COUNT 65535
38 #endif
39 
40 static struct list_head mmc_devices;
41 static int cur_dev_num = -1;
42 
43 int __board_mmc_getcd(struct mmc *mmc) {
44 	return -1;
45 }
46 
47 int board_mmc_getcd(struct mmc *mmc)__attribute__((weak,
48 	alias("__board_mmc_getcd")));
49 
50 int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
51 {
52 	struct mmc_data backup;
53 	int ret;
54 
55 	memset(&backup, 0, sizeof(backup));
56 
57 #ifdef CONFIG_MMC_TRACE
58 	int i;
59 	u8 *ptr;
60 
61 	printf("CMD_SEND:%d\n", cmd->cmdidx);
62 	printf("\t\tARG\t\t\t 0x%08X\n", cmd->cmdarg);
63 	ret = mmc->send_cmd(mmc, cmd, data);
64 	switch (cmd->resp_type) {
65 		case MMC_RSP_NONE:
66 			printf("\t\tMMC_RSP_NONE\n");
67 			break;
68 		case MMC_RSP_R1:
69 			printf("\t\tMMC_RSP_R1,5,6,7 \t 0x%08X \n",
70 				cmd->response[0]);
71 			break;
72 		case MMC_RSP_R1b:
73 			printf("\t\tMMC_RSP_R1b\t\t 0x%08X \n",
74 				cmd->response[0]);
75 			break;
76 		case MMC_RSP_R2:
77 			printf("\t\tMMC_RSP_R2\t\t 0x%08X \n",
78 				cmd->response[0]);
79 			printf("\t\t          \t\t 0x%08X \n",
80 				cmd->response[1]);
81 			printf("\t\t          \t\t 0x%08X \n",
82 				cmd->response[2]);
83 			printf("\t\t          \t\t 0x%08X \n",
84 				cmd->response[3]);
85 			printf("\n");
86 			printf("\t\t\t\t\tDUMPING DATA\n");
87 			for (i = 0; i < 4; i++) {
88 				int j;
89 				printf("\t\t\t\t\t%03d - ", i*4);
90 				ptr = (u8 *)&cmd->response[i];
91 				ptr += 3;
92 				for (j = 0; j < 4; j++)
93 					printf("%02X ", *ptr--);
94 				printf("\n");
95 			}
96 			break;
97 		case MMC_RSP_R3:
98 			printf("\t\tMMC_RSP_R3,4\t\t 0x%08X \n",
99 				cmd->response[0]);
100 			break;
101 		default:
102 			printf("\t\tERROR MMC rsp not supported\n");
103 			break;
104 	}
105 #else
106 	ret = mmc->send_cmd(mmc, cmd, data);
107 #endif
108 	return ret;
109 }
110 
111 int mmc_send_status(struct mmc *mmc, int timeout)
112 {
113 	struct mmc_cmd cmd;
114 	int err, retries = 5;
115 #ifdef CONFIG_MMC_TRACE
116 	int status;
117 #endif
118 
119 	cmd.cmdidx = MMC_CMD_SEND_STATUS;
120 	cmd.resp_type = MMC_RSP_R1;
121 	if (!mmc_host_is_spi(mmc))
122 		cmd.cmdarg = mmc->rca << 16;
123 
124 	do {
125 		err = mmc_send_cmd(mmc, &cmd, NULL);
126 		if (!err) {
127 			if ((cmd.response[0] & MMC_STATUS_RDY_FOR_DATA) &&
128 			    (cmd.response[0] & MMC_STATUS_CURR_STATE) !=
129 			     MMC_STATE_PRG)
130 				break;
131 			else if (cmd.response[0] & MMC_STATUS_MASK) {
132 				printf("Status Error: 0x%08X\n",
133 					cmd.response[0]);
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 		printf("Timeout waiting card ready\n");
149 		return TIMEOUT;
150 	}
151 
152 	return 0;
153 }
154 
155 int mmc_set_blocklen(struct mmc *mmc, int len)
156 {
157 	struct mmc_cmd cmd;
158 
159 	cmd.cmdidx = MMC_CMD_SET_BLOCKLEN;
160 	cmd.resp_type = MMC_RSP_R1;
161 	cmd.cmdarg = len;
162 
163 	return mmc_send_cmd(mmc, &cmd, NULL);
164 }
165 
166 struct mmc *find_mmc_device(int dev_num)
167 {
168 	struct mmc *m;
169 	struct list_head *entry;
170 
171 	list_for_each(entry, &mmc_devices) {
172 		m = list_entry(entry, struct mmc, link);
173 
174 		if (m->block_dev.dev == dev_num)
175 			return m;
176 	}
177 
178 	printf("MMC Device %d not found\n", dev_num);
179 
180 	return NULL;
181 }
182 
183 static ulong mmc_erase_t(struct mmc *mmc, ulong start, lbaint_t blkcnt)
184 {
185 	struct mmc_cmd cmd;
186 	ulong end;
187 	int err, start_cmd, end_cmd;
188 
189 	if (mmc->high_capacity)
190 		end = start + blkcnt - 1;
191 	else {
192 		end = (start + blkcnt - 1) * mmc->write_bl_len;
193 		start *= mmc->write_bl_len;
194 	}
195 
196 	if (IS_SD(mmc)) {
197 		start_cmd = SD_CMD_ERASE_WR_BLK_START;
198 		end_cmd = SD_CMD_ERASE_WR_BLK_END;
199 	} else {
200 		start_cmd = MMC_CMD_ERASE_GROUP_START;
201 		end_cmd = MMC_CMD_ERASE_GROUP_END;
202 	}
203 
204 	cmd.cmdidx = start_cmd;
205 	cmd.cmdarg = start;
206 	cmd.resp_type = MMC_RSP_R1;
207 
208 	err = mmc_send_cmd(mmc, &cmd, NULL);
209 	if (err)
210 		goto err_out;
211 
212 	cmd.cmdidx = end_cmd;
213 	cmd.cmdarg = end;
214 
215 	err = mmc_send_cmd(mmc, &cmd, NULL);
216 	if (err)
217 		goto err_out;
218 
219 	cmd.cmdidx = MMC_CMD_ERASE;
220 	cmd.cmdarg = SECURE_ERASE;
221 	cmd.resp_type = MMC_RSP_R1b;
222 
223 	err = mmc_send_cmd(mmc, &cmd, NULL);
224 	if (err)
225 		goto err_out;
226 
227 	return 0;
228 
229 err_out:
230 	puts("mmc erase failed\n");
231 	return err;
232 }
233 
234 static unsigned long
235 mmc_berase(int dev_num, unsigned long start, lbaint_t blkcnt)
236 {
237 	int err = 0;
238 	struct mmc *mmc = find_mmc_device(dev_num);
239 	lbaint_t blk = 0, blk_r = 0;
240 	int timeout = 1000;
241 
242 	if (!mmc)
243 		return -1;
244 
245 	if ((start % mmc->erase_grp_size) || (blkcnt % mmc->erase_grp_size))
246 		printf("\n\nCaution! Your devices Erase group is 0x%x\n"
247 			"The erase range would be change to 0x%lx~0x%lx\n\n",
248 		       mmc->erase_grp_size, start & ~(mmc->erase_grp_size - 1),
249 		       ((start + blkcnt + mmc->erase_grp_size)
250 		       & ~(mmc->erase_grp_size - 1)) - 1);
251 
252 	while (blk < blkcnt) {
253 		blk_r = ((blkcnt - blk) > mmc->erase_grp_size) ?
254 			mmc->erase_grp_size : (blkcnt - blk);
255 		err = mmc_erase_t(mmc, start + blk, blk_r);
256 		if (err)
257 			break;
258 
259 		blk += blk_r;
260 
261 		/* Waiting for the ready status */
262 		if (mmc_send_status(mmc, timeout))
263 			return 0;
264 	}
265 
266 	return blk;
267 }
268 
269 static ulong
270 mmc_write_blocks(struct mmc *mmc, ulong start, lbaint_t blkcnt, const void*src)
271 {
272 	struct mmc_cmd cmd;
273 	struct mmc_data data;
274 	int timeout = 1000;
275 
276 	if ((start + blkcnt) > mmc->block_dev.lba) {
277 		printf("MMC: block number 0x%lx exceeds max(0x%lx)\n",
278 			start + blkcnt, mmc->block_dev.lba);
279 		return 0;
280 	}
281 
282 	if (blkcnt > 1)
283 		cmd.cmdidx = MMC_CMD_WRITE_MULTIPLE_BLOCK;
284 	else
285 		cmd.cmdidx = MMC_CMD_WRITE_SINGLE_BLOCK;
286 
287 	if (mmc->high_capacity)
288 		cmd.cmdarg = start;
289 	else
290 		cmd.cmdarg = start * mmc->write_bl_len;
291 
292 	cmd.resp_type = MMC_RSP_R1;
293 
294 	data.src = src;
295 	data.blocks = blkcnt;
296 	data.blocksize = mmc->write_bl_len;
297 	data.flags = MMC_DATA_WRITE;
298 
299 	if (mmc_send_cmd(mmc, &cmd, &data)) {
300 		printf("mmc write failed\n");
301 		return 0;
302 	}
303 
304 	/* SPI multiblock writes terminate using a special
305 	 * token, not a STOP_TRANSMISSION request.
306 	 */
307 	if (!mmc_host_is_spi(mmc) && blkcnt > 1) {
308 		cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
309 		cmd.cmdarg = 0;
310 		cmd.resp_type = MMC_RSP_R1b;
311 		if (mmc_send_cmd(mmc, &cmd, NULL)) {
312 			printf("mmc fail to send stop cmd\n");
313 			return 0;
314 		}
315 	}
316 
317 	/* Waiting for the ready status */
318 	if (mmc_send_status(mmc, timeout))
319 		return 0;
320 
321 	return blkcnt;
322 }
323 
324 static ulong
325 mmc_bwrite(int dev_num, ulong start, lbaint_t blkcnt, const void*src)
326 {
327 	lbaint_t cur, blocks_todo = blkcnt;
328 
329 	struct mmc *mmc = find_mmc_device(dev_num);
330 	if (!mmc)
331 		return 0;
332 
333 	if (mmc_set_blocklen(mmc, mmc->write_bl_len))
334 		return 0;
335 
336 	do {
337 		cur = (blocks_todo > mmc->b_max) ?  mmc->b_max : blocks_todo;
338 		if(mmc_write_blocks(mmc, start, cur, src) != cur)
339 			return 0;
340 		blocks_todo -= cur;
341 		start += cur;
342 		src += cur * mmc->write_bl_len;
343 	} while (blocks_todo > 0);
344 
345 	return blkcnt;
346 }
347 
348 int mmc_read_blocks(struct mmc *mmc, void *dst, ulong start, lbaint_t blkcnt)
349 {
350 	struct mmc_cmd cmd;
351 	struct mmc_data data;
352 
353 	if (blkcnt > 1)
354 		cmd.cmdidx = MMC_CMD_READ_MULTIPLE_BLOCK;
355 	else
356 		cmd.cmdidx = MMC_CMD_READ_SINGLE_BLOCK;
357 
358 	if (mmc->high_capacity)
359 		cmd.cmdarg = start;
360 	else
361 		cmd.cmdarg = start * mmc->read_bl_len;
362 
363 	cmd.resp_type = MMC_RSP_R1;
364 
365 	data.dest = dst;
366 	data.blocks = blkcnt;
367 	data.blocksize = mmc->read_bl_len;
368 	data.flags = MMC_DATA_READ;
369 
370 	if (mmc_send_cmd(mmc, &cmd, &data))
371 		return 0;
372 
373 	if (blkcnt > 1) {
374 		cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
375 		cmd.cmdarg = 0;
376 		cmd.resp_type = MMC_RSP_R1b;
377 		if (mmc_send_cmd(mmc, &cmd, NULL)) {
378 			printf("mmc fail to send stop cmd\n");
379 			return 0;
380 		}
381 	}
382 
383 	return blkcnt;
384 }
385 
386 static ulong mmc_bread(int dev_num, ulong start, lbaint_t blkcnt, void *dst)
387 {
388 	lbaint_t cur, blocks_todo = blkcnt;
389 
390 	if (blkcnt == 0)
391 		return 0;
392 
393 	struct mmc *mmc = find_mmc_device(dev_num);
394 	if (!mmc)
395 		return 0;
396 
397 	if ((start + blkcnt) > mmc->block_dev.lba) {
398 		printf("MMC: block number 0x%lx exceeds max(0x%lx)\n",
399 			start + blkcnt, mmc->block_dev.lba);
400 		return 0;
401 	}
402 
403 	if (mmc_set_blocklen(mmc, mmc->read_bl_len))
404 		return 0;
405 
406 	do {
407 		cur = (blocks_todo > mmc->b_max) ?  mmc->b_max : blocks_todo;
408 		if(mmc_read_blocks(mmc, dst, start, cur) != cur)
409 			return 0;
410 		blocks_todo -= cur;
411 		start += cur;
412 		dst += cur * mmc->read_bl_len;
413 	} while (blocks_todo > 0);
414 
415 	return blkcnt;
416 }
417 
418 int mmc_go_idle(struct mmc* mmc)
419 {
420 	struct mmc_cmd cmd;
421 	int err;
422 
423 	udelay(1000);
424 
425 	cmd.cmdidx = MMC_CMD_GO_IDLE_STATE;
426 	cmd.cmdarg = 0;
427 	cmd.resp_type = MMC_RSP_NONE;
428 
429 	err = mmc_send_cmd(mmc, &cmd, NULL);
430 
431 	if (err)
432 		return err;
433 
434 	udelay(2000);
435 
436 	return 0;
437 }
438 
439 int
440 sd_send_op_cond(struct mmc *mmc)
441 {
442 	int timeout = 1000;
443 	int err;
444 	struct mmc_cmd cmd;
445 
446 	do {
447 		cmd.cmdidx = MMC_CMD_APP_CMD;
448 		cmd.resp_type = MMC_RSP_R1;
449 		cmd.cmdarg = 0;
450 
451 		err = mmc_send_cmd(mmc, &cmd, NULL);
452 
453 		if (err)
454 			return err;
455 
456 		cmd.cmdidx = SD_CMD_APP_SEND_OP_COND;
457 		cmd.resp_type = MMC_RSP_R3;
458 
459 		/*
460 		 * Most cards do not answer if some reserved bits
461 		 * in the ocr are set. However, Some controller
462 		 * can set bit 7 (reserved for low voltages), but
463 		 * how to manage low voltages SD card is not yet
464 		 * specified.
465 		 */
466 		cmd.cmdarg = mmc_host_is_spi(mmc) ? 0 :
467 			(mmc->voltages & 0xff8000);
468 
469 		if (mmc->version == SD_VERSION_2)
470 			cmd.cmdarg |= OCR_HCS;
471 
472 		err = mmc_send_cmd(mmc, &cmd, NULL);
473 
474 		if (err)
475 			return err;
476 
477 		udelay(1000);
478 	} while ((!(cmd.response[0] & OCR_BUSY)) && timeout--);
479 
480 	if (timeout <= 0)
481 		return UNUSABLE_ERR;
482 
483 	if (mmc->version != SD_VERSION_2)
484 		mmc->version = SD_VERSION_1_0;
485 
486 	if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
487 		cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
488 		cmd.resp_type = MMC_RSP_R3;
489 		cmd.cmdarg = 0;
490 
491 		err = mmc_send_cmd(mmc, &cmd, NULL);
492 
493 		if (err)
494 			return err;
495 	}
496 
497 	mmc->ocr = cmd.response[0];
498 
499 	mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
500 	mmc->rca = 0;
501 
502 	return 0;
503 }
504 
505 int mmc_send_op_cond(struct mmc *mmc)
506 {
507 	int timeout = 10000;
508 	struct mmc_cmd cmd;
509 	int err;
510 
511 	/* Some cards seem to need this */
512 	mmc_go_idle(mmc);
513 
514  	/* Asking to the card its capabilities */
515  	cmd.cmdidx = MMC_CMD_SEND_OP_COND;
516  	cmd.resp_type = MMC_RSP_R3;
517  	cmd.cmdarg = 0;
518 
519  	err = mmc_send_cmd(mmc, &cmd, NULL);
520 
521  	if (err)
522  		return err;
523 
524  	udelay(1000);
525 
526 	do {
527 		cmd.cmdidx = MMC_CMD_SEND_OP_COND;
528 		cmd.resp_type = MMC_RSP_R3;
529 		cmd.cmdarg = (mmc_host_is_spi(mmc) ? 0 :
530 				(mmc->voltages &
531 				(cmd.response[0] & OCR_VOLTAGE_MASK)) |
532 				(cmd.response[0] & OCR_ACCESS_MODE));
533 
534 		if (mmc->host_caps & MMC_MODE_HC)
535 			cmd.cmdarg |= OCR_HCS;
536 
537 		err = mmc_send_cmd(mmc, &cmd, NULL);
538 
539 		if (err)
540 			return err;
541 
542 		udelay(1000);
543 	} while (!(cmd.response[0] & OCR_BUSY) && timeout--);
544 
545 	if (timeout <= 0)
546 		return UNUSABLE_ERR;
547 
548 	if (mmc_host_is_spi(mmc)) { /* read OCR for spi */
549 		cmd.cmdidx = MMC_CMD_SPI_READ_OCR;
550 		cmd.resp_type = MMC_RSP_R3;
551 		cmd.cmdarg = 0;
552 
553 		err = mmc_send_cmd(mmc, &cmd, NULL);
554 
555 		if (err)
556 			return err;
557 	}
558 
559 	mmc->version = MMC_VERSION_UNKNOWN;
560 	mmc->ocr = cmd.response[0];
561 
562 	mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
563 	mmc->rca = 0;
564 
565 	return 0;
566 }
567 
568 
569 int mmc_send_ext_csd(struct mmc *mmc, u8 *ext_csd)
570 {
571 	struct mmc_cmd cmd;
572 	struct mmc_data data;
573 	int err;
574 
575 	/* Get the Card Status Register */
576 	cmd.cmdidx = MMC_CMD_SEND_EXT_CSD;
577 	cmd.resp_type = MMC_RSP_R1;
578 	cmd.cmdarg = 0;
579 
580 	data.dest = (char *)ext_csd;
581 	data.blocks = 1;
582 	data.blocksize = 512;
583 	data.flags = MMC_DATA_READ;
584 
585 	err = mmc_send_cmd(mmc, &cmd, &data);
586 
587 	return err;
588 }
589 
590 
591 int mmc_switch(struct mmc *mmc, u8 set, u8 index, u8 value)
592 {
593 	struct mmc_cmd cmd;
594 	int timeout = 1000;
595 	int ret;
596 
597 	cmd.cmdidx = MMC_CMD_SWITCH;
598 	cmd.resp_type = MMC_RSP_R1b;
599 	cmd.cmdarg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
600 				 (index << 16) |
601 				 (value << 8);
602 
603 	ret = mmc_send_cmd(mmc, &cmd, NULL);
604 
605 	/* Waiting for the ready status */
606 	if (!ret)
607 		ret = mmc_send_status(mmc, timeout);
608 
609 	return ret;
610 
611 }
612 
613 int mmc_change_freq(struct mmc *mmc)
614 {
615 	ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, 512);
616 	char cardtype;
617 	int err;
618 
619 	mmc->card_caps = 0;
620 
621 	if (mmc_host_is_spi(mmc))
622 		return 0;
623 
624 	/* Only version 4 supports high-speed */
625 	if (mmc->version < MMC_VERSION_4)
626 		return 0;
627 
628 	err = mmc_send_ext_csd(mmc, ext_csd);
629 
630 	if (err)
631 		return err;
632 
633 	cardtype = ext_csd[EXT_CSD_CARD_TYPE] & 0xf;
634 
635 	err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING, 1);
636 
637 	if (err)
638 		return err;
639 
640 	/* Now check to see that it worked */
641 	err = mmc_send_ext_csd(mmc, ext_csd);
642 
643 	if (err)
644 		return err;
645 
646 	/* No high-speed support */
647 	if (!ext_csd[EXT_CSD_HS_TIMING])
648 		return 0;
649 
650 	/* High Speed is set, there are two types: 52MHz and 26MHz */
651 	if (cardtype & MMC_HS_52MHZ)
652 		mmc->card_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
653 	else
654 		mmc->card_caps |= MMC_MODE_HS;
655 
656 	return 0;
657 }
658 
659 int mmc_switch_part(int dev_num, unsigned int part_num)
660 {
661 	struct mmc *mmc = find_mmc_device(dev_num);
662 
663 	if (!mmc)
664 		return -1;
665 
666 	return mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONF,
667 			  (mmc->part_config & ~PART_ACCESS_MASK)
668 			  | (part_num & PART_ACCESS_MASK));
669 }
670 
671 int mmc_getcd(struct mmc *mmc)
672 {
673 	int cd;
674 
675 	cd = board_mmc_getcd(mmc);
676 
677 	if ((cd < 0) && mmc->getcd)
678 		cd = mmc->getcd(mmc);
679 
680 	return cd;
681 }
682 
683 int sd_switch(struct mmc *mmc, int mode, int group, u8 value, u8 *resp)
684 {
685 	struct mmc_cmd cmd;
686 	struct mmc_data data;
687 
688 	/* Switch the frequency */
689 	cmd.cmdidx = SD_CMD_SWITCH_FUNC;
690 	cmd.resp_type = MMC_RSP_R1;
691 	cmd.cmdarg = (mode << 31) | 0xffffff;
692 	cmd.cmdarg &= ~(0xf << (group * 4));
693 	cmd.cmdarg |= value << (group * 4);
694 
695 	data.dest = (char *)resp;
696 	data.blocksize = 64;
697 	data.blocks = 1;
698 	data.flags = MMC_DATA_READ;
699 
700 	return mmc_send_cmd(mmc, &cmd, &data);
701 }
702 
703 
704 int sd_change_freq(struct mmc *mmc)
705 {
706 	int err;
707 	struct mmc_cmd cmd;
708 	ALLOC_CACHE_ALIGN_BUFFER(uint, scr, 2);
709 	ALLOC_CACHE_ALIGN_BUFFER(uint, switch_status, 16);
710 	struct mmc_data data;
711 	int timeout;
712 
713 	mmc->card_caps = 0;
714 
715 	if (mmc_host_is_spi(mmc))
716 		return 0;
717 
718 	/* Read the SCR to find out if this card supports higher speeds */
719 	cmd.cmdidx = MMC_CMD_APP_CMD;
720 	cmd.resp_type = MMC_RSP_R1;
721 	cmd.cmdarg = mmc->rca << 16;
722 
723 	err = mmc_send_cmd(mmc, &cmd, NULL);
724 
725 	if (err)
726 		return err;
727 
728 	cmd.cmdidx = SD_CMD_APP_SEND_SCR;
729 	cmd.resp_type = MMC_RSP_R1;
730 	cmd.cmdarg = 0;
731 
732 	timeout = 3;
733 
734 retry_scr:
735 	data.dest = (char *)scr;
736 	data.blocksize = 8;
737 	data.blocks = 1;
738 	data.flags = MMC_DATA_READ;
739 
740 	err = mmc_send_cmd(mmc, &cmd, &data);
741 
742 	if (err) {
743 		if (timeout--)
744 			goto retry_scr;
745 
746 		return err;
747 	}
748 
749 	mmc->scr[0] = __be32_to_cpu(scr[0]);
750 	mmc->scr[1] = __be32_to_cpu(scr[1]);
751 
752 	switch ((mmc->scr[0] >> 24) & 0xf) {
753 		case 0:
754 			mmc->version = SD_VERSION_1_0;
755 			break;
756 		case 1:
757 			mmc->version = SD_VERSION_1_10;
758 			break;
759 		case 2:
760 			mmc->version = SD_VERSION_2;
761 			break;
762 		default:
763 			mmc->version = SD_VERSION_1_0;
764 			break;
765 	}
766 
767 	if (mmc->scr[0] & SD_DATA_4BIT)
768 		mmc->card_caps |= MMC_MODE_4BIT;
769 
770 	/* Version 1.0 doesn't support switching */
771 	if (mmc->version == SD_VERSION_1_0)
772 		return 0;
773 
774 	timeout = 4;
775 	while (timeout--) {
776 		err = sd_switch(mmc, SD_SWITCH_CHECK, 0, 1,
777 				(u8 *)switch_status);
778 
779 		if (err)
780 			return err;
781 
782 		/* The high-speed function is busy.  Try again */
783 		if (!(__be32_to_cpu(switch_status[7]) & SD_HIGHSPEED_BUSY))
784 			break;
785 	}
786 
787 	/* If high-speed isn't supported, we return */
788 	if (!(__be32_to_cpu(switch_status[3]) & SD_HIGHSPEED_SUPPORTED))
789 		return 0;
790 
791 	/*
792 	 * If the host doesn't support SD_HIGHSPEED, do not switch card to
793 	 * HIGHSPEED mode even if the card support SD_HIGHSPPED.
794 	 * This can avoid furthur problem when the card runs in different
795 	 * mode between the host.
796 	 */
797 	if (!((mmc->host_caps & MMC_MODE_HS_52MHz) &&
798 		(mmc->host_caps & MMC_MODE_HS)))
799 		return 0;
800 
801 	err = sd_switch(mmc, SD_SWITCH_SWITCH, 0, 1, (u8 *)switch_status);
802 
803 	if (err)
804 		return err;
805 
806 	if ((__be32_to_cpu(switch_status[4]) & 0x0f000000) == 0x01000000)
807 		mmc->card_caps |= MMC_MODE_HS;
808 
809 	return 0;
810 }
811 
812 /* frequency bases */
813 /* divided by 10 to be nice to platforms without floating point */
814 static const int fbase[] = {
815 	10000,
816 	100000,
817 	1000000,
818 	10000000,
819 };
820 
821 /* Multiplier values for TRAN_SPEED.  Multiplied by 10 to be nice
822  * to platforms without floating point.
823  */
824 static const int multipliers[] = {
825 	0,	/* reserved */
826 	10,
827 	12,
828 	13,
829 	15,
830 	20,
831 	25,
832 	30,
833 	35,
834 	40,
835 	45,
836 	50,
837 	55,
838 	60,
839 	70,
840 	80,
841 };
842 
843 void mmc_set_ios(struct mmc *mmc)
844 {
845 	mmc->set_ios(mmc);
846 }
847 
848 void mmc_set_clock(struct mmc *mmc, uint clock)
849 {
850 	if (clock > mmc->f_max)
851 		clock = mmc->f_max;
852 
853 	if (clock < mmc->f_min)
854 		clock = mmc->f_min;
855 
856 	mmc->clock = clock;
857 
858 	mmc_set_ios(mmc);
859 }
860 
861 void mmc_set_bus_width(struct mmc *mmc, uint width)
862 {
863 	mmc->bus_width = width;
864 
865 	mmc_set_ios(mmc);
866 }
867 
868 int mmc_startup(struct mmc *mmc)
869 {
870 	int err, width;
871 	uint mult, freq;
872 	u64 cmult, csize, capacity;
873 	struct mmc_cmd cmd;
874 	ALLOC_CACHE_ALIGN_BUFFER(u8, ext_csd, 512);
875 	ALLOC_CACHE_ALIGN_BUFFER(u8, test_csd, 512);
876 	int timeout = 1000;
877 
878 #ifdef CONFIG_MMC_SPI_CRC_ON
879 	if (mmc_host_is_spi(mmc)) { /* enable CRC check for spi */
880 		cmd.cmdidx = MMC_CMD_SPI_CRC_ON_OFF;
881 		cmd.resp_type = MMC_RSP_R1;
882 		cmd.cmdarg = 1;
883 		err = mmc_send_cmd(mmc, &cmd, NULL);
884 
885 		if (err)
886 			return err;
887 	}
888 #endif
889 
890 	/* Put the Card in Identify Mode */
891 	cmd.cmdidx = mmc_host_is_spi(mmc) ? MMC_CMD_SEND_CID :
892 		MMC_CMD_ALL_SEND_CID; /* cmd not supported in spi */
893 	cmd.resp_type = MMC_RSP_R2;
894 	cmd.cmdarg = 0;
895 
896 	err = mmc_send_cmd(mmc, &cmd, NULL);
897 
898 	if (err)
899 		return err;
900 
901 	memcpy(mmc->cid, cmd.response, 16);
902 
903 	/*
904 	 * For MMC cards, set the Relative Address.
905 	 * For SD cards, get the Relatvie Address.
906 	 * This also puts the cards into Standby State
907 	 */
908 	if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
909 		cmd.cmdidx = SD_CMD_SEND_RELATIVE_ADDR;
910 		cmd.cmdarg = mmc->rca << 16;
911 		cmd.resp_type = MMC_RSP_R6;
912 
913 		err = mmc_send_cmd(mmc, &cmd, NULL);
914 
915 		if (err)
916 			return err;
917 
918 		if (IS_SD(mmc))
919 			mmc->rca = (cmd.response[0] >> 16) & 0xffff;
920 	}
921 
922 	/* Get the Card-Specific Data */
923 	cmd.cmdidx = MMC_CMD_SEND_CSD;
924 	cmd.resp_type = MMC_RSP_R2;
925 	cmd.cmdarg = mmc->rca << 16;
926 
927 	err = mmc_send_cmd(mmc, &cmd, NULL);
928 
929 	/* Waiting for the ready status */
930 	mmc_send_status(mmc, timeout);
931 
932 	if (err)
933 		return err;
934 
935 	mmc->csd[0] = cmd.response[0];
936 	mmc->csd[1] = cmd.response[1];
937 	mmc->csd[2] = cmd.response[2];
938 	mmc->csd[3] = cmd.response[3];
939 
940 	if (mmc->version == MMC_VERSION_UNKNOWN) {
941 		int version = (cmd.response[0] >> 26) & 0xf;
942 
943 		switch (version) {
944 			case 0:
945 				mmc->version = MMC_VERSION_1_2;
946 				break;
947 			case 1:
948 				mmc->version = MMC_VERSION_1_4;
949 				break;
950 			case 2:
951 				mmc->version = MMC_VERSION_2_2;
952 				break;
953 			case 3:
954 				mmc->version = MMC_VERSION_3;
955 				break;
956 			case 4:
957 				mmc->version = MMC_VERSION_4;
958 				break;
959 			default:
960 				mmc->version = MMC_VERSION_1_2;
961 				break;
962 		}
963 	}
964 
965 	/* divide frequency by 10, since the mults are 10x bigger */
966 	freq = fbase[(cmd.response[0] & 0x7)];
967 	mult = multipliers[((cmd.response[0] >> 3) & 0xf)];
968 
969 	mmc->tran_speed = freq * mult;
970 
971 	mmc->read_bl_len = 1 << ((cmd.response[1] >> 16) & 0xf);
972 
973 	if (IS_SD(mmc))
974 		mmc->write_bl_len = mmc->read_bl_len;
975 	else
976 		mmc->write_bl_len = 1 << ((cmd.response[3] >> 22) & 0xf);
977 
978 	if (mmc->high_capacity) {
979 		csize = (mmc->csd[1] & 0x3f) << 16
980 			| (mmc->csd[2] & 0xffff0000) >> 16;
981 		cmult = 8;
982 	} else {
983 		csize = (mmc->csd[1] & 0x3ff) << 2
984 			| (mmc->csd[2] & 0xc0000000) >> 30;
985 		cmult = (mmc->csd[2] & 0x00038000) >> 15;
986 	}
987 
988 	mmc->capacity = (csize + 1) << (cmult + 2);
989 	mmc->capacity *= mmc->read_bl_len;
990 
991 	if (mmc->read_bl_len > 512)
992 		mmc->read_bl_len = 512;
993 
994 	if (mmc->write_bl_len > 512)
995 		mmc->write_bl_len = 512;
996 
997 	/* Select the card, and put it into Transfer Mode */
998 	if (!mmc_host_is_spi(mmc)) { /* cmd not supported in spi */
999 		cmd.cmdidx = MMC_CMD_SELECT_CARD;
1000 		cmd.resp_type = MMC_RSP_R1;
1001 		cmd.cmdarg = mmc->rca << 16;
1002 		err = mmc_send_cmd(mmc, &cmd, NULL);
1003 
1004 		if (err)
1005 			return err;
1006 	}
1007 
1008 	/*
1009 	 * For SD, its erase group is always one sector
1010 	 */
1011 	mmc->erase_grp_size = 1;
1012 	mmc->part_config = MMCPART_NOAVAILABLE;
1013 	if (!IS_SD(mmc) && (mmc->version >= MMC_VERSION_4)) {
1014 		/* check  ext_csd version and capacity */
1015 		err = mmc_send_ext_csd(mmc, ext_csd);
1016 		if (!err & (ext_csd[EXT_CSD_REV] >= 2)) {
1017 			/*
1018 			 * According to the JEDEC Standard, the value of
1019 			 * ext_csd's capacity is valid if the value is more
1020 			 * than 2GB
1021 			 */
1022 			capacity = ext_csd[EXT_CSD_SEC_CNT] << 0
1023 					| ext_csd[EXT_CSD_SEC_CNT + 1] << 8
1024 					| ext_csd[EXT_CSD_SEC_CNT + 2] << 16
1025 					| ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
1026 			capacity *= 512;
1027 			if ((capacity >> 20) > 2 * 1024)
1028 				mmc->capacity = capacity;
1029 		}
1030 
1031 		/*
1032 		 * Check whether GROUP_DEF is set, if yes, read out
1033 		 * group size from ext_csd directly, or calculate
1034 		 * the group size from the csd value.
1035 		 */
1036 		if (ext_csd[EXT_CSD_ERASE_GROUP_DEF])
1037 			mmc->erase_grp_size =
1038 			      ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] * 512 * 1024;
1039 		else {
1040 			int erase_gsz, erase_gmul;
1041 			erase_gsz = (mmc->csd[2] & 0x00007c00) >> 10;
1042 			erase_gmul = (mmc->csd[2] & 0x000003e0) >> 5;
1043 			mmc->erase_grp_size = (erase_gsz + 1)
1044 				* (erase_gmul + 1);
1045 		}
1046 
1047 		/* store the partition info of emmc */
1048 		if ((ext_csd[EXT_CSD_PARTITIONING_SUPPORT] & PART_SUPPORT) ||
1049 		    ext_csd[EXT_CSD_BOOT_MULT])
1050 			mmc->part_config = ext_csd[EXT_CSD_PART_CONF];
1051 	}
1052 
1053 	if (IS_SD(mmc))
1054 		err = sd_change_freq(mmc);
1055 	else
1056 		err = mmc_change_freq(mmc);
1057 
1058 	if (err)
1059 		return err;
1060 
1061 	/* Restrict card's capabilities by what the host can do */
1062 	mmc->card_caps &= mmc->host_caps;
1063 
1064 	if (IS_SD(mmc)) {
1065 		if (mmc->card_caps & MMC_MODE_4BIT) {
1066 			cmd.cmdidx = MMC_CMD_APP_CMD;
1067 			cmd.resp_type = MMC_RSP_R1;
1068 			cmd.cmdarg = mmc->rca << 16;
1069 
1070 			err = mmc_send_cmd(mmc, &cmd, NULL);
1071 			if (err)
1072 				return err;
1073 
1074 			cmd.cmdidx = SD_CMD_APP_SET_BUS_WIDTH;
1075 			cmd.resp_type = MMC_RSP_R1;
1076 			cmd.cmdarg = 2;
1077 			err = mmc_send_cmd(mmc, &cmd, NULL);
1078 			if (err)
1079 				return err;
1080 
1081 			mmc_set_bus_width(mmc, 4);
1082 		}
1083 
1084 		if (mmc->card_caps & MMC_MODE_HS)
1085 			mmc->tran_speed = 50000000;
1086 		else
1087 			mmc->tran_speed = 25000000;
1088 	} else {
1089 		width = ((mmc->host_caps & MMC_MODE_MASK_WIDTH_BITS) >>
1090 			 MMC_MODE_WIDTH_BITS_SHIFT);
1091 		for (; width >= 0; width--) {
1092 			/* Set the card to use 4 bit*/
1093 			err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
1094 					EXT_CSD_BUS_WIDTH, width);
1095 
1096 			if (err)
1097 				continue;
1098 
1099 			if (!width) {
1100 				mmc_set_bus_width(mmc, 1);
1101 				break;
1102 			} else
1103 				mmc_set_bus_width(mmc, 4 * width);
1104 
1105 			err = mmc_send_ext_csd(mmc, test_csd);
1106 			if (!err && ext_csd[EXT_CSD_PARTITIONING_SUPPORT] \
1107 				    == test_csd[EXT_CSD_PARTITIONING_SUPPORT]
1108 				 && ext_csd[EXT_CSD_ERASE_GROUP_DEF] \
1109 				    == test_csd[EXT_CSD_ERASE_GROUP_DEF] \
1110 				 && ext_csd[EXT_CSD_REV] \
1111 				    == test_csd[EXT_CSD_REV]
1112 				 && ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] \
1113 				    == test_csd[EXT_CSD_HC_ERASE_GRP_SIZE]
1114 				 && memcmp(&ext_csd[EXT_CSD_SEC_CNT], \
1115 					&test_csd[EXT_CSD_SEC_CNT], 4) == 0) {
1116 
1117 				mmc->card_caps |= width;
1118 				break;
1119 			}
1120 		}
1121 
1122 		if (mmc->card_caps & MMC_MODE_HS) {
1123 			if (mmc->card_caps & MMC_MODE_HS_52MHz)
1124 				mmc->tran_speed = 52000000;
1125 			else
1126 				mmc->tran_speed = 26000000;
1127 		}
1128 	}
1129 
1130 	mmc_set_clock(mmc, mmc->tran_speed);
1131 
1132 	/* fill in device description */
1133 	mmc->block_dev.lun = 0;
1134 	mmc->block_dev.type = 0;
1135 	mmc->block_dev.blksz = mmc->read_bl_len;
1136 	mmc->block_dev.lba = lldiv(mmc->capacity, mmc->read_bl_len);
1137 	sprintf(mmc->block_dev.vendor, "Man %06x Snr %08x", mmc->cid[0] >> 8,
1138 			(mmc->cid[2] << 8) | (mmc->cid[3] >> 24));
1139 	sprintf(mmc->block_dev.product, "%c%c%c%c%c", mmc->cid[0] & 0xff,
1140 			(mmc->cid[1] >> 24), (mmc->cid[1] >> 16) & 0xff,
1141 			(mmc->cid[1] >> 8) & 0xff, mmc->cid[1] & 0xff);
1142 	sprintf(mmc->block_dev.revision, "%d.%d", mmc->cid[2] >> 28,
1143 			(mmc->cid[2] >> 24) & 0xf);
1144 #if !defined(CONFIG_SPL_BUILD) || defined(CONFIG_SPL_LIBDISK_SUPPORT)
1145 	init_part(&mmc->block_dev);
1146 #endif
1147 
1148 	return 0;
1149 }
1150 
1151 int mmc_send_if_cond(struct mmc *mmc)
1152 {
1153 	struct mmc_cmd cmd;
1154 	int err;
1155 
1156 	cmd.cmdidx = SD_CMD_SEND_IF_COND;
1157 	/* We set the bit if the host supports voltages between 2.7 and 3.6 V */
1158 	cmd.cmdarg = ((mmc->voltages & 0xff8000) != 0) << 8 | 0xaa;
1159 	cmd.resp_type = MMC_RSP_R7;
1160 
1161 	err = mmc_send_cmd(mmc, &cmd, NULL);
1162 
1163 	if (err)
1164 		return err;
1165 
1166 	if ((cmd.response[0] & 0xff) != 0xaa)
1167 		return UNUSABLE_ERR;
1168 	else
1169 		mmc->version = SD_VERSION_2;
1170 
1171 	return 0;
1172 }
1173 
1174 int mmc_register(struct mmc *mmc)
1175 {
1176 	/* Setup the universal parts of the block interface just once */
1177 	mmc->block_dev.if_type = IF_TYPE_MMC;
1178 	mmc->block_dev.dev = cur_dev_num++;
1179 	mmc->block_dev.removable = 1;
1180 	mmc->block_dev.block_read = mmc_bread;
1181 	mmc->block_dev.block_write = mmc_bwrite;
1182 	mmc->block_dev.block_erase = mmc_berase;
1183 	if (!mmc->b_max)
1184 		mmc->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
1185 
1186 	INIT_LIST_HEAD (&mmc->link);
1187 
1188 	list_add_tail (&mmc->link, &mmc_devices);
1189 
1190 	return 0;
1191 }
1192 
1193 #ifdef CONFIG_PARTITIONS
1194 block_dev_desc_t *mmc_get_dev(int dev)
1195 {
1196 	struct mmc *mmc = find_mmc_device(dev);
1197 	if (!mmc || mmc_init(mmc))
1198 		return NULL;
1199 
1200 	return &mmc->block_dev;
1201 }
1202 #endif
1203 
1204 int mmc_init(struct mmc *mmc)
1205 {
1206 	int err;
1207 
1208 	if (mmc_getcd(mmc) == 0) {
1209 		mmc->has_init = 0;
1210 		printf("MMC: no card present\n");
1211 		return NO_CARD_ERR;
1212 	}
1213 
1214 	if (mmc->has_init)
1215 		return 0;
1216 
1217 	err = mmc->init(mmc);
1218 
1219 	if (err)
1220 		return err;
1221 
1222 	mmc_set_bus_width(mmc, 1);
1223 	mmc_set_clock(mmc, 1);
1224 
1225 	/* Reset the Card */
1226 	err = mmc_go_idle(mmc);
1227 
1228 	if (err)
1229 		return err;
1230 
1231 	/* The internal partition reset to user partition(0) at every CMD0*/
1232 	mmc->part_num = 0;
1233 
1234 	/* Test for SD version 2 */
1235 	err = mmc_send_if_cond(mmc);
1236 
1237 	/* Now try to get the SD card's operating condition */
1238 	err = sd_send_op_cond(mmc);
1239 
1240 	/* If the command timed out, we check for an MMC card */
1241 	if (err == TIMEOUT) {
1242 		err = mmc_send_op_cond(mmc);
1243 
1244 		if (err) {
1245 			printf("Card did not respond to voltage select!\n");
1246 			return UNUSABLE_ERR;
1247 		}
1248 	}
1249 
1250 	err = mmc_startup(mmc);
1251 	if (err)
1252 		mmc->has_init = 0;
1253 	else
1254 		mmc->has_init = 1;
1255 	return err;
1256 }
1257 
1258 /*
1259  * CPU and board-specific MMC initializations.  Aliased function
1260  * signals caller to move on
1261  */
1262 static int __def_mmc_init(bd_t *bis)
1263 {
1264 	return -1;
1265 }
1266 
1267 int cpu_mmc_init(bd_t *bis) __attribute__((weak, alias("__def_mmc_init")));
1268 int board_mmc_init(bd_t *bis) __attribute__((weak, alias("__def_mmc_init")));
1269 
1270 void print_mmc_devices(char separator)
1271 {
1272 	struct mmc *m;
1273 	struct list_head *entry;
1274 
1275 	list_for_each(entry, &mmc_devices) {
1276 		m = list_entry(entry, struct mmc, link);
1277 
1278 		printf("%s: %d", m->name, m->block_dev.dev);
1279 
1280 		if (entry->next != &mmc_devices)
1281 			printf("%c ", separator);
1282 	}
1283 
1284 	printf("\n");
1285 }
1286 
1287 int get_mmc_num(void)
1288 {
1289 	return cur_dev_num;
1290 }
1291 
1292 int mmc_initialize(bd_t *bis)
1293 {
1294 	INIT_LIST_HEAD (&mmc_devices);
1295 	cur_dev_num = 0;
1296 
1297 	if (board_mmc_init(bis) < 0)
1298 		cpu_mmc_init(bis);
1299 
1300 	print_mmc_devices(',');
1301 
1302 	return 0;
1303 }
1304