1 /*
2  * Copyright (C) 2017 ALLWINNERTECH TECHNOLOGY CO., LTD. All rights reserved.
3  *
4  *  Redistribution and use in source and binary forms, with or without
5  *  modification, are permitted provided that the following conditions
6  *  are met:
7  *    1. Redistributions of source code must retain the above copyright
8  *       notice, this list of conditions and the following disclaimer.
9  *    2. Redistributions in binary form must reproduce the above copyright
10  *       notice, this list of conditions and the following disclaimer in the
11  *       documentation and/or other materials provided with the
12  *       distribution.
13  *    3. Neither the name of ALLWINNERTECH TECHNOLOGY CO., LTD. nor the names of
14  *       its contributors may be used to endorse or promote products derived
15  *       from this software without specific prior written permission.
16  *
17  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20  *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21  *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22  *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23  *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27  *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 #include "sunxi_hal_common.h"
30 #include "hal_base.h"
31 
32 #include "sys/endian.h"
33 
34 #include "hal_sdhost.h"
35 #include "sdmmc.h"
36 #include "_sdhost.h"
37 #include "_core.h"
38 #ifdef CONFIG_USE_MMC
39 #include "_mmc.h"
40 #endif
41 #include "_sd.h"
42 #include "_sd_define.h"
43 
44 
45 #ifdef CONFIG_USE_SD
46 static const unsigned int tran_exp[] = { /* about KB/S */
47     10000,      100000,     1000000,    10000000,
48     0,      0,      0,      0
49 };
50 
51 static const unsigned char tran_mant[] = { /* time value*10 */
52     0,  10, 12, 13, 15, 20, 25, 30,
53     35, 40, 45, 50, 55, 60, 70, 80,
54 };
55 /*
56 static const unsigned int tacc_exp[] = {
57     1,  10, 100,    1000,   10000,  100000, 1000000, 10000000,
58 };
59 
60 static const unsigned int tacc_mant[] = {
61     0,  10, 12, 13, 15, 20, 25, 30,
62     35, 40, 45, 50, 55, 60, 70, 80,
63 };
64 */
65 
mmc_send_app_op_cond(struct mmc_host * host,uint32_t ocr,uint32_t * rocr)66 static int32_t mmc_send_app_op_cond(struct mmc_host *host, uint32_t ocr, uint32_t *rocr)
67 {
68     struct mmc_command cmd = {0};
69     int32_t i, err;
70 
71     if (!host) {
72         SDC_LOGE_RAW(ROM_ERR_MASK, "%s,%d err", __func__, __LINE__);
73         return -1;
74     }
75 
76     cmd.opcode = SD_APP_OP_COND;
77     cmd.arg = ocr;
78     cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R3 | MMC_CMD_BCR;
79 
80     for (i = 100; i; i--) {
81         err = mmc_wait_for_app_cmd(host, NULL, &cmd);
82         if (err) {
83             // printf("%s,%d\n",__FUNCTION__,__LINE__);
84             break;
85         }
86 
87         /* otherwise wait until reset completes */
88         if (cmd.resp[0] & MMC_CARD_BUSY){
89             // printf("%s,%d\n",__FUNCTION__,__LINE__);
90             break;
91         }
92 
93         if (host->ocr_avail & MMC_VDD_165_195)
94             cmd.arg = 0x41000000 | (cmd.resp[0] & 0xFF8000);
95         else
96             cmd.arg = 0x40000000 | (cmd.resp[0] & 0xFF8000);
97 
98         err = -1;
99 
100         mmc_mdelay(20);
101 //      printf("%s,%d %d\n",__FUNCTION__,__LINE__,i);
102 
103     }
104 
105     if (rocr)
106         *rocr = cmd.resp[0];
107 
108     if(err)
109         printf("%s,%d %ld\n",__FUNCTION__,__LINE__, HAL_PR_SZ_L(i));
110 
111     return err;
112 }
113 
mmc_app_sd_status(struct mmc_card * card,uint8_t * ssr)114 int32_t mmc_app_sd_status(struct mmc_card *card, uint8_t *ssr)
115 {
116     struct mmc_request mrq;
117     struct mmc_command cmd = {0};
118     struct mmc_data data = {0};
119     struct scatterlist sg;
120 
121     if (!ssr) {
122         SD_LOGE("%s,%d err", __func__, __LINE__);
123         return -1;
124     }
125 
126     if (mmc_app_cmd(card->host, card)) {
127         return -1;
128     }
129 
130     mrq.cmd = &cmd;
131     mrq.data = &data;
132 
133     cmd.opcode = SD_APP_SD_STATUS;
134     cmd.arg = 0;
135     cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_ADTC;
136 
137     data.blksz = 64;
138     data.blocks = 1;
139     data.flags = MMC_DATA_READ;
140     data.sg = &sg;
141     data.sg_len = 1;
142 
143     sg.len = 64;
144     sg.buffer = ssr;
145 
146     if (mmc_wait_for_req(card->host, &mrq)) {
147         return -1;
148     }
149 
150     SD_LOGN("card raw SD status:\n");
151     sd_hex_dump_bytes((void *)ssr, 64);
152 
153     return 0;
154 }
155 
mmc_app_send_scr(struct mmc_card * card,uint32_t * raw_scr)156 int32_t mmc_app_send_scr(struct mmc_card *card, uint32_t *raw_scr)
157 {
158     struct mmc_command cmd = {0};
159     struct mmc_data data = {0};
160     struct mmc_request mrq;
161     struct scatterlist sg;
162 
163     if (mmc_app_cmd(card->host, card)) {
164         return -1;
165     }
166 
167     mrq.cmd = &cmd;
168     mrq.data = &data;
169 
170     cmd.opcode = SD_APP_SEND_SCR;
171     cmd.arg = 0;
172     cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
173 
174 
175     data.blksz = 8;
176     data.blocks = 1;
177     data.flags = MMC_DATA_READ;
178     data.sg = &sg;
179     data.sg_len = 1;
180 
181     sg.len = 8;
182     sg.buffer = (void *)raw_scr;
183 
184     /* get scr, 8 bytes */
185     if (mmc_wait_for_req(card->host, &mrq)) {
186         return -1;
187     }
188 
189     raw_scr[0] = be32_to_cpu(raw_scr[0]);
190     raw_scr[1] = be32_to_cpu(raw_scr[1]);
191 
192     return 0;
193 }
194 
195 #ifdef SD_SUPPORT_VERSION3
mmc_switch_to_1v8(struct mmc_card * card)196 int32_t mmc_switch_to_1v8(struct mmc_card *card)
197 {
198     struct mmc_command cmd = {0};
199 
200     cmd.opcode = SD_SWITCH_VOLTAGE;
201     cmd.arg = 0;
202     cmd.vol_switch  = 1;
203     cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
204 
205     if (mmc_wait_for_cmd(card->host, &cmd)) {
206         SD_LOGW("sd switch 1v8 failed\n");
207         return -1;
208     }
209 
210     return 0;
211 }
212 #endif
213 
214 /*
215  * Given the decoded CSD structure, decode the raw CID to our CID structure.
216  */
mmc_decode_cid(struct mmc_card * card,uint32_t * resp)217 void mmc_decode_cid(struct mmc_card *card, uint32_t *resp)
218 {
219     SDC_Memset(&card->cid, 0, sizeof(struct mmc_cid));
220 
221     /*
222      * SD doesn't currently have a version field so we will
223      * have to assume we can parse this.
224      */
225     card->cid.manfid = UNSTUFF_BITS(resp, 120, 8);
226     card->cid.oemid = UNSTUFF_BITS(resp, 104, 8);
227     card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
228     card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
229     card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
230     card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
231     card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
232     card->cid.hwrev = UNSTUFF_BITS(resp, 60, 4);
233     card->cid.fwrev = UNSTUFF_BITS(resp, 56, 4);
234     card->cid.serial = UNSTUFF_BITS(resp, 24, 32);
235     card->cid.year = UNSTUFF_BITS(resp, 12, 8);
236     card->cid.month = UNSTUFF_BITS(resp, 8, 4);
237 
238     card->cid.year += 2000; /* SD cards year offset */
239 }
240 
mmc_send_cid(struct mmc_card * card)241 int32_t mmc_send_cid(struct mmc_card *card)
242 {
243     struct mmc_command cmd = {0};
244     uint32_t cid[4] = {0};
245 
246     cmd.opcode = MMC_SEND_CID;
247     cmd.arg = card->rca << 16;
248     cmd.flags = MMC_RSP_R2 | MMC_CMD_AC;
249 
250     if (mmc_wait_for_cmd(card->host, &cmd)) {
251         return -1;
252     }
253 
254     HAL_Memcpy((void *)cid, (void *)cmd.resp, 16);
255     SD_LOGN("card raw cid:\n");
256     sd_hex_dump_bytes((void *)cid, 16);
257 
258     mmc_decode_cid(card, cid);
259 
260     return 0;
261 }
262 
263 /*
264  * Given a 128-bit response, decode to our card CSD structure.
265  */
mmc_decode_csd(struct mmc_card * card,uint32_t * raw_csd)266 static int32_t mmc_decode_csd(struct mmc_card *card, uint32_t *raw_csd)
267 {
268     int32_t e, m, csd_struct;
269     uint32_t *resp = raw_csd;
270     struct mmc_csd *csd = &card->csd;
271 
272     csd_struct = UNSTUFF_BITS(resp, 126, 2);
273     card->csd.csd_ver = csd_struct;
274 
275     switch (csd_struct) {
276     case 0:
277         //m = UNSTUFF_BITS(resp, 115, 4);
278         //e = UNSTUFF_BITS(resp, 112, 3);
279         //csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10;
280         //csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100;
281 
282         m = UNSTUFF_BITS(resp, 99, 4);
283         e = UNSTUFF_BITS(resp, 96, 3);
284         csd->max_dtr = tran_exp[e] * tran_mant[m];
285         csd->cmdclass =  UNSTUFF_BITS(resp, 84, 12);
286 
287         m = UNSTUFF_BITS(resp, 80, 4);
288         csd->read_blk_len = 1 << m;
289 
290         e = UNSTUFF_BITS(resp, 47, 3);
291         m = UNSTUFF_BITS(resp, 62, 12);
292         csd->capacity = (1 + m) * (1 << (e + 2)) * csd->read_blk_len;
293         csd->capacity >>= 10; /* unit:KB */
294 
295         //csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4);
296         //csd->read_partial = UNSTUFF_BITS(resp, 79, 1);
297         //csd->write_misalign = UNSTUFF_BITS(resp, 78, 1);
298         //csd->read_misalign = UNSTUFF_BITS(resp, 77, 1);
299         //csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3);
300         //csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4);
301         //csd->write_partial = UNSTUFF_BITS(resp, 21, 1);
302 
303         //if (UNSTUFF_BITS(resp, 46, 1)) {
304         //  csd->erase_size = 1;
305         //} else if (csd->write_blkbits >= 9) {
306         //  csd->erase_size = UNSTUFF_BITS(resp, 39, 7) + 1;
307         //  csd->erase_size <<= csd->write_blkbits - 9;
308         //}
309         break;
310     case 1:
311         /*
312          * This is a block-addressed SDHC or SDXC card. Most
313          * interesting fields are unused and have fixed
314          * values. To avoid getting tripped by buggy cards,
315          * we assume those fixed values ourselves.
316          */
317         mmc_card_set_blockaddr(card);
318 
319         //csd->tacc_ns = 0; /* Unused */
320         //csd->tacc_clks = 0; /* Unused */
321 
322         m = UNSTUFF_BITS(resp, 99, 4);
323         e = UNSTUFF_BITS(resp, 96, 3);
324         csd->max_dtr = tran_exp[e] * tran_mant[m];
325         csd->cmdclass =  UNSTUFF_BITS(resp, 84, 12);
326         csd->read_blk_len = UNSTUFF_BITS(resp, 80, 4);
327 
328         m = UNSTUFF_BITS(resp, 48, 22);
329         csd->capacity = (1 + m) << 9;
330 
331         /* SDXC cards have a minimum C_SIZE of 0x00FFFF */
332         if (m >= 0xFFFF)
333             mmc_card_set_ext_capacity(card);
334 
335         //csd->read_blkbits = 9;
336         //csd->read_partial = 0;
337         //csd->write_misalign = 0;
338         //csd->read_misalign = 0;
339         //csd->r2w_factor = 4; /* Unused */
340         //csd->write_blkbits = 9;
341         //csd->write_partial = 0;
342         //csd->erase_size = 1;
343         break;
344     default:
345         SD_LOGE("%s: unrecognised CSD structure version %d\n",
346             __func__, (unsigned int)csd_struct);
347         return -1;
348     }
349 
350     //card->erase_size = csd->erase_size;
351     SD_LOGD("%s %d ca:%d\n", __func__, (unsigned int)csd_struct, (unsigned int)csd->capacity);
352 
353     return 0;
354 }
355 
356 #ifdef SD_SUPPORT_VERSION3
357 
sd_update_bus_speed_mode(struct mmc_card * card)358 static void sd_update_bus_speed_mode(struct mmc_card *card)
359 {
360     /*
361      * If the host doesn't support any of the UHS-I modes, fallback on
362      * default speed.
363      */
364     if (!(card->host->caps & (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 |
365         MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_DDR50))) {
366         card->sd_bus_speed = 0;
367         return;
368     }
369 
370     if ((card->host->caps & MMC_CAP_UHS_SDR104) &&
371         (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR104)) {
372         card->sd_bus_speed = UHS_SDR104_BUS_SPEED;
373     } else if ((card->host->caps & MMC_CAP_UHS_DDR50) &&
374                (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_DDR50)) {
375         card->sd_bus_speed = UHS_DDR50_BUS_SPEED;
376     } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
377                 MMC_CAP_UHS_SDR50)) && (card->sw_caps.sd3_bus_mode &
378                 SD_MODE_UHS_SDR50)) {
379         card->sd_bus_speed = UHS_SDR50_BUS_SPEED;
380     } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
381                 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR25)) &&
382                (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR25)) {
383         card->sd_bus_speed = UHS_SDR25_BUS_SPEED;
384     } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
385                 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR25 |
386                 MMC_CAP_UHS_SDR12)) && (card->sw_caps.sd3_bus_mode &
387                 SD_MODE_UHS_SDR12)) {
388         card->sd_bus_speed = UHS_SDR12_BUS_SPEED;
389     }
390 }
391 
sd_set_bus_speed_mode(struct mmc_card * card,int8_t * status)392 static int32_t sd_set_bus_speed_mode(struct mmc_card *card, int8_t *status)
393 {
394     int32_t err;
395     uint32_t timing = 0;
396 
397     switch (card->sd_bus_speed) {
398     case UHS_SDR104_BUS_SPEED:
399         timing = MMC_TIMING_UHS_SDR104;
400         card->sw_caps.uhs_max_dtr = UHS_SDR104_MAX_DTR;
401         break;
402     case UHS_DDR50_BUS_SPEED:
403         timing = MMC_TIMING_UHS_DDR50;
404         card->sw_caps.uhs_max_dtr = UHS_DDR50_MAX_DTR;
405         break;
406     case UHS_SDR50_BUS_SPEED:
407         timing = MMC_TIMING_UHS_SDR50;
408         card->sw_caps.uhs_max_dtr = UHS_SDR50_MAX_DTR;
409         break;
410     case UHS_SDR25_BUS_SPEED:
411         timing = MMC_TIMING_UHS_SDR25;
412         card->sw_caps.uhs_max_dtr = UHS_SDR25_MAX_DTR;
413         break;
414     case UHS_SDR12_BUS_SPEED:
415         timing = MMC_TIMING_UHS_SDR12;
416         card->sw_caps.uhs_max_dtr = UHS_SDR12_MAX_DTR;
417         break;
418     default:
419         return 0;
420     }
421 
422     err = mmc_sd_switch(card, 1, 0, card->sd_bus_speed, status);
423     if (err)
424         return err;
425 
426     if ((status[16] & 0xF) != card->sd_bus_speed)
427         SD_LOGW("%s: Problem setting bus speed mode!\n", __func__);
428     else {
429         mmc_set_timing(card->host, timing);
430         mmc_set_clock(card->host, card->sw_caps.uhs_max_dtr);
431     }
432 
433     return 0;
434 }
435 
sd_set_current_limit(struct mmc_card * card,int8_t * status)436 static int32_t sd_set_current_limit(struct mmc_card *card, int8_t *status)
437 {
438     int32_t current_limit = 0;
439     int32_t err;
440 
441     /*
442      * Current limit switch is only defined for SDR50, SDR104, and DDR50
443      * bus speed modes. For other bus speed modes, we set the default
444      * current limit of 200mA.
445      */
446     if ((card->sd_bus_speed == UHS_SDR50_BUS_SPEED) ||
447         (card->sd_bus_speed == UHS_SDR104_BUS_SPEED) ||
448         (card->sd_bus_speed == UHS_DDR50_BUS_SPEED)) {
449         if (card->host->caps & MMC_CAP_MAX_CURRENT_800) {
450             if (card->sw_caps.sd3_curr_limit & SD_MAX_CURRENT_800)
451                 current_limit = SD_SET_CURRENT_LIMIT_800;
452             else if (card->sw_caps.sd3_curr_limit &
453                     SD_MAX_CURRENT_600)
454                 current_limit = SD_SET_CURRENT_LIMIT_600;
455             else if (card->sw_caps.sd3_curr_limit &
456                     SD_MAX_CURRENT_400)
457                 current_limit = SD_SET_CURRENT_LIMIT_400;
458             else if (card->sw_caps.sd3_curr_limit &
459                     SD_MAX_CURRENT_200)
460                 current_limit = SD_SET_CURRENT_LIMIT_200;
461         } else if (card->host->caps & MMC_CAP_MAX_CURRENT_600) {
462             if (card->sw_caps.sd3_curr_limit & SD_MAX_CURRENT_600)
463                 current_limit = SD_SET_CURRENT_LIMIT_600;
464             else if (card->sw_caps.sd3_curr_limit &
465                     SD_MAX_CURRENT_400)
466                 current_limit = SD_SET_CURRENT_LIMIT_400;
467             else if (card->sw_caps.sd3_curr_limit &
468                     SD_MAX_CURRENT_200)
469                 current_limit = SD_SET_CURRENT_LIMIT_200;
470         } else if (card->host->caps & MMC_CAP_MAX_CURRENT_400) {
471             if (card->sw_caps.sd3_curr_limit & SD_MAX_CURRENT_400)
472                 current_limit = SD_SET_CURRENT_LIMIT_400;
473             else if (card->sw_caps.sd3_curr_limit &
474                     SD_MAX_CURRENT_200)
475                 current_limit = SD_SET_CURRENT_LIMIT_200;
476         } else if (card->host->caps & MMC_CAP_MAX_CURRENT_200) {
477             if (card->sw_caps.sd3_curr_limit & SD_MAX_CURRENT_200)
478                 current_limit = SD_SET_CURRENT_LIMIT_200;
479         }
480     } else
481         current_limit = SD_SET_CURRENT_LIMIT_200;
482 
483     err = mmc_sd_switch(card, 1, 3, current_limit, status);
484     if (err)
485         return err;
486 
487     if (((status[15] >> 4) & 0x0F) != current_limit)
488         SD_LOGW("%s: Problem setting current limit!\n", __func__);
489 
490     return 0;
491 }
492 
mmc_sd_init_uhs_card(struct mmc_card * card)493 int32_t mmc_sd_init_uhs_card(struct mmc_card *card)
494 {
495     int32_t err;
496     struct mmc_command cmd = {0};
497     struct mmc_data data = {0};
498     struct mmc_request mrq;
499     struct scatterlist sg = {0};
500     uint32_t time = 0;
501     uint8_t pattern[] = {
502         0xff, 0x0f, 0xff, 0x00, 0xff, 0xcc, 0xc3, 0xcc, 0xc3, 0x3c, 0xcc, 0xff, 0xfe, 0xff, 0xfe, 0xef,
503         0xff, 0xdf, 0xff, 0xdd, 0xff, 0xfb, 0xff, 0xfb, 0xbf, 0xff, 0x7f, 0xff, 0x77, 0xf7, 0xbd, 0xef,
504         0xff, 0xf0, 0xff, 0xf0, 0x0f, 0xfc, 0xcc, 0x3c, 0xcc, 0x33, 0xcc, 0xcf, 0xff, 0xef, 0xff, 0xee,
505         0xff, 0xfd, 0xff, 0xfd, 0xdf, 0xff, 0xbf, 0xff, 0xbb, 0xff, 0xf7, 0xff, 0xf7, 0x7f, 0x7b, 0xde
506     };
507     uint32_t status[64/4] = {0};
508 
509     if (!card->scr.sda_spec3)
510         return 0;
511 
512     if (!(card->csd.cmdclass & CCC_SWITCH))
513         return 0;
514 
515     /* Set 4-bit bus width */
516     if ((card->host->caps & MMC_CAP_4_BIT_DATA) &&
517         (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
518         err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4);
519         if (err)
520             goto out;
521 
522         HAL_SDC_Set_BusWidth(card->host, MMC_BUS_WIDTH_4);
523     }
524 
525     /*
526      * Select the bus speed mode depending on host
527      * and card capability.
528      */
529     sd_update_bus_speed_mode(card);
530 
531     /* Set the driver strength for the card */
532     err = sd_select_driver_type(card, status);
533     if (err)
534         goto out;
535 
536     /* Set current limit for the card */
537     err = sd_set_current_limit(card, status);
538     if (err)
539         goto out;
540 
541     /* Set bus speed mode of the card */
542     err = sd_set_bus_speed_mode(card, status);
543     if (err)
544         goto out;
545 
546     /* SPI mode doesn't define CMD19 */
547     mmc_host_clk_hold(card->host);
548 
549     sg.len = 512;
550     sg.buffer = status;
551     cmd.opcode = MMC_SEND_TUNING_PATTERN;
552     cmd.arg = 0;
553     cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
554     cmd.data = &data;
555 
556     data.blksz = 64;
557     data.sg_len = 1;
558     data.sg = &sg;
559     data.flags = MMC_DATA_READ;
560     mrq.cmd = &cmd;
561     mrq.data = &data;
562     do {
563         if (mmc_wait_for_req(card->host, &mrq)) {
564             continue;
565         }
566         time++;
567     } while (time < 40);
568 
569     if (HAL_Memcmp((void *)pattern, (void *)status, 64))
570         return -1;
571 
572     mmc_host_clk_release(card->host);
573 
574 out:
575     return 0;
576 }
577 #endif
578 
579 /*
580  * Given a 64-bit response, decode to our card SCR structure.
581  */
mmc_decode_scr(struct mmc_card * card,uint32_t * raw_scr)582 static int32_t mmc_decode_scr(struct mmc_card *card, uint32_t *raw_scr)
583 {
584     struct sd_scr *scr = &card->scr;
585     uint32_t scr_struct;
586     uint32_t resp[4];
587 
588     resp[3] = raw_scr[1];
589     resp[2] = raw_scr[0];
590 
591     scr_struct = UNSTUFF_BITS(resp, 60, 4);
592     if (scr_struct != 0) {
593         SD_LOGW("sdc unrecognised SCR structure version %u\n", (unsigned int)scr_struct);
594         return -1;
595     }
596 
597     scr->sda_vsn = UNSTUFF_BITS(resp, 56, 4);
598     scr->security_sup = UNSTUFF_BITS(resp, 52, 3);
599     scr->bus_widths = UNSTUFF_BITS(resp, 48, 4);
600 
601     if (scr->sda_vsn == SCR_SPEC_VER_2)
602         /* Check if Physical Layer Spec v3.0 is supported */
603         scr->sda_spec3 = UNSTUFF_BITS(resp, 47, 1);
604 
605     //if (UNSTUFF_BITS(resp, 55, 1))
606     //  card->erased_byte = 0xFF;
607     //else
608     //  card->erased_byte = 0x0;
609 
610     if (scr->sda_spec3) {
611         scr->cmds = UNSTUFF_BITS(resp, 32, 2);
612         scr->sda_spec4 = UNSTUFF_BITS(resp, 42, 1);
613         scr->sda_spec5 = UNSTUFF_BITS(resp, 38, 4);
614     }
615 
616     return 0;
617 }
618 
619 /*
620  * Fetch and process SD Status register.
621  */
mmc_read_ssr(struct mmc_card * card)622 static int32_t mmc_read_ssr(struct mmc_card *card)
623 {
624     int32_t err, i;
625     uint32_t ssr[64/4];
626 
627     if (!(card->csd.cmdclass & CCC_APP_SPEC)) {
628         SD_LOGW("%s: card lacks mandatory SD Status function.\n", __func__);
629         return 0;
630     }
631 
632     err = mmc_app_sd_status(card, (uint8_t *)ssr);
633     if (err) {
634         SD_LOGW("%s: problem reading SD Status register.\n", __func__);
635         err = 0;
636         goto out;
637     }
638 
639     for (i = 0; i < 16; i++)
640         ssr[i] = be32_to_cpu(ssr[i]);
641 
642 #ifdef SD_SUPPORT_ERASE
643     uint32_t au, es, et, eo;
644 
645     /*
646      * UNSTUFF_BITS only works with four u32s so we have to offset the
647      * bitfield positions accordingly.
648      */
649     au = UNSTUFF_BITS(ssr, 428 - 384, 4);
650     if (au > 0 && au <= 9) {
651         card->ssr.au = 1 << (au + 4);
652         es = UNSTUFF_BITS(ssr, 408 - 384, 16);
653         et = UNSTUFF_BITS(ssr, 402 - 384, 6);
654         eo = UNSTUFF_BITS(ssr, 400 - 384, 2);
655         if (es && et) {
656             card->ssr.erase_timeout = (et * 1000) / es;
657             card->ssr.erase_offset = eo * 1000;
658         }
659     } else {
660         SD_LOGW("%s: SD Status: Invalid Allocation Unit size.\n", __func__);
661     }
662 #endif
663 
664     card->speed_class = UNSTUFF_BITS(ssr, 440 - 384, 8) * 2;
665     if (card->speed_class == 8)
666         card->speed_class = 10;
667 out:
668     return err;
669 }
670 
671 /*
672  * Fetches and decodes switch information
673  */
mmc_read_switch(struct mmc_card * card)674 static int32_t mmc_read_switch(struct mmc_card *card)
675 {
676     int32_t err;
677     uint32_t status[64/sizeof(uint32_t)] = {0};
678     uint8_t *p_sta = (uint8_t *)status;
679 
680     if (card->scr.sda_vsn < SCR_SPEC_VER_1) {
681         SD_LOGN("Card ver. does not support to read switch info!\n");
682         return 0;
683     }
684 
685     if (!(card->csd.cmdclass & CCC_SWITCH)) {
686         SD_LOGW("card lacks mandatory switch function, performance might suffer.\n");
687         return 0;
688     }
689 
690     /* Find out the supported Bus Speed Modes. */
691     err = mmc_sd_switch(card, SD_SWITCH_CHECK, SD_SWITCH_GRP_ACCESS_MODE,
692                         SD_SWITCH_ACCESS_HS, p_sta);
693     if (err) {
694         SD_LOGW("%s: problem reading Bus Speed modes.\n", __func__);
695         goto out;
696     }
697 
698     if (p_sta[13] & SD_MODE_HIGH_SPEED)
699         card->sw_caps.hs_max_dtr = HIGH_SPEED_MAX_DTR;
700 
701     if (card->scr.sda_spec3) {
702         card->sw_caps.sd3_bus_mode = p_sta[13];
703 
704         /* Find out Driver Strengths supported by the card */
705         err = mmc_sd_switch(card, SD_SWITCH_CHECK, SD_SWITCH_GRP_DRV_STRENGTH,
706                             SD_SWITCH_ACCESS_HS, p_sta);
707         if (err) {
708             SD_LOGW("%s: problem reading Driver Strength.\n", __func__);
709             goto out;
710         }
711 
712         card->sw_caps.sd3_drv_type = p_sta[9];
713 
714         /* Find out Current Limits supported by the card */
715         err = mmc_sd_switch(card, SD_SWITCH_CHECK, SD_SWITCH_GRP_CUR_LIMIT,
716                             SD_SWITCH_ACCESS_HS, p_sta);
717         if (err) {
718             SD_LOGW("%s: problem reading Current Limit.\n", __func__);
719             goto out;
720         }
721 
722         card->sw_caps.sd3_curr_limit = p_sta[7];
723     }
724 
725 out:
726     return err;
727 }
728 
729 /*
730  * Test if the card supports high-speed mode and, if so, switch to it.
731  */
mmc_sd_switch_hs(struct mmc_card * card)732 int32_t mmc_sd_switch_hs(struct mmc_card *card)
733 {
734     int32_t err;
735     uint32_t status[64/sizeof(uint32_t)] = {0};
736     uint8_t *p_sta = (uint8_t *)status;
737 
738     if (card->scr.sda_vsn < SCR_SPEC_VER_1) {
739         SD_LOGN("Card ver. does not support to switch to high speed!\n");
740         return 0;
741     }
742 
743     if (!(card->csd.cmdclass & CCC_SWITCH)) {
744         SD_LOGN("Card cmdclass not support to switch to high speed!\n");
745         return 0;
746     }
747 
748     if (!(card->host->caps & MMC_CAP_SD_HIGHSPEED))
749         return 0;
750 
751     if (card->sw_caps.hs_max_dtr == 0)
752         return 0;
753 
754     /* Check function */
755     err = mmc_sd_switch(card, SD_SWITCH_CHECK, SD_SWITCH_GRP_ACCESS_MODE,
756                         SD_SWITCH_ACCESS_HS, p_sta);
757     if (err)
758         return err;
759 
760     if ((p_sta[16] & 0x0F) != 1) {
761         SD_LOGW("%s: Problem switching card into high-speed mode!\n", __func__);
762         err = 0;
763     } else {
764         err = 1;
765     }
766 
767     return err;
768 }
769 
mmc_send_cxd_native(struct mmc_host * host,uint32_t arg,uint32_t * cxd,int32_t opcode)770 static int32_t mmc_send_cxd_native(struct mmc_host *host, uint32_t arg, uint32_t *cxd, int32_t opcode)
771 {
772     int32_t err;
773     struct mmc_command cmd = {0};
774 
775     if (!host || !cxd) {
776         SDC_LOGE_RAW(ROM_ERR_MASK, "%s,%d err", __func__, __LINE__);
777         return -1;
778     }
779 
780     cmd.opcode = opcode;
781     cmd.arg = arg;
782     cmd.flags = MMC_RSP_R2 | MMC_CMD_AC;
783 
784     err = mmc_wait_for_cmd(host, &cmd);
785     if (err)
786         return err;
787 
788     HAL_Memcpy(cxd, cmd.resp, sizeof(uint32_t) * 4);
789 
790     return 0;
791 }
792 
mmc_send_csd(struct mmc_card * card,uint32_t * csd)793 int32_t mmc_send_csd(struct mmc_card *card, uint32_t *csd)
794 {
795     return mmc_send_cxd_native(card->host, card->rca << 16,
796                 csd, MMC_SEND_CSD);
797 }
798 
mmc_sd_get_csd(struct mmc_card * card)799 int32_t mmc_sd_get_csd(struct mmc_card *card)
800 {
801     int32_t err;
802     uint32_t csd[4] = {0};
803 
804     /* Fetch CSD from card. */
805     err = mmc_send_csd(card, csd);
806     if (err)
807         return err;
808 
809     SD_LOGN("card raw csd:\n");
810     sd_hex_dump_bytes((void *)csd, 16);
811 
812     err = mmc_decode_csd(card, csd);
813     if (err)
814         return err;
815 
816     return 0;
817 }
818 
mmc_sd_setup_card(struct mmc_host * host,struct mmc_card * card)819 int32_t mmc_sd_setup_card(struct mmc_host *host, struct mmc_card *card)
820 {
821     int32_t err;
822     int32_t retries;
823     uint32_t raw_scr[2] = {0};
824 
825     /* Fetch SCR from card. */
826     err = mmc_app_send_scr(card, raw_scr);
827     if (err)
828         return err;
829 
830     SD_LOGN("card raw scr:\n");
831     sd_hex_dump_bytes((void *)raw_scr, 8);
832 
833     err = mmc_decode_scr(card, raw_scr);
834     if (err)
835         return err;
836 
837     /* Fetch and process SD Status register. */
838     err = mmc_read_ssr(card);
839     if (err)
840         return err;
841 
842 #ifdef SD_SUPPORT_ERASE
843     /* Erase init depends on CSD and SSR */
844     mmc_init_erase(card);
845 #endif
846     /* Fetch switch information from card. */
847     for (retries = 1; retries <= 3; retries++) {
848         err = mmc_read_switch(card);
849         if (!err) {
850             if (retries > 1) {
851                 SD_LOGW("%s: recovered\n", __func__);
852             }
853             break;
854         } else {
855             SD_LOGW("%s: read switch failed (attempt %d)\n",
856                     __func__, (unsigned int)retries);
857         }
858     }
859 
860     if (err)
861         return err;
862 #ifdef SD_SUPPORT_WRITEPROTECT
863     int32_t ro = -1;
864 
865     /* Check if read-only switch is active. */
866     mmc_host_clk_hold(card->host);
867     ro = HAL_SDC_Get_ReadOnly(card->host);
868     mmc_host_clk_release(card->host);
869 
870     if (ro < 0) {
871         SD_LOGW("%s: host does not support reading read-only switch."
872             " assuming write-enable.\n", __func__);
873     } else if (ro > 0) {
874         mmc_card_set_readonly(card);
875     }
876 #endif
877     return 0;
878 }
879 
mmc_sd_get_max_clock(struct mmc_card * card)880 uint32_t mmc_sd_get_max_clock(struct mmc_card *card)
881 {
882     uint32_t max_dtr = (uint32_t)-1;
883 
884     if (mmc_card_highspeed(card)) {
885         if (max_dtr > card->sw_caps.hs_max_dtr)
886             max_dtr = card->sw_caps.hs_max_dtr;
887     } else if (max_dtr > card->csd.max_dtr) {
888         max_dtr = card->csd.max_dtr;
889     }
890 
891     return max_dtr;
892 }
893 
894 /*
895  * Handle the detection and initialisation of a card.
896  *
897  * In the case of a resume, "oldcard" will contain the card
898  * we're trying to reinitialise.
899  */
mmc_sd_init_card(struct mmc_card * card,struct mmc_host * host)900 static int32_t mmc_sd_init_card(struct mmc_card *card, struct mmc_host *host)
901 {
902     int32_t err = 0;
903 
904     /*
905      * I/O voltage should be 3.3 V here for the initialization needed.
906      */
907 
908     /* cmd2, send cid, check if card support 3.3V */
909     err = mmc_all_send_cid(host, card->cidno);
910     if (err) {
911         SD_LOGW("Cards all send CID number failed !!\n");
912         return -1;
913     } else
914         SD_LOGN("Card CID number:%x\n", (unsigned int)card->cidno[0]);
915 
916     card->type = MMC_TYPE_SD;
917 
918     /* cmd3, For native busses: get card RCA and quit open drain mode. */
919     err = mmc_send_relative_addr(card->host, &card->rca);
920     if (err) {
921         SD_LOGW("Card public new RCA failed !!\n");
922         return -1;
923     } else
924         SD_LOGD("Card public new RCA:%x\n", (unsigned int)card->rca);
925 
926     /* cmd10, get CID register */
927     if (mmc_send_cid(card)) {
928         SD_LOGW("Card send CID reg failed !!\n");
929         return -1;
930     }
931 
932     /* cmd9, get CSD register */
933     if (mmc_sd_get_csd(card)) {
934         SD_LOGW("Card send CSD reg failed !!\n");
935         return -1;
936     }
937 
938     /* cmd7, Select card  to standby state, as all following commands rely on that. */
939     if (mmc_select_card(card, 1)) {
940         SD_LOGW("mmc_select_card failed !!\n");
941         return -1;
942     }
943 
944     err = mmc_sd_setup_card(host, card);
945     if (err)
946         goto free_card;
947 
948     /* Initialization sequence for UHS-I cards */
949 #ifdef SD_SUPPORT_VERSION3
950     if (card->ocr.ocr & SD_ROCR_S18A) {
951         err = mmc_sd_init_uhs_card(card);
952         if (err)
953             goto free_card;
954 
955         /* Card is an ultra-high-speed card */
956         mmc_card_set_uhs(card);
957 
958         /*
959          * Since initialization is now complete, enable preset
960          * value registers for UHS-I cards.
961          */
962         mmc_host_clk_hold(card->host);
963         HAL_SDC_Enable_Preset_Value(card->host, true);
964         mmc_host_clk_release(card->host);
965     } else
966 #endif
967     {
968         uint32_t clk;
969         uint32_t retries = 3;
970 
971         while (retries) {
972             err = mmc_sd_switch_hs(card);
973             if (err < 0) {
974                 SD_LOGE("%s: Re-switch hs, err %d (retries = %u)\n",
975                         __func__, (unsigned int)err, (unsigned int)retries);
976                 mmc_mdelay(5);
977                 retries--;
978                 continue;
979             }
980             break;
981         }
982 
983         if (err <= 0) {
984             SD_LOGW("switch to high speed error, use DS: 25 MHz\n");
985             clk = 25000000;
986             err = HAL_SDC_Update_Clk(card->host, clk);
987             if (err)
988                 return -1;
989         } else {
990             mmc_card_set_highspeed(card);
991             card->sd_bus_speed = HIGH_SPEED_BUS_SPEED;
992 
993             clk = mmc_sd_get_max_clock(card);
994             err = HAL_SDC_Update_Clk(card->host, clk);
995             if (err)
996                 return -1;
997             SD_LOGN("card is switched to high speed mode, clk:%u KHz\n", (unsigned int)clk/1000);
998         }
999 
1000         /* Switch to wider bus (if supported). */
1001         if ((host->caps & MMC_CAP_4_BIT_DATA) &&
1002             (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
1003             err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4);
1004             if (err) {
1005                 SD_LOGW("Set bus width error, use default 1 bit !!\n");
1006                 return -1;
1007             }
1008 
1009             /* config SDMMC controller bus width */
1010             HAL_SDC_Set_BusWidth(card->host, MMC_BUS_WIDTH_4);
1011             SD_LOGN("Set bus width type: %d\n", MMC_BUS_WIDTH_4);
1012         }
1013     }
1014 
1015     mmc_add_card(card);
1016 
1017     return 0;
1018 
1019 free_card:
1020     return err;
1021 }
1022 
1023 #ifdef CONFIG_SD_PM
mmc_sd_suspend(struct mmc_host * host)1024 static int32_t mmc_sd_suspend(struct mmc_host *host)
1025 {
1026     struct mmc_card *card = host->card;
1027 
1028     if (card == NULL) {
1029         SD_LOGE_RAW(ROM_ERR_MASK, "card open fail\n");
1030         return -1;
1031     }
1032 
1033     mmc_card_open(card->id);
1034     card->suspend = 1;
1035     mmc_card_deinit(host->card);
1036     SD_LOGD("%s ok\n", __func__);
1037 
1038     mmc_card_close(card->id);
1039 
1040     return 0;
1041 }
1042 
mmc_sd_resume(struct mmc_host * host)1043 static int32_t mmc_sd_resume(struct mmc_host *host)
1044 {
1045     struct mmc_card *card = host->card;
1046 
1047     if (card == NULL) {
1048         SD_LOGE_RAW(ROM_ERR_MASK, "card open fail\n");
1049         return -1;
1050     }
1051 
1052     mmc_card_open(card->id);
1053     mmc_rescan(card, host->sdc_id);
1054     card->suspend = 0;
1055     SD_LOGD("%s ok\n", __func__);
1056 
1057     mmc_card_close(card->id);
1058 
1059     return 0;
1060 }
1061 
1062 static const struct mmc_bus_ops sd_bus_ops = {
1063     .suspend = mmc_sd_suspend,
1064     .resume = mmc_sd_resume,
1065 };
1066 #endif
1067 
1068 /*
1069  * Starting point for SD card init.
1070  */
mmc_attach_sd(struct mmc_card * card,struct mmc_host * host)1071 int32_t mmc_attach_sd(struct mmc_card *card, struct mmc_host *host)
1072 {
1073     int32_t err = 0;
1074     uint32_t ocr = 0;
1075 
1076     if (!host) {
1077         SD_LOGE_RAW(ROM_ERR_MASK, "%s,%d err", __func__, __LINE__);
1078         return -1;
1079     }
1080 
1081     //SD_LOGE("%s,%d\n",__FUNCTION__,__LINE__);
1082     /* send cmd41/55 to check operation condition */
1083     err = mmc_send_app_op_cond(host, 0, &ocr);
1084     if (err) {
1085         return err;
1086     }
1087     SD_LOGN("card ocr: %08x\n", (unsigned int)ocr);
1088 
1089     /*
1090      * Sanity check the voltages that the card claims to
1091      * support.
1092      */
1093     if (ocr & 0x7F) {
1094         SD_LOGW("%s: card claims to support voltages below the defined range."
1095                 " These will be ignored.\n", __func__);
1096         ocr &= ~0x7F;
1097     }
1098 
1099     card->ocr.ocr = 0x7fffffff & ocr; /* set card not in busy state */
1100 
1101     err = mmc_sd_init_card(card, host);
1102     if (err) {
1103         goto out;
1104     }
1105     host->card = card;
1106 #ifdef CONFIG_SD_PM
1107     if (!card->suspend) {
1108         mmc_attach_bus(host, &sd_bus_ops);
1109     }
1110 #endif
1111 
1112 out:
1113     return err;
1114 }
1115 
mmc_deattach_sd(struct mmc_card * card,struct mmc_host * host)1116 void mmc_deattach_sd(struct mmc_card *card, struct mmc_host *host)
1117 {
1118     mmc_select_card(card, 0);
1119     card->state &= ~MMC_STATE_HIGHSPEED;
1120 
1121 #ifdef CONFIG_SD_PM
1122     if (!card->suspend) {
1123         mmc_detach_bus(host);
1124         host->card = NULL;
1125     }
1126 #else
1127     host->card = NULL;
1128 #endif
1129 }
1130 
1131 #endif /* CONFIG_USE_SD */
1132