From e246071d6db7130d57bbb4210bfddb9a47ccc725 Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Mon, 19 Sep 2022 21:15:23 +0200 Subject: [PATCH] sdmmc: keep clock enabled for the duration of ACMD41 SD specification requires that card clock is not disabled until the card is idle, following ACMD41 command. --- components/driver/include/driver/sdmmc_host.h | 14 ++++++++ .../driver/include/driver/sdmmc_types.h | 1 + components/driver/include/driver/sdspi_host.h | 1 + components/driver/sdmmc_host.c | 14 ++++++++ components/sdmmc/sdmmc_cmd.c | 35 +++++++++++++++++-- 5 files changed, 62 insertions(+), 3 deletions(-) diff --git a/components/driver/include/driver/sdmmc_host.h b/components/driver/include/driver/sdmmc_host.h index ff5ae71d01..2c7a596955 100644 --- a/components/driver/include/driver/sdmmc_host.h +++ b/components/driver/include/driver/sdmmc_host.h @@ -40,6 +40,7 @@ extern "C" { .get_bus_width = &sdmmc_host_get_slot_width, \ .set_bus_ddr_mode = &sdmmc_host_set_bus_ddr_mode, \ .set_card_clk = &sdmmc_host_set_card_clk, \ + .set_cclk_always_on = &sdmmc_host_set_cclk_always_on, \ .do_transaction = &sdmmc_host_do_transaction, \ .deinit = &sdmmc_host_deinit, \ .io_int_enable = sdmmc_host_io_int_enable, \ @@ -203,6 +204,19 @@ esp_err_t sdmmc_host_set_card_clk(int slot, uint32_t freq_khz); */ esp_err_t sdmmc_host_set_bus_ddr_mode(int slot, bool ddr_enabled); +/** + * @brief Enable or disable always-on card clock + * When cclk_always_on is false, the host controller is allowed to shut down + * the card clock between the commands. When cclk_always_on is true, the clock + * is generated even if no command is in progress. + * @param slot slot number + * @param cclk_always_on enable or disable always-on clock + * @return + * - ESP_OK on success + * - ESP_ERR_INVALID_ARG if the slot number is invalid + */ +esp_err_t sdmmc_host_set_cclk_always_on(int slot, bool cclk_always_on); + /** * @brief Send command to the card and get response * diff --git a/components/driver/include/driver/sdmmc_types.h b/components/driver/include/driver/sdmmc_types.h index cbb796fdbb..0307d9bf61 100644 --- a/components/driver/include/driver/sdmmc_types.h +++ b/components/driver/include/driver/sdmmc_types.h @@ -150,6 +150,7 @@ typedef struct { size_t (*get_bus_width)(int slot); /*!< host function to get bus width */ esp_err_t (*set_bus_ddr_mode)(int slot, bool ddr_enable); /*!< host function to set DDR mode */ esp_err_t (*set_card_clk)(int slot, uint32_t freq_khz); /*!< host function to set card clock frequency */ + esp_err_t (*set_cclk_always_on)(int slot, bool cclk_always_on); /*!< host function to set whether the clock is always enabled */ esp_err_t (*do_transaction)(int slot, sdmmc_command_t* cmdinfo); /*!< host function to do a transaction */ union { esp_err_t (*deinit)(void); /*!< host function to deinitialize the driver */ diff --git a/components/driver/include/driver/sdspi_host.h b/components/driver/include/driver/sdspi_host.h index a5d942e9d8..653c89a4c5 100644 --- a/components/driver/include/driver/sdspi_host.h +++ b/components/driver/include/driver/sdspi_host.h @@ -45,6 +45,7 @@ typedef int sdspi_dev_handle_t; .get_bus_width = NULL, \ .set_bus_ddr_mode = NULL, \ .set_card_clk = &sdspi_host_set_card_clk, \ + .set_cclk_always_on = NULL, \ .do_transaction = &sdspi_host_do_transaction, \ .deinit_p = &sdspi_host_remove_device, \ .io_int_enable = &sdspi_host_io_int_enable, \ diff --git a/components/driver/sdmmc_host.c b/components/driver/sdmmc_host.c index e4170e2502..bd94b76916 100644 --- a/components/driver/sdmmc_host.c +++ b/components/driver/sdmmc_host.c @@ -597,6 +597,20 @@ esp_err_t sdmmc_host_set_bus_ddr_mode(int slot, bool ddr_enabled) return ESP_OK; } +esp_err_t sdmmc_host_set_cclk_always_on(int slot, bool cclk_always_on) +{ + if (!(slot == 0 || slot == 1)) { + return ESP_ERR_INVALID_ARG; + } + if (cclk_always_on) { + SDMMC.clkena.cclk_low_power &= ~BIT(slot); + } else { + SDMMC.clkena.cclk_low_power |= BIT(slot); + } + sdmmc_host_clock_update_command(slot); + return ESP_OK; +} + static void sdmmc_host_dma_init(void) { SDMMC.ctrl.dma_enable = 1; diff --git a/components/sdmmc/sdmmc_cmd.c b/components/sdmmc/sdmmc_cmd.c index 0a39575d17..2a1a33973c 100644 --- a/components/sdmmc/sdmmc_cmd.c +++ b/components/sdmmc/sdmmc_cmd.c @@ -107,6 +107,19 @@ esp_err_t sdmmc_send_cmd_send_op_cond(sdmmc_card_t* card, uint32_t ocr, uint32_t { esp_err_t err; + /* If the host supports this, keep card clock enabled + * from the start of ACMD41 until the card is idle. + * (Ref. SD spec, section 4.4 "Clock control".) + */ + if (card->host.set_cclk_always_on != NULL) { + err = card->host.set_cclk_always_on(card->host.slot, true); + if (err != ESP_OK) { + ESP_LOGE(TAG, "%s: set_cclk_always_on (1) err=0x%x", __func__, err); + return err; + } + ESP_LOGV(TAG, "%s: keeping clock on during ACMD41", __func__); + } + sdmmc_command_t cmd = { .arg = ocr, .flags = SCF_CMD_BCR | SCF_RSP_R3, @@ -131,7 +144,7 @@ esp_err_t sdmmc_send_cmd_send_op_cond(sdmmc_card_t* card, uint32_t ocr, uint32_t if (err != ESP_OK) { if (--err_cnt == 0) { ESP_LOGD(TAG, "%s: sdmmc_send_app_cmd err=0x%x", __func__, err); - return err; + goto done; } else { ESP_LOGV(TAG, "%s: ignoring err=0x%x", __func__, err); continue; @@ -151,13 +164,29 @@ esp_err_t sdmmc_send_cmd_send_op_cond(sdmmc_card_t* card, uint32_t ocr, uint32_t } vTaskDelay(10 / portTICK_PERIOD_MS); } + if (nretries == 0) { - return ESP_ERR_TIMEOUT; + err = ESP_ERR_TIMEOUT; + goto done; } + if (ocrp) { *ocrp = MMC_R3(cmd.response); } - return ESP_OK; + + err = ESP_OK; +done: + + if (card->host.set_cclk_always_on != NULL) { + esp_err_t err_cclk_dis = card->host.set_cclk_always_on(card->host.slot, false); + if (err_cclk_dis != ESP_OK) { + ESP_LOGE(TAG, "%s: set_cclk_always_on (2) err=0x%x", __func__, err); + /* If we failed to disable clock, don't overwrite 'err' to return the original error */ + } + ESP_LOGV(TAG, "%s: clock always-on mode disabled", __func__); + } + + return err; } esp_err_t sdmmc_send_cmd_read_ocr(sdmmc_card_t *card, uint32_t *ocrp)