xref: /openbmc/u-boot/drivers/mmc/mmc.c (revision 3e3eec39)
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 <mmc.h>
34 
35 static struct list_head mmc_devices;
36 static int cur_dev_num = -1;
37 
38 int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
39 {
40 	return mmc->send_cmd(mmc, cmd, data);
41 }
42 
43 int mmc_set_blocklen(struct mmc *mmc, int len)
44 {
45 	struct mmc_cmd cmd;
46 
47 	cmd.cmdidx = MMC_CMD_SET_BLOCKLEN;
48 	cmd.resp_type = MMC_RSP_R1;
49 	cmd.cmdarg = len;
50 	cmd.flags = 0;
51 
52 	return mmc_send_cmd(mmc, &cmd, NULL);
53 }
54 
55 struct mmc *find_mmc_device(int dev_num)
56 {
57 	struct mmc *m;
58 	struct list_head *entry;
59 
60 	list_for_each(entry, &mmc_devices) {
61 		m = list_entry(entry, struct mmc, link);
62 
63 		if (m->block_dev.dev == dev_num)
64 			return m;
65 	}
66 
67 	printf("MMC Device %d not found\n", dev_num);
68 
69 	return NULL;
70 }
71 
72 static ulong
73 mmc_bwrite(int dev_num, ulong start, lbaint_t blkcnt, const void*src)
74 {
75 	struct mmc_cmd cmd;
76 	struct mmc_data data;
77 	int err;
78 	int stoperr = 0;
79 	struct mmc *mmc = find_mmc_device(dev_num);
80 	int blklen;
81 
82 	if (!mmc)
83 		return -1;
84 
85 	blklen = mmc->write_bl_len;
86 
87 	err = mmc_set_blocklen(mmc, mmc->write_bl_len);
88 
89 	if (err) {
90 		printf("set write bl len failed\n\r");
91 		return err;
92 	}
93 
94 	if (blkcnt > 1)
95 		cmd.cmdidx = MMC_CMD_WRITE_MULTIPLE_BLOCK;
96 	else
97 		cmd.cmdidx = MMC_CMD_WRITE_SINGLE_BLOCK;
98 
99 	if (mmc->high_capacity)
100 		cmd.cmdarg = start;
101 	else
102 		cmd.cmdarg = start * blklen;
103 
104 	cmd.resp_type = MMC_RSP_R1;
105 	cmd.flags = 0;
106 
107 	data.src = src;
108 	data.blocks = blkcnt;
109 	data.blocksize = blklen;
110 	data.flags = MMC_DATA_WRITE;
111 
112 	err = mmc_send_cmd(mmc, &cmd, &data);
113 
114 	if (err) {
115 		printf("mmc write failed\n\r");
116 		return err;
117 	}
118 
119 	if (blkcnt > 1) {
120 		cmd.cmdidx = MMC_CMD_STOP_TRANSMISSION;
121 		cmd.cmdarg = 0;
122 		cmd.resp_type = MMC_RSP_R1b;
123 		cmd.flags = 0;
124 		stoperr = mmc_send_cmd(mmc, &cmd, NULL);
125 	}
126 
127 	return blkcnt;
128 }
129 
130 int mmc_read_block(struct mmc *mmc, void *dst, uint blocknum)
131 {
132 	struct mmc_cmd cmd;
133 	struct mmc_data data;
134 
135 	cmd.cmdidx = MMC_CMD_READ_SINGLE_BLOCK;
136 
137 	if (mmc->high_capacity)
138 		cmd.cmdarg = blocknum;
139 	else
140 		cmd.cmdarg = blocknum * mmc->read_bl_len;
141 
142 	cmd.resp_type = MMC_RSP_R1;
143 	cmd.flags = 0;
144 
145 	data.dest = dst;
146 	data.blocks = 1;
147 	data.blocksize = mmc->read_bl_len;
148 	data.flags = MMC_DATA_READ;
149 
150 	return mmc_send_cmd(mmc, &cmd, &data);
151 }
152 
153 int mmc_read(struct mmc *mmc, u64 src, uchar *dst, int size)
154 {
155 	char *buffer;
156 	int i;
157 	int blklen = mmc->read_bl_len;
158 	int startblock = src / blklen;
159 	int endblock = (src + size - 1) / blklen;
160 	int err = 0;
161 
162 	/* Make a buffer big enough to hold all the blocks we might read */
163 	buffer = malloc(blklen);
164 
165 	if (!buffer) {
166 		printf("Could not allocate buffer for MMC read!\n");
167 		return -1;
168 	}
169 
170 	/* We always do full block reads from the card */
171 	err = mmc_set_blocklen(mmc, mmc->read_bl_len);
172 
173 	if (err)
174 		return err;
175 
176 	for (i = startblock; i <= endblock; i++) {
177 		int segment_size;
178 		int offset;
179 
180 		err = mmc_read_block(mmc, buffer, i);
181 
182 		if (err)
183 			goto free_buffer;
184 
185 		/*
186 		 * The first block may not be aligned, so we
187 		 * copy from the desired point in the block
188 		 */
189 		offset = (src & (blklen - 1));
190 		segment_size = MIN(blklen - offset, size);
191 
192 		memcpy(dst, buffer + offset, segment_size);
193 
194 		dst += segment_size;
195 		src += segment_size;
196 		size -= segment_size;
197 	}
198 
199 free_buffer:
200 	free(buffer);
201 
202 	return err;
203 }
204 
205 static ulong mmc_bread(int dev_num, ulong start, lbaint_t blkcnt, void *dst)
206 {
207 	int err;
208 	int i;
209 	struct mmc *mmc = find_mmc_device(dev_num);
210 
211 	if (!mmc)
212 		return 0;
213 
214 	/* We always do full block reads from the card */
215 	err = mmc_set_blocklen(mmc, mmc->read_bl_len);
216 
217 	if (err) {
218 		return 0;
219 	}
220 
221 	for (i = start; i < start + blkcnt; i++, dst += mmc->read_bl_len) {
222 		err = mmc_read_block(mmc, dst, i);
223 
224 		if (err) {
225 			printf("block read failed: %d\n", err);
226 			return i - start;
227 		}
228 	}
229 
230 	return blkcnt;
231 }
232 
233 int mmc_go_idle(struct mmc* mmc)
234 {
235 	struct mmc_cmd cmd;
236 	int err;
237 
238 	udelay(1000);
239 
240 	cmd.cmdidx = MMC_CMD_GO_IDLE_STATE;
241 	cmd.cmdarg = 0;
242 	cmd.resp_type = MMC_RSP_NONE;
243 	cmd.flags = 0;
244 
245 	err = mmc_send_cmd(mmc, &cmd, NULL);
246 
247 	if (err)
248 		return err;
249 
250 	udelay(2000);
251 
252 	return 0;
253 }
254 
255 int
256 sd_send_op_cond(struct mmc *mmc)
257 {
258 	int timeout = 1000;
259 	int err;
260 	struct mmc_cmd cmd;
261 
262 	do {
263 		cmd.cmdidx = MMC_CMD_APP_CMD;
264 		cmd.resp_type = MMC_RSP_R1;
265 		cmd.cmdarg = 0;
266 		cmd.flags = 0;
267 
268 		err = mmc_send_cmd(mmc, &cmd, NULL);
269 
270 		if (err)
271 			return err;
272 
273 		cmd.cmdidx = SD_CMD_APP_SEND_OP_COND;
274 		cmd.resp_type = MMC_RSP_R3;
275 		cmd.cmdarg = mmc->voltages;
276 
277 		if (mmc->version == SD_VERSION_2)
278 			cmd.cmdarg |= OCR_HCS;
279 
280 		err = mmc_send_cmd(mmc, &cmd, NULL);
281 
282 		if (err)
283 			return err;
284 
285 		udelay(1000);
286 	} while ((!(cmd.response[0] & OCR_BUSY)) && timeout--);
287 
288 	if (timeout <= 0)
289 		return UNUSABLE_ERR;
290 
291 	if (mmc->version != SD_VERSION_2)
292 		mmc->version = SD_VERSION_1_0;
293 
294 	mmc->ocr = ((uint *)(cmd.response))[0];
295 
296 	mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
297 	mmc->rca = 0;
298 
299 	return 0;
300 }
301 
302 int mmc_send_op_cond(struct mmc *mmc)
303 {
304 	int timeout = 1000;
305 	struct mmc_cmd cmd;
306 	int err;
307 
308 	/* Some cards seem to need this */
309 	mmc_go_idle(mmc);
310 
311 	do {
312 		cmd.cmdidx = MMC_CMD_SEND_OP_COND;
313 		cmd.resp_type = MMC_RSP_R3;
314 		cmd.cmdarg = OCR_HCS | mmc->voltages;
315 		cmd.flags = 0;
316 
317 		err = mmc_send_cmd(mmc, &cmd, NULL);
318 
319 		if (err)
320 			return err;
321 
322 		udelay(1000);
323 	} while (!(cmd.response[0] & OCR_BUSY) && timeout--);
324 
325 	if (timeout <= 0)
326 		return UNUSABLE_ERR;
327 
328 	mmc->version = MMC_VERSION_UNKNOWN;
329 	mmc->ocr = ((uint *)(cmd.response))[0];
330 
331 	mmc->high_capacity = ((mmc->ocr & OCR_HCS) == OCR_HCS);
332 	mmc->rca = 0;
333 
334 	return 0;
335 }
336 
337 
338 int mmc_send_ext_csd(struct mmc *mmc, char *ext_csd)
339 {
340 	struct mmc_cmd cmd;
341 	struct mmc_data data;
342 	int err;
343 
344 	/* Get the Card Status Register */
345 	cmd.cmdidx = MMC_CMD_SEND_EXT_CSD;
346 	cmd.resp_type = MMC_RSP_R1;
347 	cmd.cmdarg = 0;
348 	cmd.flags = 0;
349 
350 	data.dest = ext_csd;
351 	data.blocks = 1;
352 	data.blocksize = 512;
353 	data.flags = MMC_DATA_READ;
354 
355 	err = mmc_send_cmd(mmc, &cmd, &data);
356 
357 	return err;
358 }
359 
360 
361 int mmc_switch(struct mmc *mmc, u8 set, u8 index, u8 value)
362 {
363 	struct mmc_cmd cmd;
364 
365 	cmd.cmdidx = MMC_CMD_SWITCH;
366 	cmd.resp_type = MMC_RSP_R1b;
367 	cmd.cmdarg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
368 		(index << 16) |
369 		(value << 8);
370 	cmd.flags = 0;
371 
372 	return mmc_send_cmd(mmc, &cmd, NULL);
373 }
374 
375 int mmc_change_freq(struct mmc *mmc)
376 {
377 	char ext_csd[512];
378 	char cardtype;
379 	int err;
380 
381 	mmc->card_caps = 0;
382 
383 	/* Only version 4 supports high-speed */
384 	if (mmc->version < MMC_VERSION_4)
385 		return 0;
386 
387 	mmc->card_caps |= MMC_MODE_4BIT;
388 
389 	err = mmc_send_ext_csd(mmc, ext_csd);
390 
391 	if (err)
392 		return err;
393 
394 	if (ext_csd[212] || ext_csd[213] || ext_csd[214] || ext_csd[215])
395 		mmc->high_capacity = 1;
396 
397 	cardtype = ext_csd[196] & 0xf;
398 
399 	err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING, 1);
400 
401 	if (err)
402 		return err;
403 
404 	/* Now check to see that it worked */
405 	err = mmc_send_ext_csd(mmc, ext_csd);
406 
407 	if (err)
408 		return err;
409 
410 	/* No high-speed support */
411 	if (!ext_csd[185])
412 		return 0;
413 
414 	/* High Speed is set, there are two types: 52MHz and 26MHz */
415 	if (cardtype & MMC_HS_52MHZ)
416 		mmc->card_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
417 	else
418 		mmc->card_caps |= MMC_MODE_HS;
419 
420 	return 0;
421 }
422 
423 int sd_switch(struct mmc *mmc, int mode, int group, u8 value, u8 *resp)
424 {
425 	struct mmc_cmd cmd;
426 	struct mmc_data data;
427 
428 	/* Switch the frequency */
429 	cmd.cmdidx = SD_CMD_SWITCH_FUNC;
430 	cmd.resp_type = MMC_RSP_R1;
431 	cmd.cmdarg = (mode << 31) | 0xffffff;
432 	cmd.cmdarg &= ~(0xf << (group * 4));
433 	cmd.cmdarg |= value << (group * 4);
434 	cmd.flags = 0;
435 
436 	data.dest = (char *)resp;
437 	data.blocksize = 64;
438 	data.blocks = 1;
439 	data.flags = MMC_DATA_READ;
440 
441 	return mmc_send_cmd(mmc, &cmd, &data);
442 }
443 
444 
445 int sd_change_freq(struct mmc *mmc)
446 {
447 	int err;
448 	struct mmc_cmd cmd;
449 	uint scr[2];
450 	uint switch_status[16];
451 	struct mmc_data data;
452 	int timeout;
453 
454 	mmc->card_caps = 0;
455 
456 	/* Read the SCR to find out if this card supports higher speeds */
457 	cmd.cmdidx = MMC_CMD_APP_CMD;
458 	cmd.resp_type = MMC_RSP_R1;
459 	cmd.cmdarg = mmc->rca << 16;
460 	cmd.flags = 0;
461 
462 	err = mmc_send_cmd(mmc, &cmd, NULL);
463 
464 	if (err)
465 		return err;
466 
467 	cmd.cmdidx = SD_CMD_APP_SEND_SCR;
468 	cmd.resp_type = MMC_RSP_R1;
469 	cmd.cmdarg = 0;
470 	cmd.flags = 0;
471 
472 	timeout = 3;
473 
474 retry_scr:
475 	data.dest = (char *)&scr;
476 	data.blocksize = 8;
477 	data.blocks = 1;
478 	data.flags = MMC_DATA_READ;
479 
480 	err = mmc_send_cmd(mmc, &cmd, &data);
481 
482 	if (err) {
483 		if (timeout--)
484 			goto retry_scr;
485 
486 		return err;
487 	}
488 
489 	mmc->scr[0] = scr[0];
490 	mmc->scr[1] = scr[1];
491 
492 	switch ((mmc->scr[0] >> 24) & 0xf) {
493 		case 0:
494 			mmc->version = SD_VERSION_1_0;
495 			break;
496 		case 1:
497 			mmc->version = SD_VERSION_1_10;
498 			break;
499 		case 2:
500 			mmc->version = SD_VERSION_2;
501 			break;
502 		default:
503 			mmc->version = SD_VERSION_1_0;
504 			break;
505 	}
506 
507 	/* Version 1.0 doesn't support switching */
508 	if (mmc->version == SD_VERSION_1_0)
509 		return 0;
510 
511 	timeout = 4;
512 	while (timeout--) {
513 		err = sd_switch(mmc, SD_SWITCH_CHECK, 0, 1,
514 				(u8 *)&switch_status);
515 
516 		if (err)
517 			return err;
518 
519 		/* The high-speed function is busy.  Try again */
520 		if (!switch_status[7] & SD_HIGHSPEED_BUSY)
521 			break;
522 	}
523 
524 	if (mmc->scr[0] & SD_DATA_4BIT)
525 		mmc->card_caps |= MMC_MODE_4BIT;
526 
527 	/* If high-speed isn't supported, we return */
528 	if (!(switch_status[3] & SD_HIGHSPEED_SUPPORTED))
529 		return 0;
530 
531 	err = sd_switch(mmc, SD_SWITCH_SWITCH, 0, 1, (u8 *)&switch_status);
532 
533 	if (err)
534 		return err;
535 
536 	if ((switch_status[4] & 0x0f000000) == 0x01000000)
537 		mmc->card_caps |= MMC_MODE_HS;
538 
539 	return 0;
540 }
541 
542 /* frequency bases */
543 /* divided by 10 to be nice to platforms without floating point */
544 int fbase[] = {
545 	10000,
546 	100000,
547 	1000000,
548 	10000000,
549 };
550 
551 /* Multiplier values for TRAN_SPEED.  Multiplied by 10 to be nice
552  * to platforms without floating point.
553  */
554 int multipliers[] = {
555 	0,	/* reserved */
556 	10,
557 	12,
558 	13,
559 	15,
560 	20,
561 	25,
562 	30,
563 	35,
564 	40,
565 	45,
566 	50,
567 	55,
568 	60,
569 	70,
570 	80,
571 };
572 
573 void mmc_set_ios(struct mmc *mmc)
574 {
575 	mmc->set_ios(mmc);
576 }
577 
578 void mmc_set_clock(struct mmc *mmc, uint clock)
579 {
580 	if (clock > mmc->f_max)
581 		clock = mmc->f_max;
582 
583 	if (clock < mmc->f_min)
584 		clock = mmc->f_min;
585 
586 	mmc->clock = clock;
587 
588 	mmc_set_ios(mmc);
589 }
590 
591 void mmc_set_bus_width(struct mmc *mmc, uint width)
592 {
593 	mmc->bus_width = width;
594 
595 	mmc_set_ios(mmc);
596 }
597 
598 int mmc_startup(struct mmc *mmc)
599 {
600 	int err;
601 	uint mult, freq;
602 	u64 cmult, csize;
603 	struct mmc_cmd cmd;
604 
605 	/* Put the Card in Identify Mode */
606 	cmd.cmdidx = MMC_CMD_ALL_SEND_CID;
607 	cmd.resp_type = MMC_RSP_R2;
608 	cmd.cmdarg = 0;
609 	cmd.flags = 0;
610 
611 	err = mmc_send_cmd(mmc, &cmd, NULL);
612 
613 	if (err)
614 		return err;
615 
616 	memcpy(mmc->cid, cmd.response, 16);
617 
618 	/*
619 	 * For MMC cards, set the Relative Address.
620 	 * For SD cards, get the Relatvie Address.
621 	 * This also puts the cards into Standby State
622 	 */
623 	cmd.cmdidx = SD_CMD_SEND_RELATIVE_ADDR;
624 	cmd.cmdarg = mmc->rca << 16;
625 	cmd.resp_type = MMC_RSP_R6;
626 	cmd.flags = 0;
627 
628 	err = mmc_send_cmd(mmc, &cmd, NULL);
629 
630 	if (err)
631 		return err;
632 
633 	if (IS_SD(mmc))
634 		mmc->rca = (((uint *)(cmd.response))[0] >> 16) & 0xffff;
635 
636 	/* Get the Card-Specific Data */
637 	cmd.cmdidx = MMC_CMD_SEND_CSD;
638 	cmd.resp_type = MMC_RSP_R2;
639 	cmd.cmdarg = mmc->rca << 16;
640 	cmd.flags = 0;
641 
642 	err = mmc_send_cmd(mmc, &cmd, NULL);
643 
644 	if (err)
645 		return err;
646 
647 	mmc->csd[0] = ((uint *)(cmd.response))[0];
648 	mmc->csd[1] = ((uint *)(cmd.response))[1];
649 	mmc->csd[2] = ((uint *)(cmd.response))[2];
650 	mmc->csd[3] = ((uint *)(cmd.response))[3];
651 
652 	if (mmc->version == MMC_VERSION_UNKNOWN) {
653 		int version = (cmd.response[0] >> 2) & 0xf;
654 
655 		switch (version) {
656 			case 0:
657 				mmc->version = MMC_VERSION_1_2;
658 				break;
659 			case 1:
660 				mmc->version = MMC_VERSION_1_4;
661 				break;
662 			case 2:
663 				mmc->version = MMC_VERSION_2_2;
664 				break;
665 			case 3:
666 				mmc->version = MMC_VERSION_3;
667 				break;
668 			case 4:
669 				mmc->version = MMC_VERSION_4;
670 				break;
671 			default:
672 				mmc->version = MMC_VERSION_1_2;
673 				break;
674 		}
675 	}
676 
677 	/* divide frequency by 10, since the mults are 10x bigger */
678 	freq = fbase[(cmd.response[3] & 0x7)];
679 	mult = multipliers[((cmd.response[3] >> 3) & 0xf)];
680 
681 	mmc->tran_speed = freq * mult;
682 
683 	mmc->read_bl_len = 1 << ((((uint *)(cmd.response))[1] >> 16) & 0xf);
684 
685 	if (IS_SD(mmc))
686 		mmc->write_bl_len = mmc->read_bl_len;
687 	else
688 		mmc->write_bl_len = 1 << ((((uint *)(cmd.response))[3] >> 22) & 0xf);
689 
690 	if (mmc->high_capacity) {
691 		csize = (mmc->csd[1] & 0x3f) << 16
692 			| (mmc->csd[2] & 0xffff0000) >> 16;
693 		cmult = 8;
694 	} else {
695 		csize = (mmc->csd[1] & 0x3ff) << 2
696 			| (mmc->csd[2] & 0xc0000000) >> 30;
697 		cmult = (mmc->csd[2] & 0x00038000) >> 15;
698 	}
699 
700 	mmc->capacity = (csize + 1) << (cmult + 2);
701 	mmc->capacity *= mmc->read_bl_len;
702 
703 	if (mmc->read_bl_len > 512)
704 		mmc->read_bl_len = 512;
705 
706 	if (mmc->write_bl_len > 512)
707 		mmc->write_bl_len = 512;
708 
709 	/* Select the card, and put it into Transfer Mode */
710 	cmd.cmdidx = MMC_CMD_SELECT_CARD;
711 	cmd.resp_type = MMC_RSP_R1b;
712 	cmd.cmdarg = mmc->rca << 16;
713 	cmd.flags = 0;
714 	err = mmc_send_cmd(mmc, &cmd, NULL);
715 
716 	if (err)
717 		return err;
718 
719 	if (IS_SD(mmc))
720 		err = sd_change_freq(mmc);
721 	else
722 		err = mmc_change_freq(mmc);
723 
724 	if (err)
725 		return err;
726 
727 	/* Restrict card's capabilities by what the host can do */
728 	mmc->card_caps &= mmc->host_caps;
729 
730 	if (IS_SD(mmc)) {
731 		if (mmc->card_caps & MMC_MODE_4BIT) {
732 			cmd.cmdidx = MMC_CMD_APP_CMD;
733 			cmd.resp_type = MMC_RSP_R1;
734 			cmd.cmdarg = mmc->rca << 16;
735 			cmd.flags = 0;
736 
737 			err = mmc_send_cmd(mmc, &cmd, NULL);
738 			if (err)
739 				return err;
740 
741 			cmd.cmdidx = SD_CMD_APP_SET_BUS_WIDTH;
742 			cmd.resp_type = MMC_RSP_R1;
743 			cmd.cmdarg = 2;
744 			cmd.flags = 0;
745 			err = mmc_send_cmd(mmc, &cmd, NULL);
746 			if (err)
747 				return err;
748 
749 			mmc_set_bus_width(mmc, 4);
750 		}
751 
752 		if (mmc->card_caps & MMC_MODE_HS)
753 			mmc_set_clock(mmc, 50000000);
754 		else
755 			mmc_set_clock(mmc, 25000000);
756 	} else {
757 		if (mmc->card_caps & MMC_MODE_4BIT) {
758 			/* Set the card to use 4 bit*/
759 			err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
760 					EXT_CSD_BUS_WIDTH,
761 					EXT_CSD_BUS_WIDTH_4);
762 
763 			if (err)
764 				return err;
765 
766 			mmc_set_bus_width(mmc, 4);
767 		} else if (mmc->card_caps & MMC_MODE_8BIT) {
768 			/* Set the card to use 8 bit*/
769 			err = mmc_switch(mmc, EXT_CSD_CMD_SET_NORMAL,
770 					EXT_CSD_BUS_WIDTH,
771 					EXT_CSD_BUS_WIDTH_8);
772 
773 			if (err)
774 				return err;
775 
776 			mmc_set_bus_width(mmc, 8);
777 		}
778 
779 		if (mmc->card_caps & MMC_MODE_HS) {
780 			if (mmc->card_caps & MMC_MODE_HS_52MHz)
781 				mmc_set_clock(mmc, 52000000);
782 			else
783 				mmc_set_clock(mmc, 26000000);
784 		} else
785 			mmc_set_clock(mmc, 20000000);
786 	}
787 
788 	/* fill in device description */
789 	mmc->block_dev.lun = 0;
790 	mmc->block_dev.type = 0;
791 	mmc->block_dev.blksz = mmc->read_bl_len;
792 	mmc->block_dev.lba = mmc->capacity/mmc->read_bl_len;
793 	sprintf(mmc->block_dev.vendor,"Man %02x%02x%02x Snr %02x%02x%02x%02x",
794 			mmc->cid[0], mmc->cid[1], mmc->cid[2],
795 			mmc->cid[9], mmc->cid[10], mmc->cid[11], mmc->cid[12]);
796 	sprintf(mmc->block_dev.product,"%c%c%c%c%c", mmc->cid[3],
797 			mmc->cid[4], mmc->cid[5], mmc->cid[6], mmc->cid[7]);
798 	sprintf(mmc->block_dev.revision,"%d.%d", mmc->cid[8] >> 4,
799 			mmc->cid[8] & 0xf);
800 	init_part(&mmc->block_dev);
801 
802 	return 0;
803 }
804 
805 int mmc_send_if_cond(struct mmc *mmc)
806 {
807 	struct mmc_cmd cmd;
808 	int err;
809 
810 	cmd.cmdidx = SD_CMD_SEND_IF_COND;
811 	/* We set the bit if the host supports voltages between 2.7 and 3.6 V */
812 	cmd.cmdarg = ((mmc->voltages & 0xff8000) != 0) << 8 | 0xaa;
813 	cmd.resp_type = MMC_RSP_R7;
814 	cmd.flags = 0;
815 
816 	err = mmc_send_cmd(mmc, &cmd, NULL);
817 
818 	if (err)
819 		return err;
820 
821 	if ((((uint *)(cmd.response))[0] & 0xff) != 0xaa)
822 		return UNUSABLE_ERR;
823 	else
824 		mmc->version = SD_VERSION_2;
825 
826 	return 0;
827 }
828 
829 int mmc_register(struct mmc *mmc)
830 {
831 	/* Setup the universal parts of the block interface just once */
832 	mmc->block_dev.if_type = IF_TYPE_MMC;
833 	mmc->block_dev.dev = cur_dev_num++;
834 	mmc->block_dev.removable = 1;
835 	mmc->block_dev.block_read = mmc_bread;
836 	mmc->block_dev.block_write = mmc_bwrite;
837 
838 	INIT_LIST_HEAD (&mmc->link);
839 
840 	list_add_tail (&mmc->link, &mmc_devices);
841 
842 	return 0;
843 }
844 
845 block_dev_desc_t *mmc_get_dev(int dev)
846 {
847 	struct mmc *mmc = find_mmc_device(dev);
848 
849 	return &mmc->block_dev;
850 }
851 
852 int mmc_init(struct mmc *mmc)
853 {
854 	int err;
855 
856 	err = mmc->init(mmc);
857 
858 	if (err)
859 		return err;
860 
861 	/* Reset the Card */
862 	err = mmc_go_idle(mmc);
863 
864 	if (err)
865 		return err;
866 
867 	/* Test for SD version 2 */
868 	err = mmc_send_if_cond(mmc);
869 
870 	/* If we got an error other than timeout, we bail */
871 	if (err && err != TIMEOUT)
872 		return err;
873 
874 	/* Now try to get the SD card's operating condition */
875 	err = sd_send_op_cond(mmc);
876 
877 	/* If the command timed out, we check for an MMC card */
878 	if (err == TIMEOUT) {
879 		err = mmc_send_op_cond(mmc);
880 
881 		if (err) {
882 			printf("Card did not respond to voltage select!\n");
883 			return UNUSABLE_ERR;
884 		}
885 	}
886 
887 	return mmc_startup(mmc);
888 }
889 
890 /*
891  * CPU and board-specific MMC initializations.  Aliased function
892  * signals caller to move on
893  */
894 static int __def_mmc_init(bd_t *bis)
895 {
896 	return -1;
897 }
898 
899 int cpu_mmc_init(bd_t *bis) __attribute((weak, alias("__def_mmc_init")));
900 int board_mmc_init(bd_t *bis) __attribute((weak, alias("__def_mmc_init")));
901 
902 void print_mmc_devices(char separator)
903 {
904 	struct mmc *m;
905 	struct list_head *entry;
906 
907 	list_for_each(entry, &mmc_devices) {
908 		m = list_entry(entry, struct mmc, link);
909 
910 		printf("%s: %d", m->name, m->block_dev.dev);
911 
912 		if (entry->next != &mmc_devices)
913 			printf("%c ", separator);
914 	}
915 
916 	printf("\n");
917 }
918 
919 int mmc_initialize(bd_t *bis)
920 {
921 	INIT_LIST_HEAD (&mmc_devices);
922 	cur_dev_num = 0;
923 
924 	if (board_mmc_init(bis) < 0)
925 		cpu_mmc_init(bis);
926 
927 	print_mmc_devices(',');
928 
929 	return 0;
930 }
931