mmc.c 16.3 KB
Newer Older
Yann Gautier's avatar
Yann Gautier committed
1
/*
2
 * Copyright (c) 2018-2021, ARM Limited and Contributors. All rights reserved.
Yann Gautier's avatar
Yann Gautier committed
3
4
5
6
7
8
9
10
11
12
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

/* Define a simple and generic interface to access eMMC and SD-card devices. */

#include <assert.h>
#include <errno.h>
#include <stdbool.h>
#include <string.h>
13
14
15
16
17
18

#include <arch_helpers.h>
#include <common/debug.h>
#include <drivers/delay_timer.h>
#include <drivers/mmc.h>
#include <lib/utils.h>
Yann Gautier's avatar
Yann Gautier committed
19
20
21
22
23
24
25
26
27

#define MMC_DEFAULT_MAX_RETRIES		5
#define SEND_OP_COND_MAX_RETRIES	100

#define MULT_BY_512K_SHIFT		19

static const struct mmc_ops *ops;
static unsigned int mmc_ocr_value;
static struct mmc_csd_emmc mmc_csd;
28
static unsigned char mmc_ext_csd[512] __aligned(16);
Yann Gautier's avatar
Yann Gautier committed
29
static unsigned int mmc_flags;
30
static struct mmc_device_info *mmc_dev_info;
Yann Gautier's avatar
Yann Gautier committed
31
static unsigned int rca;
32
static unsigned int scr[2]__aligned(16) = { 0 };
Yann Gautier's avatar
Yann Gautier committed
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91

static const unsigned char tran_speed_base[16] = {
	0, 10, 12, 13, 15, 20, 26, 30, 35, 40, 45, 52, 55, 60, 70, 80
};

static const unsigned char sd_tran_speed_base[16] = {
	0, 10, 12, 13, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 70, 80
};

static bool is_cmd23_enabled(void)
{
	return ((mmc_flags & MMC_FLAG_CMD23) != 0U);
}

static int mmc_send_cmd(unsigned int idx, unsigned int arg,
			unsigned int r_type, unsigned int *r_data)
{
	struct mmc_cmd cmd;
	int ret;

	zeromem(&cmd, sizeof(struct mmc_cmd));

	cmd.cmd_idx = idx;
	cmd.cmd_arg = arg;
	cmd.resp_type = r_type;

	ret = ops->send_cmd(&cmd);

	if ((ret == 0) && (r_data != NULL)) {
		int i;

		for (i = 0; i < 4; i++) {
			*r_data = cmd.resp_data[i];
			r_data++;
		}
	}

	if (ret != 0) {
		VERBOSE("Send command %u error: %d\n", idx, ret);
	}

	return ret;
}

static int mmc_device_state(void)
{
	int retries = MMC_DEFAULT_MAX_RETRIES;
	unsigned int resp_data[4];

	do {
		int ret;

		if (retries == 0) {
			ERROR("CMD13 failed after %d retries\n",
			      MMC_DEFAULT_MAX_RETRIES);
			return -EIO;
		}

		ret = mmc_send_cmd(MMC_CMD(13), rca << RCA_SHIFT_OFFSET,
92
				   MMC_RESPONSE_R1, &resp_data[0]);
Yann Gautier's avatar
Yann Gautier committed
93
		if (ret != 0) {
94
95
			retries--;
			continue;
Yann Gautier's avatar
Yann Gautier committed
96
97
98
99
100
101
102
103
104
105
106
107
		}

		if ((resp_data[0] & STATUS_SWITCH_ERROR) != 0U) {
			return -EIO;
		}

		retries--;
	} while ((resp_data[0] & STATUS_READY_FOR_DATA) == 0U);

	return MMC_GET_STATE(resp_data[0]);
}

