Skip to content

Commit 88c1e6c

Browse files
authored
sdio: fix missing card status polling after CMD6 in eMMC DDR mode switch
1 parent 743b614 commit 88c1e6c

File tree

2 files changed

+70
-2
lines changed

2 files changed

+70
-2
lines changed

components/drivers/include/drivers/dev_mmcsd_core.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,8 @@ struct rt_mmcsd_req
142142
#define R1_READY_FOR_DATA (1 << 8) /* sx, a */
143143
#define R1_APP_CMD (1 << 5) /* sr, c */
144144

145+
/*the programing is state*/
146+
#define R1_STATE_PRG 0x07
145147

146148
#define R1_SPI_IDLE (1 << 0)
147149
#define R1_SPI_ERASE_RESET (1 << 1)

components/drivers/sdio/dev_mmc.c

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2006-2024, RT-Thread Development Team
2+
* Copyright (c) 2006-2026, RT-Thread Development Team
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*
@@ -270,6 +270,64 @@ static int mmc_parse_ext_csd(struct rt_mmcsd_card *card, rt_uint8_t *ext_csd)
270270
return 0;
271271
}
272272

273+
/*
274+
* Send Status.
275+
*/
276+
static int mmc_send_status(struct rt_mmcsd_card *card, rt_uint32_t *status, unsigned retries)
277+
{
278+
int err;
279+
struct rt_mmcsd_cmd cmd = (struct rt_mmcsd_cmd){ 0 };
280+
281+
cmd.busy_timeout = 0;
282+
cmd.cmd_code = SEND_STATUS;
283+
cmd.arg = card->rca << 16;
284+
cmd.flags = RESP_R1 | CMD_AC;
285+
err = mmcsd_send_cmd(card->host, &cmd, retries);
286+
if (err)
287+
return err;
288+
289+
if (status)
290+
*status = cmd.resp[0];
291+
292+
return 0;
293+
}
294+
295+
/*
296+
* Poll Busy.
297+
*/
298+
static int mmc_poll_for_busy(struct rt_mmcsd_card *card, rt_uint32_t timeout_ms, unsigned retries)
299+
{
300+
int timeout = rt_tick_from_millisecond(timeout_ms);
301+
int err = 0;
302+
rt_uint32_t status;
303+
rt_tick_t start;
304+
305+
start = rt_tick_get();
306+
do
307+
{
308+
rt_bool_t out = (int)(rt_tick_get() - start) >= timeout;
309+
310+
if (out)
311+
{
312+
LOG_E("wait card busy timeout");
313+
return -RT_ETIMEOUT;
314+
}
315+
316+
rt_thread_mdelay(1);
317+
318+
err = mmc_send_status(card, &status, retries);
319+
if (R1_STATUS(err))
320+
{
321+
LOG_E("error %d requesting status", err);
322+
return err;
323+
}
324+
}
325+
while (!(status & R1_READY_FOR_DATA) ||
326+
(R1_CURRENT_STATE(status) == R1_STATE_PRG));
327+
328+
return err;
329+
}
330+
273331
/**
274332
* mmc_switch - modify EXT_CSD register
275333
* @card: the MMC card associated with the data transfer
@@ -295,6 +353,13 @@ static int mmc_switch(struct rt_mmcsd_card *card, rt_uint8_t set,
295353
if (err)
296354
return err;
297355

356+
/*
357+
* Poll the card status using CMD13 with a timeout of 500ms and a polling interval of 1ms.
358+
*/
359+
err = mmc_poll_for_busy(card, 500, 3);
360+
if (err)
361+
return err;
362+
298363
return 0;
299364
}
300365

@@ -490,7 +555,7 @@ rt_err_t mmc_send_op_cond(struct rt_mmcsd_host *host,
490555

491556
err = -RT_ETIMEOUT;
492557

493-
rt_thread_mdelay(10); //delay 10ms
558+
rt_thread_mdelay(10); /* delay 10ms */
494559
}
495560

496561
if (rocr && !controller_is_spi(host))
@@ -815,3 +880,4 @@ rt_int32_t init_mmc(struct rt_mmcsd_host *host, rt_uint32_t ocr)
815880

816881
return err;
817882
}
883+

0 commit comments

Comments
 (0)