sdmmc_sd.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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 "esp_timer.h"
  19. #include "sdmmc_common.h"
  20. static const char* TAG = "sdmmc_sd";
  21. esp_err_t sdmmc_init_sd_if_cond(sdmmc_card_t* card)
  22. {
  23. /* SEND_IF_COND (CMD8) command is used to identify SDHC/SDXC cards.
  24. * SD v1 and non-SD cards will not respond to this command.
  25. */
  26. uint32_t host_ocr = get_host_ocr(card->host.io_voltage);
  27. esp_err_t err = sdmmc_send_cmd_send_if_cond(card, host_ocr);
  28. if (err == ESP_OK) {
  29. ESP_LOGD(TAG, "SDHC/SDXC card");
  30. host_ocr |= SD_OCR_SDHC_CAP;
  31. } else if (err == ESP_ERR_TIMEOUT) {
  32. ESP_LOGD(TAG, "CMD8 timeout; not an SD v2.00 card");
  33. } else if (host_is_spi(card) && err == ESP_ERR_NOT_SUPPORTED) {
  34. ESP_LOGD(TAG, "CMD8 rejected; not an SD v2.00 card");
  35. } else {
  36. ESP_LOGE(TAG, "%s: send_if_cond (1) returned 0x%x", __func__, err);
  37. return err;
  38. }
  39. card->ocr = host_ocr;
  40. return ESP_OK;
  41. }
  42. esp_err_t sdmmc_init_sd_blocklen(sdmmc_card_t* card)
  43. {
  44. /* SDSC cards support configurable data block lengths.
  45. * We don't use this feature and set the block length to 512 bytes,
  46. * same as the block length for SDHC cards.
  47. */
  48. if ((card->ocr & SD_OCR_SDHC_CAP) == 0) {
  49. esp_err_t err = sdmmc_send_cmd_set_blocklen(card, &card->csd);
  50. if (err != ESP_OK) {
  51. ESP_LOGE(TAG, "%s: set_blocklen returned 0x%x", __func__, err);
  52. return err;
  53. }
  54. }
  55. return ESP_OK;
  56. }
  57. esp_err_t sdmmc_init_sd_scr(sdmmc_card_t* card)
  58. {
  59. esp_err_t err;
  60. /* Get the contents of SCR register: bus width and the version of SD spec
  61. * supported by the card.
  62. * In SD mode, this is the first command which uses D0 line. Errors at
  63. * this step usually indicate connection issue or lack of pull-up resistor.
  64. */
  65. err = sdmmc_send_cmd_send_scr(card, &card->scr);
  66. if (err != ESP_OK) {
  67. ESP_LOGE(TAG, "%s: send_scr (1) returned 0x%x", __func__, err);
  68. return err;
  69. }
  70. if ((card->scr.bus_width & SCR_SD_BUS_WIDTHS_4BIT)
  71. && (card->host.flags & SDMMC_HOST_FLAG_4BIT)) {
  72. card->log_bus_width = 2;
  73. } else {
  74. card->log_bus_width = 0;
  75. }
  76. return ESP_OK;
  77. }
  78. esp_err_t sdmmc_init_sd_ssr(sdmmc_card_t* card)
  79. {
  80. esp_err_t err = ESP_OK;
  81. /* Get the contents of SSR register: SD additional information
  82. * ACMD13 to read 512byte SD status information
  83. */
  84. uint32_t* sd_ssr = NULL;
  85. size_t actual_size = 0;
  86. err = esp_dma_calloc(1, SD_SSR_SIZE, 0, (void *)&sd_ssr, &actual_size);
  87. if (err != ESP_OK) {
  88. ESP_LOGE(TAG, "%s: could not allocate sd_ssr", __func__);
  89. return err;
  90. }
  91. sdmmc_command_t cmd = {
  92. .data = sd_ssr,
  93. .datalen = SD_SSR_SIZE,
  94. .buflen = actual_size,
  95. .blklen = SD_SSR_SIZE,
  96. .opcode = SD_APP_SD_STATUS,
  97. .arg = 0,
  98. .flags = SCF_CMD_ADTC | SCF_RSP_R1 | SCF_CMD_READ
  99. };
  100. // read SD status register
  101. err = sdmmc_send_app_cmd(card, &cmd);
  102. if (err != ESP_OK) {
  103. free(sd_ssr);
  104. ESP_LOGE(TAG, "%s: sdmmc_send_cmd returned 0x%x", __func__, err);
  105. return err;
  106. }
  107. err = sdmmc_decode_ssr(sd_ssr, &card->ssr);
  108. if (err != ESP_OK) {
  109. ESP_LOGE(TAG, "%s: error sdmmc_decode_scr returned 0x%x", __func__, err);
  110. }
  111. free(sd_ssr);
  112. return err;
  113. }
  114. esp_err_t sdmmc_init_sd_bus_width(sdmmc_card_t* card)
  115. {
  116. int width = 1;
  117. if (card->log_bus_width == 2) {
  118. width = 4;
  119. } else if (card->log_bus_width == 3) {
  120. width = 8;
  121. }
  122. esp_err_t err = sdmmc_send_cmd_set_bus_width(card, width);
  123. if (err != ESP_OK) {
  124. ESP_LOGE(TAG, "set_bus_width failed (0x%x)", err);
  125. return err;
  126. }
  127. return ESP_OK;
  128. }
  129. esp_err_t sdmmc_init_sd_wait_data_ready(sdmmc_card_t* card)
  130. {
  131. /* Wait for the card to be ready for data transfers */
  132. uint32_t status = 0;
  133. uint32_t count = 0;
  134. int64_t yield_delay_us = 100 * 1000; // initially 100ms
  135. int64_t t0 = esp_timer_get_time();
  136. int64_t t1 = 0;
  137. while (!host_is_spi(card) && !(status & MMC_R1_READY_FOR_DATA)) {
  138. t1 = esp_timer_get_time();
  139. if (t1 - t0 > SDMMC_INIT_WAIT_DATA_READY_TIMEOUT_US) {
  140. ESP_LOGE(TAG, "init wait data ready - timeout");
  141. return ESP_ERR_TIMEOUT;
  142. }
  143. if (t1 - t0 > yield_delay_us) {
  144. yield_delay_us *= 2;
  145. vTaskDelay(1);
  146. }
  147. esp_err_t err = sdmmc_send_cmd_send_status(card, &status);
  148. if (err != ESP_OK) {
  149. return err;
  150. }
  151. if (++count % 16 == 0) {
  152. ESP_LOGV(TAG, "waiting for card to become ready (%" PRIu32 ")", count);
  153. }
  154. }
  155. return ESP_OK;
  156. }
  157. esp_err_t sdmmc_send_cmd_switch_func(sdmmc_card_t* card,
  158. uint32_t mode, uint32_t group, uint32_t function,
  159. sdmmc_switch_func_rsp_t* resp)
  160. {
  161. if (card->scr.sd_spec < SCR_SD_SPEC_VER_1_10 ||
  162. ((card->csd.card_command_class & SD_CSD_CCC_SWITCH) == 0)) {
  163. return ESP_ERR_NOT_SUPPORTED;
  164. }
  165. if (group == 0 ||
  166. group > SD_SFUNC_GROUP_MAX ||
  167. function > SD_SFUNC_FUNC_MAX) {
  168. return ESP_ERR_INVALID_ARG;
  169. }
  170. if (mode > 1) {
  171. return ESP_ERR_INVALID_ARG;
  172. }
  173. uint32_t group_shift = (group - 1) << 2;
  174. /* all functions which should not be affected are set to 0xf (no change) */
  175. uint32_t other_func_mask = (0x00ffffff & ~(0xf << group_shift));
  176. uint32_t func_val = (function << group_shift) | other_func_mask;
  177. sdmmc_command_t cmd = {
  178. .opcode = MMC_SWITCH,
  179. .flags = SCF_CMD_ADTC | SCF_CMD_READ | SCF_RSP_R1,
  180. .blklen = sizeof(sdmmc_switch_func_rsp_t),
  181. .data = resp->data,
  182. .datalen = sizeof(sdmmc_switch_func_rsp_t),
  183. .arg = (!!mode << 31) | func_val
  184. };
  185. esp_err_t err = sdmmc_send_cmd(card, &cmd);
  186. if (err != ESP_OK) {
  187. ESP_LOGE(TAG, "%s: sdmmc_send_cmd returned 0x%x", __func__, err);
  188. return err;
  189. }
  190. sdmmc_flip_byte_order(resp->data, sizeof(sdmmc_switch_func_rsp_t));
  191. uint32_t resp_ver = SD_SFUNC_VER(resp->data);
  192. if (resp_ver == 0) {
  193. /* busy response is never sent */
  194. } else if (resp_ver == 1) {
  195. if (SD_SFUNC_BUSY(resp->data, group) & (1 << function)) {
  196. ESP_LOGD(TAG, "%s: response indicates function %" PRIu32 ":%" PRIu32 " is busy",
  197. __func__, group, function);
  198. return ESP_ERR_INVALID_STATE;
  199. }
  200. } else {
  201. ESP_LOGD(TAG, "%s: got an invalid version of SWITCH_FUNC response: 0x%02" PRIx32,
  202. __func__, resp_ver);
  203. return ESP_ERR_INVALID_RESPONSE;
  204. }
  205. return ESP_OK;
  206. }
  207. esp_err_t sdmmc_enable_hs_mode(sdmmc_card_t* card)
  208. {
  209. /* This will determine if the card supports SWITCH_FUNC command,
  210. * and high speed mode. If the cards supports both, this will enable
  211. * high speed mode at the card side.
  212. */
  213. if (card->scr.sd_spec < SCR_SD_SPEC_VER_1_10 ||
  214. ((card->csd.card_command_class & SD_CSD_CCC_SWITCH) == 0)) {
  215. return ESP_ERR_NOT_SUPPORTED;
  216. }
  217. sdmmc_switch_func_rsp_t* response = (sdmmc_switch_func_rsp_t*)
  218. heap_caps_malloc(sizeof(*response), MALLOC_CAP_DMA);
  219. if (response == NULL) {
  220. return ESP_ERR_NO_MEM;
  221. }
  222. esp_err_t err = sdmmc_send_cmd_switch_func(card, 0, SD_ACCESS_MODE, 0, response);
  223. if (err != ESP_OK) {
  224. ESP_LOGD(TAG, "%s: sdmmc_send_cmd_switch_func (1) returned 0x%x", __func__, err);
  225. goto out;
  226. }
  227. uint32_t supported_mask = SD_SFUNC_SUPPORTED(response->data, 1);
  228. if ((supported_mask & BIT(SD_ACCESS_MODE_SDR25)) == 0) {
  229. err = ESP_ERR_NOT_SUPPORTED;
  230. goto out;
  231. }
  232. err = sdmmc_send_cmd_switch_func(card, 1, SD_ACCESS_MODE, SD_ACCESS_MODE_SDR25, response);
  233. if (err != ESP_OK) {
  234. ESP_LOGD(TAG, "%s: sdmmc_send_cmd_switch_func (2) returned 0x%x", __func__, err);
  235. goto out;
  236. }
  237. out:
  238. free(response);
  239. return err;
  240. }
  241. esp_err_t sdmmc_enable_hs_mode_and_check(sdmmc_card_t* card)
  242. {
  243. /* All cards should support at least default speed */
  244. card->max_freq_khz = SDMMC_FREQ_DEFAULT;
  245. if (card->host.max_freq_khz <= card->max_freq_khz) {
  246. /* Host is configured to use low frequency, don't attempt to switch */
  247. card->max_freq_khz = card->host.max_freq_khz;
  248. return ESP_OK;
  249. }
  250. /* Try to enabled HS mode */
  251. esp_err_t err = sdmmc_enable_hs_mode(card);
  252. if (err != ESP_OK) {
  253. return err;
  254. }
  255. /* HS mode has been enabled on the card.
  256. * Read CSD again, it should now indicate that the card supports
  257. * 50MHz clock.
  258. * Since SEND_CSD is allowed only in standby mode, and the card is currently in data transfer
  259. * mode, deselect the card first, then get the CSD, then select the card again. This step is
  260. * not required in SPI mode, since CMD7 (select_card) is not supported.
  261. */
  262. const bool is_spi = host_is_spi(card);
  263. if (!is_spi) {
  264. err = sdmmc_send_cmd_select_card(card, 0);
  265. if (err != ESP_OK) {
  266. ESP_LOGE(TAG, "%s: select_card (1) returned 0x%x", __func__, err);
  267. return err;
  268. }
  269. }
  270. err = sdmmc_send_cmd_send_csd(card, &card->csd);
  271. if (err != ESP_OK) {
  272. ESP_LOGE(TAG, "%s: send_csd returned 0x%x", __func__, err);
  273. return err;
  274. }
  275. if (!is_spi) {
  276. err = sdmmc_send_cmd_select_card(card, card->rca);
  277. if (err != ESP_OK) {
  278. ESP_LOGE(TAG, "%s: select_card (2) returned 0x%x", __func__, err);
  279. return err;
  280. }
  281. }
  282. if (card->csd.tr_speed != 50000000) {
  283. ESP_LOGW(TAG, "unexpected: after enabling HS mode, tr_speed=%d", card->csd.tr_speed);
  284. return ESP_ERR_NOT_SUPPORTED;
  285. }
  286. card->max_freq_khz = MIN(card->host.max_freq_khz, SDMMC_FREQ_HIGHSPEED);
  287. return ESP_OK;
  288. }
  289. esp_err_t sdmmc_check_scr(sdmmc_card_t* card)
  290. {
  291. /* If frequency switch has been performed, read SCR register one more time
  292. * and compare the result with the previous one. Use this simple check as
  293. * an indicator of potential signal integrity issues.
  294. */
  295. sdmmc_scr_t scr_tmp = { 0 };
  296. esp_err_t err = sdmmc_send_cmd_send_scr(card, &scr_tmp);
  297. if (err != ESP_OK) {
  298. ESP_LOGE(TAG, "%s: send_scr returned 0x%x", __func__, err);
  299. return err;
  300. }
  301. if (memcmp(&card->scr, &scr_tmp, sizeof(scr_tmp)) != 0) {
  302. ESP_LOGE(TAG, "got corrupted data after increasing clock frequency");
  303. return ESP_ERR_INVALID_RESPONSE;
  304. }
  305. return ESP_OK;
  306. }
  307. esp_err_t sdmmc_init_spi_crc(sdmmc_card_t* card)
  308. {
  309. /* In SD mode, CRC checks of data transfers are mandatory and performed
  310. * by the hardware. In SPI mode, CRC16 of data transfers is optional and
  311. * needs to be enabled.
  312. */
  313. assert(host_is_spi(card));
  314. esp_err_t err = sdmmc_send_cmd_crc_on_off(card, true);
  315. if (err != ESP_OK) {
  316. ESP_LOGE(TAG, "%s: sdmmc_send_cmd_crc_on_off returned 0x%x", __func__, err);
  317. return err;
  318. }
  319. return ESP_OK;
  320. }
  321. esp_err_t sdmmc_decode_cid(sdmmc_response_t resp, sdmmc_cid_t* out_cid)
  322. {
  323. out_cid->mfg_id = SD_CID_MID(resp);
  324. out_cid->oem_id = SD_CID_OID(resp);
  325. SD_CID_PNM_CPY(resp, out_cid->name);
  326. out_cid->revision = SD_CID_REV(resp);
  327. out_cid->serial = SD_CID_PSN(resp);
  328. out_cid->date = SD_CID_MDT(resp);
  329. return ESP_OK;
  330. }
  331. esp_err_t sdmmc_decode_csd(sdmmc_response_t response, sdmmc_csd_t* out_csd)
  332. {
  333. out_csd->csd_ver = SD_CSD_CSDVER(response);
  334. switch (out_csd->csd_ver) {
  335. case SD_CSD_CSDVER_2_0:
  336. out_csd->capacity = SD_CSD_V2_CAPACITY(response);
  337. out_csd->read_block_len = SD_CSD_V2_BL_LEN;
  338. break;
  339. case SD_CSD_CSDVER_1_0:
  340. out_csd->capacity = SD_CSD_CAPACITY(response);
  341. out_csd->read_block_len = SD_CSD_READ_BL_LEN(response);
  342. break;
  343. default:
  344. ESP_LOGE(TAG, "unknown SD CSD structure version 0x%x", out_csd->csd_ver);
  345. return ESP_ERR_NOT_SUPPORTED;
  346. }
  347. out_csd->card_command_class = SD_CSD_CCC(response);
  348. int read_bl_size = 1 << out_csd->read_block_len;
  349. out_csd->sector_size = MIN(read_bl_size, 512);
  350. if (out_csd->sector_size < read_bl_size) {
  351. out_csd->capacity *= read_bl_size / out_csd->sector_size;
  352. }
  353. int speed = SD_CSD_SPEED(response);
  354. if (speed == SD_CSD_SPEED_50_MHZ) {
  355. out_csd->tr_speed = 50000000;
  356. } else {
  357. out_csd->tr_speed = 25000000;
  358. }
  359. return ESP_OK;
  360. }
  361. esp_err_t sdmmc_decode_scr(uint32_t *raw_scr, sdmmc_scr_t* out_scr)
  362. {
  363. sdmmc_response_t resp = { 0 };
  364. resp[1] = __builtin_bswap32(raw_scr[0]);
  365. resp[0] = __builtin_bswap32(raw_scr[1]);
  366. int ver = SCR_STRUCTURE(resp);
  367. if (ver != 0) {
  368. return ESP_ERR_NOT_SUPPORTED;
  369. }
  370. out_scr->sd_spec = SCR_SD_SPEC(resp);
  371. out_scr->erase_mem_state = SCR_DATA_STAT_AFTER_ERASE(resp);
  372. out_scr->bus_width = SCR_SD_BUS_WIDTHS(resp);
  373. return ESP_OK;
  374. }
  375. static const uint32_t s_au_to_size_kb[] = {
  376. 0, 16, 32, 64,
  377. 128, 256, 512, 1024,
  378. 2 * 1024, 4 * 1024,
  379. 8 * 1024, 12 * 1024,
  380. 16 * 1024, 24 * 1024,
  381. 32 * 1024, 64 * 1024
  382. };
  383. _Static_assert(sizeof(s_au_to_size_kb)/sizeof(s_au_to_size_kb[0]) == 16, "invalid number of elements in s_au_to_size_kb");
  384. esp_err_t sdmmc_decode_ssr(uint32_t *raw_ssr, sdmmc_ssr_t* out_ssr)
  385. {
  386. uint32_t ssr[(SD_SSR_SIZE/sizeof(uint32_t))] = { 0 };
  387. size_t j = (SD_SSR_SIZE/sizeof(uint32_t) - 1);
  388. for(size_t i = 0; i < (SD_SSR_SIZE/sizeof(uint32_t)); i++) {
  389. ssr[j - i] = __builtin_bswap32(raw_ssr[i]);
  390. }
  391. out_ssr->cur_bus_width = SSR_DAT_BUS_WIDTH(ssr);
  392. out_ssr->discard_support = SSR_DISCARD_SUPPORT(ssr);
  393. out_ssr->fule_support = SSR_FULE_SUPPORT(ssr);
  394. uint32_t au = SSR_AU_SIZE(ssr);
  395. out_ssr->alloc_unit_kb = s_au_to_size_kb[au];
  396. out_ssr->erase_timeout = SSR_ERASE_TIMEOUT(ssr);
  397. out_ssr->erase_size_au = SSR_ERASE_SIZE(ssr);
  398. out_ssr->erase_offset = SSR_ERASE_OFFSET(ssr);
  399. return ESP_OK;
  400. }
  401. uint32_t sdmmc_sd_get_erase_timeout_ms(const sdmmc_card_t* card, int arg, size_t erase_size_kb)
  402. {
  403. if (arg == SDMMC_SD_DISCARD_ARG) {
  404. return SDMMC_SD_DISCARD_TIMEOUT;
  405. } else if (arg == SDMMC_SD_ERASE_ARG) {
  406. if (card->ssr.alloc_unit_kb != 0 &&
  407. card->ssr.erase_size_au != 0 &&
  408. card->ssr.erase_timeout != 0 &&
  409. card->ssr.erase_offset != 0) {
  410. /* Card supports erase timeout estimation. See the erase timeout equation in SD spec. */
  411. uint32_t timeout_sec = card->ssr.erase_offset +
  412. card->ssr.erase_timeout * (erase_size_kb + card->ssr.alloc_unit_kb - 1) /
  413. (card->ssr.erase_size_au * card->ssr.alloc_unit_kb);
  414. ESP_LOGD(TAG, "%s: erase timeout %" PRIu32 " s (erasing %" PRIu32 " kB, ES=%" PRIu32 ", ET=%" PRIu32 ", EO=%" PRIu32 ", AU=%" PRIu32 " kB)",
  415. __func__, timeout_sec, (uint32_t) erase_size_kb, (uint32_t) card->ssr.erase_size_au,
  416. (uint32_t) card->ssr.erase_timeout, (uint32_t) card->ssr.erase_offset, (uint32_t) card->ssr.alloc_unit_kb);
  417. return timeout_sec * 1000;
  418. } else {
  419. uint32_t timeout_ms = SDMMC_SD_DISCARD_TIMEOUT * erase_size_kb / card->csd.sector_size;
  420. timeout_ms = MAX(1000, timeout_ms);
  421. ESP_LOGD(TAG, "%s: erase timeout %" PRIu32 " s (erasing %" PRIu32 " kB, %" PRIu32 " ms per sector)",
  422. __func__, (uint32_t) (timeout_ms / 1000), (uint32_t) erase_size_kb, (uint32_t) SDMMC_SD_DISCARD_TIMEOUT);
  423. return timeout_ms;
  424. }
  425. } else {
  426. assert(false && "unexpected SD erase argument");
  427. return 0;
  428. }
  429. }