108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
static int mmc_send_part_switch_cmd(unsigned int part_config)
{
	int ret;
	unsigned int part_time = 0;

	ret = mmc_send_cmd(MMC_CMD(6),
			   EXTCSD_WRITE_BYTES |
			   EXTCSD_CMD(CMD_EXTCSD_PARTITION_CONFIG) |
			   EXTCSD_VALUE(part_config) |
			   EXTCSD_CMD_SET_NORMAL,
			   MMC_RESPONSE_R1B, NULL);
	if (ret != 0) {
		return ret;
	}

	/* Partition switch timing is in 10ms units */
	part_time = mmc_ext_csd[CMD_EXTCSD_PART_SWITCH_TIME] * 10;

	mdelay(part_time);

	do {
		ret = mmc_device_state();
		if (ret < 0) {
			return ret;
		}
	} while (ret == MMC_STATE_PRG);

	return 0;
}

Yann Gautier's avatar
Yann Gautier committed
138
139
140
141
142
143
144
static int mmc_set_ext_csd(unsigned int ext_cmd, unsigned int value)
{
	int ret;

	ret = mmc_send_cmd(MMC_CMD(6),
			   EXTCSD_WRITE_BYTES | EXTCSD_CMD(ext_cmd) |
			   EXTCSD_VALUE(value) | EXTCSD_CMD_SET_NORMAL,
145
			   MMC_RESPONSE_R1B, NULL);
Yann Gautier's avatar
Yann Gautier committed
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
	if (ret != 0) {
		return ret;
	}

	do {
		ret = mmc_device_state();
		if (ret < 0) {
			return ret;
		}
	} while (ret == MMC_STATE_PRG);

	return 0;
}

static int mmc_sd_switch(unsigned int bus_width)
{
	int ret;
	int retries = MMC_DEFAULT_MAX_RETRIES;
	unsigned int bus_width_arg = 0;

	ret = ops->prepare(0, (uintptr_t)&scr, sizeof(scr));
	if (ret != 0) {
		return ret;
	}

	/* CMD55: Application Specific Command */
	ret = mmc_send_cmd(MMC_CMD(55), rca << RCA_SHIFT_OFFSET,
173
			   MMC_RESPONSE_R5, NULL);
Yann Gautier's avatar
Yann Gautier committed
174
175
176
177
178
179
	if (ret != 0) {
		return ret;
	}

	/* ACMD51: SEND_SCR */
	do {
180
		ret = mmc_send_cmd(MMC_ACMD(51), 0, MMC_RESPONSE_R1, NULL);
Yann Gautier's avatar
Yann Gautier committed
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
		if ((ret != 0) && (retries == 0)) {
			ERROR("ACMD51 failed after %d retries (ret=%d)\n",
			      MMC_DEFAULT_MAX_RETRIES, ret);
			return ret;
		}

		retries--;
	} while (ret != 0);

	ret = ops->read(0, (uintptr_t)&scr, sizeof(scr));
	if (ret != 0) {
		return ret;
	}

	if (((scr[0] & SD_SCR_BUS_WIDTH_4) != 0U) &&
	    (bus_width == MMC_BUS_WIDTH_4)) {
		bus_width_arg = 2;
	}

	/* CMD55: Application Specific Command */
	ret = mmc_send_cmd(MMC_CMD(55), rca << RCA_SHIFT_OFFSET,
202
			   MMC_RESPONSE_R5, NULL);
Yann Gautier's avatar
Yann Gautier committed
203
204
205
206
207
	if (ret != 0) {
		return ret;
	}

	/* ACMD6: SET_BUS_WIDTH */
208
	ret = mmc_send_cmd(MMC_ACMD(6), bus_width_arg, MMC_RESPONSE_R1, NULL);
Yann Gautier's avatar
Yann Gautier committed
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
	if (ret != 0) {
		return ret;
	}

	do {
		ret = mmc_device_state();
		if (ret < 0) {
			return ret;
		}
	} while (ret == MMC_STATE_PRG);

	return 0;
}

