blob: 57f47482013abf243a7e2933baf63798620fbcdf [file] [log] [blame]
Yann Gautier1e5e85a2018-07-03 18:32:12 +02001/*
Ahmad Fatoumee8f3422022-05-23 17:06:37 +02002 * Copyright (c) 2018-2022, ARM Limited and Contributors. All rights reserved.
Yann Gautier1e5e85a2018-07-03 18:32:12 +02003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7/* Define a simple and generic interface to access eMMC and SD-card devices. */
8
Yann Gautier1e5e85a2018-07-03 18:32:12 +02009#include <assert.h>
Yann Gautier1e5e85a2018-07-03 18:32:12 +020010#include <errno.h>
Yann Gautier1e5e85a2018-07-03 18:32:12 +020011#include <stdbool.h>
12#include <string.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000013
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>
Jayanth Dodderi Chidanand345af842022-09-09 17:21:24 +010019#include <plat/common/common_def.h>
Yann Gautier1e5e85a2018-07-03 18:32:12 +020020
21#define MMC_DEFAULT_MAX_RETRIES 5
22#define SEND_OP_COND_MAX_RETRIES 100
23
24#define MULT_BY_512K_SHIFT 19
25
26static const struct mmc_ops *ops;
27static unsigned int mmc_ocr_value;
28static struct mmc_csd_emmc mmc_csd;
Yann Gautier0307b712019-06-12 15:55:37 +020029static struct sd_switch_status sd_switch_func_status;
Haojian Zhuangdf7271c2018-08-02 14:50:12 +080030static unsigned char mmc_ext_csd[512] __aligned(16);
Yann Gautier1e5e85a2018-07-03 18:32:12 +020031static unsigned int mmc_flags;
Yann Gautierd8b6b8e2021-03-22 14:02:54 +010032static struct mmc_device_info *mmc_dev_info;
Yann Gautier1e5e85a2018-07-03 18:32:12 +020033static unsigned int rca;
Tien Hock, Loh313e9002019-03-07 11:34:20 +080034static unsigned int scr[2]__aligned(16) = { 0 };
Yann Gautier1e5e85a2018-07-03 18:32:12 +020035
36static 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
40static 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
44static bool is_cmd23_enabled(void)
45{
46 return ((mmc_flags & MMC_FLAG_CMD23) != 0U);
47}
48
Yann Gautier0307b712019-06-12 15:55:37 +020049static bool is_sd_cmd6_enabled(void)
50{
51 return ((mmc_flags & MMC_FLAG_SD_CMD6) != 0U);
52}
53
Yann Gautier1e5e85a2018-07-03 18:32:12 +020054static 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++) {
Yann Gautier258904f2022-11-21 11:38:50 +010072 r_data[i] = cmd.resp_data[i];
Yann Gautier1e5e85a2018-07-03 18:32:12 +020073 }
74 }
75
76 if (ret != 0) {
77 VERBOSE("Send command %u error: %d\n", idx, ret);
78 }
79
80 return ret;
81}
82
83static int mmc_device_state(void)
84{
85 int retries = MMC_DEFAULT_MAX_RETRIES;
86 unsigned int resp_data[4];
87
88 do {
89 int ret;
90
91 if (retries == 0) {
92 ERROR("CMD13 failed after %d retries\n",
93 MMC_DEFAULT_MAX_RETRIES);
94 return -EIO;
95 }
96
97 ret = mmc_send_cmd(MMC_CMD(13), rca << RCA_SHIFT_OFFSET,
Yann Gautierf2e8b162018-09-28 16:48:37 +020098 MMC_RESPONSE_R1, &resp_data[0]);
Yann Gautier1e5e85a2018-07-03 18:32:12 +020099 if (ret != 0) {
Tien Hock, Loh313e9002019-03-07 11:34:20 +0800100 retries--;
101 continue;
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200102 }
103
104 if ((resp_data[0] & STATUS_SWITCH_ERROR) != 0U) {
105 return -EIO;
106 }
107
108 retries--;
109 } while ((resp_data[0] & STATUS_READY_FOR_DATA) == 0U);
110
111 return MMC_GET_STATE(resp_data[0]);
112}
113
Yann Gautierb7c4c022022-06-20 18:03:08 +0200114static int mmc_send_part_switch_cmd(unsigned char part_config)
Vyacheslav Yurkovb3d5f342021-03-30 08:16:20 +0200115{
116 int ret;
117 unsigned int part_time = 0;
118
119 ret = mmc_send_cmd(MMC_CMD(6),
120 EXTCSD_WRITE_BYTES |
121 EXTCSD_CMD(CMD_EXTCSD_PARTITION_CONFIG) |
122 EXTCSD_VALUE(part_config) |
123 EXTCSD_CMD_SET_NORMAL,
124 MMC_RESPONSE_R1B, NULL);
125 if (ret != 0) {
126 return ret;
127 }
128
129 /* Partition switch timing is in 10ms units */
130 part_time = mmc_ext_csd[CMD_EXTCSD_PART_SWITCH_TIME] * 10;
131
132 mdelay(part_time);
133
134 do {
135 ret = mmc_device_state();
136 if (ret < 0) {
137 return ret;
138 }
139 } while (ret == MMC_STATE_PRG);
140
141 return 0;
142}
143
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200144static int mmc_set_ext_csd(unsigned int ext_cmd, unsigned int value)
145{
146 int ret;
147
148 ret = mmc_send_cmd(MMC_CMD(6),
149 EXTCSD_WRITE_BYTES | EXTCSD_CMD(ext_cmd) |
150 EXTCSD_VALUE(value) | EXTCSD_CMD_SET_NORMAL,
Bryan O'Donoghuee6b56cc2018-08-15 16:25:30 +0100151 MMC_RESPONSE_R1B, NULL);
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200152 if (ret != 0) {
153 return ret;
154 }
155
156 do {
157 ret = mmc_device_state();
158 if (ret < 0) {
159 return ret;
160 }
161 } while (ret == MMC_STATE_PRG);
162
163 return 0;
164}
165
166static int mmc_sd_switch(unsigned int bus_width)
167{
168 int ret;
169 int retries = MMC_DEFAULT_MAX_RETRIES;
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200170 unsigned int bus_width_arg = 0;
171
172 ret = ops->prepare(0, (uintptr_t)&scr, sizeof(scr));
173 if (ret != 0) {
174 return ret;
175 }
176
177 /* CMD55: Application Specific Command */
178 ret = mmc_send_cmd(MMC_CMD(55), rca << RCA_SHIFT_OFFSET,
Yann Gautierf2e8b162018-09-28 16:48:37 +0200179 MMC_RESPONSE_R5, NULL);
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200180 if (ret != 0) {
181 return ret;
182 }
183
184 /* ACMD51: SEND_SCR */
185 do {
Yann Gautierf2e8b162018-09-28 16:48:37 +0200186 ret = mmc_send_cmd(MMC_ACMD(51), 0, MMC_RESPONSE_R1, NULL);
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200187 if ((ret != 0) && (retries == 0)) {
188 ERROR("ACMD51 failed after %d retries (ret=%d)\n",
189 MMC_DEFAULT_MAX_RETRIES, ret);
190 return ret;
191 }
192
193 retries--;
194 } while (ret != 0);
195
196 ret = ops->read(0, (uintptr_t)&scr, sizeof(scr));
197 if (ret != 0) {
198 return ret;
199 }
200
201 if (((scr[0] & SD_SCR_BUS_WIDTH_4) != 0U) &&
202 (bus_width == MMC_BUS_WIDTH_4)) {
203 bus_width_arg = 2;
204 }
205
206 /* CMD55: Application Specific Command */
207 ret = mmc_send_cmd(MMC_CMD(55), rca << RCA_SHIFT_OFFSET,
Yann Gautierf2e8b162018-09-28 16:48:37 +0200208 MMC_RESPONSE_R5, NULL);
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200209 if (ret != 0) {
210 return ret;
211 }
212
213 /* ACMD6: SET_BUS_WIDTH */
Yann Gautierf2e8b162018-09-28 16:48:37 +0200214 ret = mmc_send_cmd(MMC_ACMD(6), bus_width_arg, MMC_RESPONSE_R1, NULL);
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200215 if (ret != 0) {
216 return ret;
217 }
218
219 do {
220 ret = mmc_device_state();
221 if (ret < 0) {
222 return ret;
223 }
224 } while (ret == MMC_STATE_PRG);
225
226 return 0;
227}
228
229static int mmc_set_ios(unsigned int clk, unsigned int bus_width)
230{
231 int ret;
232 unsigned int width = bus_width;
233
Yann Gautierd8b6b8e2021-03-22 14:02:54 +0100234 if (mmc_dev_info->mmc_dev_type != MMC_IS_EMMC) {
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200235 if (width == MMC_BUS_WIDTH_8) {
236 WARN("Wrong bus config for SD-card, force to 4\n");
237 width = MMC_BUS_WIDTH_4;
238 }
239 ret = mmc_sd_switch(width);
240 if (ret != 0) {
241 return ret;
242 }
243 } else if (mmc_csd.spec_vers == 4U) {
244 ret = mmc_set_ext_csd(CMD_EXTCSD_BUS_WIDTH,
245 (unsigned int)width);
246 if (ret != 0) {
247 return ret;
248 }
249 } else {
250 VERBOSE("Wrong MMC type or spec version\n");
251 }
252
253 return ops->set_ios(clk, width);
254}
255
256static int mmc_fill_device_info(void)
257{
258 unsigned long long c_size;
259 unsigned int speed_idx;
260 unsigned int nb_blocks;
261 unsigned int freq_unit;
Antonio Nino Diaz5d95e0a2018-08-10 13:04:02 +0100262 int ret = 0;
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200263 struct mmc_csd_sd_v2 *csd_sd_v2;
264
Yann Gautierd8b6b8e2021-03-22 14:02:54 +0100265 switch (mmc_dev_info->mmc_dev_type) {
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200266 case MMC_IS_EMMC:
Yann Gautierd8b6b8e2021-03-22 14:02:54 +0100267 mmc_dev_info->block_size = MMC_BLOCK_SIZE;
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200268
269 ret = ops->prepare(0, (uintptr_t)&mmc_ext_csd,
270 sizeof(mmc_ext_csd));
271 if (ret != 0) {
272 return ret;
273 }
274
275 /* MMC CMD8: SEND_EXT_CSD */
Yann Gautierf2e8b162018-09-28 16:48:37 +0200276 ret = mmc_send_cmd(MMC_CMD(8), 0, MMC_RESPONSE_R1, NULL);
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200277 if (ret != 0) {
278 return ret;
279 }
280
281 ret = ops->read(0, (uintptr_t)&mmc_ext_csd,
282 sizeof(mmc_ext_csd));
283 if (ret != 0) {
284 return ret;
285 }
286
Haojian Zhuang05274cb2018-11-21 09:19:49 +0800287 do {
288 ret = mmc_device_state();
289 if (ret < 0) {
290 return ret;
291 }
292 } while (ret != MMC_STATE_TRAN);
293
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200294 nb_blocks = (mmc_ext_csd[CMD_EXTCSD_SEC_CNT] << 0) |
295 (mmc_ext_csd[CMD_EXTCSD_SEC_CNT + 1] << 8) |
296 (mmc_ext_csd[CMD_EXTCSD_SEC_CNT + 2] << 16) |
297 (mmc_ext_csd[CMD_EXTCSD_SEC_CNT + 3] << 24);
298
Yann Gautierd8b6b8e2021-03-22 14:02:54 +0100299 mmc_dev_info->device_size = (unsigned long long)nb_blocks *
300 mmc_dev_info->block_size;
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200301
302 break;
303
304 case MMC_IS_SD:
305 /*
306 * Use the same mmc_csd struct, as required fields here
307 * (READ_BL_LEN, C_SIZE, CSIZE_MULT) are common with eMMC.
308 */
Yann Gautierd8b6b8e2021-03-22 14:02:54 +0100309 mmc_dev_info->block_size = BIT_32(mmc_csd.read_bl_len);
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200310
311 c_size = ((unsigned long long)mmc_csd.c_size_high << 2U) |
312 (unsigned long long)mmc_csd.c_size_low;
313 assert(c_size != 0xFFFU);
314
Yann Gautierd8b6b8e2021-03-22 14:02:54 +0100315 mmc_dev_info->device_size = (c_size + 1U) *
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200316 BIT_64(mmc_csd.c_size_mult + 2U) *
Yann Gautierd8b6b8e2021-03-22 14:02:54 +0100317 mmc_dev_info->block_size;
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200318
319 break;
320
321 case MMC_IS_SD_HC:
322 assert(mmc_csd.csd_structure == 1U);
323
Yann Gautierd8b6b8e2021-03-22 14:02:54 +0100324 mmc_dev_info->block_size = MMC_BLOCK_SIZE;
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200325
326 /* Need to use mmc_csd_sd_v2 struct */
327 csd_sd_v2 = (struct mmc_csd_sd_v2 *)&mmc_csd;
328 c_size = ((unsigned long long)csd_sd_v2->c_size_high << 16) |
329 (unsigned long long)csd_sd_v2->c_size_low;
330
Yann Gautierd8b6b8e2021-03-22 14:02:54 +0100331 mmc_dev_info->device_size = (c_size + 1U) << MULT_BY_512K_SHIFT;
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200332
333 break;
334
335 default:
336 ret = -EINVAL;
337 break;
338 }
339
Yann Gautiere8eb33f2019-01-17 11:17:05 +0100340 if (ret < 0) {
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200341 return ret;
342 }
343
344 speed_idx = (mmc_csd.tran_speed & CSD_TRAN_SPEED_MULT_MASK) >>
345 CSD_TRAN_SPEED_MULT_SHIFT;
346
347 assert(speed_idx > 0U);
348
Yann Gautierd8b6b8e2021-03-22 14:02:54 +0100349 if (mmc_dev_info->mmc_dev_type == MMC_IS_EMMC) {
350 mmc_dev_info->max_bus_freq = tran_speed_base[speed_idx];
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200351 } else {
Yann Gautierd8b6b8e2021-03-22 14:02:54 +0100352 mmc_dev_info->max_bus_freq = sd_tran_speed_base[speed_idx];
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200353 }
354
355 freq_unit = mmc_csd.tran_speed & CSD_TRAN_SPEED_UNIT_MASK;
356 while (freq_unit != 0U) {
Yann Gautierd8b6b8e2021-03-22 14:02:54 +0100357 mmc_dev_info->max_bus_freq *= 10U;
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200358 --freq_unit;
359 }
360
Yann Gautierd8b6b8e2021-03-22 14:02:54 +0100361 mmc_dev_info->max_bus_freq *= 10000U;
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200362
363 return 0;
364}
365
Yann Gautier0307b712019-06-12 15:55:37 +0200366static int sd_switch(unsigned int mode, unsigned char group,
367 unsigned char func)
368{
369 unsigned int group_shift = (group - 1U) * 4U;
370 unsigned int group_mask = GENMASK(group_shift + 3U, group_shift);
371 unsigned int arg;
372 int ret;
373
374 ret = ops->prepare(0, (uintptr_t)&sd_switch_func_status,
375 sizeof(sd_switch_func_status));
376 if (ret != 0) {
377 return ret;
378 }
379
380 /* MMC CMD6: SWITCH_FUNC */
381 arg = mode | SD_SWITCH_ALL_GROUPS_MASK;
382 arg &= ~group_mask;
383 arg |= func << group_shift;
384 ret = mmc_send_cmd(MMC_CMD(6), arg, MMC_RESPONSE_R1, NULL);
385 if (ret != 0) {
386 return ret;
387 }
388
389 return ops->read(0, (uintptr_t)&sd_switch_func_status,
390 sizeof(sd_switch_func_status));
391}
392
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200393static int sd_send_op_cond(void)
394{
Yann Gautierde6959e2018-08-02 13:46:17 +0200395 int n;
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200396 unsigned int resp_data[4];
397
Yann Gautierde6959e2018-08-02 13:46:17 +0200398 for (n = 0; n < SEND_OP_COND_MAX_RETRIES; n++) {
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200399 int ret;
400
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200401 /* CMD55: Application Specific Command */
Yann Gautierf2e8b162018-09-28 16:48:37 +0200402 ret = mmc_send_cmd(MMC_CMD(55), 0, MMC_RESPONSE_R1, NULL);
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200403 if (ret != 0) {
404 return ret;
405 }
406
407 /* ACMD41: SD_SEND_OP_COND */
Tien Hock, Loh313e9002019-03-07 11:34:20 +0800408 ret = mmc_send_cmd(MMC_ACMD(41), OCR_HCS |
Yann Gautierd8b6b8e2021-03-22 14:02:54 +0100409 mmc_dev_info->ocr_voltage, MMC_RESPONSE_R3,
Tien Hock, Loh313e9002019-03-07 11:34:20 +0800410 &resp_data[0]);
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200411 if (ret != 0) {
412 return ret;
413 }
414
Yann Gautierde6959e2018-08-02 13:46:17 +0200415 if ((resp_data[0] & OCR_POWERUP) != 0U) {
416 mmc_ocr_value = resp_data[0];
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200417
Yann Gautierde6959e2018-08-02 13:46:17 +0200418 if ((mmc_ocr_value & OCR_HCS) != 0U) {
Yann Gautierd8b6b8e2021-03-22 14:02:54 +0100419 mmc_dev_info->mmc_dev_type = MMC_IS_SD_HC;
Yann Gautierde6959e2018-08-02 13:46:17 +0200420 } else {
Yann Gautierd8b6b8e2021-03-22 14:02:54 +0100421 mmc_dev_info->mmc_dev_type = MMC_IS_SD;
Yann Gautierde6959e2018-08-02 13:46:17 +0200422 }
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200423
Yann Gautierde6959e2018-08-02 13:46:17 +0200424 return 0;
425 }
426
Yann Gautier38d80352019-08-16 16:49:41 +0200427 mdelay(10);
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200428 }
429
Yann Gautierde6959e2018-08-02 13:46:17 +0200430 ERROR("ACMD41 failed after %d retries\n", SEND_OP_COND_MAX_RETRIES);
431
432 return -EIO;
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200433}
434
Yann Gautierde6959e2018-08-02 13:46:17 +0200435static int mmc_reset_to_idle(void)
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200436{
437 int ret;
Yann Gautierde6959e2018-08-02 13:46:17 +0200438
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200439 /* CMD0: reset to IDLE */
440 ret = mmc_send_cmd(MMC_CMD(0), 0, 0, NULL);
441 if (ret != 0) {
442 return ret;
443 }
444
Yann Gautierde6959e2018-08-02 13:46:17 +0200445 mdelay(2);
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200446
Yann Gautierde6959e2018-08-02 13:46:17 +0200447 return 0;
448}
449
450static int mmc_send_op_cond(void)
451{
452 int ret, n;
453 unsigned int resp_data[4];
454
Yann Gautierde6959e2018-08-02 13:46:17 +0200455 for (n = 0; n < SEND_OP_COND_MAX_RETRIES; n++) {
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200456 ret = mmc_send_cmd(MMC_CMD(1), OCR_SECTOR_MODE |
457 OCR_VDD_MIN_2V7 | OCR_VDD_MIN_1V7,
Bryan O'Donoghuee62f4192018-08-15 15:59:07 +0100458 MMC_RESPONSE_R3, &resp_data[0]);
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200459 if (ret != 0) {
460 return ret;
461 }
462
Yann Gautierde6959e2018-08-02 13:46:17 +0200463 if ((resp_data[0] & OCR_POWERUP) != 0U) {
464 mmc_ocr_value = resp_data[0];
465 return 0;
466 }
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200467
Joakim Bech8428a042018-12-18 10:09:02 +0100468 mdelay(10);
Yann Gautierde6959e2018-08-02 13:46:17 +0200469 }
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200470
Yann Gautierde6959e2018-08-02 13:46:17 +0200471 ERROR("CMD1 failed after %d retries\n", SEND_OP_COND_MAX_RETRIES);
472
473 return -EIO;
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200474}
475
476static int mmc_enumerate(unsigned int clk, unsigned int bus_width)
477{
478 int ret;
479 unsigned int resp_data[4];
480
481 ops->init();
482
Yann Gautier4c7888a2018-11-29 15:43:37 +0100483 ret = mmc_reset_to_idle();
484 if (ret != 0) {
485 return ret;
Yann Gautiere77be522021-03-23 13:30:43 +0100486 }
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200487
Yann Gautierd8b6b8e2021-03-22 14:02:54 +0100488 if (mmc_dev_info->mmc_dev_type == MMC_IS_EMMC) {
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200489 ret = mmc_send_op_cond();
Haojian Zhuanged59cd52018-08-02 14:48:17 +0800490 } else {
491 /* CMD8: Send Interface Condition Command */
492 ret = mmc_send_cmd(MMC_CMD(8), VHS_2_7_3_6_V | CMD8_CHECK_PATTERN,
Yann Gautierf2e8b162018-09-28 16:48:37 +0200493 MMC_RESPONSE_R5, &resp_data[0]);
Haojian Zhuanged59cd52018-08-02 14:48:17 +0800494
495 if ((ret == 0) && ((resp_data[0] & 0xffU) == CMD8_CHECK_PATTERN)) {
496 ret = sd_send_op_cond();
497 }
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200498 }
499 if (ret != 0) {
500 return ret;
501 }
502
503 /* CMD2: Card Identification */
Shawn Guoab3b62b2018-09-28 14:21:01 +0800504 ret = mmc_send_cmd(MMC_CMD(2), 0, MMC_RESPONSE_R2, NULL);
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200505 if (ret != 0) {
506 return ret;
507 }
508
509 /* CMD3: Set Relative Address */
Yann Gautierd8b6b8e2021-03-22 14:02:54 +0100510 if (mmc_dev_info->mmc_dev_type == MMC_IS_EMMC) {
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200511 rca = MMC_FIX_RCA;
512 ret = mmc_send_cmd(MMC_CMD(3), rca << RCA_SHIFT_OFFSET,
Yann Gautierf2e8b162018-09-28 16:48:37 +0200513 MMC_RESPONSE_R1, NULL);
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200514 if (ret != 0) {
515 return ret;
516 }
517 } else {
518 ret = mmc_send_cmd(MMC_CMD(3), 0,
Yann Gautierf2e8b162018-09-28 16:48:37 +0200519 MMC_RESPONSE_R6, &resp_data[0]);
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200520 if (ret != 0) {
521 return ret;
522 }
523
524 rca = (resp_data[0] & 0xFFFF0000U) >> 16;
525 }
526
527 /* CMD9: CSD Register */
528 ret = mmc_send_cmd(MMC_CMD(9), rca << RCA_SHIFT_OFFSET,
Shawn Guoab3b62b2018-09-28 14:21:01 +0800529 MMC_RESPONSE_R2, &resp_data[0]);
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200530 if (ret != 0) {
531 return ret;
532 }
533
534 memcpy(&mmc_csd, &resp_data, sizeof(resp_data));
535
536 /* CMD7: Select Card */
537 ret = mmc_send_cmd(MMC_CMD(7), rca << RCA_SHIFT_OFFSET,
Yann Gautierf2e8b162018-09-28 16:48:37 +0200538 MMC_RESPONSE_R1, NULL);
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200539 if (ret != 0) {
540 return ret;
541 }
542
543 do {
544 ret = mmc_device_state();
545 if (ret < 0) {
546 return ret;
547 }
548 } while (ret != MMC_STATE_TRAN);
549
Haojian Zhuangd618b272018-08-04 18:04:30 +0800550 ret = mmc_set_ios(clk, bus_width);
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200551 if (ret != 0) {
552 return ret;
553 }
554
Yann Gautier0307b712019-06-12 15:55:37 +0200555 ret = mmc_fill_device_info();
556 if (ret != 0) {
557 return ret;
558 }
559
560 if (is_sd_cmd6_enabled() &&
561 (mmc_dev_info->mmc_dev_type == MMC_IS_SD_HC)) {
562 /* Try to switch to High Speed Mode */
563 ret = sd_switch(SD_SWITCH_FUNC_CHECK, 1U, 1U);
564 if (ret != 0) {
565 return ret;
566 }
567
568 if ((sd_switch_func_status.support_g1 & BIT(9)) == 0U) {
569 /* High speed not supported, keep default speed */
570 return 0;
571 }
572
573 ret = sd_switch(SD_SWITCH_FUNC_SWITCH, 1U, 1U);
574 if (ret != 0) {
575 return ret;
576 }
577
578 if ((sd_switch_func_status.sel_g2_g1 & 0x1U) == 0U) {
579 /* Cannot switch to high speed, keep default speed */
580 return 0;
581 }
582
583 mmc_dev_info->max_bus_freq = 50000000U;
584 ret = ops->set_ios(clk, bus_width);
585 }
586
587 return ret;
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200588}
589
Haojian Zhuangd87f0b72018-08-02 14:49:51 +0800590size_t mmc_read_blocks(int lba, uintptr_t buf, size_t size)
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200591{
592 int ret;
593 unsigned int cmd_idx, cmd_arg;
594
595 assert((ops != NULL) &&
596 (ops->read != NULL) &&
597 (size != 0U) &&
598 ((size & MMC_BLOCK_MASK) == 0U));
599
600 ret = ops->prepare(lba, buf, size);
601 if (ret != 0) {
602 return 0;
603 }
604
605 if (is_cmd23_enabled()) {
606 /* Set block count */
607 ret = mmc_send_cmd(MMC_CMD(23), size / MMC_BLOCK_SIZE,
Yann Gautierf2e8b162018-09-28 16:48:37 +0200608 MMC_RESPONSE_R1, NULL);
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200609 if (ret != 0) {
610 return 0;
611 }
612
613 cmd_idx = MMC_CMD(18);
614 } else {
615 if (size > MMC_BLOCK_SIZE) {
616 cmd_idx = MMC_CMD(18);
617 } else {
618 cmd_idx = MMC_CMD(17);
619 }
620 }
621
622 if (((mmc_ocr_value & OCR_ACCESS_MODE_MASK) == OCR_BYTE_MODE) &&
Yann Gautierd8b6b8e2021-03-22 14:02:54 +0100623 (mmc_dev_info->mmc_dev_type != MMC_IS_SD_HC)) {
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200624 cmd_arg = lba * MMC_BLOCK_SIZE;
625 } else {
626 cmd_arg = lba;
627 }
628
Yann Gautierf2e8b162018-09-28 16:48:37 +0200629 ret = mmc_send_cmd(cmd_idx, cmd_arg, MMC_RESPONSE_R1, NULL);
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200630 if (ret != 0) {
631 return 0;
632 }
633
634 ret = ops->read(lba, buf, size);
635 if (ret != 0) {
636 return 0;
637 }
638
639 /* Wait buffer empty */
640 do {
641 ret = mmc_device_state();
642 if (ret < 0) {
643 return 0;
644 }
645 } while ((ret != MMC_STATE_TRAN) && (ret != MMC_STATE_DATA));
646
647 if (!is_cmd23_enabled() && (size > MMC_BLOCK_SIZE)) {
Bryan O'Donoghuee6b56cc2018-08-15 16:25:30 +0100648 ret = mmc_send_cmd(MMC_CMD(12), 0, MMC_RESPONSE_R1B, NULL);
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200649 if (ret != 0) {
650 return 0;
651 }
652 }
653
654 return size;
655}
656
Haojian Zhuangd87f0b72018-08-02 14:49:51 +0800657size_t mmc_write_blocks(int lba, const uintptr_t buf, size_t size)
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200658{
659 int ret;
660 unsigned int cmd_idx, cmd_arg;
661
662 assert((ops != NULL) &&
663 (ops->write != NULL) &&
664 (size != 0U) &&
665 ((buf & MMC_BLOCK_MASK) == 0U) &&
666 ((size & MMC_BLOCK_MASK) == 0U));
667
668 ret = ops->prepare(lba, buf, size);
669 if (ret != 0) {
670 return 0;
671 }
672
673 if (is_cmd23_enabled()) {
674 /* Set block count */
675 ret = mmc_send_cmd(MMC_CMD(23), size / MMC_BLOCK_SIZE,
Yann Gautierf2e8b162018-09-28 16:48:37 +0200676 MMC_RESPONSE_R1, NULL);
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200677 if (ret != 0) {
678 return 0;
679 }
680
681 cmd_idx = MMC_CMD(25);
682 } else {
683 if (size > MMC_BLOCK_SIZE) {
684 cmd_idx = MMC_CMD(25);
685 } else {
686 cmd_idx = MMC_CMD(24);
687 }
688 }
689
690 if ((mmc_ocr_value & OCR_ACCESS_MODE_MASK) == OCR_BYTE_MODE) {
691 cmd_arg = lba * MMC_BLOCK_SIZE;
692 } else {
693 cmd_arg = lba;
694 }
695
Yann Gautierf2e8b162018-09-28 16:48:37 +0200696 ret = mmc_send_cmd(cmd_idx, cmd_arg, MMC_RESPONSE_R1, NULL);
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200697 if (ret != 0) {
698 return 0;
699 }
700
701 ret = ops->write(lba, buf, size);
702 if (ret != 0) {
703 return 0;
704 }
705
706 /* Wait buffer empty */
707 do {
708 ret = mmc_device_state();
709 if (ret < 0) {
710 return 0;
711 }
712 } while ((ret != MMC_STATE_TRAN) && (ret != MMC_STATE_RCV));
713
714 if (!is_cmd23_enabled() && (size > MMC_BLOCK_SIZE)) {
Bryan O'Donoghuee6b56cc2018-08-15 16:25:30 +0100715 ret = mmc_send_cmd(MMC_CMD(12), 0, MMC_RESPONSE_R1B, NULL);
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200716 if (ret != 0) {
717 return 0;
718 }
719 }
720
721 return size;
722}
723
Haojian Zhuangd87f0b72018-08-02 14:49:51 +0800724size_t mmc_erase_blocks(int lba, size_t size)
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200725{
726 int ret;
727
728 assert(ops != NULL);
729 assert((size != 0U) && ((size & MMC_BLOCK_MASK) == 0U));
730
Yann Gautierf2e8b162018-09-28 16:48:37 +0200731 ret = mmc_send_cmd(MMC_CMD(35), lba, MMC_RESPONSE_R1, NULL);
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200732 if (ret != 0) {
733 return 0;
734 }
735
736 ret = mmc_send_cmd(MMC_CMD(36), lba + (size / MMC_BLOCK_SIZE) - 1U,
Yann Gautierf2e8b162018-09-28 16:48:37 +0200737 MMC_RESPONSE_R1, NULL);
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200738 if (ret != 0) {
739 return 0;
740 }
741
Yann Gautierf2e8b162018-09-28 16:48:37 +0200742 ret = mmc_send_cmd(MMC_CMD(38), lba, MMC_RESPONSE_R1B, NULL);
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200743 if (ret != 0) {
744 return 0;
745 }
746
747 do {
748 ret = mmc_device_state();
749 if (ret < 0) {
750 return 0;
751 }
752 } while (ret != MMC_STATE_TRAN);
753
754 return size;
755}
756
Yann Gautierb7c4c022022-06-20 18:03:08 +0200757static int mmc_part_switch(unsigned char part_type)
Vyacheslav Yurkovb3d5f342021-03-30 08:16:20 +0200758{
Yann Gautierb7c4c022022-06-20 18:03:08 +0200759 unsigned char part_config = mmc_ext_csd[CMD_EXTCSD_PARTITION_CONFIG];
Vyacheslav Yurkovb3d5f342021-03-30 08:16:20 +0200760
761 part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
762 part_config |= part_type;
763
764 return mmc_send_part_switch_cmd(part_config);
765}
766
767static unsigned char mmc_current_boot_part(void)
768{
769 return PART_CFG_CURRENT_BOOT_PARTITION(mmc_ext_csd[CMD_EXTCSD_PARTITION_CONFIG]);
770}
771
Ahmad Fatoumee8f3422022-05-23 17:06:37 +0200772int mmc_part_switch_current_boot(void)
Vyacheslav Yurkovb3d5f342021-03-30 08:16:20 +0200773{
Vyacheslav Yurkovb3d5f342021-03-30 08:16:20 +0200774 unsigned char current_boot_part = mmc_current_boot_part();
Ahmad Fatoumee8f3422022-05-23 17:06:37 +0200775 int ret;
Vyacheslav Yurkovb3d5f342021-03-30 08:16:20 +0200776
Yann Gautier7693fdf2022-11-18 15:02:35 +0100777 if ((current_boot_part != 1U) && (current_boot_part != 2U)) {
Vyacheslav Yurkovb3d5f342021-03-30 08:16:20 +0200778 ERROR("Got unexpected value for active boot partition, %u\n", current_boot_part);
Ahmad Fatoumee8f3422022-05-23 17:06:37 +0200779 return -EIO;
Vyacheslav Yurkovb3d5f342021-03-30 08:16:20 +0200780 }
781
782 ret = mmc_part_switch(current_boot_part);
783 if (ret < 0) {
784 ERROR("Failed to switch to boot partition, %d\n", ret);
Ahmad Fatoumee8f3422022-05-23 17:06:37 +0200785 }
786
787 return ret;
788}
789
790int mmc_part_switch_user(void)
791{
792 int ret;
793
Ahmad Fatoumb61eb752022-05-31 10:03:04 +0200794 ret = mmc_part_switch(PART_CFG_BOOT_PARTITION_NO_ACCESS);
Ahmad Fatoumee8f3422022-05-23 17:06:37 +0200795 if (ret < 0) {
796 ERROR("Failed to switch to user partition, %d\n", ret);
797 }
798
799 return ret;
800}
801
Yann Gautier053d7f22022-09-01 19:23:39 +0200802size_t mmc_boot_part_size(void)
803{
804 return mmc_ext_csd[CMD_EXTCSD_BOOT_SIZE_MULT] * SZ_128K;
805}
806
Ahmad Fatoumee8f3422022-05-23 17:06:37 +0200807size_t mmc_boot_part_read_blocks(int lba, uintptr_t buf, size_t size)
808{
809 size_t size_read;
810 int ret;
811
812 ret = mmc_part_switch_current_boot();
813 if (ret < 0) {
Vyacheslav Yurkovb3d5f342021-03-30 08:16:20 +0200814 return 0;
815 }
816
817 size_read = mmc_read_blocks(lba, buf, size);
818
Ahmad Fatoumee8f3422022-05-23 17:06:37 +0200819 ret = mmc_part_switch_user();
Vyacheslav Yurkovb3d5f342021-03-30 08:16:20 +0200820 if (ret < 0) {
Vyacheslav Yurkovb3d5f342021-03-30 08:16:20 +0200821 return 0;
822 }
823
824 return size_read;
825}
826
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200827int mmc_init(const struct mmc_ops *ops_ptr, unsigned int clk,
828 unsigned int width, unsigned int flags,
829 struct mmc_device_info *device_info)
830{
831 assert((ops_ptr != NULL) &&
832 (ops_ptr->init != NULL) &&
833 (ops_ptr->send_cmd != NULL) &&
834 (ops_ptr->set_ios != NULL) &&
835 (ops_ptr->prepare != NULL) &&
836 (ops_ptr->read != NULL) &&
837 (ops_ptr->write != NULL) &&
838 (device_info != NULL) &&
839 (clk != 0) &&
840 ((width == MMC_BUS_WIDTH_1) ||
841 (width == MMC_BUS_WIDTH_4) ||
842 (width == MMC_BUS_WIDTH_8) ||
843 (width == MMC_BUS_WIDTH_DDR_4) ||
844 (width == MMC_BUS_WIDTH_DDR_8)));
845
846 ops = ops_ptr;
847 mmc_flags = flags;
Yann Gautierd8b6b8e2021-03-22 14:02:54 +0100848 mmc_dev_info = device_info;
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200849
850 return mmc_enumerate(clk, width);
851}