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