static int mmc_set_ios(unsigned int clk, unsigned int bus_width)
{
	int ret;
	unsigned int width = bus_width;

228
	if (mmc_dev_info->mmc_dev_type != MMC_IS_EMMC) {
Yann Gautier's avatar
Yann Gautier committed
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
		if (width == MMC_BUS_WIDTH_8) {
			WARN("Wrong bus config for SD-card, force to 4\n");
			width = MMC_BUS_WIDTH_4;
		}
		ret = mmc_sd_switch(width);
		if (ret != 0) {
			return ret;
		}
	} else if (mmc_csd.spec_vers == 4U) {
		ret = mmc_set_ext_csd(CMD_EXTCSD_BUS_WIDTH,
				      (unsigned int)width);
		if (ret != 0) {
			return ret;
		}
	} else {
		VERBOSE("Wrong MMC type or spec version\n");
	}

	return ops->set_ios(clk, width);
}

static int mmc_fill_device_info(void)
{
	unsigned long long c_size;
	unsigned int speed_idx;
	unsigned int nb_blocks;
	unsigned int freq_unit;
256
	int ret = 0;
Yann Gautier's avatar
Yann Gautier committed
257
258
	struct mmc_csd_sd_v2 *csd_sd_v2;

259
	switch (mmc_dev_info->mmc_dev_type) {
Yann Gautier's avatar
Yann Gautier committed
260
	case MMC_IS_EMMC:
261
		mmc_dev_info->block_size = MMC_BLOCK_SIZE;
Yann Gautier's avatar
Yann Gautier committed
262
263
264
265
266
267
268
269

		ret = ops->prepare(0, (uintptr_t)&mmc_ext_csd,
				   sizeof(mmc_ext_csd));
		if (ret != 0) {
			return ret;
		}

		/* MMC CMD8: SEND_EXT_CSD */
270
		ret = mmc_send_cmd(MMC_CMD(8), 0, MMC_RESPONSE_R1, NULL);
Yann Gautier's avatar
Yann Gautier committed
271
272
273
274
275
276
277
278
279
280
		if (ret != 0) {
			return ret;
		}

		ret = ops->read(0, (uintptr_t)&mmc_ext_csd,
				sizeof(mmc_ext_csd));
		if (ret != 0) {
			return ret;
		}

281
282
283
284
285
286
287
		do {
			ret = mmc_device_state();
			if (ret < 0) {
				return ret;
			}
		} while (ret != MMC_STATE_TRAN);

Yann Gautier's avatar
Yann Gautier committed
288
289
290
291
292
		nb_blocks = (mmc_ext_csd[CMD_EXTCSD_SEC_CNT] << 0) |
			    (mmc_ext_csd[CMD_EXTCSD_SEC_CNT + 1] << 8) |
			    (mmc_ext_csd[CMD_EXTCSD_SEC_CNT + 2] << 16) |
			    (mmc_ext_csd[CMD_EXTCSD_SEC_CNT + 3] << 24);

293
294
		mmc_dev_info->device_size = (unsigned long long)nb_blocks *
			mmc_dev_info->block_size;
Yann Gautier's avatar
Yann Gautier committed
295
296
297
298
299
300
301
302

		break;

	case MMC_IS_SD:
		/*
		 * Use the same mmc_csd struct, as required fields here
		 * (READ_BL_LEN, C_SIZE, CSIZE_MULT) are common with eMMC.
		 */
303
		mmc_dev_info->block_size = BIT_32(mmc_csd.read_bl_len);
Yann Gautier's avatar
Yann Gautier committed
304
305
306
307
308

		c_size = ((unsigned long long)mmc_csd.c_size_high << 2U) |
			 (unsigned long long)mmc_csd.c_size_low;
		assert(c_size != 0xFFFU);

309
		mmc_dev_info->device_size = (c_size + 1U) *
Yann Gautier's avatar
Yann Gautier committed
310
					    BIT_64(mmc_csd.c_size_mult + 2U) *
311
					    mmc_dev_info->block_size;
Yann Gautier's avatar
Yann Gautier committed
312
313
314
315
316
317

		break;

	case MMC_IS_SD_HC:
		assert(mmc_csd.csd_structure == 1U);

318
		mmc_dev_info->block_size = MMC_BLOCK_SIZE;
Yann Gautier's avatar
Yann Gautier committed
319
320
321
322
323
324

		/* Need to use mmc_csd_sd_v2 struct */
		csd_sd_v2 = (struct mmc_csd_sd_v2 *)&mmc_csd;
		c_size = ((unsigned long long)csd_sd_v2->c_size_high << 16) |
			 (unsigned long long)csd_sd_v2->c_size_low;

325
		mmc_dev_info->device_size = (c_size + 1U) << MULT_BY_512K_SHIFT;
Yann Gautier's avatar
Yann Gautier committed
326
327
328
329
330
331
332
333

		break;

	default:
		ret = -EINVAL;
		break;
	}

334
	if (ret < 0) {
Yann Gautier's avatar
Yann Gautier committed
335
336
337
338
339
340
341
342
		return ret;
	}

	speed_idx = (mmc_csd.tran_speed & CSD_TRAN_SPEED_MULT_MASK) >>
			 CSD_TRAN_SPEED_MULT_SHIFT;

	assert(speed_idx > 0U);

343
344
	if (mmc_dev_info->mmc_dev_type == MMC_IS_EMMC) {
		mmc_dev_info->max_bus_freq = tran_speed_base[speed_idx];
Yann Gautier's avatar
Yann Gautier committed
345
	} else {
346
		mmc_dev_info->max_bus_freq = sd_tran_speed_base[speed_idx];
Yann Gautier's avatar
Yann Gautier committed
347
348
349
350
	}

	freq_unit = mmc_csd.tran_speed & CSD_TRAN_SPEED_UNIT_MASK;
	while (freq_unit != 0U) {
351
		mmc_dev_info->max_bus_freq *= 10U;
Yann Gautier's avatar
Yann Gautier committed
352
353
354
		--freq_unit;
	}

355
	mmc_dev_info->max_bus_freq *= 10000U;
Yann Gautier's avatar
Yann Gautier committed
356
357
358
359
360
361

	return 0;
}

