1 /*
2 * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include "sdmmc_common.h"
8
9 static const char* TAG = "sdmmc_cmd";
10
11
sdmmc_send_cmd(sdmmc_card_t * card,sdmmc_command_t * cmd)12 esp_err_t sdmmc_send_cmd(sdmmc_card_t* card, sdmmc_command_t* cmd)
13 {
14 if (card->host.command_timeout_ms != 0) {
15 cmd->timeout_ms = card->host.command_timeout_ms;
16 } else if (cmd->timeout_ms == 0) {
17 cmd->timeout_ms = SDMMC_DEFAULT_CMD_TIMEOUT_MS;
18 }
19
20 int slot = card->host.slot;
21 ESP_LOGV(TAG, "sending cmd slot=%d op=%d arg=%x flags=%x data=%p blklen=%d datalen=%d timeout=%d",
22 slot, cmd->opcode, cmd->arg, cmd->flags, cmd->data, cmd->blklen, cmd->datalen, cmd->timeout_ms);
23 esp_err_t err = (*card->host.do_transaction)(slot, cmd);
24 if (err != 0) {
25 ESP_LOGD(TAG, "cmd=%d, sdmmc_req_run returned 0x%x", cmd->opcode, err);
26 return err;
27 }
28 int state = MMC_R1_CURRENT_STATE(cmd->response);
29 ESP_LOGV(TAG, "cmd response %08x %08x %08x %08x err=0x%x state=%d",
30 cmd->response[0],
31 cmd->response[1],
32 cmd->response[2],
33 cmd->response[3],
34 cmd->error,
35 state);
36 return cmd->error;
37 }
38
sdmmc_send_app_cmd(sdmmc_card_t * card,sdmmc_command_t * cmd)39 esp_err_t sdmmc_send_app_cmd(sdmmc_card_t* card, sdmmc_command_t* cmd)
40 {
41 sdmmc_command_t app_cmd = {
42 .opcode = MMC_APP_CMD,
43 .flags = SCF_CMD_AC | SCF_RSP_R1,
44 .arg = MMC_ARG_RCA(card->rca),
45 };
46 esp_err_t err = sdmmc_send_cmd(card, &app_cmd);
47 if (err != ESP_OK) {
48 return err;
49 }
50 // Check APP_CMD status bit (only in SD mode)
51 if (!host_is_spi(card) && !(MMC_R1(app_cmd.response) & MMC_R1_APP_CMD)) {
52 ESP_LOGW(TAG, "card doesn't support APP_CMD");
53 return ESP_ERR_NOT_SUPPORTED;
54 }
55 return sdmmc_send_cmd(card, cmd);
56 }
57
58
sdmmc_send_cmd_go_idle_state(sdmmc_card_t * card)59 esp_err_t sdmmc_send_cmd_go_idle_state(sdmmc_card_t* card)
60 {
61 sdmmc_command_t cmd = {
62 .opcode = MMC_GO_IDLE_STATE,
63 .flags = SCF_CMD_BC | SCF_RSP_R0,
64 };
65 esp_err_t err = sdmmc_send_cmd(card, &cmd);
66 if (host_is_spi(card)) {
67 /* To enter SPI mode, CMD0 needs to be sent twice (see figure 4-1 in
68 * SD Simplified spec v4.10). Some cards enter SD mode on first CMD0,
69 * so don't expect the above command to succeed.
70 * SCF_RSP_R1 flag below tells the lower layer to expect correct R1
71 * response (in SPI mode).
72 */
73 (void) err;
74 vTaskDelay(SDMMC_GO_IDLE_DELAY_MS / portTICK_PERIOD_MS);
75
76 cmd.flags |= SCF_RSP_R1;
77 err = sdmmc_send_cmd(card, &cmd);
78 }
79 if (err == ESP_OK) {
80 vTaskDelay(SDMMC_GO_IDLE_DELAY_MS / portTICK_PERIOD_MS);
81 }
82 return err;
83 }
84
85
sdmmc_send_cmd_send_if_cond(sdmmc_card_t * card,uint32_t ocr)86 esp_err_t sdmmc_send_cmd_send_if_cond(sdmmc_card_t* card, uint32_t ocr)
87 {
88 const uint8_t pattern = 0xaa; /* any pattern will do here */
89 sdmmc_command_t cmd = {
90 .opcode = SD_SEND_IF_COND,
91 .arg = (((ocr & SD_OCR_VOL_MASK) != 0) << 8) | pattern,
92 .flags = SCF_CMD_BCR | SCF_RSP_R7,
93 };
94 esp_err_t err = sdmmc_send_cmd(card, &cmd);
95 if (err != ESP_OK) {
96 return err;
97 }
98 uint8_t response = cmd.response[0] & 0xff;
99 if (response != pattern) {
100 ESP_LOGD(TAG, "%s: received=0x%x expected=0x%x", __func__, response, pattern);
101 return ESP_ERR_INVALID_RESPONSE;
102 }
103 return ESP_OK;
104 }
105
sdmmc_send_cmd_send_op_cond(sdmmc_card_t * card,uint32_t ocr,uint32_t * ocrp)106 esp_err_t sdmmc_send_cmd_send_op_cond(sdmmc_card_t* card, uint32_t ocr, uint32_t *ocrp)
107 {
108 esp_err_t err;
109
110 sdmmc_command_t cmd = {
111 .arg = ocr,
112 .flags = SCF_CMD_BCR | SCF_RSP_R3,
113 .opcode = SD_APP_OP_COND
114 };
115 int nretries = SDMMC_SEND_OP_COND_MAX_RETRIES;
116 int err_cnt = SDMMC_SEND_OP_COND_MAX_ERRORS;
117 for (; nretries != 0; --nretries) {
118 bzero(&cmd, sizeof cmd);
119 cmd.arg = ocr;
120 cmd.flags = SCF_CMD_BCR | SCF_RSP_R3;
121 if (!card->is_mmc) { /* SD mode */
122 cmd.opcode = SD_APP_OP_COND;
123 err = sdmmc_send_app_cmd(card, &cmd);
124 } else { /* MMC mode */
125 cmd.arg &= ~MMC_OCR_ACCESS_MODE_MASK;
126 cmd.arg |= MMC_OCR_SECTOR_MODE;
127 cmd.opcode = MMC_SEND_OP_COND;
128 err = sdmmc_send_cmd(card, &cmd);
129 }
130
131 if (err != ESP_OK) {
132 if (--err_cnt == 0) {
133 ESP_LOGD(TAG, "%s: sdmmc_send_app_cmd err=0x%x", __func__, err);
134 return err;
135 } else {
136 ESP_LOGV(TAG, "%s: ignoring err=0x%x", __func__, err);
137 continue;
138 }
139 }
140 // In SD protocol, card sets MEM_READY bit in OCR when it is ready.
141 // In SPI protocol, card clears IDLE_STATE bit in R1 response.
142 if (!host_is_spi(card)) {
143 if ((MMC_R3(cmd.response) & MMC_OCR_MEM_READY) ||
144 ocr == 0) {
145 break;
146 }
147 } else {
148 if ((SD_SPI_R1(cmd.response) & SD_SPI_R1_IDLE_STATE) == 0) {
149 break;
150 }
151 }
152 vTaskDelay(10 / portTICK_PERIOD_MS);
153 }
154 if (nretries == 0) {
155 return ESP_ERR_TIMEOUT;
156 }
157 if (ocrp) {
158 *ocrp = MMC_R3(cmd.response);
159 }
160 return ESP_OK;
161 }
162
sdmmc_send_cmd_read_ocr(sdmmc_card_t * card,uint32_t * ocrp)163 esp_err_t sdmmc_send_cmd_read_ocr(sdmmc_card_t *card, uint32_t *ocrp)
164 {
165 assert(ocrp);
166 sdmmc_command_t cmd = {
167 .opcode = SD_READ_OCR,
168 .flags = SCF_CMD_BCR | SCF_RSP_R2
169 };
170 esp_err_t err = sdmmc_send_cmd(card, &cmd);
171 if (err != ESP_OK) {
172 return err;
173 }
174 *ocrp = SD_SPI_R3(cmd.response);
175 return ESP_OK;
176 }
177
178
sdmmc_send_cmd_all_send_cid(sdmmc_card_t * card,sdmmc_response_t * out_raw_cid)179 esp_err_t sdmmc_send_cmd_all_send_cid(sdmmc_card_t* card, sdmmc_response_t* out_raw_cid)
180 {
181 assert(out_raw_cid);
182 sdmmc_command_t cmd = {
183 .opcode = MMC_ALL_SEND_CID,
184 .flags = SCF_CMD_BCR | SCF_RSP_R2
185 };
186 esp_err_t err = sdmmc_send_cmd(card, &cmd);
187 if (err != ESP_OK) {
188 return err;
189 }
190 memcpy(out_raw_cid, &cmd.response, sizeof(sdmmc_response_t));
191 return ESP_OK;
192 }
193
sdmmc_send_cmd_send_cid(sdmmc_card_t * card,sdmmc_cid_t * out_cid)194 esp_err_t sdmmc_send_cmd_send_cid(sdmmc_card_t *card, sdmmc_cid_t *out_cid)
195 {
196 assert(out_cid);
197 assert(host_is_spi(card) && "SEND_CID should only be used in SPI mode");
198 assert(!card->is_mmc && "MMC cards are not supported in SPI mode");
199 sdmmc_response_t buf;
200 sdmmc_command_t cmd = {
201 .opcode = MMC_SEND_CID,
202 .flags = SCF_CMD_READ | SCF_CMD_ADTC,
203 .arg = 0,
204 .data = &buf[0],
205 .datalen = sizeof(buf)
206 };
207 esp_err_t err = sdmmc_send_cmd(card, &cmd);
208 if (err != ESP_OK) {
209 return err;
210 }
211 sdmmc_flip_byte_order(buf, sizeof(buf));
212 return sdmmc_decode_cid(buf, out_cid);
213 }
214
215
sdmmc_send_cmd_set_relative_addr(sdmmc_card_t * card,uint16_t * out_rca)216 esp_err_t sdmmc_send_cmd_set_relative_addr(sdmmc_card_t* card, uint16_t* out_rca)
217 {
218 assert(out_rca);
219 sdmmc_command_t cmd = {
220 .opcode = SD_SEND_RELATIVE_ADDR,
221 .flags = SCF_CMD_BCR | SCF_RSP_R6
222 };
223
224 /* MMC cards expect us to set the RCA.
225 * Set RCA to 1 since we don't support multiple cards on the same bus, for now.
226 */
227 uint16_t mmc_rca = 1;
228 if (card->is_mmc) {
229 cmd.arg = MMC_ARG_RCA(mmc_rca);
230 }
231
232 esp_err_t err = sdmmc_send_cmd(card, &cmd);
233 if (err != ESP_OK) {
234 return err;
235 }
236 *out_rca = (card->is_mmc) ? mmc_rca : SD_R6_RCA(cmd.response);
237 return ESP_OK;
238 }
239
sdmmc_send_cmd_set_blocklen(sdmmc_card_t * card,sdmmc_csd_t * csd)240 esp_err_t sdmmc_send_cmd_set_blocklen(sdmmc_card_t* card, sdmmc_csd_t* csd)
241 {
242 sdmmc_command_t cmd = {
243 .opcode = MMC_SET_BLOCKLEN,
244 .arg = csd->sector_size,
245 .flags = SCF_CMD_AC | SCF_RSP_R1
246 };
247 return sdmmc_send_cmd(card, &cmd);
248 }
249
sdmmc_send_cmd_send_csd(sdmmc_card_t * card,sdmmc_csd_t * out_csd)250 esp_err_t sdmmc_send_cmd_send_csd(sdmmc_card_t* card, sdmmc_csd_t* out_csd)
251 {
252 /* The trick with SEND_CSD is that in SPI mode, it acts as a data read
253 * command, while in SD mode it is an AC command with R2 response.
254 */
255 sdmmc_response_t spi_buf;
256 const bool is_spi = host_is_spi(card);
257 sdmmc_command_t cmd = {
258 .opcode = MMC_SEND_CSD,
259 .arg = is_spi ? 0 : MMC_ARG_RCA(card->rca),
260 .flags = is_spi ? (SCF_CMD_READ | SCF_CMD_ADTC | SCF_RSP_R1) :
261 (SCF_CMD_AC | SCF_RSP_R2),
262 .data = is_spi ? &spi_buf[0] : 0,
263 .datalen = is_spi ? sizeof(spi_buf) : 0,
264 };
265 esp_err_t err = sdmmc_send_cmd(card, &cmd);
266 if (err != ESP_OK) {
267 return err;
268 }
269 uint32_t* ptr = cmd.response;
270 if (is_spi) {
271 sdmmc_flip_byte_order(spi_buf, sizeof(spi_buf));
272 ptr = spi_buf;
273 }
274 if (card->is_mmc) {
275 err = sdmmc_mmc_decode_csd(cmd.response, out_csd);
276 } else {
277 err = sdmmc_decode_csd(ptr, out_csd);
278 }
279 return err;
280 }
281
sdmmc_send_cmd_select_card(sdmmc_card_t * card,uint32_t rca)282 esp_err_t sdmmc_send_cmd_select_card(sdmmc_card_t* card, uint32_t rca)
283 {
284 /* Don't expect to see a response when de-selecting a card */
285 uint32_t response = (rca == 0) ? 0 : SCF_RSP_R1;
286 sdmmc_command_t cmd = {
287 .opcode = MMC_SELECT_CARD,
288 .arg = MMC_ARG_RCA(rca),
289 .flags = SCF_CMD_AC | response
290 };
291 return sdmmc_send_cmd(card, &cmd);
292 }
293
sdmmc_send_cmd_send_scr(sdmmc_card_t * card,sdmmc_scr_t * out_scr)294 esp_err_t sdmmc_send_cmd_send_scr(sdmmc_card_t* card, sdmmc_scr_t *out_scr)
295 {
296 size_t datalen = 8;
297 uint32_t* buf = (uint32_t*) heap_caps_malloc(datalen, MALLOC_CAP_DMA);
298 if (buf == NULL) {
299 return ESP_ERR_NO_MEM;
300 }
301 sdmmc_command_t cmd = {
302 .data = buf,
303 .datalen = datalen,
304 .blklen = datalen,
305 .flags = SCF_CMD_ADTC | SCF_CMD_READ | SCF_RSP_R1,
306 .opcode = SD_APP_SEND_SCR
307 };
308 esp_err_t err = sdmmc_send_app_cmd(card, &cmd);
309 if (err == ESP_OK) {
310 err = sdmmc_decode_scr(buf, out_scr);
311 }
312 free(buf);
313 return err;
314 }
315
sdmmc_send_cmd_set_bus_width(sdmmc_card_t * card,int width)316 esp_err_t sdmmc_send_cmd_set_bus_width(sdmmc_card_t* card, int width)
317 {
318 sdmmc_command_t cmd = {
319 .opcode = SD_APP_SET_BUS_WIDTH,
320 .flags = SCF_RSP_R1 | SCF_CMD_AC,
321 .arg = (width == 4) ? SD_ARG_BUS_WIDTH_4 : SD_ARG_BUS_WIDTH_1,
322 };
323
324 return sdmmc_send_app_cmd(card, &cmd);
325 }
326
sdmmc_send_cmd_crc_on_off(sdmmc_card_t * card,bool crc_enable)327 esp_err_t sdmmc_send_cmd_crc_on_off(sdmmc_card_t* card, bool crc_enable)
328 {
329 assert(host_is_spi(card) && "CRC_ON_OFF can only be used in SPI mode");
330 sdmmc_command_t cmd = {
331 .opcode = SD_CRC_ON_OFF,
332 .arg = crc_enable ? 1 : 0,
333 .flags = SCF_CMD_AC | SCF_RSP_R1
334 };
335 return sdmmc_send_cmd(card, &cmd);
336 }
337
sdmmc_send_cmd_send_status(sdmmc_card_t * card,uint32_t * out_status)338 esp_err_t sdmmc_send_cmd_send_status(sdmmc_card_t* card, uint32_t* out_status)
339 {
340 sdmmc_command_t cmd = {
341 .opcode = MMC_SEND_STATUS,
342 .arg = MMC_ARG_RCA(card->rca),
343 .flags = SCF_CMD_AC | SCF_RSP_R1
344 };
345 esp_err_t err = sdmmc_send_cmd(card, &cmd);
346 if (err != ESP_OK) {
347 return err;
348 }
349 if (out_status) {
350 *out_status = MMC_R1(cmd.response);
351 }
352 return ESP_OK;
353 }
354
sdmmc_write_sectors(sdmmc_card_t * card,const void * src,size_t start_block,size_t block_count)355 esp_err_t sdmmc_write_sectors(sdmmc_card_t* card, const void* src,
356 size_t start_block, size_t block_count)
357 {
358 esp_err_t err = ESP_OK;
359 size_t block_size = card->csd.sector_size;
360 if (esp_ptr_dma_capable(src) && (intptr_t)src % 4 == 0) {
361 err = sdmmc_write_sectors_dma(card, src, start_block, block_count);
362 } else {
363 // SDMMC peripheral needs DMA-capable buffers. Split the write into
364 // separate single block writes, if needed, and allocate a temporary
365 // DMA-capable buffer.
366 void* tmp_buf = heap_caps_malloc(block_size, MALLOC_CAP_DMA);
367 if (tmp_buf == NULL) {
368 return ESP_ERR_NO_MEM;
369 }
370 const uint8_t* cur_src = (const uint8_t*) src;
371 for (size_t i = 0; i < block_count; ++i) {
372 memcpy(tmp_buf, cur_src, block_size);
373 cur_src += block_size;
374 err = sdmmc_write_sectors_dma(card, tmp_buf, start_block + i, 1);
375 if (err != ESP_OK) {
376 ESP_LOGD(TAG, "%s: error 0x%x writing block %d+%d",
377 __func__, err, start_block, i);
378 break;
379 }
380 }
381 free(tmp_buf);
382 }
383 return err;
384 }
385
sdmmc_write_sectors_dma(sdmmc_card_t * card,const void * src,size_t start_block,size_t block_count)386 esp_err_t sdmmc_write_sectors_dma(sdmmc_card_t* card, const void* src,
387 size_t start_block, size_t block_count)
388 {
389 if (start_block + block_count > card->csd.capacity) {
390 return ESP_ERR_INVALID_SIZE;
391 }
392 size_t block_size = card->csd.sector_size;
393 sdmmc_command_t cmd = {
394 .flags = SCF_CMD_ADTC | SCF_RSP_R1,
395 .blklen = block_size,
396 .data = (void*) src,
397 .datalen = block_count * block_size,
398 .timeout_ms = SDMMC_WRITE_CMD_TIMEOUT_MS
399 };
400 if (block_count == 1) {
401 cmd.opcode = MMC_WRITE_BLOCK_SINGLE;
402 } else {
403 cmd.opcode = MMC_WRITE_BLOCK_MULTIPLE;
404 }
405 if (card->ocr & SD_OCR_SDHC_CAP) {
406 cmd.arg = start_block;
407 } else {
408 cmd.arg = start_block * block_size;
409 }
410 esp_err_t err = sdmmc_send_cmd(card, &cmd);
411 if (err != ESP_OK) {
412 ESP_LOGE(TAG, "%s: sdmmc_send_cmd returned 0x%x", __func__, err);
413 return err;
414 }
415 uint32_t status = 0;
416 size_t count = 0;
417 while (!host_is_spi(card) && !(status & MMC_R1_READY_FOR_DATA)) {
418 // TODO: add some timeout here
419 err = sdmmc_send_cmd_send_status(card, &status);
420 if (err != ESP_OK) {
421 return err;
422 }
423 if (++count % 10 == 0) {
424 ESP_LOGV(TAG, "waiting for card to become ready (%d)", count);
425 }
426 }
427 return ESP_OK;
428 }
429
sdmmc_read_sectors(sdmmc_card_t * card,void * dst,size_t start_block,size_t block_count)430 esp_err_t sdmmc_read_sectors(sdmmc_card_t* card, void* dst,
431 size_t start_block, size_t block_count)
432 {
433 esp_err_t err = ESP_OK;
434 size_t block_size = card->csd.sector_size;
435 if (esp_ptr_dma_capable(dst) && (intptr_t)dst % 4 == 0) {
436 err = sdmmc_read_sectors_dma(card, dst, start_block, block_count);
437 } else {
438 // SDMMC peripheral needs DMA-capable buffers. Split the read into
439 // separate single block reads, if needed, and allocate a temporary
440 // DMA-capable buffer.
441 void* tmp_buf = heap_caps_malloc(block_size, MALLOC_CAP_DMA);
442 if (tmp_buf == NULL) {
443 return ESP_ERR_NO_MEM;
444 }
445 uint8_t* cur_dst = (uint8_t*) dst;
446 for (size_t i = 0; i < block_count; ++i) {
447 err = sdmmc_read_sectors_dma(card, tmp_buf, start_block + i, 1);
448 if (err != ESP_OK) {
449 ESP_LOGD(TAG, "%s: error 0x%x writing block %d+%d",
450 __func__, err, start_block, i);
451 break;
452 }
453 memcpy(cur_dst, tmp_buf, block_size);
454 cur_dst += block_size;
455 }
456 free(tmp_buf);
457 }
458 return err;
459 }
460
sdmmc_read_sectors_dma(sdmmc_card_t * card,void * dst,size_t start_block,size_t block_count)461 esp_err_t sdmmc_read_sectors_dma(sdmmc_card_t* card, void* dst,
462 size_t start_block, size_t block_count)
463 {
464 if (start_block + block_count > card->csd.capacity) {
465 return ESP_ERR_INVALID_SIZE;
466 }
467 size_t block_size = card->csd.sector_size;
468 sdmmc_command_t cmd = {
469 .flags = SCF_CMD_ADTC | SCF_CMD_READ | SCF_RSP_R1,
470 .blklen = block_size,
471 .data = (void*) dst,
472 .datalen = block_count * block_size
473 };
474 if (block_count == 1) {
475 cmd.opcode = MMC_READ_BLOCK_SINGLE;
476 } else {
477 cmd.opcode = MMC_READ_BLOCK_MULTIPLE;
478 }
479 if (card->ocr & SD_OCR_SDHC_CAP) {
480 cmd.arg = start_block;
481 } else {
482 cmd.arg = start_block * block_size;
483 }
484 esp_err_t err = sdmmc_send_cmd(card, &cmd);
485 if (err != ESP_OK) {
486 ESP_LOGE(TAG, "%s: sdmmc_send_cmd returned 0x%x", __func__, err);
487 return err;
488 }
489 uint32_t status = 0;
490 size_t count = 0;
491 while (!host_is_spi(card) && !(status & MMC_R1_READY_FOR_DATA)) {
492 // TODO: add some timeout here
493 err = sdmmc_send_cmd_send_status(card, &status);
494 if (err != ESP_OK) {
495 return err;
496 }
497 if (++count % 10 == 0) {
498 ESP_LOGV(TAG, "waiting for card to become ready (%d)", count);
499 }
500 }
501 return ESP_OK;
502 }
503
sdmmc_get_status(sdmmc_card_t * card)504 esp_err_t sdmmc_get_status(sdmmc_card_t* card)
505 {
506 uint32_t stat;
507 return sdmmc_send_cmd_send_status(card, &stat);
508 }
509