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