static int sd_send_op_cond(void)
{
362
	int n;
Yann Gautier's avatar
Yann Gautier committed
363
364
	unsigned int resp_data[4];

365
	for (n = 0; n < SEND_OP_COND_MAX_RETRIES; n++) {
Yann Gautier's avatar
Yann Gautier committed
366
367
368
		int ret;

		/* CMD55: Application Specific Command */
369
		ret = mmc_send_cmd(MMC_CMD(55), 0, MMC_RESPONSE_R1, NULL);
Yann Gautier's avatar
Yann Gautier committed
370
371
372
373
374
		if (ret != 0) {
			return ret;
		}

		/* ACMD41: SD_SEND_OP_COND */
375
		ret = mmc_send_cmd(MMC_ACMD(41), OCR_HCS |
376
			mmc_dev_info->ocr_voltage, MMC_RESPONSE_R3,
377
			&resp_data[0]);
Yann Gautier's avatar
Yann Gautier committed
378
379
380
381
		if (ret != 0) {
			return ret;
		}

382
383
		if ((resp_data[0] & OCR_POWERUP) != 0U) {
			mmc_ocr_value = resp_data[0];
Yann Gautier's avatar
Yann Gautier committed
384

385
			if ((mmc_ocr_value & OCR_HCS) != 0U) {
386
				mmc_dev_info->mmc_dev_type = MMC_IS_SD_HC;
387
			} else {
388
				mmc_dev_info->mmc_dev_type = MMC_IS_SD;
389
			}
Yann Gautier's avatar
Yann Gautier committed
390

391
392
393
			return 0;
		}

394
		mdelay(10);
Yann Gautier's avatar
Yann Gautier committed
395
396
	}

397
398
399
	ERROR("ACMD41 failed after %d retries\n", SEND_OP_COND_MAX_RETRIES);

	return -EIO;
Yann Gautier's avatar
Yann Gautier committed
400
401
}

