sdmmc_sd.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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_bus_width(sdmmc_card_t* card)
  77. {
  78. int width = 1;
  79. if (card->log_bus_width == 2) {
  80. width = 4;
  81. } else if (card->log_bus_width == 3) {
  82. width = 8;
  83. }
  84. esp_err_t err = sdmmc_send_cmd_set_bus_width(card, width);
  85. if (err != ESP_OK) {
  86. ESP_LOGE(TAG, "set_bus_width failed (0x%x)", err);
  87. return err;
  88. }
  89. return ESP_OK;
  90. }
  91. esp_err_t sdmmc_init_sd_wait_data_ready(sdmmc_card_t* card)
  92. {
  93. /* Wait for the card to be ready for data transfers */
  94. uint32_t status = 0;
  95. uint32_t count = 0;
  96. while (!host_is_spi(card) && !(status & MMC_R1_READY_FOR_DATA)) {
  97. // TODO: add some timeout here
  98. esp_err_t err = sdmmc_send_cmd_send_status(card, &status);
  99. if (err != ESP_OK) {
  100. return err;
  101. }
  102. if (++count % 16 == 0) {
  103. ESP_LOGV(TAG, "waiting for card to become ready (%d)", count);
  104. }
  105. }
  106. return ESP_OK;
  107. }
  108. esp_err_t sdmmc_send_cmd_switch_func(sdmmc_card_t* card,
  109. uint32_t mode, uint32_t group, uint32_t function,
  110. sdmmc_switch_func_rsp_t* resp)
  111. {
  112. if (card->scr.sd_spec < SCR_SD_SPEC_VER_1_10 ||
  113. ((card->csd.card_command_class & SD_CSD_CCC_SWITCH) == 0)) {
  114. return ESP_ERR_NOT_SUPPORTED;
  115. }
  116. if (group == 0 ||
  117. group > SD_SFUNC_GROUP_MAX ||
  118. function > SD_SFUNC_FUNC_MAX) {
  119. return ESP_ERR_INVALID_ARG;
  120. }
  121. if (mode > 1) {
  122. return ESP_ERR_INVALID_ARG;
  123. }
  124. uint32_t group_shift = (group - 1) << 2;
  125. /* all functions which should not be affected are set to 0xf (no change) */
  126. uint32_t other_func_mask = (0x00ffffff & ~(0xf << group_shift));
  127. uint32_t func_val = (function << group_shift) | other_func_mask;
  128. sdmmc_command_t cmd = {
  129. .opcode = MMC_SWITCH,
  130. .flags = SCF_CMD_ADTC | SCF_CMD_READ | SCF_RSP_R1,
  131. .blklen = sizeof(sdmmc_switch_func_rsp_t),
  132. .data = resp->data,
  133. .datalen = sizeof(sdmmc_switch_func_rsp_t),
  134. .arg = (!!mode << 31) | func_val
  135. };
  136. esp_err_t err = sdmmc_send_cmd(card, &cmd);
  137. if (err != ESP_OK) {
  138. ESP_LOGE(TAG, "%s: sdmmc_send_cmd returned 0x%x", __func__, err);
  139. return err;
  140. }
  141. sdmmc_flip_byte_order(resp->data, sizeof(sdmmc_switch_func_rsp_t));
  142. uint32_t resp_ver = SD_SFUNC_VER(resp->data);
  143. if (resp_ver == 0) {
  144. /* busy response is never sent */
  145. } else if (resp_ver == 1) {
  146. if (SD_SFUNC_BUSY(resp->data, group) & (1 << function)) {
  147. ESP_LOGD(TAG, "%s: response indicates function %d:%d is busy",
  148. __func__, group, function);
  149. return ESP_ERR_INVALID_STATE;
  150. }
  151. } else {
  152. ESP_LOGD(TAG, "%s: got an invalid version of SWITCH_FUNC response: 0x%02x",
  153. __func__, resp_ver);
  154. return ESP_ERR_INVALID_RESPONSE;
  155. }
  156. return ESP_OK;
  157. }
  158. esp_err_t sdmmc_enable_hs_mode(sdmmc_card_t* card)
  159. {
  160. /* This will determine if the card supports SWITCH_FUNC command,
  161. * and high speed mode. If the cards supports both, this will enable
  162. * high speed mode at the card side.
  163. */
  164. if (card->scr.sd_spec < SCR_SD_SPEC_VER_1_10 ||
  165. ((card->csd.card_command_class & SD_CSD_CCC_SWITCH) == 0)) {
  166. return ESP_ERR_NOT_SUPPORTED;
  167. }
  168. sdmmc_switch_func_rsp_t* response = (sdmmc_switch_func_rsp_t*)
  169. heap_caps_malloc(sizeof(*response), MALLOC_CAP_DMA);
  170. if (response == NULL) {
  171. return ESP_ERR_NO_MEM;
  172. }
  173. esp_err_t err = sdmmc_send_cmd_switch_func(card, 0, SD_ACCESS_MODE, 0, response);
  174. if (err != ESP_OK) {
  175. ESP_LOGD(TAG, "%s: sdmmc_send_cmd_switch_func (1) returned 0x%x", __func__, err);
  176. goto out;
  177. }
  178. uint32_t supported_mask = SD_SFUNC_SUPPORTED(response->data, 1);
  179. if ((supported_mask & BIT(SD_ACCESS_MODE_SDR25)) == 0) {
  180. err = ESP_ERR_NOT_SUPPORTED;
  181. goto out;
  182. }
  183. err = sdmmc_send_cmd_switch_func(card, 1, SD_ACCESS_MODE, SD_ACCESS_MODE_SDR25, response);
  184. if (err != ESP_OK) {
  185. ESP_LOGD(TAG, "%s: sdmmc_send_cmd_switch_func (2) returned 0x%x", __func__, err);
  186. goto out;
  187. }
  188. out:
  189. free(response);
  190. return err;
  191. }
  192. esp_err_t sdmmc_enable_hs_mode_and_check(sdmmc_card_t* card)
  193. {
  194. /* All cards should support at least default speed */
  195. card->max_freq_khz = SDMMC_FREQ_DEFAULT;
  196. if (card->host.max_freq_khz <= card->max_freq_khz) {
  197. /* Host is configured to use low frequency, don't attempt to switch */
  198. card->max_freq_khz = card->host.max_freq_khz;
  199. return ESP_OK;
  200. }
  201. /* Try to enabled HS mode */
  202. esp_err_t err = sdmmc_enable_hs_mode(card);
  203. if (err != ESP_OK) {
  204. return err;
  205. }
  206. /* HS mode has been enabled on the card.
  207. * Read CSD again, it should now indicate that the card supports
  208. * 50MHz clock.
  209. * Since SEND_CSD is allowed only in standby mode, and the card is currently in data transfer
  210. * mode, deselect the card first, then get the CSD, then select the card again. This step is
  211. * not required in SPI mode, since CMD7 (select_card) is not supported.
  212. */
  213. const bool is_spi = host_is_spi(card);
  214. if (!is_spi) {
  215. err = sdmmc_send_cmd_select_card(card, 0);
  216. if (err != ESP_OK) {
  217. ESP_LOGE(TAG, "%s: select_card (1) returned 0x%x", __func__, err);
  218. return err;
  219. }
  220. }
  221. err = sdmmc_send_cmd_send_csd(card, &card->csd);
  222. if (err != ESP_OK) {
  223. ESP_LOGE(TAG, "%s: send_csd returned 0x%x", __func__, err);
  224. return err;
  225. }
  226. if (!is_spi) {
  227. err = sdmmc_send_cmd_select_card(card, card->rca);
  228. if (err != ESP_OK) {
  229. ESP_LOGE(TAG, "%s: select_card (2) returned 0x%x", __func__, err);
  230. return err;
  231. }
  232. }
  233. if (card->csd.tr_speed != 50000000) {
  234. ESP_LOGW(TAG, "unexpected: after enabling HS mode, tr_speed=%d", card->csd.tr_speed);
  235. return ESP_ERR_NOT_SUPPORTED;
  236. }
  237. card->max_freq_khz = SDMMC_FREQ_HIGHSPEED;
  238. return ESP_OK;
  239. }
  240. esp_err_t sdmmc_check_scr(sdmmc_card_t* card)
  241. {
  242. /* If frequency switch has been performed, read SCR register one more time
  243. * and compare the result with the previous one. Use this simple check as
  244. * an indicator of potential signal integrity issues.
  245. */
  246. sdmmc_scr_t scr_tmp;
  247. esp_err_t err = sdmmc_send_cmd_send_scr(card, &scr_tmp);
  248. if (err != ESP_OK) {
  249. ESP_LOGE(TAG, "%s: send_scr returned 0x%x", __func__, err);
  250. return err;
  251. }
  252. if (memcmp(&card->scr, &scr_tmp, sizeof(scr_tmp)) != 0) {
  253. ESP_LOGE(TAG, "got corrupted data after increasing clock frequency");
  254. return ESP_ERR_INVALID_RESPONSE;
  255. }
  256. return ESP_OK;
  257. }
  258. esp_err_t sdmmc_init_spi_crc(sdmmc_card_t* card)
  259. {
  260. /* In SD mode, CRC checks of data transfers are mandatory and performed
  261. * by the hardware. In SPI mode, CRC16 of data transfers is optional and
  262. * needs to be enabled.
  263. */
  264. assert(host_is_spi(card));
  265. esp_err_t err = sdmmc_send_cmd_crc_on_off(card, true);
  266. if (err != ESP_OK) {
  267. ESP_LOGE(TAG, "%s: sdmmc_send_cmd_crc_on_off returned 0x%x", __func__, err);
  268. return err;
  269. }
  270. return ESP_OK;
  271. }
  272. esp_err_t sdmmc_decode_cid(sdmmc_response_t resp, sdmmc_cid_t* out_cid)
  273. {
  274. out_cid->mfg_id = SD_CID_MID(resp);
  275. out_cid->oem_id = SD_CID_OID(resp);
  276. SD_CID_PNM_CPY(resp, out_cid->name);
  277. out_cid->revision = SD_CID_REV(resp);
  278. out_cid->serial = SD_CID_PSN(resp);
  279. out_cid->date = SD_CID_MDT(resp);
  280. return ESP_OK;
  281. }
  282. esp_err_t sdmmc_decode_csd(sdmmc_response_t response, sdmmc_csd_t* out_csd)
  283. {
  284. out_csd->csd_ver = SD_CSD_CSDVER(response);
  285. switch (out_csd->csd_ver) {
  286. case SD_CSD_CSDVER_2_0:
  287. out_csd->capacity = SD_CSD_V2_CAPACITY(response);
  288. out_csd->read_block_len = SD_CSD_V2_BL_LEN;
  289. break;
  290. case SD_CSD_CSDVER_1_0:
  291. out_csd->capacity = SD_CSD_CAPACITY(response);
  292. out_csd->read_block_len = SD_CSD_READ_BL_LEN(response);
  293. break;
  294. default:
  295. ESP_LOGE(TAG, "unknown SD CSD structure version 0x%x", out_csd->csd_ver);
  296. return ESP_ERR_NOT_SUPPORTED;
  297. }
  298. out_csd->card_command_class = SD_CSD_CCC(response);
  299. int read_bl_size = 1 << out_csd->read_block_len;
  300. out_csd->sector_size = MIN(read_bl_size, 512);
  301. if (out_csd->sector_size < read_bl_size) {
  302. out_csd->capacity *= read_bl_size / out_csd->sector_size;
  303. }
  304. int speed = SD_CSD_SPEED(response);
  305. if (speed == SD_CSD_SPEED_50_MHZ) {
  306. out_csd->tr_speed = 50000000;
  307. } else {
  308. out_csd->tr_speed = 25000000;
  309. }
  310. return ESP_OK;
  311. }
  312. esp_err_t sdmmc_decode_scr(uint32_t *raw_scr, sdmmc_scr_t* out_scr)
  313. {
  314. sdmmc_response_t resp = { 0 };
  315. resp[1] = __builtin_bswap32(raw_scr[0]);
  316. resp[0] = __builtin_bswap32(raw_scr[1]);
  317. int ver = SCR_STRUCTURE(resp);
  318. if (ver != 0) {
  319. return ESP_ERR_NOT_SUPPORTED;
  320. }
  321. out_scr->sd_spec = SCR_SD_SPEC(resp);
  322. out_scr->bus_width = SCR_SD_BUS_WIDTHS(resp);
  323. return ESP_OK;
  324. }