sdmmc_mmc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /*
  2. * Copyright (c) 2006 Uwe Stuehler <uwe@openbsd.org>
  3. * Adaptations to ESP-IDF Copyright (c) 2016-2018 Espressif Systems (Shanghai) PTE LTD
  4. *
  5. * Permission to use, copy, modify, and distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  12. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  14. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  15. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. #include <inttypes.h>
  18. #include <unistd.h>
  19. #include "sdmmc_common.h"
  20. static const char* TAG = "sdmmc_mmc";
  21. esp_err_t sdmmc_init_mmc_read_ext_csd(sdmmc_card_t* card)
  22. {
  23. int card_type;
  24. esp_err_t err = ESP_OK;
  25. uint8_t* ext_csd = NULL;
  26. size_t actual_size = 0;
  27. err = esp_dma_malloc(EXT_CSD_MMC_SIZE, 0, (void *)&ext_csd, &actual_size);
  28. if (err != ESP_OK) {
  29. ESP_LOGE(TAG, "%s: could not allocate ext_csd", __func__);
  30. return err;
  31. }
  32. uint32_t sectors = 0;
  33. ESP_LOGD(TAG, "MMC version: %d", card->csd.mmc_ver);
  34. if (card->csd.mmc_ver < MMC_CSD_MMCVER_4_0) {
  35. err = ESP_ERR_NOT_SUPPORTED;
  36. goto out;
  37. }
  38. /* read EXT_CSD */
  39. err = sdmmc_mmc_send_ext_csd_data(card, ext_csd, EXT_CSD_MMC_SIZE, actual_size);
  40. if (err != ESP_OK) {
  41. ESP_LOGE(TAG, "%s: send_ext_csd_data error 0x%x", __func__, err);
  42. goto out;
  43. }
  44. card_type = ext_csd[EXT_CSD_CARD_TYPE];
  45. card->is_ddr = 0;
  46. if (card_type & EXT_CSD_CARD_TYPE_F_52M_1_8V) {
  47. card->max_freq_khz = SDMMC_FREQ_52M;
  48. if ((card->host.flags & SDMMC_HOST_FLAG_DDR) &&
  49. card->host.max_freq_khz >= SDMMC_FREQ_26M &&
  50. card->host.get_bus_width(card->host.slot) == 4) {
  51. ESP_LOGD(TAG, "card and host support DDR mode");
  52. card->is_ddr = 1;
  53. }
  54. } else if (card_type & EXT_CSD_CARD_TYPE_F_52M) {
  55. card->max_freq_khz = SDMMC_FREQ_52M;
  56. } else if (card_type & EXT_CSD_CARD_TYPE_F_26M) {
  57. card->max_freq_khz = SDMMC_FREQ_26M;
  58. } else {
  59. ESP_LOGW(TAG, "%s: unknown CARD_TYPE 0x%x", __func__, card_type);
  60. }
  61. /* For MMC cards, use speed value from EXT_CSD */
  62. card->csd.tr_speed = card->max_freq_khz * 1000;
  63. ESP_LOGD(TAG, "MMC card type %d, max_freq_khz=%d, is_ddr=%d", card_type, card->max_freq_khz, card->is_ddr);
  64. card->max_freq_khz = MIN(card->max_freq_khz, card->host.max_freq_khz);
  65. if (card->host.flags & SDMMC_HOST_FLAG_8BIT) {
  66. card->ext_csd.power_class = ext_csd[(card->max_freq_khz > SDMMC_FREQ_26M) ?
  67. EXT_CSD_PWR_CL_52_360 : EXT_CSD_PWR_CL_26_360] >> 4;
  68. card->log_bus_width = 3;
  69. } else if (card->host.flags & SDMMC_HOST_FLAG_4BIT) {
  70. card->ext_csd.power_class = ext_csd[(card->max_freq_khz > SDMMC_FREQ_26M) ?
  71. EXT_CSD_PWR_CL_52_360 : EXT_CSD_PWR_CL_26_360] & 0x0f;
  72. card->log_bus_width = 2;
  73. } else {
  74. card->ext_csd.power_class = 0; //card must be able to do full rate at powerclass 0 in 1-bit mode
  75. card->log_bus_width = 0;
  76. }
  77. sectors = ( ext_csd[EXT_CSD_SEC_COUNT + 0] << 0 )
  78. | ( ext_csd[EXT_CSD_SEC_COUNT + 1] << 8 )
  79. | ( ext_csd[EXT_CSD_SEC_COUNT + 2] << 16 )
  80. | ( ext_csd[EXT_CSD_SEC_COUNT + 3] << 24 );
  81. if (sectors > (2u * 1024 * 1024 * 1024) / 512) {
  82. card->csd.capacity = sectors;
  83. }
  84. /* erased state of a bit, if 1 byte value read is 0xFF else 0x00 */
  85. card->ext_csd.erase_mem_state = ext_csd[EXT_CSD_ERASED_MEM_CONT];
  86. card->ext_csd.rev = ext_csd[EXT_CSD_REV];
  87. card->ext_csd.sec_feature = ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT];
  88. out:
  89. free(ext_csd);
  90. return err;
  91. }
  92. esp_err_t sdmmc_init_mmc_bus_width(sdmmc_card_t* card)
  93. {
  94. esp_err_t err;
  95. if (card->ext_csd.power_class != 0) {
  96. err = sdmmc_mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  97. EXT_CSD_POWER_CLASS, card->ext_csd.power_class);
  98. if (err != ESP_OK) {
  99. ESP_LOGE(TAG, "%s: can't change power class (%d bit), 0x%x"
  100. , __func__, card->ext_csd.power_class, err);
  101. return err;
  102. }
  103. }
  104. if (card->log_bus_width > 0) {
  105. int csd_bus_width_value = EXT_CSD_BUS_WIDTH_1;
  106. int bus_width = 1;
  107. if (card->log_bus_width == 2) {
  108. if (card->is_ddr) {
  109. csd_bus_width_value = EXT_CSD_BUS_WIDTH_4_DDR;
  110. } else {
  111. csd_bus_width_value = EXT_CSD_BUS_WIDTH_4;
  112. }
  113. bus_width = 4;
  114. } else if (card->log_bus_width == 3) {
  115. if (card->is_ddr) {
  116. csd_bus_width_value = EXT_CSD_BUS_WIDTH_8_DDR;
  117. } else {
  118. csd_bus_width_value = EXT_CSD_BUS_WIDTH_8;
  119. }
  120. bus_width = 8;
  121. }
  122. err = sdmmc_mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  123. EXT_CSD_BUS_WIDTH, csd_bus_width_value);
  124. if (err != ESP_OK) {
  125. ESP_LOGE(TAG, "%s: can't change bus width (%d bit), 0x%x",
  126. __func__, bus_width, err);
  127. return err;
  128. }
  129. }
  130. return ESP_OK;
  131. }
  132. esp_err_t sdmmc_mmc_enable_hs_mode(sdmmc_card_t* card)
  133. {
  134. esp_err_t err;
  135. if (card->max_freq_khz > SDMMC_FREQ_26M) {
  136. /* switch to high speed timing */
  137. err = sdmmc_mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
  138. EXT_CSD_HS_TIMING, EXT_CSD_HS_TIMING_HS);
  139. if (err != ESP_OK) {
  140. ESP_LOGE(TAG, "%s: mmc_switch EXT_CSD_HS_TIMING_HS error 0x%x",
  141. __func__, err);
  142. return err;
  143. }
  144. }
  145. return ESP_OK;
  146. }
  147. esp_err_t sdmmc_mmc_decode_cid(int mmc_ver, sdmmc_response_t resp, sdmmc_cid_t* out_cid)
  148. {
  149. if (mmc_ver == MMC_CSD_MMCVER_1_0 ||
  150. mmc_ver == MMC_CSD_MMCVER_1_4) {
  151. out_cid->mfg_id = MMC_CID_MID_V1(resp);
  152. out_cid->oem_id = 0;
  153. MMC_CID_PNM_V1_CPY(resp, out_cid->name);
  154. out_cid->revision = MMC_CID_REV_V1(resp);
  155. out_cid->serial = MMC_CID_PSN_V1(resp);
  156. out_cid->date = MMC_CID_MDT_V1(resp);
  157. } else if (mmc_ver == MMC_CSD_MMCVER_2_0 ||
  158. mmc_ver == MMC_CSD_MMCVER_3_1 ||
  159. mmc_ver == MMC_CSD_MMCVER_4_0) {
  160. out_cid->mfg_id = MMC_CID_MID_V2(resp);
  161. out_cid->oem_id = MMC_CID_OID_V2(resp);
  162. MMC_CID_PNM_V1_CPY(resp, out_cid->name);
  163. out_cid->revision = 0;
  164. out_cid->serial = MMC_CID_PSN_V1(resp);
  165. out_cid->date = 0;
  166. }
  167. return ESP_OK;
  168. }
  169. esp_err_t sdmmc_mmc_decode_csd(sdmmc_response_t response, sdmmc_csd_t* out_csd)
  170. {
  171. out_csd->csd_ver = MMC_CSD_CSDVER(response);
  172. if (out_csd->csd_ver == MMC_CSD_CSDVER_1_0 ||
  173. out_csd->csd_ver == MMC_CSD_CSDVER_2_0 ||
  174. out_csd->csd_ver == MMC_CSD_CSDVER_EXT_CSD) {
  175. out_csd->mmc_ver = MMC_CSD_MMCVER(response);
  176. out_csd->capacity = MMC_CSD_CAPACITY(response);
  177. out_csd->read_block_len = MMC_CSD_READ_BL_LEN(response);
  178. } else {
  179. ESP_LOGE(TAG, "unknown MMC CSD structure version 0x%x", out_csd->csd_ver);
  180. return 1;
  181. }
  182. int read_bl_size = 1 << out_csd->read_block_len;
  183. out_csd->sector_size = MIN(read_bl_size, 512);
  184. if (out_csd->sector_size < read_bl_size) {
  185. out_csd->capacity *= read_bl_size / out_csd->sector_size;
  186. }
  187. /* tr_speed will be determined when reading CXD */
  188. out_csd->tr_speed = 0;
  189. return ESP_OK;
  190. }
  191. esp_err_t sdmmc_mmc_send_ext_csd_data(sdmmc_card_t* card, void *out_data, size_t datalen, size_t buffer_len)
  192. {
  193. assert(esp_ptr_dma_capable(out_data));
  194. sdmmc_command_t cmd = {
  195. .data = out_data,
  196. .datalen = datalen,
  197. .buflen = buffer_len,
  198. .blklen = datalen,
  199. .opcode = MMC_SEND_EXT_CSD,
  200. .arg = 0,
  201. .flags = SCF_CMD_ADTC | SCF_RSP_R1 | SCF_CMD_READ
  202. };
  203. return sdmmc_send_cmd(card, &cmd);
  204. }
  205. esp_err_t sdmmc_mmc_switch(sdmmc_card_t* card, uint8_t set, uint8_t index, uint8_t value)
  206. {
  207. sdmmc_command_t cmd = {
  208. .opcode = MMC_SWITCH,
  209. .arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) | (index << 16) | (value << 8) | set,
  210. .flags = SCF_RSP_R1B | SCF_CMD_AC | SCF_WAIT_BUSY,
  211. };
  212. esp_err_t err = sdmmc_send_cmd(card, &cmd);
  213. if (err == ESP_OK) {
  214. //check response bit to see that switch was accepted
  215. if (MMC_R1(cmd.response) & MMC_R1_SWITCH_ERROR) {
  216. err = ESP_ERR_INVALID_RESPONSE;
  217. }
  218. }
  219. return err;
  220. }
  221. esp_err_t sdmmc_init_mmc_check_ext_csd(sdmmc_card_t* card)
  222. {
  223. assert(card->is_mem == 1 && card->rca != 0);
  224. /*
  225. * Integrity check required if card switched to HS mode
  226. * card->max_freq_khz = MIN(card->max_freq_khz, card->host.max_freq_khz)
  227. * For 26MHz limit background see sdmmc_mmc_enable_hs_mode()
  228. */
  229. if (card->max_freq_khz <= SDMMC_FREQ_26M) {
  230. return ESP_OK;
  231. }
  232. /* ensure EXT_CSD buffer is available before starting any SD-card operation */
  233. uint8_t* ext_csd = NULL;
  234. size_t actual_size = 0;
  235. esp_err_t err = esp_dma_malloc(EXT_CSD_MMC_SIZE, 0, (void *)&ext_csd, &actual_size);
  236. if (err != ESP_OK) {
  237. ESP_LOGE(TAG, "%s: could not allocate ext_csd", __func__);
  238. return err;
  239. }
  240. /* ensure card is in transfer state before read ext_csd */
  241. uint32_t status;
  242. err = sdmmc_send_cmd_send_status(card, &status);
  243. if (err != ESP_OK) {
  244. ESP_LOGE(TAG, "%s: send_status returned 0x%x", __func__, err);
  245. goto out;
  246. }
  247. status = ((status & MMC_R1_CURRENT_STATE_MASK) >> MMC_R1_CURRENT_STATE_POS);
  248. if (status != MMC_R1_CURRENT_STATE_TRAN) {
  249. ESP_LOGE(TAG, "%s: card not in transfer state", __func__);
  250. err = ESP_ERR_INVALID_STATE;
  251. goto out;
  252. }
  253. /* read EXT_CSD to ensure device works fine in HS mode */
  254. err = sdmmc_mmc_send_ext_csd_data(card, ext_csd, EXT_CSD_MMC_SIZE, actual_size);
  255. if (err != ESP_OK) {
  256. ESP_LOGE(TAG, "%s: send_ext_csd_data error 0x%x", __func__, err);
  257. goto out;
  258. }
  259. /* EXT_CSD static fields should match the previous read values in sdmmc_card_init */
  260. if ((card->ext_csd.rev != ext_csd[EXT_CSD_REV]) ||
  261. (card->ext_csd.sec_feature != ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT])) {
  262. ESP_LOGE(TAG, "%s: Data integrity test fail in HS mode", __func__);
  263. err = ESP_FAIL;
  264. }
  265. out:
  266. free(ext_csd);
  267. return err;
  268. }
  269. uint32_t sdmmc_mmc_get_erase_timeout_ms(const sdmmc_card_t* card, int arg, size_t erase_size_kb)
  270. {
  271. /* TODO: calculate erase timeout based on ext_csd (trim_timeout) */
  272. uint32_t timeout_ms = SDMMC_SD_DISCARD_TIMEOUT * erase_size_kb / card->csd.sector_size;
  273. timeout_ms = MAX(1000, timeout_ms);
  274. ESP_LOGD(TAG, "%s: erase timeout %" PRIu32 " s (erasing %" PRIu32 " kB, %" PRIu32 " ms per sector)",
  275. __func__, (uint32_t) (timeout_ms / 1000), (uint32_t) erase_size_kb, (uint32_t) SDMMC_SD_DISCARD_TIMEOUT);
  276. return timeout_ms;
  277. }