402
static int mmc_reset_to_idle(void)
Yann Gautier's avatar
Yann Gautier committed
403
404
{
	int ret;
405

Yann Gautier's avatar
Yann Gautier committed
406
407
408
409
410
411
	/* CMD0: reset to IDLE */
	ret = mmc_send_cmd(MMC_CMD(0), 0, 0, NULL);
	if (ret != 0) {
		return ret;
	}

412
	mdelay(2);
Yann Gautier's avatar
Yann Gautier committed
413

414
415
416
417
418
419
420
421
	return 0;
}

static int mmc_send_op_cond(void)
{
	int ret, n;
	unsigned int resp_data[4];

422
423
424
	ret = mmc_reset_to_idle();
	if (ret != 0) {
		return ret;
425
	}
426
427

	for (n = 0; n < SEND_OP_COND_MAX_RETRIES; n++) {
Yann Gautier's avatar
Yann Gautier committed
428
429
		ret = mmc_send_cmd(MMC_CMD(1), OCR_SECTOR_MODE |
				   OCR_VDD_MIN_2V7 | OCR_VDD_MIN_1V7,
430
				   MMC_RESPONSE_R3, &resp_data[0]);
Yann Gautier's avatar
Yann Gautier committed
431
432
433
434
		if (ret != 0) {
			return ret;
		}

435
436
437
438
		if ((resp_data[0] & OCR_POWERUP) != 0U) {
			mmc_ocr_value = resp_data[0];
			return 0;
		}
Yann Gautier's avatar
Yann Gautier committed
439

440
		mdelay(10);
441
	}
Yann Gautier's avatar
Yann Gautier committed
442

443
444
445
	ERROR("CMD1 failed after %d retries\n", SEND_OP_COND_MAX_RETRIES);

	return -EIO;
Yann Gautier's avatar
Yann Gautier committed
446
447
448
449
450
451
452
453
454
}

static int mmc_enumerate(unsigned int clk, unsigned int bus_width)
{
	int ret;
	unsigned int resp_data[4];

	ops->init();

455
456
457
	ret = mmc_reset_to_idle();
	if (ret != 0) {
		return ret;
458
	}
Yann Gautier's avatar
Yann Gautier committed
459

460
	if (mmc_dev_info->mmc_dev_type == MMC_IS_EMMC) {
Yann Gautier's avatar
Yann Gautier committed
461
		ret = mmc_send_op_cond();
462
463
464
	} else {
		/* CMD8: Send Interface Condition Command */
		ret = mmc_send_cmd(MMC_CMD(8), VHS_2_7_3_6_V | CMD8_CHECK_PATTERN,
465
				   MMC_RESPONSE_R5, &resp_data[0]);
466
467
468
469

		if ((ret == 0) && ((resp_data[0] & 0xffU) == CMD8_CHECK_PATTERN)) {
			ret = sd_send_op_cond();
		}
Yann Gautier's avatar
Yann Gautier committed
470
471
472
473
474
475
	}
	if (ret != 0) {
		return ret;
	}

	/* CMD2: Card Identification */
476
	ret = mmc_send_cmd(MMC_CMD(2), 0, MMC_RESPONSE_R2, NULL);
Yann Gautier's avatar
Yann Gautier committed
477
478
479
480
481
	if (ret != 0) {
		return ret;
	}

	/* CMD3: Set Relative Address */
482
	if (mmc_dev_info->mmc_dev_type == MMC_IS_EMMC) {
Yann Gautier's avatar
Yann Gautier committed
483
484
		rca = MMC_FIX_RCA;
		ret = mmc_send_cmd(MMC_CMD(3), rca << RCA_SHIFT_OFFSET,
485
				   MMC_RESPONSE_R1, NULL);
Yann Gautier's avatar
Yann Gautier committed
486
487
488
489
490
		if (ret != 0) {
			return ret;
		}
	} else {
		ret = mmc_send_cmd(MMC_CMD(3), 0,
491
				   MMC_RESPONSE_R6, &resp_data[0]);
Yann Gautier's avatar
Yann Gautier committed
492
493
494
495
496
497
498
499
500
		if (ret != 0) {
			return ret;
		}

		rca = (resp_data[0] & 0xFFFF0000U) >> 16;
	}

	/* CMD9: CSD Register */
	ret = mmc_send_cmd(MMC_CMD(9), rca << RCA_SHIFT_OFFSET,
501
			   MMC_RESPONSE_R2, &resp_data[0]);
Yann Gautier's avatar
Yann Gautier committed
502
503
504
505
506
507
508
509
	if (ret != 0) {
		return ret;
	}

	memcpy(&mmc_csd, &resp_data, sizeof(resp_data));

	/* CMD7: Select Card */
	ret = mmc_send_cmd(MMC_CMD(7), rca << RCA_SHIFT_OFFSET,
510
			   MMC_RESPONSE_R1, NULL);
Yann Gautier's avatar
Yann Gautier committed
511
512
513
514
515
516
517
518
519
520
521
	if (ret != 0) {
		return ret;
	}

	do {
		ret = mmc_device_state();
		if (ret < 0) {
			return ret;
		}
	} while (ret != MMC_STATE_TRAN);

522
	ret = mmc_set_ios(clk, bus_width);
Yann Gautier's avatar
Yann Gautier committed
523
524
525
526
	if (ret != 0) {
		return ret;
	}

527
	return mmc_fill_device_info();
Yann Gautier's avatar
Yann Gautier committed
528
529
}

