sdmmc_sd.c 15 KB

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