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 <unistd.h>
19 #include "sdmmc_common.h"
20 
21 static const char* TAG = "sdmmc_mmc";
22 
23 
sdmmc_init_mmc_read_ext_csd(sdmmc_card_t * card)24 esp_err_t sdmmc_init_mmc_read_ext_csd(sdmmc_card_t* card)
25 {
26     int card_type;
27     esp_err_t err = ESP_OK;
28 
29     uint8_t* ext_csd = heap_caps_malloc(EXT_CSD_MMC_SIZE, MALLOC_CAP_DMA);
30     if (!ext_csd) {
31         ESP_LOGE(TAG, "%s: could not allocate ext_csd", __func__);
32         return ESP_ERR_NO_MEM;
33     }
34 
35     uint32_t sectors = 0;
36 
37     ESP_LOGD(TAG, "MMC version: %d", card->csd.mmc_ver);
38     if (card->csd.mmc_ver < MMC_CSD_MMCVER_4_0) {
39         err = ESP_ERR_NOT_SUPPORTED;
40         goto out;
41     }
42 
43     /* read EXT_CSD */
44     err = sdmmc_mmc_send_ext_csd_data(card, ext_csd, EXT_CSD_MMC_SIZE);
45     if (err != ESP_OK) {
46         ESP_LOGE(TAG, "%s: send_ext_csd_data error 0x%x", __func__, err);
47         goto out;
48     }
49     card_type = ext_csd[EXT_CSD_CARD_TYPE];
50 
51     card->is_ddr = 0;
52     if (card_type & EXT_CSD_CARD_TYPE_F_52M_1_8V) {
53         card->max_freq_khz = SDMMC_FREQ_52M;
54         if ((card->host.flags & SDMMC_HOST_FLAG_DDR) &&
55                 card->host.max_freq_khz >= SDMMC_FREQ_26M &&
56                 card->host.get_bus_width(card->host.slot) == 4) {
57             ESP_LOGD(TAG, "card and host support DDR mode");
58             card->is_ddr = 1;
59         }
60     } else if (card_type & EXT_CSD_CARD_TYPE_F_52M) {
61         card->max_freq_khz = SDMMC_FREQ_52M;
62     } else if (card_type & EXT_CSD_CARD_TYPE_F_26M) {
63         card->max_freq_khz = SDMMC_FREQ_26M;
64     } else {
65         ESP_LOGW(TAG, "%s: unknown CARD_TYPE 0x%x", __func__, card_type);
66     }
67     /* For MMC cards, use speed value from EXT_CSD */
68     card->csd.tr_speed = card->max_freq_khz * 1000;
69     ESP_LOGD(TAG, "MMC card type %d, max_freq_khz=%d, is_ddr=%d", card_type, card->max_freq_khz, card->is_ddr);
70     card->max_freq_khz = MIN(card->max_freq_khz, card->host.max_freq_khz);
71 
72     if (card->host.flags & SDMMC_HOST_FLAG_8BIT) {
73         card->ext_csd.power_class = ext_csd[(card->max_freq_khz > SDMMC_FREQ_26M) ?
74                 EXT_CSD_PWR_CL_52_360 : EXT_CSD_PWR_CL_26_360] >> 4;
75         card->log_bus_width = 3;
76     } else if (card->host.flags & SDMMC_HOST_FLAG_4BIT) {
77         card->ext_csd.power_class = ext_csd[(card->max_freq_khz > SDMMC_FREQ_26M) ?
78                 EXT_CSD_PWR_CL_52_360 : EXT_CSD_PWR_CL_26_360] & 0x0f;
79         card->log_bus_width = 2;
80     } else {
81         card->ext_csd.power_class = 0; //card must be able to do full rate at powerclass 0 in 1-bit mode
82         card->log_bus_width = 0;
83     }
84 
85     sectors = ( ext_csd[EXT_CSD_SEC_COUNT + 0] << 0 )
86         | ( ext_csd[EXT_CSD_SEC_COUNT + 1] << 8 )
87         | ( ext_csd[EXT_CSD_SEC_COUNT + 2] << 16 )
88         | ( ext_csd[EXT_CSD_SEC_COUNT + 3] << 24 );
89 
90     if (sectors > (2u * 1024 * 1024 * 1024) / 512) {
91         card->csd.capacity = sectors;
92     }
93 
94 out:
95     free(ext_csd);
96     return err;
97 }
98 
sdmmc_init_mmc_bus_width(sdmmc_card_t * card)99 esp_err_t sdmmc_init_mmc_bus_width(sdmmc_card_t* card)
100 {
101     esp_err_t err;
102     if (card->ext_csd.power_class != 0) {
103         err = sdmmc_mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
104                 EXT_CSD_POWER_CLASS, card->ext_csd.power_class);
105         if (err != ESP_OK) {
106             ESP_LOGE(TAG, "%s: can't change power class (%d bit), 0x%x"
107                     , __func__, card->ext_csd.power_class, err);
108             return err;
109         }
110     }
111 
112     if (card->log_bus_width > 0) {
113         int csd_bus_width_value = EXT_CSD_BUS_WIDTH_1;
114         int bus_width = 1;
115         if (card->log_bus_width == 2) {
116             if (card->is_ddr) {
117                 csd_bus_width_value = EXT_CSD_BUS_WIDTH_4_DDR;
118             } else {
119                 csd_bus_width_value = EXT_CSD_BUS_WIDTH_4;
120             }
121             bus_width = 4;
122         } else if (card->log_bus_width == 3) {
123             if (card->is_ddr) {
124                 csd_bus_width_value = EXT_CSD_BUS_WIDTH_8_DDR;
125             } else {
126                 csd_bus_width_value = EXT_CSD_BUS_WIDTH_8;
127             }
128             bus_width = 8;
129         }
130         err = sdmmc_mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
131                 EXT_CSD_BUS_WIDTH, csd_bus_width_value);
132         if (err != ESP_OK) {
133             ESP_LOGE(TAG, "%s: can't change bus width (%d bit), 0x%x",
134                     __func__, bus_width, err);
135             return err;
136         }
137     }
138     return ESP_OK;
139 }
140 
sdmmc_mmc_enable_hs_mode(sdmmc_card_t * card)141 esp_err_t sdmmc_mmc_enable_hs_mode(sdmmc_card_t* card)
142 {
143     esp_err_t err;
144     if (card->max_freq_khz > SDMMC_FREQ_26M) {
145         /* switch to high speed timing */
146         err = sdmmc_mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
147                 EXT_CSD_HS_TIMING, EXT_CSD_HS_TIMING_HS);
148         if (err != ESP_OK) {
149             ESP_LOGE(TAG, "%s: mmc_switch EXT_CSD_HS_TIMING_HS error 0x%x",
150                     __func__, err);
151             return err;
152         }
153     }
154     return ESP_OK;
155 }
156 
sdmmc_mmc_decode_cid(int mmc_ver,sdmmc_response_t resp,sdmmc_cid_t * out_cid)157 esp_err_t sdmmc_mmc_decode_cid(int mmc_ver, sdmmc_response_t resp, sdmmc_cid_t* out_cid)
158 {
159     if (mmc_ver == MMC_CSD_MMCVER_1_0 ||
160             mmc_ver == MMC_CSD_MMCVER_1_4) {
161         out_cid->mfg_id = MMC_CID_MID_V1(resp);
162         out_cid->oem_id = 0;
163         MMC_CID_PNM_V1_CPY(resp, out_cid->name);
164         out_cid->revision = MMC_CID_REV_V1(resp);
165         out_cid->serial = MMC_CID_PSN_V1(resp);
166         out_cid->date = MMC_CID_MDT_V1(resp);
167     } else if (mmc_ver == MMC_CSD_MMCVER_2_0 ||
168             mmc_ver == MMC_CSD_MMCVER_3_1 ||
169             mmc_ver == MMC_CSD_MMCVER_4_0) {
170         out_cid->mfg_id = MMC_CID_MID_V2(resp);
171         out_cid->oem_id = MMC_CID_OID_V2(resp);
172         MMC_CID_PNM_V1_CPY(resp, out_cid->name);
173         out_cid->revision = 0;
174         out_cid->serial = MMC_CID_PSN_V1(resp);
175         out_cid->date = 0;
176     }
177     return ESP_OK;
178 }
179 
sdmmc_mmc_decode_csd(sdmmc_response_t response,sdmmc_csd_t * out_csd)180 esp_err_t sdmmc_mmc_decode_csd(sdmmc_response_t response, sdmmc_csd_t* out_csd)
181 {
182     out_csd->csd_ver = MMC_CSD_CSDVER(response);
183     if (out_csd->csd_ver == MMC_CSD_CSDVER_1_0 ||
184             out_csd->csd_ver == MMC_CSD_CSDVER_2_0 ||
185             out_csd->csd_ver == MMC_CSD_CSDVER_EXT_CSD) {
186         out_csd->mmc_ver = MMC_CSD_MMCVER(response);
187         out_csd->capacity = MMC_CSD_CAPACITY(response);
188         out_csd->read_block_len = MMC_CSD_READ_BL_LEN(response);
189     } else {
190         ESP_LOGE(TAG, "unknown MMC CSD structure version 0x%x\n", out_csd->csd_ver);
191         return 1;
192     }
193     int read_bl_size = 1 << out_csd->read_block_len;
194     out_csd->sector_size = MIN(read_bl_size, 512);
195     if (out_csd->sector_size < read_bl_size) {
196         out_csd->capacity *= read_bl_size / out_csd->sector_size;
197     }
198     /* tr_speed will be determined when reading CXD */
199     out_csd->tr_speed = 0;
200     return ESP_OK;
201 }
202 
sdmmc_mmc_send_ext_csd_data(sdmmc_card_t * card,void * out_data,size_t datalen)203 esp_err_t sdmmc_mmc_send_ext_csd_data(sdmmc_card_t* card, void *out_data, size_t datalen)
204 {
205     assert(esp_ptr_dma_capable(out_data));
206     sdmmc_command_t cmd = {
207         .data = out_data,
208         .datalen = datalen,
209         .blklen = datalen,
210         .opcode = MMC_SEND_EXT_CSD,
211         .arg = 0,
212         .flags = SCF_CMD_ADTC | SCF_RSP_R1 | SCF_CMD_READ
213     };
214     return sdmmc_send_cmd(card, &cmd);
215 }
216 
sdmmc_mmc_switch(sdmmc_card_t * card,uint8_t set,uint8_t index,uint8_t value)217 esp_err_t sdmmc_mmc_switch(sdmmc_card_t* card, uint8_t set, uint8_t index, uint8_t value)
218 {
219     sdmmc_command_t cmd = {
220             .opcode = MMC_SWITCH,
221             .arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) | (index << 16) | (value << 8) | set,
222             .flags = SCF_RSP_R1B | SCF_CMD_AC | SCF_WAIT_BUSY,
223     };
224     esp_err_t err = sdmmc_send_cmd(card, &cmd);
225     if (err == ESP_OK) {
226         //check response bit to see that switch was accepted
227         if (MMC_R1(cmd.response) & MMC_R1_SWITCH_ERROR)
228             err = ESP_ERR_INVALID_RESPONSE;
229     }
230 
231     return err;
232 }
233 
sdmmc_init_mmc_check_csd(sdmmc_card_t * card)234 esp_err_t sdmmc_init_mmc_check_csd(sdmmc_card_t* card)
235 {
236     esp_err_t err;
237     assert(card->is_mem == 1);
238     assert(card->rca != 0);
239     //The card will not respond to send_csd command in the transfer state.
240     //Deselect it first.
241     err = sdmmc_send_cmd_select_card(card, 0);
242     if (err != ESP_OK) {
243         ESP_LOGE(TAG, "%s: select_card returned 0x%x", __func__, err);
244         return err;
245     }
246 
247     sdmmc_csd_t csd;
248     /* Get the contents of CSD register to verify the communication over CMD line
249        is OK. */
250     err = sdmmc_send_cmd_send_csd(card, &csd);
251     if (err != ESP_OK) {
252         ESP_LOGE(TAG, "%s: send_csd returned 0x%x", __func__, err);
253         return err;
254     }
255 
256     //Select the card again
257     err = sdmmc_send_cmd_select_card(card, card->rca);
258     if (err != ESP_OK) {
259         ESP_LOGE(TAG, "%s: select_card returned 0x%x", __func__, err);
260         return err;
261     }
262     return ESP_OK;
263 }
264