530
size_t mmc_read_blocks(int lba, uintptr_t buf, size_t size)
Yann Gautier's avatar
Yann Gautier committed
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
{
	int ret;
	unsigned int cmd_idx, cmd_arg;

	assert((ops != NULL) &&
	       (ops->read != NULL) &&
	       (size != 0U) &&
	       ((size & MMC_BLOCK_MASK) == 0U));

	ret = ops->prepare(lba, buf, size);
	if (ret != 0) {
		return 0;
	}

	if (is_cmd23_enabled()) {
		/* Set block count */
		ret = mmc_send_cmd(MMC_CMD(23), size / MMC_BLOCK_SIZE,
548
				   MMC_RESPONSE_R1, NULL);
Yann Gautier's avatar
Yann Gautier committed
549
550
551
552
553
554
555
556
557
558
559
560
561
562
		if (ret != 0) {
			return 0;
		}

		cmd_idx = MMC_CMD(18);
	} else {
		if (size > MMC_BLOCK_SIZE) {
			cmd_idx = MMC_CMD(18);
		} else {
			cmd_idx = MMC_CMD(17);
		}
	}

	if (((mmc_ocr_value & OCR_ACCESS_MODE_MASK) == OCR_BYTE_MODE) &&
563
	    (mmc_dev_info->mmc_dev_type != MMC_IS_SD_HC)) {
Yann Gautier's avatar
Yann Gautier committed
564
565
566
567
568
		cmd_arg = lba * MMC_BLOCK_SIZE;
	} else {
		cmd_arg = lba;
	}

569
	ret = mmc_send_cmd(cmd_idx, cmd_arg, MMC_RESPONSE_R1, NULL);
Yann Gautier's avatar
Yann Gautier committed
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
	if (ret != 0) {
		return 0;
	}

	ret = ops->read(lba, buf, size);
	if (ret != 0) {
		return 0;
	}

	/* Wait buffer empty */
	do {
		ret = mmc_device_state();
		if (ret < 0) {
			return 0;
		}
	} while ((ret != MMC_STATE_TRAN) && (ret != MMC_STATE_DATA));

	if (!is_cmd23_enabled() && (size > MMC_BLOCK_SIZE)) {
588
		ret = mmc_send_cmd(MMC_CMD(12), 0, MMC_RESPONSE_R1B, NULL);
Yann Gautier's avatar
Yann Gautier committed
589
590
591
592
593
594
595
596
		if (ret != 0) {
			return 0;
		}
	}

	return size;
}

