xref: /nrf52832-nimble/rt-thread/components/drivers/sdio/mmc.c (revision 104654410c56c573564690304ae786df310c91fc)
1 /*
2  * Copyright (c) 2006-2018, RT-Thread Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date           Author        Notes
8  * 2015-06-15     hichard     first version
9  */
10 
11 #include <drivers/mmcsd_core.h>
12 #include <drivers/mmc.h>
13 
14 #define DBG_ENABLE
15 #define DBG_SECTION_NAME               "SDIO"
16 #ifdef RT_SDIO_DEBUG
17 #define DBG_LEVEL                      DBG_LOG
18 #else
19 #define DBG_LEVEL                      DBG_INFO
20 #endif /* RT_SDIO_DEBUG */
21 #define DBG_COLOR
22 #include <rtdbg.h>
23 
24 static const rt_uint32_t tran_unit[] =
25 {
26     10000, 100000, 1000000, 10000000,
27     0,     0,      0,       0
28 };
29 
30 static const rt_uint8_t tran_value[] =
31 {
32     0,  10, 12, 13, 15, 20, 25, 30,
33     35, 40, 45, 50, 55, 60, 70, 80,
34 };
35 
36 static const rt_uint32_t tacc_uint[] =
37 {
38     1, 10, 100, 1000, 10000, 100000, 1000000, 10000000,
39 };
40 
41 static const rt_uint8_t tacc_value[] =
42 {
43     0,  10, 12, 13, 15, 20, 25, 30,
44     35, 40, 45, 50, 55, 60, 70, 80,
45 };
46 
GET_BITS(rt_uint32_t * resp,rt_uint32_t start,rt_uint32_t size)47 rt_inline rt_uint32_t GET_BITS(rt_uint32_t *resp,
48                                rt_uint32_t  start,
49                                rt_uint32_t  size)
50 {
51         const rt_int32_t __size = size;
52         const rt_uint32_t __mask = (__size < 32 ? 1 << __size : 0) - 1;
53         const rt_int32_t __off = 3 - ((start) / 32);
54         const rt_int32_t __shft = (start) & 31;
55         rt_uint32_t __res;
56 
57         __res = resp[__off] >> __shft;
58         if (__size + __shft > 32)
59             __res |= resp[__off-1] << ((32 - __shft) % 32);
60 
61         return __res & __mask;
62 }
63 
64 /*
65  * Given a 128-bit response, decode to our card CSD structure.
66  */
mmcsd_parse_csd(struct rt_mmcsd_card * card)67 static rt_int32_t mmcsd_parse_csd(struct rt_mmcsd_card *card)
68 {
69       rt_uint32_t a, b;
70       struct rt_mmcsd_csd *csd = &card->csd;
71       rt_uint32_t *resp = card->resp_csd;
72 
73       /*
74       * We only understand CSD structure v1.1 and v1.2.
75       * v1.2 has extra information in bits 15, 11 and 10.
76       * We also support eMMC v4.4 & v4.41.
77       */
78       csd->csd_structure = GET_BITS(resp, 126, 2);
79       if (csd->csd_structure == 0) {
80         LOG_E("unrecognised CSD structure version %d!", csd->csd_structure);
81 
82         return -RT_ERROR;
83       }
84 
85       csd->taac = GET_BITS(resp, 112, 8);
86       csd->nsac = GET_BITS(resp, 104, 8);
87       csd->tran_speed = GET_BITS(resp, 96, 8);
88       csd->card_cmd_class = GET_BITS(resp, 84, 12);
89       csd->rd_blk_len = GET_BITS(resp, 80, 4);
90       csd->rd_blk_part = GET_BITS(resp, 79, 1);
91       csd->wr_blk_misalign = GET_BITS(resp, 78, 1);
92       csd->rd_blk_misalign = GET_BITS(resp, 77, 1);
93       csd->dsr_imp = GET_BITS(resp, 76, 1);
94       csd->c_size = GET_BITS(resp, 62, 12);
95       csd->c_size_mult = GET_BITS(resp, 47, 3);
96       csd->r2w_factor = GET_BITS(resp, 26, 3);
97       csd->wr_blk_len = GET_BITS(resp, 22, 4);
98       csd->wr_blk_partial = GET_BITS(resp, 21, 1);
99       csd->csd_crc = GET_BITS(resp, 1, 7);
100 
101       card->card_blksize = 1 << csd->rd_blk_len;
102       card->tacc_clks = csd->nsac * 100;
103       card->tacc_ns = (tacc_uint[csd->taac&0x07] * tacc_value[(csd->taac&0x78)>>3] + 9) / 10;
104       card->max_data_rate = tran_unit[csd->tran_speed&0x07] * tran_value[(csd->tran_speed&0x78)>>3];
105       if (csd->wr_blk_len >= 9) {
106         a = GET_BITS(resp, 42, 5);
107         b = GET_BITS(resp, 37, 5);
108         card->erase_size = (a + 1) * (b + 1);
109         card->erase_size <<= csd->wr_blk_len - 9;
110       }
111 
112       return 0;
113 }
114 
115 /*
116  * Read extended CSD.
117  */
mmc_get_ext_csd(struct rt_mmcsd_card * card,rt_uint8_t ** new_ext_csd)118 static int mmc_get_ext_csd(struct rt_mmcsd_card *card, rt_uint8_t **new_ext_csd)
119 {
120   void *ext_csd;
121   struct rt_mmcsd_req req;
122   struct rt_mmcsd_cmd cmd;
123   struct rt_mmcsd_data data;
124 
125   *new_ext_csd = RT_NULL;
126 
127   if (GET_BITS(card->resp_cid, 122, 4) < 4)
128      return 0;
129 
130   /*
131   * As the ext_csd is so large and mostly unused, we don't store the
132   * raw block in mmc_card.
133   */
134   ext_csd = rt_malloc(512);
135   if (!ext_csd) {
136     LOG_E("alloc memory failed when get ext csd!");
137     return -RT_ENOMEM;
138   }
139 
140   rt_memset(&req, 0, sizeof(struct rt_mmcsd_req));
141   rt_memset(&cmd, 0, sizeof(struct rt_mmcsd_cmd));
142   rt_memset(&data, 0, sizeof(struct rt_mmcsd_data));
143 
144   req.cmd = &cmd;
145   req.data = &data;
146 
147   cmd.cmd_code = SEND_EXT_CSD;
148   cmd.arg = 0;
149 
150   /* NOTE HACK:  the RESP_SPI_R1 is always correct here, but we
151   * rely on callers to never use this with "native" calls for reading
152   * CSD or CID.  Native versions of those commands use the R2 type,
153   * not R1 plus a data block.
154   */
155   cmd.flags = RESP_SPI_R1 | RESP_R1 | CMD_ADTC;
156 
157   data.blksize = 512;
158   data.blks = 1;
159   data.flags = DATA_DIR_READ;
160   data.buf = ext_csd;
161 
162   /*
163   * Some cards require longer data read timeout than indicated in CSD.
164   * Address this by setting the read timeout to a "reasonably high"
165   * value. For the cards tested, 300ms has proven enough. If necessary,
166   * this value can be increased if other problematic cards require this.
167   */
168   data.timeout_ns = 300000000;
169   data.timeout_clks = 0;
170 
171   mmcsd_send_request(card->host, &req);
172 
173   if (cmd.err)
174     return cmd.err;
175   if (data.err)
176     return data.err;
177 
178   *new_ext_csd = ext_csd;
179   return 0;
180 }
181 
182 /*
183  * Decode extended CSD.
184  */
mmc_parse_ext_csd(struct rt_mmcsd_card * card,rt_uint8_t * ext_csd)185 static int mmc_parse_ext_csd(struct rt_mmcsd_card *card, rt_uint8_t *ext_csd)
186 {
187   if(card == RT_NULL || ext_csd == RT_NULL)
188   {
189     LOG_E("emmc parse ext csd fail, invaild args");
190     return -1;
191   }
192 
193   card->flags |=  CARD_FLAG_HIGHSPEED;
194   card->hs_max_data_rate = 200000000;
195 
196   card->card_capacity = *((rt_uint32_t *)&ext_csd[EXT_CSD_SEC_CNT]);
197   card->card_capacity *= card->card_blksize;
198   card->card_capacity >>= 10; /* unit:KB */
199   LOG_I("emmc card capacity %d KB.", card->card_capacity);
200 
201   return 0;
202 }
203 
204 /**
205  *   mmc_switch - modify EXT_CSD register
206  *   @card: the MMC card associated with the data transfer
207  *   @set: cmd set values
208  *   @index: EXT_CSD register index
209  *   @value: value to program into EXT_CSD register
210  *
211  *   Modifies the EXT_CSD register for selected card.
212  */
mmc_switch(struct rt_mmcsd_card * card,rt_uint8_t set,rt_uint8_t index,rt_uint8_t value)213 static int mmc_switch(struct rt_mmcsd_card *card, rt_uint8_t set,
214                       rt_uint8_t index, rt_uint8_t value)
215 {
216   int err;
217   struct rt_mmcsd_host *host = card->host;
218   struct rt_mmcsd_cmd cmd = {0};
219 
220   cmd.cmd_code = SWITCH;
221   cmd.arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
222     (index << 16) | (value << 8) | set;
223   cmd.flags = RESP_SPI_R1 | RESP_R1 | CMD_AC;
224 
225   err = mmcsd_send_cmd(host, &cmd, 3);
226   if (err)
227     return err;
228 
229   return 0;
230 }
231 
mmc_compare_ext_csds(struct rt_mmcsd_card * card,rt_uint8_t * ext_csd,rt_uint32_t bus_width)232 static int mmc_compare_ext_csds(struct rt_mmcsd_card *card,
233                                 rt_uint8_t *ext_csd, rt_uint32_t bus_width)
234 {
235   rt_uint8_t *bw_ext_csd;
236   int err;
237 
238   if (bus_width == MMCSD_BUS_WIDTH_1)
239     return 0;
240 
241   err = mmc_get_ext_csd(card, &bw_ext_csd);
242 
243   if (err || bw_ext_csd == RT_NULL) {
244     err = -RT_ERROR;
245     goto out;
246   }
247 
248   /* only compare read only fields */
249   err = !((ext_csd[EXT_CSD_PARTITION_SUPPORT] == bw_ext_csd[EXT_CSD_PARTITION_SUPPORT]) &&
250           (ext_csd[EXT_CSD_ERASED_MEM_CONT] == bw_ext_csd[EXT_CSD_ERASED_MEM_CONT]) &&
251           (ext_csd[EXT_CSD_REV] == bw_ext_csd[EXT_CSD_REV]) &&
252           (ext_csd[EXT_CSD_STRUCTURE] == bw_ext_csd[EXT_CSD_STRUCTURE]) &&
253           (ext_csd[EXT_CSD_CARD_TYPE] == bw_ext_csd[EXT_CSD_CARD_TYPE]) &&
254           (ext_csd[EXT_CSD_S_A_TIMEOUT] == bw_ext_csd[EXT_CSD_S_A_TIMEOUT]) &&
255           (ext_csd[EXT_CSD_HC_WP_GRP_SIZE] == bw_ext_csd[EXT_CSD_HC_WP_GRP_SIZE]) &&
256           (ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT] == bw_ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT]) &&
257           (ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] == bw_ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]) &&
258           (ext_csd[EXT_CSD_SEC_TRIM_MULT] == bw_ext_csd[EXT_CSD_SEC_TRIM_MULT]) &&
259           (ext_csd[EXT_CSD_SEC_ERASE_MULT] == bw_ext_csd[EXT_CSD_SEC_ERASE_MULT]) &&
260           (ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT] == bw_ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT]) &&
261           (ext_csd[EXT_CSD_TRIM_MULT] == bw_ext_csd[EXT_CSD_TRIM_MULT]) &&
262           (ext_csd[EXT_CSD_SEC_CNT + 0] == bw_ext_csd[EXT_CSD_SEC_CNT + 0]) &&
263           (ext_csd[EXT_CSD_SEC_CNT + 1] == bw_ext_csd[EXT_CSD_SEC_CNT + 1]) &&
264           (ext_csd[EXT_CSD_SEC_CNT + 2] == bw_ext_csd[EXT_CSD_SEC_CNT + 2]) &&
265           (ext_csd[EXT_CSD_SEC_CNT + 3] == bw_ext_csd[EXT_CSD_SEC_CNT + 3]) &&
266           (ext_csd[EXT_CSD_PWR_CL_52_195] == bw_ext_csd[EXT_CSD_PWR_CL_52_195]) &&
267           (ext_csd[EXT_CSD_PWR_CL_26_195] == bw_ext_csd[EXT_CSD_PWR_CL_26_195]) &&
268           (ext_csd[EXT_CSD_PWR_CL_52_360] == bw_ext_csd[EXT_CSD_PWR_CL_52_360]) &&
269           (ext_csd[EXT_CSD_PWR_CL_26_360] == bw_ext_csd[EXT_CSD_PWR_CL_26_360]) &&
270           (ext_csd[EXT_CSD_PWR_CL_200_195] == bw_ext_csd[EXT_CSD_PWR_CL_200_195]) &&
271           (ext_csd[EXT_CSD_PWR_CL_200_360] == bw_ext_csd[EXT_CSD_PWR_CL_200_360]) &&
272           (ext_csd[EXT_CSD_PWR_CL_DDR_52_195] == bw_ext_csd[EXT_CSD_PWR_CL_DDR_52_195]) &&
273           (ext_csd[EXT_CSD_PWR_CL_DDR_52_360] == bw_ext_csd[EXT_CSD_PWR_CL_DDR_52_360]) &&
274           (ext_csd[EXT_CSD_PWR_CL_DDR_200_360] == bw_ext_csd[EXT_CSD_PWR_CL_DDR_200_360]));
275 
276   if (err)
277      err = -RT_ERROR;
278 
279 out:
280   rt_free(bw_ext_csd);
281   return err;
282 }
283 
284 /*
285  * Select the bus width amoung 4-bit and 8-bit(SDR).
286  * If the bus width is changed successfully, return the selected width value.
287  * Zero is returned instead of error value if the wide width is not supported.
288  */
mmc_select_bus_width(struct rt_mmcsd_card * card,rt_uint8_t * ext_csd)289 static int mmc_select_bus_width(struct rt_mmcsd_card *card, rt_uint8_t *ext_csd)
290 {
291   rt_uint32_t ext_csd_bits[] = {
292     EXT_CSD_BUS_WIDTH_8,
293     EXT_CSD_BUS_WIDTH_4,
294     EXT_CSD_BUS_WIDTH_1
295   };
296   rt_uint32_t bus_widths[] = {
297     MMCSD_BUS_WIDTH_8,
298     MMCSD_BUS_WIDTH_4,
299     MMCSD_BUS_WIDTH_1
300   };
301   struct rt_mmcsd_host *host = card->host;
302   unsigned idx, bus_width = 0;
303   int err = 0;
304 
305   if (GET_BITS(card->resp_cid, 122, 4) < 4)
306      return 0;
307 
308   /*
309   * Unlike SD, MMC cards dont have a configuration register to notify
310   * supported bus width. So bus test command should be run to identify
311   * the supported bus width or compare the ext csd values of current
312   * bus width and ext csd values of 1 bit mode read earlier.
313   */
314   for (idx = 0; idx < sizeof(bus_widths)/sizeof(rt_uint32_t); idx++) {
315     /*
316     * Host is capable of 8bit transfer, then switch
317     * the device to work in 8bit transfer mode. If the
318     * mmc switch command returns error then switch to
319     * 4bit transfer mode. On success set the corresponding
320     * bus width on the host.
321     */
322     err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
323                      EXT_CSD_BUS_WIDTH,
324                      ext_csd_bits[idx]);
325     if (err)
326       continue;
327 
328     bus_width = bus_widths[idx];
329     mmcsd_set_bus_width(host, bus_width);
330     mmcsd_delay_ms(20); //delay 10ms
331     err = mmc_compare_ext_csds(card, ext_csd, bus_width);
332     if (!err) {
333       err = bus_width;
334       break;
335     } else {
336       switch(ext_csd_bits[idx]){
337           case 0:
338             LOG_E("switch to bus width 1 bit failed!");
339             break;
340           case 1:
341             LOG_E("switch to bus width 4 bit failed!");
342             break;
343           case 2:
344             LOG_E("switch to bus width 8 bit failed!");
345             break;
346           default:
347             break;
348       }
349     }
350   }
351 
352   return err;
353 }
mmc_send_op_cond(struct rt_mmcsd_host * host,rt_uint32_t ocr,rt_uint32_t * rocr)354 rt_err_t mmc_send_op_cond(struct rt_mmcsd_host *host,
355                           rt_uint32_t ocr, rt_uint32_t *rocr)
356 {
357     struct rt_mmcsd_cmd cmd;
358     rt_uint32_t i;
359     rt_err_t err = RT_EOK;
360 
361     rt_memset(&cmd, 0, sizeof(struct rt_mmcsd_cmd));
362 
363     cmd.cmd_code = SEND_OP_COND;
364     cmd.arg = controller_is_spi(host) ? 0 : ocr;
365     cmd.flags = RESP_SPI_R1 | RESP_R3 | CMD_BCR;
366 
367     for (i = 100; i; i--) {
368       err = mmcsd_send_cmd(host, &cmd, 3);
369       if (err)
370         break;
371 
372       /* if we're just probing, do a single pass */
373       if (ocr == 0)
374         break;
375 
376       /* otherwise wait until reset completes */
377       if (controller_is_spi(host)) {
378         if (!(cmd.resp[0] & R1_SPI_IDLE))
379           break;
380       } else {
381         if (cmd.resp[0] & CARD_BUSY)
382           break;
383       }
384 
385       err = -RT_ETIMEOUT;
386 
387       mmcsd_delay_ms(10); //delay 10ms
388     }
389 
390   if (rocr && !controller_is_spi(host))
391     *rocr = cmd.resp[0];
392 
393   return err;
394 }
395 
mmc_set_card_addr(struct rt_mmcsd_host * host,rt_uint32_t rca)396 static rt_err_t mmc_set_card_addr(struct rt_mmcsd_host *host, rt_uint32_t rca)
397 {
398   rt_err_t err;
399   struct rt_mmcsd_cmd cmd;
400 
401   rt_memset(&cmd, 0, sizeof(struct rt_mmcsd_cmd));
402 
403   cmd.cmd_code = SET_RELATIVE_ADDR;
404   cmd.arg = rca << 16;
405   cmd.flags = RESP_R1 | CMD_AC;
406 
407   err = mmcsd_send_cmd(host, &cmd, 3);
408   if (err)
409     return err;
410 
411   return 0;
412 }
413 
mmcsd_mmc_init_card(struct rt_mmcsd_host * host,rt_uint32_t ocr)414 static rt_int32_t mmcsd_mmc_init_card(struct rt_mmcsd_host *host,
415                                      rt_uint32_t           ocr)
416 {
417     rt_int32_t err;
418     rt_uint32_t resp[4];
419     rt_uint32_t rocr = 0;
420     rt_uint32_t max_data_rate;
421     rt_uint8_t *ext_csd = RT_NULL;
422     struct rt_mmcsd_card *card = RT_NULL;
423 
424     mmcsd_go_idle(host);
425 
426     /* The extra bit indicates that we support high capacity */
427     err = mmc_send_op_cond(host, ocr | (1 << 30), &rocr);
428     if (err)
429       goto err;
430 
431     if (controller_is_spi(host))
432     {
433         err = mmcsd_spi_use_crc(host, 1);
434         if (err)
435             goto err1;
436     }
437 
438     if (controller_is_spi(host))
439         err = mmcsd_get_cid(host, resp);
440     else
441         err = mmcsd_all_get_cid(host, resp);
442     if (err)
443         goto err;
444 
445     card = rt_malloc(sizeof(struct rt_mmcsd_card));
446     if (!card)
447     {
448         LOG_E("malloc card failed!");
449         err = -RT_ENOMEM;
450         goto err;
451     }
452     rt_memset(card, 0, sizeof(struct rt_mmcsd_card));
453 
454     card->card_type = CARD_TYPE_MMC;
455     card->host = host;
456     card->rca = 1;
457     rt_memcpy(card->resp_cid, resp, sizeof(card->resp_cid));
458 
459     /*
460      * For native busses:  get card RCA and quit open drain mode.
461      */
462     if (!controller_is_spi(host))
463     {
464         err = mmc_set_card_addr(host, card->rca);
465         if (err)
466             goto err1;
467 
468         mmcsd_set_bus_mode(host, MMCSD_BUSMODE_PUSHPULL);
469     }
470 
471     err = mmcsd_get_csd(card, card->resp_csd);
472     if (err)
473         goto err1;
474 
475     err = mmcsd_parse_csd(card);
476     if (err)
477         goto err1;
478 
479     if (!controller_is_spi(host))
480     {
481         err = mmcsd_select_card(card);
482         if (err)
483             goto err1;
484     }
485 
486     /*
487     * Fetch and process extended CSD.
488     */
489 
490     err = mmc_get_ext_csd(card, &ext_csd);
491     if (err)
492       goto err1;
493     err = mmc_parse_ext_csd(card, ext_csd);
494     if (err)
495       goto err1;
496 
497     /* If doing byte addressing, check if required to do sector
498     * addressing.  Handle the case of <2GB cards needing sector
499     * addressing.  See section 8.1 JEDEC Standard JED84-A441;
500     * ocr register has bit 30 set for sector addressing.
501     */
502     if (!(card->flags & CARD_FLAG_SDHC) && (rocr & (1<<30)))
503         card->flags |= CARD_FLAG_SDHC;
504 
505     /* set bus speed */
506     max_data_rate = (unsigned int)-1;
507     if (card->flags & CARD_FLAG_HIGHSPEED)
508     {
509         if (max_data_rate > card->hs_max_data_rate)
510             max_data_rate = card->hs_max_data_rate;
511     }
512     else if (max_data_rate > card->max_data_rate)
513     {
514         max_data_rate = card->max_data_rate;
515     }
516 
517     mmcsd_set_clock(host, max_data_rate);
518 
519     /*switch bus width*/
520     mmc_select_bus_width(card, ext_csd);
521 
522     host->card = card;
523 
524     rt_free(ext_csd);
525     return 0;
526 
527 err1:
528     rt_free(card);
529 err:
530 
531     return err;
532 }
533 
534 /*
535  * Starting point for mmc card init.
536  */
init_mmc(struct rt_mmcsd_host * host,rt_uint32_t ocr)537 rt_int32_t init_mmc(struct rt_mmcsd_host *host, rt_uint32_t ocr)
538 {
539     rt_int32_t err;
540     rt_uint32_t  current_ocr;
541     /*
542      * We need to get OCR a different way for SPI.
543      */
544     if (controller_is_spi(host))
545     {
546         err = mmcsd_spi_read_ocr(host, 0, &ocr);
547         if (err)
548             goto err;
549     }
550 
551     current_ocr = mmcsd_select_voltage(host, ocr);
552 
553     /*
554      * Can we support the voltage(s) of the card(s)?
555      */
556     if (!current_ocr)
557     {
558         err = -RT_ERROR;
559         goto err;
560     }
561 
562     /*
563      * Detect and init the card.
564      */
565     err = mmcsd_mmc_init_card(host, current_ocr);
566     if (err)
567         goto err;
568 
569     mmcsd_host_unlock(host);
570 
571     err = rt_mmcsd_blk_probe(host->card);
572     if (err)
573         goto remove_card;
574     mmcsd_host_lock(host);
575 
576     return 0;
577 
578 remove_card:
579     mmcsd_host_lock(host);
580     rt_mmcsd_blk_remove(host->card);
581     rt_free(host->card);
582     host->card = RT_NULL;
583 err:
584 
585     LOG_E("init MMC card failed!");
586 
587     return err;
588 }
589