1 /*
2  * Copyright (c) 2018-2022, ARM Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 /* Define a simple and generic interface to access eMMC and SD-card devices. */
8 
9 #include <assert.h>
10 #include <errno.h>
11 #include <stdbool.h>
12 #include <string.h>
13 
14 #include <arch_helpers.h>
15 #include <common/debug.h>
16 #include <drivers/delay_timer.h>
17 #include <drivers/mmc.h>
18 #include <lib/utils.h>
19 #include <plat/common/common_def.h>
20 
21 #define MMC_DEFAULT_MAX_RETRIES		5
22 #define SEND_OP_COND_MAX_RETRIES	100
23 
24 #define MULT_BY_512K_SHIFT		19
25 
26 static const struct mmc_ops *ops;
27 static unsigned int mmc_ocr_value;
28 static struct mmc_csd_emmc mmc_csd;
29 static struct sd_switch_status sd_switch_func_status;
30 static unsigned char mmc_ext_csd[512] __aligned(16);
31 static unsigned int mmc_flags;
32 static struct mmc_device_info *mmc_dev_info;
33 static unsigned int rca;
34 static unsigned int scr[2]__aligned(16) = { 0 };
35 
36 static const unsigned char tran_speed_base[16] = {
37 	0, 10, 12, 13, 15, 20, 26, 30, 35, 40, 45, 52, 55, 60, 70, 80
38 };
39 
40 static const unsigned char sd_tran_speed_base[16] = {
41 	0, 10, 12, 13, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 70, 80
42 };
43 
is_cmd23_enabled(void)44 static bool is_cmd23_enabled(void)
45 {
46 	return ((mmc_flags & MMC_FLAG_CMD23) != 0U);
47 }
48 
is_sd_cmd6_enabled(void)49 static bool is_sd_cmd6_enabled(void)
50 {
51 	return ((mmc_flags & MMC_FLAG_SD_CMD6) != 0U);
52 }
53 
mmc_send_cmd(unsigned int idx,unsigned int arg,unsigned int r_type,unsigned int * r_data)54 static int mmc_send_cmd(unsigned int idx, unsigned int arg,
55 			unsigned int r_type, unsigned int *r_data)
56 {
57 	struct mmc_cmd cmd;
58 	int ret;
59 
60 	zeromem(&cmd, sizeof(struct mmc_cmd));
61 
62 	cmd.cmd_idx = idx;
63 	cmd.cmd_arg = arg;
64 	cmd.resp_type = r_type;
65 
66 	ret = ops->send_cmd(&cmd);
67 
68 	if ((ret == 0) && (r_data != NULL)) {
69 		int i;
70 
71 		for (i = 0; i < 4; i++) {
72 			*r_data = cmd.resp_data[i];
73 			r_data++;
74 		}
75 	}
76 
77 	if (ret != 0) {
78 		VERBOSE("Send command %u error: %d\n", idx, ret);
79 	}
80 
81 	return ret;
82 }
83 
mmc_device_state(void)84 static int mmc_device_state(void)
85 {
86 	int retries = MMC_DEFAULT_MAX_RETRIES;
87 	unsigned int resp_data[4];
88 
89 	do {
90 		int ret;
91 
92 		if (retries == 0) {
93 			ERROR("CMD13 failed after %d retries\n",
94 			      MMC_DEFAULT_MAX_RETRIES);
95 			return -EIO;
96 		}
97 
98 		ret = mmc_send_cmd(MMC_CMD(13), rca << RCA_SHIFT_OFFSET,
99 				   MMC_RESPONSE_R1, &resp_data[0]);
100 		if (ret != 0) {
101 			retries--;
102 			continue;
103 		}
104 
105 		if ((resp_data[0] & STATUS_SWITCH_ERROR) != 0U) {
106 			return -EIO;
107 		}
108 
109 		retries--;
110 	} while ((resp_data[0] & STATUS_READY_FOR_DATA) == 0U);
111 
112 	return MMC_GET_STATE(resp_data[0]);
113 }
114 
mmc_send_part_switch_cmd(unsigned int part_config)115 static int mmc_send_part_switch_cmd(unsigned int part_config)
116 {
117 	int ret;
118 	unsigned int part_time = 0;
119 
120 	ret = mmc_send_cmd(MMC_CMD(6),
121 			   EXTCSD_WRITE_BYTES |
122 			   EXTCSD_CMD(CMD_EXTCSD_PARTITION_CONFIG) |
123 			   EXTCSD_VALUE(part_config) |
124 			   EXTCSD_CMD_SET_NORMAL,
125 			   MMC_RESPONSE_R1B, NULL);
126 	if (ret != 0) {
127 		return ret;
128 	}
129 
130 	/* Partition switch timing is in 10ms units */
131 	part_time = mmc_ext_csd[CMD_EXTCSD_PART_SWITCH_TIME] * 10;
132 
133 	mdelay(part_time);
134 
135 	do {
136 		ret = mmc_device_state();
137 		if (ret < 0) {
138 			return ret;
139 		}
140 	} while (ret == MMC_STATE_PRG);
141 
142 	return 0;
143 }
144 
mmc_set_ext_csd(unsigned int ext_cmd,unsigned int value)145 static int mmc_set_ext_csd(unsigned int ext_cmd, unsigned int value)
146 {
147 	int ret;
148 
149 	ret = mmc_send_cmd(MMC_CMD(6),
150 			   EXTCSD_WRITE_BYTES | EXTCSD_CMD(ext_cmd) |
151 			   EXTCSD_VALUE(value) | EXTCSD_CMD_SET_NORMAL,
152 			   MMC_RESPONSE_R1B, NULL);
153 	if (ret != 0) {
154 		return ret;
155 	}
156 
157 	do {
158 		ret = mmc_device_state();
159 		if (ret < 0) {
160 			return ret;
161 		}
162 	} while (ret == MMC_STATE_PRG);
163 
164 	return 0;
165 }
166 
mmc_sd_switch(unsigned int bus_width)167 static int mmc_sd_switch(unsigned int bus_width)
168 {
169 	int ret;
170 	int retries = MMC_DEFAULT_MAX_RETRIES;
171 	unsigned int bus_width_arg = 0;
172 
173 	ret = ops->prepare(0, (uintptr_t)&scr, sizeof(scr));
174 	if (ret != 0) {
175 		return ret;
176 	}
177 
178 	/* CMD55: Application Specific Command */
179 	ret = mmc_send_cmd(MMC_CMD(55), rca << RCA_SHIFT_OFFSET,
180 			   MMC_RESPONSE_R5, NULL);
181 	if (ret != 0) {
182 		return ret;
183 	}
184 
185 	/* ACMD51: SEND_SCR */
186 	do {
187 		ret = mmc_send_cmd(MMC_ACMD(51), 0, MMC_RESPONSE_R1, NULL);
188 		if ((ret != 0) && (retries == 0)) {
189 			ERROR("ACMD51 failed after %d retries (ret=%d)\n",
190 			      MMC_DEFAULT_MAX_RETRIES, ret);
191 			return ret;
192 		}
193 
194 		retries--;
195 	} while (ret != 0);
196 
197 	ret = ops->read(0, (uintptr_t)&scr, sizeof(scr));
198 	if (ret != 0) {
199 		return ret;
200 	}
201 
202 	if (((scr[0] & SD_SCR_BUS_WIDTH_4) != 0U) &&
203 	    (bus_width == MMC_BUS_WIDTH_4)) {
204 		bus_width_arg = 2;
205 	}
206 
207 	/* CMD55: Application Specific Command */
208 	ret = mmc_send_cmd(MMC_CMD(55), rca << RCA_SHIFT_OFFSET,
209 			   MMC_RESPONSE_R5, NULL);
210 	if (ret != 0) {
211 		return ret;
212 	}
213 
214 	/* ACMD6: SET_BUS_WIDTH */
215 	ret = mmc_send_cmd(MMC_ACMD(6), bus_width_arg, MMC_RESPONSE_R1, NULL);
216 	if (ret != 0) {
217 		return ret;
218 	}
219 
220 	do {
221 		ret = mmc_device_state();
222 		if (ret < 0) {
223 			return ret;
224 		}
225 	} while (ret == MMC_STATE_PRG);
226 
227 	return 0;
228 }
229 
mmc_set_ios(unsigned int clk,unsigned int bus_width)230 static int mmc_set_ios(unsigned int clk, unsigned int bus_width)
231 {
232 	int ret;
233 	unsigned int width = bus_width;
234 
235 	if (mmc_dev_info->mmc_dev_type != MMC_IS_EMMC) {
236 		if (width == MMC_BUS_WIDTH_8) {
237 			WARN("Wrong bus config for SD-card, force to 4\n");
238 			width = MMC_BUS_WIDTH_4;
239 		}
240 		ret = mmc_sd_switch(width);
241 		if (ret != 0) {
242 			return ret;
243 		}
244 	} else if (mmc_csd.spec_vers == 4U) {
245 		ret = mmc_set_ext_csd(CMD_EXTCSD_BUS_WIDTH,
246 				      (unsigned int)width);
247 		if (ret != 0) {
248 			return ret;
249 		}
250 	} else {
251 		VERBOSE("Wrong MMC type or spec version\n");
252 	}
253 
254 	return ops->set_ios(clk, width);
255 }
256 
mmc_fill_device_info(void)257 static int mmc_fill_device_info(void)
258 {
259 	unsigned long long c_size;
260 	unsigned int speed_idx;
261 	unsigned int nb_blocks;
262 	unsigned int freq_unit;
263 	int ret = 0;
264 	struct mmc_csd_sd_v2 *csd_sd_v2;
265 
266 	switch (mmc_dev_info->mmc_dev_type) {
267 	case MMC_IS_EMMC:
268 		mmc_dev_info->block_size = MMC_BLOCK_SIZE;
269 
270 		ret = ops->prepare(0, (uintptr_t)&mmc_ext_csd,
271 				   sizeof(mmc_ext_csd));
272 		if (ret != 0) {
273 			return ret;
274 		}
275 
276 		/* MMC CMD8: SEND_EXT_CSD */
277 		ret = mmc_send_cmd(MMC_CMD(8), 0, MMC_RESPONSE_R1, NULL);
278 		if (ret != 0) {
279 			return ret;
280 		}
281 
282 		ret = ops->read(0, (uintptr_t)&mmc_ext_csd,
283 				sizeof(mmc_ext_csd));
284 		if (ret != 0) {
285 			return ret;
286 		}
287 
288 		do {
289 			ret = mmc_device_state();
290 			if (ret < 0) {
291 				return ret;
292 			}
293 		} while (ret != MMC_STATE_TRAN);
294 
295 		nb_blocks = (mmc_ext_csd[CMD_EXTCSD_SEC_CNT] << 0) |
296 			    (mmc_ext_csd[CMD_EXTCSD_SEC_CNT + 1] << 8) |
297 			    (mmc_ext_csd[CMD_EXTCSD_SEC_CNT + 2] << 16) |
298 			    (mmc_ext_csd[CMD_EXTCSD_SEC_CNT + 3] << 24);
299 
300 		mmc_dev_info->device_size = (unsigned long long)nb_blocks *
301 			mmc_dev_info->block_size;
302 
303 		break;
304 
305 	case MMC_IS_SD:
306 		/*
307 		 * Use the same mmc_csd struct, as required fields here
308 		 * (READ_BL_LEN, C_SIZE, CSIZE_MULT) are common with eMMC.
309 		 */
310 		mmc_dev_info->block_size = BIT_32(mmc_csd.read_bl_len);
311 
312 		c_size = ((unsigned long long)mmc_csd.c_size_high << 2U) |
313 			 (unsigned long long)mmc_csd.c_size_low;
314 		assert(c_size != 0xFFFU);
315 
316 		mmc_dev_info->device_size = (c_size + 1U) *
317 					    BIT_64(mmc_csd.c_size_mult + 2U) *
318 					    mmc_dev_info->block_size;
319 
320 		break;
321 
322 	case MMC_IS_SD_HC:
323 		assert(mmc_csd.csd_structure == 1U);
324 
325 		mmc_dev_info->block_size = MMC_BLOCK_SIZE;
326 
327 		/* Need to use mmc_csd_sd_v2 struct */
328 		csd_sd_v2 = (struct mmc_csd_sd_v2 *)&mmc_csd;
329 		c_size = ((unsigned long long)csd_sd_v2->c_size_high << 16) |
330 			 (unsigned long long)csd_sd_v2->c_size_low;
331 
332 		mmc_dev_info->device_size = (c_size + 1U) << MULT_BY_512K_SHIFT;
333 
334 		break;
335 
336 	default:
337 		ret = -EINVAL;
338 		break;
339 	}
340 
341 	if (ret < 0) {
342 		return ret;
343 	}
344 
345 	speed_idx = (mmc_csd.tran_speed & CSD_TRAN_SPEED_MULT_MASK) >>
346 			 CSD_TRAN_SPEED_MULT_SHIFT;
347 
348 	assert(speed_idx > 0U);
349 
350 	if (mmc_dev_info->mmc_dev_type == MMC_IS_EMMC) {
351 		mmc_dev_info->max_bus_freq = tran_speed_base[speed_idx];
352 	} else {
353 		mmc_dev_info->max_bus_freq = sd_tran_speed_base[speed_idx];
354 	}
355 
356 	freq_unit = mmc_csd.tran_speed & CSD_TRAN_SPEED_UNIT_MASK;
357 	while (freq_unit != 0U) {
358 		mmc_dev_info->max_bus_freq *= 10U;
359 		--freq_unit;
360 	}
361 
362 	mmc_dev_info->max_bus_freq *= 10000U;
363 
364 	return 0;
365 }
366 
sd_switch(unsigned int mode,unsigned char group,unsigned char func)367 static int sd_switch(unsigned int mode, unsigned char group,
368 		     unsigned char func)
369 {
370 	unsigned int group_shift = (group - 1U) * 4U;
371 	unsigned int group_mask = GENMASK(group_shift + 3U,  group_shift);
372 	unsigned int arg;
373 	int ret;
374 
375 	ret = ops->prepare(0, (uintptr_t)&sd_switch_func_status,
376 			   sizeof(sd_switch_func_status));
377 	if (ret != 0) {
378 		return ret;
379 	}
380 
381 	/* MMC CMD6: SWITCH_FUNC */
382 	arg = mode | SD_SWITCH_ALL_GROUPS_MASK;
383 	arg &= ~group_mask;
384 	arg |= func << group_shift;
385 	ret = mmc_send_cmd(MMC_CMD(6), arg, MMC_RESPONSE_R1, NULL);
386 	if (ret != 0) {
387 		return ret;
388 	}
389 
390 	return ops->read(0, (uintptr_t)&sd_switch_func_status,
391 			 sizeof(sd_switch_func_status));
392 }
393 
sd_send_op_cond(void)394 static int sd_send_op_cond(void)
395 {
396 	int n;
397 	unsigned int resp_data[4];
398 
399 	for (n = 0; n < SEND_OP_COND_MAX_RETRIES; n++) {
400 		int ret;
401 
402 		/* CMD55: Application Specific Command */
403 		ret = mmc_send_cmd(MMC_CMD(55), 0, MMC_RESPONSE_R1, NULL);
404 		if (ret != 0) {
405 			return ret;
406 		}
407 
408 		/* ACMD41: SD_SEND_OP_COND */
409 		ret = mmc_send_cmd(MMC_ACMD(41), OCR_HCS |
410 			mmc_dev_info->ocr_voltage, MMC_RESPONSE_R3,
411 			&resp_data[0]);
412 		if (ret != 0) {
413 			return ret;
414 		}
415 
416 		if ((resp_data[0] & OCR_POWERUP) != 0U) {
417 			mmc_ocr_value = resp_data[0];
418 
419 			if ((mmc_ocr_value & OCR_HCS) != 0U) {
420 				mmc_dev_info->mmc_dev_type = MMC_IS_SD_HC;
421 			} else {
422 				mmc_dev_info->mmc_dev_type = MMC_IS_SD;
423 			}
424 
425 			return 0;
426 		}
427 
428 		mdelay(10);
429 	}
430 
431 	ERROR("ACMD41 failed after %d retries\n", SEND_OP_COND_MAX_RETRIES);
432 
433 	return -EIO;
434 }
435 
mmc_reset_to_idle(void)436 static int mmc_reset_to_idle(void)
437 {
438 	int ret;
439 
440 	/* CMD0: reset to IDLE */
441 	ret = mmc_send_cmd(MMC_CMD(0), 0, 0, NULL);
442 	if (ret != 0) {
443 		return ret;
444 	}
445 
446 	mdelay(2);
447 
448 	return 0;
449 }
450 
mmc_send_op_cond(void)451 static int mmc_send_op_cond(void)
452 {
453 	int ret, n;
454 	unsigned int resp_data[4];
455 
456 	ret = mmc_reset_to_idle();
457 	if (ret != 0) {
458 		return ret;
459 	}
460 
461 	for (n = 0; n < SEND_OP_COND_MAX_RETRIES; n++) {
462 		ret = mmc_send_cmd(MMC_CMD(1), OCR_SECTOR_MODE |
463 				   OCR_VDD_MIN_2V7 | OCR_VDD_MIN_1V7,
464 				   MMC_RESPONSE_R3, &resp_data[0]);
465 		if (ret != 0) {
466 			return ret;
467 		}
468 
469 		if ((resp_data[0] & OCR_POWERUP) != 0U) {
470 			mmc_ocr_value = resp_data[0];
471 			return 0;
472 		}
473 
474 		mdelay(10);
475 	}
476 
477 	ERROR("CMD1 failed after %d retries\n", SEND_OP_COND_MAX_RETRIES);
478 
479 	return -EIO;
480 }
481 
mmc_enumerate(unsigned int clk,unsigned int bus_width)482 static int mmc_enumerate(unsigned int clk, unsigned int bus_width)
483 {
484 	int ret;
485 	unsigned int resp_data[4];
486 
487 	ops->init();
488 
489 	ret = mmc_reset_to_idle();
490 	if (ret != 0) {
491 		return ret;
492 	}
493 
494 	if (mmc_dev_info->mmc_dev_type == MMC_IS_EMMC) {
495 		ret = mmc_send_op_cond();
496 	} else {
497 		/* CMD8: Send Interface Condition Command */
498 		ret = mmc_send_cmd(MMC_CMD(8), VHS_2_7_3_6_V | CMD8_CHECK_PATTERN,
499 				   MMC_RESPONSE_R5, &resp_data[0]);
500 
501 		if ((ret == 0) && ((resp_data[0] & 0xffU) == CMD8_CHECK_PATTERN)) {
502 			ret = sd_send_op_cond();
503 		}
504 	}
505 	if (ret != 0) {
506 		return ret;
507 	}
508 
509 	/* CMD2: Card Identification */
510 	ret = mmc_send_cmd(MMC_CMD(2), 0, MMC_RESPONSE_R2, NULL);
511 	if (ret != 0) {
512 		return ret;
513 	}
514 
515 	/* CMD3: Set Relative Address */
516 	if (mmc_dev_info->mmc_dev_type == MMC_IS_EMMC) {
517 		rca = MMC_FIX_RCA;
518 		ret = mmc_send_cmd(MMC_CMD(3), rca << RCA_SHIFT_OFFSET,
519 				   MMC_RESPONSE_R1, NULL);
520 		if (ret != 0) {
521 			return ret;
522 		}
523 	} else {
524 		ret = mmc_send_cmd(MMC_CMD(3), 0,
525 				   MMC_RESPONSE_R6, &resp_data[0]);
526 		if (ret != 0) {
527 			return ret;
528 		}
529 
530 		rca = (resp_data[0] & 0xFFFF0000U) >> 16;
531 	}
532 
533 	/* CMD9: CSD Register */
534 	ret = mmc_send_cmd(MMC_CMD(9), rca << RCA_SHIFT_OFFSET,
535 			   MMC_RESPONSE_R2, &resp_data[0]);
536 	if (ret != 0) {
537 		return ret;
538 	}
539 
540 	memcpy(&mmc_csd, &resp_data, sizeof(resp_data));
541 
542 	/* CMD7: Select Card */
543 	ret = mmc_send_cmd(MMC_CMD(7), rca << RCA_SHIFT_OFFSET,
544 			   MMC_RESPONSE_R1, NULL);
545 	if (ret != 0) {
546 		return ret;
547 	}
548 
549 	do {
550 		ret = mmc_device_state();
551 		if (ret < 0) {
552 			return ret;
553 		}
554 	} while (ret != MMC_STATE_TRAN);
555 
556 	ret = mmc_set_ios(clk, bus_width);
557 	if (ret != 0) {
558 		return ret;
559 	}
560 
561 	ret = mmc_fill_device_info();
562 	if (ret != 0) {
563 		return ret;
564 	}
565 
566 	if (is_sd_cmd6_enabled() &&
567 	    (mmc_dev_info->mmc_dev_type == MMC_IS_SD_HC)) {
568 		/* Try to switch to High Speed Mode */
569 		ret = sd_switch(SD_SWITCH_FUNC_CHECK, 1U, 1U);
570 		if (ret != 0) {
571 			return ret;
572 		}
573 
574 		if ((sd_switch_func_status.support_g1 & BIT(9)) == 0U) {
575 			/* High speed not supported, keep default speed */
576 			return 0;
577 		}
578 
579 		ret = sd_switch(SD_SWITCH_FUNC_SWITCH, 1U, 1U);
580 		if (ret != 0) {
581 			return ret;
582 		}
583 
584 		if ((sd_switch_func_status.sel_g2_g1 & 0x1U) == 0U) {
585 			/* Cannot switch to high speed, keep default speed */
586 			return 0;
587 		}
588 
589 		mmc_dev_info->max_bus_freq = 50000000U;
590 		ret = ops->set_ios(clk, bus_width);
591 	}
592 
593 	return ret;
594 }
595 
mmc_read_blocks(int lba,uintptr_t buf,size_t size)596 size_t mmc_read_blocks(int lba, uintptr_t buf, size_t size)
597 {
598 	int ret;
599 	unsigned int cmd_idx, cmd_arg;
600 
601 	assert((ops != NULL) &&
602 	       (ops->read != NULL) &&
603 	       (size != 0U) &&
604 	       ((size & MMC_BLOCK_MASK) == 0U));
605 
606 	ret = ops->prepare(lba, buf, size);
607 	if (ret != 0) {
608 		return 0;
609 	}
610 
611 	if (is_cmd23_enabled()) {
612 		/* Set block count */
613 		ret = mmc_send_cmd(MMC_CMD(23), size / MMC_BLOCK_SIZE,
614 				   MMC_RESPONSE_R1, NULL);
615 		if (ret != 0) {
616 			return 0;
617 		}
618 
619 		cmd_idx = MMC_CMD(18);
620 	} else {
621 		if (size > MMC_BLOCK_SIZE) {
622 			cmd_idx = MMC_CMD(18);
623 		} else {
624 			cmd_idx = MMC_CMD(17);
625 		}
626 	}
627 
628 	if (((mmc_ocr_value & OCR_ACCESS_MODE_MASK) == OCR_BYTE_MODE) &&
629 	    (mmc_dev_info->mmc_dev_type != MMC_IS_SD_HC)) {
630 		cmd_arg = lba * MMC_BLOCK_SIZE;
631 	} else {
632 		cmd_arg = lba;
633 	}
634 
635 	ret = mmc_send_cmd(cmd_idx, cmd_arg, MMC_RESPONSE_R1, NULL);
636 	if (ret != 0) {
637 		return 0;
638 	}
639 
640 	ret = ops->read(lba, buf, size);
641 	if (ret != 0) {
642 		return 0;
643 	}
644 
645 	/* Wait buffer empty */
646 	do {
647 		ret = mmc_device_state();
648 		if (ret < 0) {
649 			return 0;
650 		}
651 	} while ((ret != MMC_STATE_TRAN) && (ret != MMC_STATE_DATA));
652 
653 	if (!is_cmd23_enabled() && (size > MMC_BLOCK_SIZE)) {
654 		ret = mmc_send_cmd(MMC_CMD(12), 0, MMC_RESPONSE_R1B, NULL);
655 		if (ret != 0) {
656 			return 0;
657 		}
658 	}
659 
660 	return size;
661 }
662 
mmc_write_blocks(int lba,const uintptr_t buf,size_t size)663 size_t mmc_write_blocks(int lba, const uintptr_t buf, size_t size)
664 {
665 	int ret;
666 	unsigned int cmd_idx, cmd_arg;
667 
668 	assert((ops != NULL) &&
669 	       (ops->write != NULL) &&
670 	       (size != 0U) &&
671 	       ((buf & MMC_BLOCK_MASK) == 0U) &&
672 	       ((size & MMC_BLOCK_MASK) == 0U));
673 
674 	ret = ops->prepare(lba, buf, size);
675 	if (ret != 0) {
676 		return 0;
677 	}
678 
679 	if (is_cmd23_enabled()) {
680 		/* Set block count */
681 		ret = mmc_send_cmd(MMC_CMD(23), size / MMC_BLOCK_SIZE,
682 				   MMC_RESPONSE_R1, NULL);
683 		if (ret != 0) {
684 			return 0;
685 		}
686 
687 		cmd_idx = MMC_CMD(25);
688 	} else {
689 		if (size > MMC_BLOCK_SIZE) {
690 			cmd_idx = MMC_CMD(25);
691 		} else {
692 			cmd_idx = MMC_CMD(24);
693 		}
694 	}
695 
696 	if ((mmc_ocr_value & OCR_ACCESS_MODE_MASK) == OCR_BYTE_MODE) {
697 		cmd_arg = lba * MMC_BLOCK_SIZE;
698 	} else {
699 		cmd_arg = lba;
700 	}
701 
702 	ret = mmc_send_cmd(cmd_idx, cmd_arg, MMC_RESPONSE_R1, NULL);
703 	if (ret != 0) {
704 		return 0;
705 	}
706 
707 	ret = ops->write(lba, buf, size);
708 	if (ret != 0) {
709 		return 0;
710 	}
711 
712 	/* Wait buffer empty */
713 	do {
714 		ret = mmc_device_state();
715 		if (ret < 0) {
716 			return 0;
717 		}
718 	} while ((ret != MMC_STATE_TRAN) && (ret != MMC_STATE_RCV));
719 
720 	if (!is_cmd23_enabled() && (size > MMC_BLOCK_SIZE)) {
721 		ret = mmc_send_cmd(MMC_CMD(12), 0, MMC_RESPONSE_R1B, NULL);
722 		if (ret != 0) {
723 			return 0;
724 		}
725 	}
726 
727 	return size;
728 }
729 
mmc_erase_blocks(int lba,size_t size)730 size_t mmc_erase_blocks(int lba, size_t size)
731 {
732 	int ret;
733 
734 	assert(ops != NULL);
735 	assert((size != 0U) && ((size & MMC_BLOCK_MASK) == 0U));
736 
737 	ret = mmc_send_cmd(MMC_CMD(35), lba, MMC_RESPONSE_R1, NULL);
738 	if (ret != 0) {
739 		return 0;
740 	}
741 
742 	ret = mmc_send_cmd(MMC_CMD(36), lba + (size / MMC_BLOCK_SIZE) - 1U,
743 			   MMC_RESPONSE_R1, NULL);
744 	if (ret != 0) {
745 		return 0;
746 	}
747 
748 	ret = mmc_send_cmd(MMC_CMD(38), lba, MMC_RESPONSE_R1B, NULL);
749 	if (ret != 0) {
750 		return 0;
751 	}
752 
753 	do {
754 		ret = mmc_device_state();
755 		if (ret < 0) {
756 			return 0;
757 		}
758 	} while (ret != MMC_STATE_TRAN);
759 
760 	return size;
761 }
762 
mmc_part_switch(unsigned int part_type)763 static int mmc_part_switch(unsigned int part_type)
764 {
765 	uint8_t part_config = mmc_ext_csd[CMD_EXTCSD_PARTITION_CONFIG];
766 
767 	part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
768 	part_config |= part_type;
769 
770 	return mmc_send_part_switch_cmd(part_config);
771 }
772 
mmc_current_boot_part(void)773 static unsigned char mmc_current_boot_part(void)
774 {
775 	return PART_CFG_CURRENT_BOOT_PARTITION(mmc_ext_csd[CMD_EXTCSD_PARTITION_CONFIG]);
776 }
777 
mmc_part_switch_current_boot(void)778 int mmc_part_switch_current_boot(void)
779 {
780 	unsigned char current_boot_part = mmc_current_boot_part();
781 	int ret;
782 
783 	if (current_boot_part != 1U &&
784 	    current_boot_part != 2U) {
785 		ERROR("Got unexpected value for active boot partition, %u\n", current_boot_part);
786 		return -EIO;
787 	}
788 
789 	ret = mmc_part_switch(current_boot_part);
790 	if (ret < 0) {
791 		ERROR("Failed to switch to boot partition, %d\n", ret);
792 	}
793 
794 	return ret;
795 }
796 
mmc_part_switch_user(void)797 int mmc_part_switch_user(void)
798 {
799 	int ret;
800 
801 	ret = mmc_part_switch(PART_CFG_BOOT_PARTITION_NO_ACCESS);
802 	if (ret < 0) {
803 		ERROR("Failed to switch to user partition, %d\n", ret);
804 	}
805 
806 	return ret;
807 }
808 
mmc_boot_part_size(void)809 size_t mmc_boot_part_size(void)
810 {
811 	return mmc_ext_csd[CMD_EXTCSD_BOOT_SIZE_MULT] * SZ_128K;
812 }
813 
mmc_boot_part_read_blocks(int lba,uintptr_t buf,size_t size)814 size_t mmc_boot_part_read_blocks(int lba, uintptr_t buf, size_t size)
815 {
816 	size_t size_read;
817 	int ret;
818 
819 	ret = mmc_part_switch_current_boot();
820 	if (ret < 0) {
821 		return 0;
822 	}
823 
824 	size_read = mmc_read_blocks(lba, buf, size);
825 
826 	ret = mmc_part_switch_user();
827 	if (ret < 0) {
828 		return 0;
829 	}
830 
831 	return size_read;
832 }
833 
mmc_init(const struct mmc_ops * ops_ptr,unsigned int clk,unsigned int width,unsigned int flags,struct mmc_device_info * device_info)834 int mmc_init(const struct mmc_ops *ops_ptr, unsigned int clk,
835 	     unsigned int width, unsigned int flags,
836 	     struct mmc_device_info *device_info)
837 {
838 	assert((ops_ptr != NULL) &&
839 	       (ops_ptr->init != NULL) &&
840 	       (ops_ptr->send_cmd != NULL) &&
841 	       (ops_ptr->set_ios != NULL) &&
842 	       (ops_ptr->prepare != NULL) &&
843 	       (ops_ptr->read != NULL) &&
844 	       (ops_ptr->write != NULL) &&
845 	       (device_info != NULL) &&
846 	       (clk != 0) &&
847 	       ((width == MMC_BUS_WIDTH_1) ||
848 		(width == MMC_BUS_WIDTH_4) ||
849 		(width == MMC_BUS_WIDTH_8) ||
850 		(width == MMC_BUS_WIDTH_DDR_4) ||
851 		(width == MMC_BUS_WIDTH_DDR_8)));
852 
853 	ops = ops_ptr;
854 	mmc_flags = flags;
855 	mmc_dev_info = device_info;
856 
857 	return mmc_enumerate(clk, width);
858 }
859