597
size_t mmc_write_blocks(int lba, const uintptr_t buf, size_t size)
Yann Gautier's avatar
Yann Gautier committed
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
{
	int ret;
	unsigned int cmd_idx, cmd_arg;

	assert((ops != NULL) &&
	       (ops->write != NULL) &&
	       (size != 0U) &&
	       ((buf & MMC_BLOCK_MASK) == 0U) &&
	       ((size & MMC_BLOCK_MASK) == 0U));

	ret = ops->prepare(lba, buf, size);
	if (ret != 0) {
		return 0;
	}

	if (is_cmd23_enabled()) {
		/* Set block count */
		ret = mmc_send_cmd(MMC_CMD(23), size / MMC_BLOCK_SIZE,
616
				   MMC_RESPONSE_R1, NULL);
Yann Gautier's avatar
Yann Gautier committed
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
		if (ret != 0) {
			return 0;
		}

		cmd_idx = MMC_CMD(25);
	} else {
		if (size > MMC_BLOCK_SIZE) {
			cmd_idx = MMC_CMD(25);
		} else {
			cmd_idx = MMC_CMD(24);
		}
	}

	if ((mmc_ocr_value & OCR_ACCESS_MODE_MASK) == OCR_BYTE_MODE) {
		cmd_arg = lba * MMC_BLOCK_SIZE;
	} else {
		cmd_arg = lba;
	}

636
	ret = mmc_send_cmd(cmd_idx, cmd_arg, MMC_RESPONSE_R1, NULL);
Yann Gautier's avatar
Yann Gautier committed
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
	if (ret != 0) {
		return 0;
	}

	ret = ops->write(lba, buf, size);
	if (ret != 0) {
		return 0;
	}

	/* Wait buffer empty */
	do {
		ret = mmc_device_state();
		if (ret < 0) {
			return 0;
		}
	} while ((ret != MMC_STATE_TRAN) && (ret != MMC_STATE_RCV));

	if (!is_cmd23_enabled() && (size > MMC_BLOCK_SIZE)) {
655
		ret = mmc_send_cmd(MMC_CMD(12), 0, MMC_RESPONSE_R1B, NULL);
Yann Gautier's avatar
Yann Gautier committed
656
657
658
659
660
661
662
663
		if (ret != 0) {
			return 0;
		}
	}

	return size;
}

664
size_t mmc_erase_blocks(int lba, size_t size)
Yann Gautier's avatar
Yann Gautier committed
665
666
667
668
669
670
{
	int ret;

	assert(ops != NULL);
	assert((size != 0U) && ((size & MMC_BLOCK_MASK) == 0U));

671
	ret = mmc_send_cmd(MMC_CMD(35), lba, MMC_RESPONSE_R1, NULL);
Yann Gautier's avatar
Yann Gautier committed
672
673
674
675
676
	if (ret != 0) {
		return 0;
	}

	ret = mmc_send_cmd(MMC_CMD(36), lba + (size / MMC_BLOCK_SIZE) - 1U,
677
			   MMC_RESPONSE_R1, NULL);
Yann Gautier's avatar
Yann Gautier committed
678
679
680
681
	if (ret != 0) {
		return 0;
	}

682
	ret = mmc_send_cmd(MMC_CMD(38), lba, MMC_RESPONSE_R1B, NULL);
Yann Gautier's avatar
Yann Gautier committed
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
	if (ret != 0) {
		return 0;
	}

	do {
		ret = mmc_device_state();
		if (ret < 0) {
			return 0;
		}
	} while (ret != MMC_STATE_TRAN);

	return size;
}

static inline void mmc_rpmb_enable(void)
{
	mmc_set_ext_csd(CMD_EXTCSD_PARTITION_CONFIG,
			PART_CFG_BOOT_PARTITION1_ENABLE |
701
			PART_CFG_BOOT_PARTITION1_ACCESS);
Yann Gautier's avatar
Yann Gautier committed
702
703
704
705
706
707
708
709
}

