blob: 8e83464d5e1aa35da1d7691a8a72eb3d822eb983 [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++) {
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
84static 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,
Yann Gautierf2e8b162018-09-28 16:48:37 +020099 MMC_RESPONSE_R1, &resp_data[0]);
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200100 if (ret != 0) {
Tien Hock, Loh313e9002019-03-07 11:34:20 +0800101 retries--;
102 continue;
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200103 }
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
Vyacheslav Yurkovb3d5f342021-03-30 08:16:20 +0200115static 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
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200145static 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,
Bryan O'Donoghuee6b56cc2018-08-15 16:25:30 +0100152 MMC_RESPONSE_R1B, NULL);
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200153 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
167static int mmc_sd_switch(unsigned int bus_width)
168{
169 int ret;
170 int retries = MMC_DEFAULT_MAX_RETRIES;
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200171 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,
Yann Gautierf2e8b162018-09-28 16:48:37 +0200180 MMC_RESPONSE_R5, NULL);
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200181 if (ret != 0) {
182 return ret;
183 }
184
185 /* ACMD51: SEND_SCR */
186 do {
Yann Gautierf2e8b162018-09-28 16:48:37 +0200187 ret = mmc_send_cmd(MMC_ACMD(51), 0, MMC_RESPONSE_R1, NULL);
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200188 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,
Yann Gautierf2e8b162018-09-28 16:48:37 +0200209 MMC_RESPONSE_R5, NULL);
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200210 if (ret != 0) {
211 return ret;
212 }
213
214 /* ACMD6: SET_BUS_WIDTH */
Yann Gautierf2e8b162018-09-28 16:48:37 +0200215 ret = mmc_send_cmd(MMC_ACMD(6), bus_width_arg, MMC_RESPONSE_R1, NULL);
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200216 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
230static int mmc_set_ios(unsigned int clk, unsigned int bus_width)
231{
232 int ret;
233 unsigned int width = bus_width;
234
Yann Gautierd8b6b8e2021-03-22 14:02:54 +0100235 if (mmc_dev_info->mmc_dev_type != MMC_IS_EMMC) {
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200236 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
257static 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;
Antonio Nino Diaz5d95e0a2018-08-10 13:04:02 +0100263 int ret = 0;
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200264 struct mmc_csd_sd_v2 *csd_sd_v2;
265
Yann Gautierd8b6b8e2021-03-22 14:02:54 +0100266 switch (mmc_dev_info->mmc_dev_type) {
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200267 case MMC_IS_EMMC:
Yann Gautierd8b6b8e2021-03-22 14:02:54 +0100268 mmc_dev_info->block_size = MMC_BLOCK_SIZE;
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200269
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 */
Yann Gautierf2e8b162018-09-28 16:48:37 +0200277 ret = mmc_send_cmd(MMC_CMD(8), 0, MMC_RESPONSE_R1, NULL);
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200278 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
Haojian Zhuang05274cb2018-11-21 09:19:49 +0800288 do {
289 ret = mmc_device_state();
290 if (ret < 0) {
291 return ret;
292 }
293 } while (ret != MMC_STATE_TRAN);
294
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200295 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
Yann Gautierd8b6b8e2021-03-22 14:02:54 +0100300 mmc_dev_info->device_size = (unsigned long long)nb_blocks *
301 mmc_dev_info->block_size;
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200302
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 */
Yann Gautierd8b6b8e2021-03-22 14:02:54 +0100310 mmc_dev_info->block_size = BIT_32(mmc_csd.read_bl_len);
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200311
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
Yann Gautierd8b6b8e2021-03-22 14:02:54 +0100316 mmc_dev_info->device_size = (c_size + 1U) *
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200317 BIT_64(mmc_csd.c_size_mult + 2U) *
Yann Gautierd8b6b8e2021-03-22 14:02:54 +0100318 mmc_dev_info->block_size;
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200319
320 break;
321
322 case MMC_IS_SD_HC:
323 assert(mmc_csd.csd_structure == 1U);
324
Yann Gautierd8b6b8e2021-03-22 14:02:54 +0100325 mmc_dev_info->block_size = MMC_BLOCK_SIZE;
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200326
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
Yann Gautierd8b6b8e2021-03-22 14:02:54 +0100332 mmc_dev_info->device_size = (c_size + 1U) << MULT_BY_512K_SHIFT;
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200333
334 break;
335
336 default:
337 ret = -EINVAL;
338 break;
339 }
340
Yann Gautiere8eb33f2019-01-17 11:17:05 +0100341 if (ret < 0) {
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200342 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
Yann Gautierd8b6b8e2021-03-22 14:02:54 +0100350 if (mmc_dev_info->mmc_dev_type == MMC_IS_EMMC) {
351 mmc_dev_info->max_bus_freq = tran_speed_base[speed_idx];
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200352 } else {
Yann Gautierd8b6b8e2021-03-22 14:02:54 +0100353 mmc_dev_info->max_bus_freq = sd_tran_speed_base[speed_idx];
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200354 }
355
356 freq_unit = mmc_csd.tran_speed & CSD_TRAN_SPEED_UNIT_MASK;
357 while (freq_unit != 0U) {
Yann Gautierd8b6b8e2021-03-22 14:02:54 +0100358 mmc_dev_info->max_bus_freq *= 10U;
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200359 --freq_unit;
360 }
361
Yann Gautierd8b6b8e2021-03-22 14:02:54 +0100362 mmc_dev_info->max_bus_freq *= 10000U;
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200363
364 return 0;
365}
366
Yann Gautier0307b712019-06-12 15:55:37 +0200367static 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
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200394static int sd_send_op_cond(void)
395{
Yann Gautierde6959e2018-08-02 13:46:17 +0200396 int n;
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200397 unsigned int resp_data[4];
398
Yann Gautierde6959e2018-08-02 13:46:17 +0200399 for (n = 0; n < SEND_OP_COND_MAX_RETRIES; n++) {
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200400 int ret;
401
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200402 /* CMD55: Application Specific Command */
Yann Gautierf2e8b162018-09-28 16:48:37 +0200403 ret = mmc_send_cmd(MMC_CMD(55), 0, MMC_RESPONSE_R1, NULL);
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200404 if (ret != 0) {
405 return ret;
406 }
407
408 /* ACMD41: SD_SEND_OP_COND */
Tien Hock, Loh313e9002019-03-07 11:34:20 +0800409 ret = mmc_send_cmd(MMC_ACMD(41), OCR_HCS |
Yann Gautierd8b6b8e2021-03-22 14:02:54 +0100410 mmc_dev_info->ocr_voltage, MMC_RESPONSE_R3,
Tien Hock, Loh313e9002019-03-07 11:34:20 +0800411 &resp_data[0]);
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200412 if (ret != 0) {
413 return ret;
414 }
415
Yann Gautierde6959e2018-08-02 13:46:17 +0200416 if ((resp_data[0] & OCR_POWERUP) != 0U) {
417 mmc_ocr_value = resp_data[0];
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200418
Yann Gautierde6959e2018-08-02 13:46:17 +0200419 if ((mmc_ocr_value & OCR_HCS) != 0U) {
Yann Gautierd8b6b8e2021-03-22 14:02:54 +0100420 mmc_dev_info->mmc_dev_type = MMC_IS_SD_HC;
Yann Gautierde6959e2018-08-02 13:46:17 +0200421 } else {
Yann Gautierd8b6b8e2021-03-22 14:02:54 +0100422 mmc_dev_info->mmc_dev_type = MMC_IS_SD;
Yann Gautierde6959e2018-08-02 13:46:17 +0200423 }
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200424
Yann Gautierde6959e2018-08-02 13:46:17 +0200425 return 0;
426 }
427
Yann Gautier38d80352019-08-16 16:49:41 +0200428 mdelay(10);
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200429 }
430
Yann Gautierde6959e2018-08-02 13:46:17 +0200431 ERROR("ACMD41 failed after %d retries\n", SEND_OP_COND_MAX_RETRIES);
432
433 return -EIO;
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200434}
435
Yann Gautierde6959e2018-08-02 13:46:17 +0200436static int mmc_reset_to_idle(void)
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200437{
438 int ret;
Yann Gautierde6959e2018-08-02 13:46:17 +0200439
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200440 /* CMD0: reset to IDLE */
441 ret = mmc_send_cmd(MMC_CMD(0), 0, 0, NULL);
442 if (ret != 0) {
443 return ret;
444 }
445
Yann Gautierde6959e2018-08-02 13:46:17 +0200446 mdelay(2);
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200447
Yann Gautierde6959e2018-08-02 13:46:17 +0200448 return 0;
449}
450
451static int mmc_send_op_cond(void)
452{
453 int ret, n;
454 unsigned int resp_data[4];
455
Yann Gautier4c7888a2018-11-29 15:43:37 +0100456 ret = mmc_reset_to_idle();
457 if (ret != 0) {
458 return ret;
Yann Gautiere77be522021-03-23 13:30:43 +0100459 }
Yann Gautierde6959e2018-08-02 13:46:17 +0200460
461 for (n = 0; n < SEND_OP_COND_MAX_RETRIES; n++) {
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200462 ret = mmc_send_cmd(MMC_CMD(1), OCR_SECTOR_MODE |
463 OCR_VDD_MIN_2V7 | OCR_VDD_MIN_1V7,
Bryan O'Donoghuee62f4192018-08-15 15:59:07 +0100464 MMC_RESPONSE_R3, &resp_data[0]);
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200465 if (ret != 0) {
466 return ret;
467 }
468
Yann Gautierde6959e2018-08-02 13:46:17 +0200469 if ((resp_data[0] & OCR_POWERUP) != 0U) {
470 mmc_ocr_value = resp_data[0];
471 return 0;
472 }
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200473
Joakim Bech8428a042018-12-18 10:09:02 +0100474 mdelay(10);
Yann Gautierde6959e2018-08-02 13:46:17 +0200475 }
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200476
Yann Gautierde6959e2018-08-02 13:46:17 +0200477 ERROR("CMD1 failed after %d retries\n", SEND_OP_COND_MAX_RETRIES);
478
479 return -EIO;
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200480}
481
482static 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
Yann Gautier4c7888a2018-11-29 15:43:37 +0100489 ret = mmc_reset_to_idle();
490 if (ret != 0) {
491 return ret;
Yann Gautiere77be522021-03-23 13:30:43 +0100492 }
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200493
Yann Gautierd8b6b8e2021-03-22 14:02:54 +0100494 if (mmc_dev_info->mmc_dev_type == MMC_IS_EMMC) {
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200495 ret = mmc_send_op_cond();
Haojian Zhuanged59cd52018-08-02 14:48:17 +0800496 } else {
497 /* CMD8: Send Interface Condition Command */
498 ret = mmc_send_cmd(MMC_CMD(8), VHS_2_7_3_6_V | CMD8_CHECK_PATTERN,
Yann Gautierf2e8b162018-09-28 16:48:37 +0200499 MMC_RESPONSE_R5, &resp_data[0]);
Haojian Zhuanged59cd52018-08-02 14:48:17 +0800500
501 if ((ret == 0) && ((resp_data[0] & 0xffU) == CMD8_CHECK_PATTERN)) {
502 ret = sd_send_op_cond();
503 }
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200504 }
505 if (ret != 0) {
506 return ret;
507 }
508
509 /* CMD2: Card Identification */
Shawn Guoab3b62b2018-09-28 14:21:01 +0800510 ret = mmc_send_cmd(MMC_CMD(2), 0, MMC_RESPONSE_R2, NULL);
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200511 if (ret != 0) {
512 return ret;
513 }
514
515 /* CMD3: Set Relative Address */
Yann Gautierd8b6b8e2021-03-22 14:02:54 +0100516 if (mmc_dev_info->mmc_dev_type == MMC_IS_EMMC) {
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200517 rca = MMC_FIX_RCA;
518 ret = mmc_send_cmd(MMC_CMD(3), rca << RCA_SHIFT_OFFSET,
Yann Gautierf2e8b162018-09-28 16:48:37 +0200519 MMC_RESPONSE_R1, NULL);
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200520 if (ret != 0) {
521 return ret;
522 }
523 } else {
524 ret = mmc_send_cmd(MMC_CMD(3), 0,
Yann Gautierf2e8b162018-09-28 16:48:37 +0200525 MMC_RESPONSE_R6, &resp_data[0]);
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200526 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,
Shawn Guoab3b62b2018-09-28 14:21:01 +0800535 MMC_RESPONSE_R2, &resp_data[0]);
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200536 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,
Yann Gautierf2e8b162018-09-28 16:48:37 +0200544 MMC_RESPONSE_R1, NULL);
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200545 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
Haojian Zhuangd618b272018-08-04 18:04:30 +0800556 ret = mmc_set_ios(clk, bus_width);
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200557 if (ret != 0) {
558 return ret;
559 }
560
Yann Gautier0307b712019-06-12 15:55:37 +0200561 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;
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200594}
595
Haojian Zhuangd87f0b72018-08-02 14:49:51 +0800596size_t mmc_read_blocks(int lba, uintptr_t buf, size_t size)
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200597{
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,
Yann Gautierf2e8b162018-09-28 16:48:37 +0200614 MMC_RESPONSE_R1, NULL);
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200615 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) &&
Yann Gautierd8b6b8e2021-03-22 14:02:54 +0100629 (mmc_dev_info->mmc_dev_type != MMC_IS_SD_HC)) {
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200630 cmd_arg = lba * MMC_BLOCK_SIZE;
631 } else {
632 cmd_arg = lba;
633 }
634
Yann Gautierf2e8b162018-09-28 16:48:37 +0200635 ret = mmc_send_cmd(cmd_idx, cmd_arg, MMC_RESPONSE_R1, NULL);
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200636 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)) {
Bryan O'Donoghuee6b56cc2018-08-15 16:25:30 +0100654 ret = mmc_send_cmd(MMC_CMD(12), 0, MMC_RESPONSE_R1B, NULL);
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200655 if (ret != 0) {
656 return 0;
657 }
658 }
659
660 return size;
661}
662
Haojian Zhuangd87f0b72018-08-02 14:49:51 +0800663size_t mmc_write_blocks(int lba, const uintptr_t buf, size_t size)
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200664{
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,
Yann Gautierf2e8b162018-09-28 16:48:37 +0200682 MMC_RESPONSE_R1, NULL);
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200683 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
Yann Gautierf2e8b162018-09-28 16:48:37 +0200702 ret = mmc_send_cmd(cmd_idx, cmd_arg, MMC_RESPONSE_R1, NULL);
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200703 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)) {
Bryan O'Donoghuee6b56cc2018-08-15 16:25:30 +0100721 ret = mmc_send_cmd(MMC_CMD(12), 0, MMC_RESPONSE_R1B, NULL);
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200722 if (ret != 0) {
723 return 0;
724 }
725 }
726
727 return size;
728}
729
Haojian Zhuangd87f0b72018-08-02 14:49:51 +0800730size_t mmc_erase_blocks(int lba, size_t size)
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200731{
732 int ret;
733
734 assert(ops != NULL);
735 assert((size != 0U) && ((size & MMC_BLOCK_MASK) == 0U));
736
Yann Gautierf2e8b162018-09-28 16:48:37 +0200737 ret = mmc_send_cmd(MMC_CMD(35), lba, MMC_RESPONSE_R1, NULL);
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200738 if (ret != 0) {
739 return 0;
740 }
741
742 ret = mmc_send_cmd(MMC_CMD(36), lba + (size / MMC_BLOCK_SIZE) - 1U,
Yann Gautierf2e8b162018-09-28 16:48:37 +0200743 MMC_RESPONSE_R1, NULL);
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200744 if (ret != 0) {
745 return 0;
746 }
747
Yann Gautierf2e8b162018-09-28 16:48:37 +0200748 ret = mmc_send_cmd(MMC_CMD(38), lba, MMC_RESPONSE_R1B, NULL);
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200749 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
Vyacheslav Yurkovb3d5f342021-03-30 08:16:20 +0200763static 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
773static unsigned char mmc_current_boot_part(void)
774{
775 return PART_CFG_CURRENT_BOOT_PARTITION(mmc_ext_csd[CMD_EXTCSD_PARTITION_CONFIG]);
776}
777
Ahmad Fatoumee8f3422022-05-23 17:06:37 +0200778int mmc_part_switch_current_boot(void)
Vyacheslav Yurkovb3d5f342021-03-30 08:16:20 +0200779{
Vyacheslav Yurkovb3d5f342021-03-30 08:16:20 +0200780 unsigned char current_boot_part = mmc_current_boot_part();
Ahmad Fatoumee8f3422022-05-23 17:06:37 +0200781 int ret;
Vyacheslav Yurkovb3d5f342021-03-30 08:16:20 +0200782
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);
Ahmad Fatoumee8f3422022-05-23 17:06:37 +0200786 return -EIO;
Vyacheslav Yurkovb3d5f342021-03-30 08:16:20 +0200787 }
788
789 ret = mmc_part_switch(current_boot_part);
790 if (ret < 0) {
791 ERROR("Failed to switch to boot partition, %d\n", ret);
Ahmad Fatoumee8f3422022-05-23 17:06:37 +0200792 }
793
794 return ret;
795}
796
797int mmc_part_switch_user(void)
798{
799 int ret;
800
Ahmad Fatoumb61eb752022-05-31 10:03:04 +0200801 ret = mmc_part_switch(PART_CFG_BOOT_PARTITION_NO_ACCESS);
Ahmad Fatoumee8f3422022-05-23 17:06:37 +0200802 if (ret < 0) {
803 ERROR("Failed to switch to user partition, %d\n", ret);
804 }
805
806 return ret;
807}
808
Yann Gautier053d7f22022-09-01 19:23:39 +0200809size_t mmc_boot_part_size(void)
810{
811 return mmc_ext_csd[CMD_EXTCSD_BOOT_SIZE_MULT] * SZ_128K;
812}
813
Ahmad Fatoumee8f3422022-05-23 17:06:37 +0200814size_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) {
Vyacheslav Yurkovb3d5f342021-03-30 08:16:20 +0200821 return 0;
822 }
823
824 size_read = mmc_read_blocks(lba, buf, size);
825
Ahmad Fatoumee8f3422022-05-23 17:06:37 +0200826 ret = mmc_part_switch_user();
Vyacheslav Yurkovb3d5f342021-03-30 08:16:20 +0200827 if (ret < 0) {
Vyacheslav Yurkovb3d5f342021-03-30 08:16:20 +0200828 return 0;
829 }
830
831 return size_read;
832}
833
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200834int 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;
Yann Gautierd8b6b8e2021-03-22 14:02:54 +0100855 mmc_dev_info = device_info;
Yann Gautier1e5e85a2018-07-03 18:32:12 +0200856
857 return mmc_enumerate(clk, width);
858}