static inline void mmc_rpmb_disable(void)
{
	mmc_set_ext_csd(CMD_EXTCSD_PARTITION_CONFIG,
			PART_CFG_BOOT_PARTITION1_ENABLE);
}

710
size_t mmc_rpmb_read_blocks(int lba, uintptr_t buf, size_t size)
Yann Gautier's avatar
Yann Gautier committed
711
712
713
714
715
716
717
718
719
720
{
	size_t size_read;

	mmc_rpmb_enable();
	size_read = mmc_read_blocks(lba, buf, size);
	mmc_rpmb_disable();

	return size_read;
}

721
size_t mmc_rpmb_write_blocks(int lba, const uintptr_t buf, size_t size)
Yann Gautier's avatar
Yann Gautier committed
722
723
724
725
726
727
728
729
730
731
{
	size_t size_written;

	mmc_rpmb_enable();
	size_written = mmc_write_blocks(lba, buf, size);
	mmc_rpmb_disable();

	return size_written;
}

732
size_t mmc_rpmb_erase_blocks(int lba, size_t size)
Yann Gautier's avatar
Yann Gautier committed
733
734
735
736
737
738
739
740
741
742
{
	size_t size_erased;

	mmc_rpmb_enable();
	size_erased = mmc_erase_blocks(lba, size);
	mmc_rpmb_disable();

	return size_erased;
}

743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
static int mmc_part_switch(unsigned int part_type)
{
	uint8_t part_config = mmc_ext_csd[CMD_EXTCSD_PARTITION_CONFIG];

	part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
	part_config |= part_type;

	return mmc_send_part_switch_cmd(part_config);
}

static unsigned char mmc_current_boot_part(void)
{
	return PART_CFG_CURRENT_BOOT_PARTITION(mmc_ext_csd[CMD_EXTCSD_PARTITION_CONFIG]);
}

size_t mmc_boot_part_read_blocks(int lba, uintptr_t buf, size_t size)
{
	size_t size_read;
	int ret;
	unsigned char current_boot_part = mmc_current_boot_part();

	if (current_boot_part != 1U &&
	    current_boot_part != 2U) {
		ERROR("Got unexpected value for active boot partition, %u\n", current_boot_part);
		return 0;
	}

	ret = mmc_part_switch(current_boot_part);
	if (ret < 0) {
		ERROR("Failed to switch to boot partition, %d\n", ret);
		return 0;
	}

	size_read = mmc_read_blocks(lba, buf, size);

	ret = mmc_part_switch(0);
	if (ret < 0) {
		ERROR("Failed to switch back to user partition, %d\n", ret);
		return 0;
	}

	return size_read;
}

Yann Gautier's avatar
Yann Gautier committed
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
int mmc_init(const struct mmc_ops *ops_ptr, unsigned int clk,
	     unsigned int width, unsigned int flags,
	     struct mmc_device_info *device_info)
{
	assert((ops_ptr != NULL) &&
	       (ops_ptr->init != NULL) &&
	       (ops_ptr->send_cmd != NULL) &&
	       (ops_ptr->set_ios != NULL) &&
	       (ops_ptr->prepare != NULL) &&
	       (ops_ptr->read != NULL) &&
	       (ops_ptr->write != NULL) &&
	       (device_info != NULL) &&
	       (clk != 0) &&
	       ((width == MMC_BUS_WIDTH_1) ||
		(width == MMC_BUS_WIDTH_4) ||
		(width == MMC_BUS_WIDTH_8) ||
		(width == MMC_BUS_WIDTH_DDR_4) ||
		(width == MMC_BUS_WIDTH_DDR_8)));

	ops = ops_ptr;
	mmc_flags = flags;
808
	mmc_dev_info = device_info;
Yann Gautier's avatar
Yann Gautier committed
809
810
811

	return mmc_enumerate(clk, width);
}