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 
30 #include <stddef.h>
31 #include <stdint.h>
32 
33 #include "hal_sdhost.h"
34 #include "sdmmc.h"
35 #ifdef CONFIG_USE_SDIO
36 #include "sdio.h"
37 #endif
38 
39 #include "hal/hal_base.h"
40 
41 #include "_sd_define.h"
42 
43 #include "_sdhost.h"
44 #include "_core.h"
45 #ifdef CONFIG_USE_SDIO
46 #include "_sdio.h"
47 #endif
48 #ifdef CONFIG_USE_SD
49 #include "_sd.h"
50 #endif
51 #ifdef CONFIG_USE_MMC
52 #include "_mmc.h"
53 #endif
54 
55 hal_spinlock_t sdmmc_lock;
56 
57 /**
58  *  mmc_wait_for_req - start a request and wait for completion
59  *  @host: MMC host to start command
60  *  @mrq: MMC request to start
61  *
62  *  Start a new MMC custom command request for a host, and wait
63  *  for the command to complete. Does not attempt to parse the
64  *  response.
65  */
mmc_wait_for_req(struct mmc_host * host,struct mmc_request * mrq)66 int32_t mmc_wait_for_req(struct mmc_host *host, struct mmc_request *mrq)
67 {
68     mrq->cmd->data = mrq->data;
69     return HAL_SDC_Request(host, mrq);
70 }
71 
72 /**
73  *  mmc_wait_for_cmd - start a command and wait for completion
74  *  @host: MMC host to start command
75  *  @cmd: MMC command to start
76  *  @retries: maximum number of retries
77  *
78  *  Start a new MMC command for a host, and wait for the command
79  *  to complete.  Return any error that occurred while the command
80  *  was executing.  Do not attempt to parse the response.
81  */
mmc_wait_for_cmd(struct mmc_host * host,struct mmc_command * cmd)82 int32_t mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd)
83 {
84     struct mmc_request mrq = {0};
85 
86     SDC_Memset(cmd->resp, 0, sizeof(cmd->resp));
87 
88     mrq.cmd = cmd;
89 
90     return mmc_wait_for_req(host, &mrq);
91 }
92 
93 /**
94  *  mmc_align_data_size - pads a transfer size to a more optimal value
95  *  @card: the MMC card associated with the data transfer
96  *  @sz: original transfer size
97  *
98  *  Pads the original data size with a number of extra bytes in
99  *  order to avoid controller bugs and/or performance hits
100  *  (e.g. some controllers revert to PIO for certain sizes).
101  *
102  *  Returns the improved size, which might be unmodified.
103  *
104  *  Note that this function is only relevant when issuing a
105  *  single scatter gather entry.
106  */
mmc_align_data_size(struct mmc_card * card,uint32_t sz)107 int32_t mmc_align_data_size(struct mmc_card *card, uint32_t sz)
108 {
109     /*
110      * FIXME: We don't have a system for the controller to tell
111      * the core about its problems yet, so for now we just 32-bit
112      * align the size.
113      */
114     sz = ((sz + 3) / 4) * 4;
115 
116     return sz;
117 }
118 
mmc_host_clk_hold(struct mmc_host * host)119 static inline void mmc_host_clk_hold(struct mmc_host *host)
120 {
121     HAL_SDC_Clk_PWR_Opt(host, 1, 0);
122 }
123 
mmc_host_clk_release(struct mmc_host * host)124 static inline void mmc_host_clk_release(struct mmc_host *host)
125 {
126     HAL_SDC_Clk_PWR_Opt(host, 0, 0);
127 }
128 
129 /*
130  * Apply power to the MMC stack.  This is a two-stage process.
131  * First, we enable power to the card without the clock running.
132  * We then wait a bit for the power to stabilise.  Finally,
133  * enable the bus drivers and clock to the card.
134  *
135  * We must _NOT_ enable the clock prior to power stablising.
136  *
137  * If a host does all the power sequencing itself, ignore the
138  * initial MMC_POWER_UP stage.
139  */
mmc_power_up(struct mmc_host * host)140 static void mmc_power_up(struct mmc_host *host)
141 {
142     //int bit;
143 
144     mmc_host_clk_hold(host);
145 
146     /* If ocr is set, we use it */
147     //if (host->ocr)
148     //  bit = ffs(host->ocr) - 1;
149     //else
150     //  bit = fls(host->ocr_avail) - 1;
151 
152     /*This delay should be sufficient to allow the power supply
153     *to reach the minimum voltage
154     * according to figure 6-5 of sd3.0 spec,about 40 ms
155     */
156     mmc_mdelay(40);
157 
158     /*
159      * This delay must be at least 74 clock sizes, or 1 ms, or the
160      * time required to reach a stable voltage.
161      */
162     mmc_mdelay(5);
163     HAL_SDC_PowerOn(host);
164 
165     mmc_host_clk_release(host);
166 }
167 
mmc_power_off(struct mmc_host * host)168 static void mmc_power_off(struct mmc_host *host)
169 {
170     mmc_host_clk_hold(host);
171 
172     /*
173      * For eMMC 4.5 device send AWAKE command before
174      * POWER_OFF_NOTIFY command, because in sleep state
175      * eMMC 4.5 devices respond to only RESET and AWAKE cmd
176      */
177     //if (host->card && mmc_card_is_sleep(host->card)) {
178     //  mmc_poweroff_notify(host);
179     //}
180 
181     /*
182      * Reset ocr mask to be the highest possible voltage supported for
183      * this mmc host. This value will be used at next power up.
184      */
185     //host->ocr = 1 << (fls(host->ocr_avail) - 1);
186     HAL_SDC_PowerOff(host);
187     /*
188      * Some configurations, such as the 802.11 SDIO card in the OLPC
189      * XO-1.5, require a short delay after poweroff before the card
190      * can be successfully turned on again.
191      */
192     mmc_mdelay(1);
193 
194     mmc_host_clk_release(host);
195 }
196 
mmc_go_idle(struct mmc_host * host)197 static int32_t mmc_go_idle(struct mmc_host *host)
198 {
199     int32_t err;
200     struct mmc_command cmd = {0};
201 
202     cmd.opcode = MMC_GO_IDLE_STATE;
203     cmd.arg = 0;
204     cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_NONE | MMC_CMD_BC;
205 
206     err = mmc_wait_for_cmd(host, &cmd);
207 
208     mmc_mdelay(1);
209 
210     return err;
211 }
212 
mmc_send_status(struct mmc_card * card,uint32_t * status)213 int32_t mmc_send_status(struct mmc_card *card, uint32_t *status)
214 {
215     int32_t err;
216     struct mmc_command cmd = {0};
217 
218     cmd.opcode = MMC_SEND_STATUS;
219     cmd.arg = card->rca << 16;
220     cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
221 
222     err = mmc_wait_for_cmd(card->host, &cmd);
223     if (err)
224         return err;
225 
226     /* NOTE: callers are required to understand the difference
227      * between "native" and SPI format status words!
228      */
229     if (status)
230         *status = cmd.resp[0];
231 
232     return 0;
233 }
234 
235 #if ((defined CONFIG_USE_SD) || (defined CONFIG_USE_MMC))
mmc_sd_switch(struct mmc_card * card,uint8_t mode,uint8_t group,uint16_t value,uint8_t * resp)236 int32_t mmc_sd_switch(struct mmc_card *card, uint8_t mode, uint8_t group,
237                              uint16_t value, uint8_t *resp)
238 {
239     struct mmc_request mrq;
240     struct mmc_command cmd = {0};
241     struct mmc_data data = {0};
242     struct scatterlist sg;
243 
244     /* NOTE: caller guarantees resp is heap-allocated */
245 
246     mode = !!mode;
247     value &= 0xF;
248 
249     mrq.cmd = &cmd;
250     mrq.data = &data;
251 
252     cmd.opcode = SD_SWITCH;
253     cmd.arg = mode << 31 | 0x00FFFFFF;
254     cmd.arg &= ~(0xF << (group * 4));
255     cmd.arg |= value << (group * 4);
256     cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
257 
258     data.blksz = 64;
259     data.blocks = 1;
260     data.flags = MMC_DATA_READ;
261     data.sg = &sg;
262     data.sg_len = 1;
263 
264     sg.len = 64;
265     sg.buffer = resp;
266 
267     if (mmc_wait_for_req(card->host, &mrq)) {
268         return -1;
269     }
270 
271     return 0;
272 }
273 
mmc_switch(struct mmc_card * card,uint8_t set,uint8_t index,uint8_t value)274 static int32_t mmc_switch(struct mmc_card *card, uint8_t set, uint8_t index, uint8_t value)
275 {
276     struct mmc_command cmd = {0};
277     int32_t ret;
278     uint32_t status = 0;
279 
280     cmd.opcode = MMC_SWITCH;
281     cmd.arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) | (index << 16) | (value << 8) | set;
282     cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
283 
284     if (mmc_wait_for_cmd(card->host, &cmd)) {
285         return -1;
286     }
287 
288     /* Must check status to be sure of no errors */
289     do {
290         ret = mmc_send_status(card, &status);
291         if (ret)
292             return ret;
293     } while (R1_CURRENT_STATE(status) == R1_STATE_PRG);
294 
295     if (status & 0xFDFFA000)
296         SD_LOGW("unexpected status %x after switch", (unsigned int)status);
297     if (status & R1_SWITCH_ERROR)
298         return -1;
299 
300     return 0;
301 }
302 
mmc_app_cmd(struct mmc_host * host,struct mmc_card * card)303 int32_t mmc_app_cmd(struct mmc_host *host, struct mmc_card *card)
304 {
305     int32_t err;
306     struct mmc_command cmd = {0};
307 
308     if (!host || (card && (card->host != host))) {
309         SD_LOGE_RAW(ROM_ERR_MASK, "%s,%d err", __func__, __LINE__);
310         return -1;
311     }
312 
313     cmd.opcode = MMC_APP_CMD;
314 
315     if (card) {
316         cmd.arg = card->rca << 16;
317         cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
318     } else {
319         cmd.arg = 0;
320         cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_BCR;
321     }
322 
323     err = mmc_wait_for_cmd(host, &cmd);
324     if (err)
325         return err;
326 
327     /* Check that card supported application commands */
328     if (!(cmd.resp[0] & R1_APP_CMD))
329         return -1;
330 
331     return 0;
332 }
333 
334 /**
335  *  mmc_wait_for_app_cmd - start an application command and wait for
336  *                 completion
337  *  @host: MMC host to start command
338  *  @card: Card to send MMC_APP_CMD to
339  *  @cmd: MMC command to start
340  *
341  *  Sends a MMC_APP_CMD, checks the card response, sends the command
342  *  in the parameter and waits for it to complete. Return any error
343  *  that occurred while the command was executing.  Do not attempt to
344  *  parse the response.
345  */
mmc_wait_for_app_cmd(struct mmc_host * host,struct mmc_card * card,struct mmc_command * cmd)346 int32_t mmc_wait_for_app_cmd(struct mmc_host *host, struct mmc_card *card,
347                              struct mmc_command *cmd)
348 {
349     struct mmc_request mrq = {NULL};
350 
351     int32_t i, err;
352 
353     if (!cmd) {
354         SD_LOGE("%s,%d err", __func__, __LINE__);
355         return -1;
356     }
357 
358     err = -1;
359 
360     /*
361      * We have to resend MMC_APP_CMD for each attempt so
362      * we cannot use the retries field in mmc_command.
363      */
364     for (i = 0; i <= MMC_CMD_RETRIES; i++) {
365         err = mmc_app_cmd(host, card);
366         if (err) {
367             continue;
368         }
369 
370         SDC_Memset(&mrq, 0, sizeof(struct mmc_request));
371 
372         SDC_Memset(cmd->resp, 0, sizeof(cmd->resp));
373 
374         mrq.cmd = cmd;
375         cmd->data = NULL;
376 
377         err = mmc_wait_for_req(host, &mrq);
378         if (!err)
379             break;
380     }
381 
382     return err;
383 }
384 
mmc_app_set_bus_width(struct mmc_card * card,uint32_t width)385 int32_t mmc_app_set_bus_width(struct mmc_card *card, uint32_t width)
386 {
387     struct mmc_command cmd = {0};
388 
389     cmd.opcode = SET_BUS_WIDTH;
390     cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
391 
392     switch (width) {
393         case MMC_BUS_WIDTH_1:
394             cmd.arg = SD_BUS_WIDTH_1;
395             break;
396         case MMC_BUS_WIDTH_4:
397             cmd.arg = SD_BUS_WIDTH_4;
398             break;
399         default:
400             cmd.arg = SD_BUS_WIDTH_1;
401     }
402 
403     if (mmc_wait_for_app_cmd(card->host, card, &cmd)) {
404         return -1;
405     }
406     card->bus_width = width;
407 
408     return 0;
409 }
410 
mmc_switch_to_high_speed(struct mmc_card * card)411 int32_t mmc_switch_to_high_speed(struct mmc_card *card)
412 {
413     int32_t err;
414 
415 #ifdef CONFIG_USE_MMC
416     if (card->csd.mmc_spec_ver < MMC_CSD_SPEC_VER_4) {
417         SD_LOGD("MMC card doesn't support to switch to high speed mode !!\n");
418         return -1;
419     }
420 #endif
421 
422     err = mmc_switch(card, MMC_EXT_CSD_CMD_SET_NORMAL, MMC_EXT_CSD_HS_TIMING, 1);
423     if (err) {
424         SD_LOGD("MMC card failed to switch to high speed mode !!\n");
425         return err;
426     }
427 
428     SD_LOGD("MMC card is switched to high speed!!\n");
429 
430     return 0;
431 }
432 
__sdmmc_block_rw(struct mmc_card * card,uint32_t blk_num,uint32_t blk_cnt,uint32_t sg_len,struct scatterlist * sg,int write)433 int32_t __sdmmc_block_rw(struct mmc_card *card, uint32_t blk_num, uint32_t blk_cnt,
434                            uint32_t sg_len, struct scatterlist *sg, int write)
435 {
436     struct mmc_command cmd = {0};
437     struct mmc_data data = {0};
438     struct mmc_request mrq;
439     uint32_t status = 0;
440 
441     SD_LOGD("%s %s blk_num:%u, blk_cnt:%u, sg_len:%u sg->len:%u\n", __func__,
442             write?"wirte":"read", (unsigned int)blk_num, (unsigned int)blk_cnt, (unsigned int)sg_len, (unsigned int)sg->len);
443 
444     if (blk_cnt > 1) {
445         cmd.opcode = write ? MMC_WRITE_MULTIPLE_BLOCK : MMC_READ_MULTIPLE_BLOCK;
446     } else {
447         cmd.opcode = write ? MMC_WRITE_SINGLE_BLOCK : MMC_READ_SINGLE_BLOCK;
448     }
449     cmd.arg = blk_num;
450     if (!mmc_card_blockaddr(card))
451         cmd.arg <<= 9;
452     cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
453     cmd.stop = (blk_cnt == 1) ? 0 : 1;
454 
455     data.blksz = 512;
456     data.flags = write ? MMC_DATA_WRITE : MMC_DATA_READ;
457     data.blocks = blk_cnt;
458 
459     data.sg = sg;
460     data.sg_len = sg_len;
461 
462     mrq.cmd = &cmd;
463     mrq.data = &data;
464 
465     SD_LOGD("starting CMD%u arg 0x%08x flags %x\n", (unsigned int)cmd.opcode, (unsigned int)cmd.arg, (unsigned int)cmd.flags);
466     SD_LOGD("  blksz %u blocks %u flags %x\n", (unsigned int)data.blksz, (unsigned int)data.blocks, (unsigned int)data.flags);
467     if (mmc_wait_for_req(card->host, &mrq)) {
468         SD_LOGE("%s,%d %s sector:%x BSZ:%u Err!!\n", __func__, __LINE__,
469             write?"W":"R", (unsigned int)blk_num, (unsigned int)blk_cnt);
470 
471         return -1;
472     }
473     if (write) {
474         uint32_t timeout = 0x3ff;
475         do {
476             if (HAL_SDC_Is_Busy(card->host) && timeout) {
477                 timeout--;
478                 continue;
479             } else if (HAL_SDC_Is_Busy(card->host)) {
480                 goto mdelay;
481             }
482             if (mmc_send_status(card, &status)) {
483                 break;
484             }
485 mdelay:
486             timeout = 0x3ff;
487             /* mmc_mdelay(1); // no mdelay will be faster */
488         } while (!(status & 0x100));
489     }
490     return 0;
491 }
492 #endif
493 
mmc_add_card(struct mmc_card * card)494 void mmc_add_card(struct mmc_card *card)
495 {
496     uint8_t spec_ver = 0;
497     uint32_t speed_hz = 0;
498     const char *type = NULL;
499     const char *speed_mode = "";
500 #if SD_DEBUG
501 #ifdef SD_SUPPORT_VERSION3
502     static const char *const uhs_speeds[] = {
503         [UHS_SDR12_BUS_SPEED] = "SDR12 ",
504         [UHS_SDR25_BUS_SPEED] = "SDR25 ",
505         [UHS_SDR50_BUS_SPEED] = "SDR50 ",
506         [UHS_SDR104_BUS_SPEED] = "SDR104 ",
507         [UHS_DDR50_BUS_SPEED] = "DDR50 ",
508     };
509 #endif
510 #endif
511 
512     switch (card->type) {
513 #ifdef CONFIG_USE_MMC
514     case MMC_TYPE_MMC:
515         type = "MMC";
516         if (card->csd.mmc_spec_ver == 4)
517             spec_ver = 0x40;
518         else
519             spec_ver = 0x31;
520         speed_hz = mmc_mmc_get_max_clock(card);
521         break;
522 #endif
523 #ifdef CONFIG_USE_SD
524     case MMC_TYPE_SD:
525         type = "SD";
526         if (mmc_card_blockaddr(card)) {
527             if (mmc_card_ext_capacity(card))
528                 type = "SDXC";
529             else
530                 type = "SDHC";
531         }
532         if (card->scr.sda_vsn == 0)
533             spec_ver = 0x10;
534         else if (card->scr.sda_vsn == 1)
535             spec_ver = 0x11;
536         else if (card->scr.sda_vsn == 2 && card->scr.sda_spec3)
537             spec_ver = 0x30;
538         else
539             spec_ver = 0x20;
540         if (card->scr.sda_vsn == 2 && card->scr.sda_spec3 && card->scr.sda_spec4)
541             spec_ver = 0x40;
542         if (card->scr.sda_vsn == 2 && card->scr.sda_spec3 && card->scr.sda_spec5)
543             spec_ver = 0x50;
544         speed_hz = mmc_sd_get_max_clock(card);
545         break;
546 #endif
547 #ifdef CONFIG_USE_SDIO
548     case MMC_TYPE_SDIO:
549         type = "SDIO";
550         spec_ver = 0x10;
551         speed_hz = mmc_sdio_get_max_clock(card);
552         break;
553 #endif
554 #ifdef CONFIG_USE_SD_COMBO
555     case MMC_TYPE_SD_COMBO:
556         type = "SD-combo";
557         if (mmc_card_blockaddr(card))
558             type = "SDHC-combo";
559         break;
560 #endif
561     default:
562         type = "?";
563         break;
564     }
565 
566 #ifdef SD_SUPPORT_VERSION3
567     if (mmc_sd_card_uhs(card) &&
568         (card->sd_bus_speed < ARRAY_SIZE(uhs_speeds)))
569         speed_mode = uhs_speeds[card->sd_bus_speed];
570     else
571 #endif
572     if (card->sd_bus_speed == HIGH_SPEED_BUS_SPEED)
573         speed_mode = "HS: 50 MHz";
574     else
575         speed_mode = "DS: 25 MHz";
576 
577     SD_LOGN("\n============= card information ==============\n");
578     SD_LOGN("Card Type     : %s\n", type);
579     SD_LOGN("Card Spec Ver : %x.%x\n", spec_ver>>4, spec_ver&0xf);
580     SD_LOGN("Card RCA      : 0x%04x \n", (unsigned int)card->rca);
581     SD_LOGN("Card OCR      : 0x%x\n", (unsigned int)card->ocr.ocr);
582     SD_LOGN("    vol_window  : 0x%08x\n", (unsigned int)card->ocr.vol_window);
583     SD_LOGN("    to_1v8_acpt : %x\n", (unsigned int)card->ocr.to_1v8_acpt);
584     SD_LOGN("    high_capac  : %x\n", (unsigned int)card->ocr.high_capacity);
585     SD_LOGN("Card CSD      :\n");
586     SD_LOGN("    speed       : %u KHz\n", (unsigned int)speed_hz/1000);
587 #if ((defined CONFIG_USE_SD) || (defined CONFIG_USE_MMC))
588     SD_LOGN("    cmd class   : 0x%x\n", (unsigned int)card->csd.cmdclass);
589     SD_LOGN("    capacity    : %dMB\n", (unsigned int)card->csd.capacity/1024);
590     SD_LOGN("Card CUR_STA  :\n");
591     SD_LOGN("    speed_mode  : %s\n", speed_mode);
592     SD_LOGN("    bus_width   : %d\n", (unsigned int)card->bus_width);
593     SD_LOGN("    speed_class : %d\n", (unsigned int)card->speed_class);
594 #else
595     (void)speed_mode;
596 #endif
597     SD_LOGN("=============================================\n");
598     (void)spec_ver;
599     (void)type;
600     (void)speed_hz;
601     (void)speed_mode;
602 
603     mmc_card_set_present(card);
604 }
605 
606 #if ((defined CONFIG_USE_SD) || (defined CONFIG_USE_MMC))
mmc_set_blocklen(struct mmc_card * card,unsigned int blocklen)607 int mmc_set_blocklen(struct mmc_card *card, unsigned int blocklen)
608 {
609     struct mmc_command cmd = {0};
610 
611     if (mmc_card_blockaddr(card) || mmc_card_ddr_mode(card))
612         return 0;
613 
614     cmd.opcode = MMC_SET_BLOCKLEN;
615     cmd.arg = blocklen;
616     cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
617     return mmc_wait_for_cmd(card->host, &cmd);
618 }
619 
620 /**
621  * @brief read SD card.
622  * @param card:
623  *        @arg card->card handler.
624  * @param buf:
625  *        @arg buf->for store readed data.
626  * @param sblk:
627  *        @arg sblk->start block num.
628  * @param nblk:
629  *        @arg nblk->number of blocks.
630  * @retval  0 if success or other if failed.
631  */
mmc_block_read(struct mmc_card * card,uint8_t * buf,uint64_t sblk,uint32_t nblk)632 int32_t mmc_block_read(struct mmc_card *card, uint8_t *buf, uint64_t sblk, uint32_t nblk)
633 {
634     int32_t err;
635     struct scatterlist sg = {0};
636 
637     if (!card || !card->host) {
638         SD_LOGE_RAW(ROM_ERR_MASK, "%s,%d err", __func__, __LINE__);
639         return -1;
640     }
641 
642     if (nblk > SDXC_MAX_TRANS_LEN/512) {
643         SD_LOGW("%s only support len < %d\n", __func__, SDXC_MAX_TRANS_LEN/512);
644         return -1;
645     }
646 
647     if (card->suspend) {
648         SD_LOGW("%s id:%d has suspend\n", __func__, (unsigned int)card->id);
649         return -1;
650     }
651 
652     mmc_claim_host(card->host);
653 
654     err = mmc_set_blocklen(card, 512);
655     if (err)
656         goto out;
657 
658     sg.len = 512 * nblk;
659     sg.buffer = buf;
660     //if ((unsigned int)buf & 0x03) {
661     //  SD_LOGW("%s buf not align 4!!!\n", __func__);
662     //  return -1;
663     //}
664 
665     err = __sdmmc_block_rw(card, sblk, nblk, 1, &sg, 0);
666 
667 out:
668     mmc_release_host(card->host);
669     return err;
670 }
671 
672 /**
673  * @brief write SD card.
674  * @param card:
675  *        @arg card->card handler.
676  * @param buf:
677  *        @arg buf->data will be write.
678  * @param sblk:
679  *        @arg sblk->start block num.
680  * @param nblk:
681  *        @arg nblk->number of blocks.
682  * @retval  0 if success or other if failed.
683  */
mmc_block_write(struct mmc_card * card,const uint8_t * buf,uint64_t sblk,uint32_t nblk)684 int32_t mmc_block_write(struct mmc_card *card, const uint8_t *buf, uint64_t sblk, uint32_t nblk)
685 {
686     int32_t err;
687     struct scatterlist sg = {0};
688 
689     if (!card || !card->host) {
690         SD_LOGE_RAW(ROM_ERR_MASK, "%s,%d err", __func__, __LINE__);
691         return -1;
692     }
693 
694     if (nblk > SDXC_MAX_TRANS_LEN/512) {
695         SD_LOGW("%s only support block number < %d\n", __func__, SDXC_MAX_TRANS_LEN/512);
696         return -1;
697     }
698 
699     if (card->suspend) {
700         SD_LOGW("%s id:%d has suspend\n", __func__, (unsigned int)card->id);
701         return -1;
702     }
703 
704     mmc_claim_host(card->host);
705 
706     err = mmc_set_blocklen(card, 512);
707     if (err)
708         goto out;
709 
710     sg.len = 512 * nblk;
711     sg.buffer = (uint8_t *)buf;
712     //if ((unsigned int)buf & 0x03) {
713     //  SD_LOGW("%s buf not align 4!!!\n", __func__);
714     //  return -1;
715     //}
716 
717     err = __sdmmc_block_rw(card, sblk, nblk, 1, &sg, 1);
718 
719 out:
720     mmc_release_host(card->host);
721     return err;
722 }
723 #endif
724 
mmc_send_relative_addr(struct mmc_host * host,uint32_t * rca)725 int32_t mmc_send_relative_addr(struct mmc_host *host, uint32_t *rca)
726 {
727     struct mmc_command cmd = {0};
728 
729     cmd.opcode = SD_SEND_RELATIVE_ADDR;
730     cmd.arg = 0;
731     cmd.flags = MMC_RSP_R6 | MMC_CMD_BCR;
732 
733     do {
734         if (mmc_wait_for_cmd(host, &cmd)) {
735             return -1;
736         }
737         *rca = cmd.resp[0] >> 16;
738     } while (!*rca);
739 
740     return 0;
741 }
742 
mmc_send_if_cond(struct mmc_host * host,uint32_t ocr)743 int32_t mmc_send_if_cond(struct mmc_host *host, uint32_t ocr)
744 {
745     struct mmc_command cmd = {0};
746     int32_t err;
747     static const uint8_t test_pattern = 0xAA;
748     uint8_t result_pattern;
749 
750     /*
751      * To support SD 2.0 cards, we must always invoke SD_SEND_IF_COND
752      * before SD_APP_OP_COND. This command will harmlessly fail for
753      * SD 1.0 cards.
754      */
755     cmd.opcode = SD_SEND_IF_COND;
756     cmd.arg = ((ocr & 0xFF8000) != 0) << 8 | test_pattern;
757     cmd.flags = MMC_RSP_SPI_R7 | MMC_RSP_R7 | MMC_CMD_BCR;
758 
759     err = mmc_wait_for_cmd(host, &cmd);
760     if (err)
761         return err;
762 
763     result_pattern = cmd.resp[0] & 0xFF;
764 
765     if (result_pattern != test_pattern)
766         return -1;
767 
768     return 0;
769 }
770 
mmc_select_card(struct mmc_card * card,uint32_t select)771 int32_t mmc_select_card(struct mmc_card *card, uint32_t select)
772 {
773     struct mmc_command cmd = {0};
774 
775     cmd.opcode = MMC_SELECT_CARD;
776     if (select) {
777         cmd.arg = card->rca << 16;
778         cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
779     } else {
780         cmd.arg = 0;
781         cmd.flags = MMC_RSP_NONE | MMC_CMD_AC;
782     }
783 
784     if (mmc_wait_for_cmd(card->host, &cmd)) {
785         return -1;
786     }
787 
788     return 0;
789 }
790 
mmc_all_send_cid(struct mmc_host * host,uint32_t * cid)791 int32_t mmc_all_send_cid(struct mmc_host *host, uint32_t *cid)
792 {
793     int32_t err;
794     struct mmc_command cmd = {0};
795 
796     if (!host || !cid) {
797         SDC_LOGE_RAW(ROM_ERR_MASK, "%s,%d err", __func__, __LINE__);
798         return -1;
799     }
800 
801     cmd.opcode = MMC_ALL_SEND_CID;
802     cmd.arg = 0;
803     cmd.flags = MMC_RSP_R2 | MMC_CMD_BCR;
804 
805     err = mmc_wait_for_cmd(host, &cmd);
806     if (err)
807         return err;
808 
809     HAL_Memcpy(cid, cmd.resp, sizeof(uint32_t) * 4);
810 
811     return 0;
812 }
813 
814 #ifdef CONFIG_SD_PM
815 /*
816  * Assign a mmc bus handler to a host. Only one bus handler may control a
817  * host at any given time.
818  */
mmc_attach_bus(struct mmc_host * host,const struct mmc_bus_ops * ops)819 void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops)
820 {
821     unsigned long flags;
822 
823     if (!host || !ops) {
824         SDC_LOGE_RAW(ROM_ERR_MASK, "%s,%d err", __func__, __LINE__);
825         return ;
826     }
827 
828     flags = HAL_EnterCriticalSection();
829     SD_WARN_ON(host->bus_ops);
830     host->bus_ops = ops;
831     HAL_ExitCriticalSection(flags);
832 }
833 
834 /*
835  * Remove the current bus handler from a host.
836  */
mmc_detach_bus(struct mmc_host * host)837 void mmc_detach_bus(struct mmc_host *host)
838 {
839     unsigned long flags;
840 
841     if (!host) {
842         SDC_LOGE_RAW(ROM_ERR_MASK, "%s err\n", __func__);
843         return ;
844     }
845 
846     if (!host->bus_ops)
847         return ;
848 
849     flags = HAL_EnterCriticalSection();
850     host->bus_ops = NULL;
851     HAL_ExitCriticalSection(flags);
852 }
853 
854 #endif
855 
856 /**
857  * @brief scan or rescan SD card.
858  * @param card:
859  *        @arg card->card handler.
860  * @param sdc_id:
861  *        @arg sdc_id->SDC ID which card on.
862  * @retval  0 if success or other if failed.
863  */
mmc_rescan(struct mmc_card * card,uint32_t sdc_id)864 int32_t mmc_rescan(struct mmc_card *card, uint32_t sdc_id)
865 {
866     int32_t err = -1;
867     struct mmc_host *host = hal_sdc_open(sdc_id);
868 
869     if (!host) {
870         SD_LOGE_RAW(ROM_ERR_MASK, "%s init sdc host first!!\n", __func__);
871         return -1;
872     }
873 
874     card->host = host;
875 
876     mmc_claim_host(host);
877 
878     mmc_power_up(host);
879 
880     /* set identification clock 400KHz */
881     HAL_SDC_Update_Clk(card->host, 400000);
882 
883     /* Initialization should be done at 3.3 V I/O voltage. */
884     //mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_330, 0);
885 
886     /*
887      * sdio_reset sends CMD52 to reset card.  Since we do not know
888      * if the card is being re-initialized, just send it.  CMD52
889      * should be ignored by SD/eMMC cards.
890      */
891 #ifdef CONFIG_USE_SDIO
892     if (!card->type || card->type == MMC_TYPE_SDIO || card->type == MMC_TYPE_SD_COMBO)
893         sdio_reset(host);
894 #endif
895     mmc_go_idle(host);
896 
897 #ifdef CONFIG_USE_SD
898     /* cmd8 for SD2.0 */
899     if (mmc_send_if_cond(host, host->ocr_avail)) {
900         SD_LOGN("sd1.0 or mmc\n");
901     }
902 #endif
903 
904     /* Order's important: probe SDIO, then SD, then MMC */
905 #ifdef CONFIG_USE_SDIO
906     if (!card->type || card->type == MMC_TYPE_SDIO || card->type == MMC_TYPE_SD_COMBO) {
907         SD_LOGN("***** Try sdio *****\n");
908         if (!mmc_attach_sdio(card, host)){
909             SD_LOGN("***** sdio init ok *****\n");
910             err = 0;
911             goto out;
912         }
913     }
914 #endif
915 #ifdef CONFIG_USE_SD
916     if (!card->type || card->type == MMC_TYPE_SD) {
917         SD_LOGN("***** Try sd *****\n");
918         if (!mmc_attach_sd(card, host)){
919             SD_LOGN("***** sd init ok *****\n");
920             err = 0;
921             goto out;
922         }
923     }
924 #endif
925 #ifdef CONFIG_USE_MMC
926     if (!card->type || card->type == MMC_TYPE_MMC) {
927         SD_LOGN("***** Try mmc *****\n");
928         if (!mmc_attach_mmc(card, host)){
929             SD_LOGN("***** mmc init ok *****\n");
930             err = 0;
931             goto out;
932         }
933     }
934 #endif
935 
936     SD_LOGD("Undown Card Detected!!\n");
937 
938 #ifdef CONFIG_USE_SD
939     mmc_deattach_sd(card, host);
940 #endif
941 
942     mmc_power_off(host);
943 
944 out:
945     mmc_release_host(host);
946     return err;
947 }
948 
949 /**
950  * @brief deinit SD card.
951  * @param card:
952  *        @arg card->card handler.
953  * @retval  0 if success or other if failed.
954  */
mmc_card_deinit(struct mmc_card * card)955 int32_t mmc_card_deinit(struct mmc_card *card)
956 {
957     struct mmc_host *host;
958 
959     if (!card || !card->host) {
960         SD_LOGE_RAW(ROM_ERR_MASK, "%s err\n", __func__);
961         return -1;
962     }
963 
964     host = card->host;
965 
966 #ifdef CONFIG_USE_SDIO
967     if (mmc_card_sdio(card) || mmc_card_sd_combo(card))
968         mmc_deattach_sdio(card, host);
969 #endif
970 #ifdef CONFIG_USE_SD
971     if (mmc_card_sd(card))
972         mmc_deattach_sd(card, host);
973 #endif
974 
975     mmc_power_off(host);
976     hal_sdc_close(host->sdc_id);
977 
978     return 0;
979 }
980 
981 static struct mmc_card *card_info[SDC_NUM];
982 
get_mmc_card_func(uint8_t card_id)983 struct sdio_func ** get_mmc_card_func(uint8_t card_id)
984 {
985     struct mmc_card *card = card_info[card_id];
986     if(card == NULL) {
987         return NULL;
988     }else {
989         return card->sdio_func;
990     }
991 }
992 
993 /**
994  * @brief malloc for card_info.
995  * @param card_id:
996  *        @arg card ID.
997  * @retval  0 if success or other if failed.
998  */
mmc_card_create(uint8_t card_id,SDCard_InitTypeDef * param)999 int32_t mmc_card_create(uint8_t card_id, SDCard_InitTypeDef *param)
1000 {
1001     int ret = 0;
1002     struct mmc_card *card = card_info[card_id];
1003 
1004     if (card != NULL) {
1005         if (card->id == card_id) {
1006             SD_LOGW_RAW(param->debug_mask, "%s id:%d already!!\n", __func__, card_id);
1007             return 0;
1008         } else {
1009             SD_LOGE_RAW(param->debug_mask, "%s id:%d unvalid card id!!\n", __func__, card_id);
1010             return -1;
1011         }
1012     }
1013 
1014     card = HAL_Malloc(sizeof(struct mmc_card));
1015     if (card == NULL) {
1016         SD_LOGE_RAW(param->debug_mask, "%s malloc fail\n", __func__);
1017         ret = -1;
1018     } else {
1019         SDC_Memset(card, 0, sizeof(struct mmc_card));
1020         SDC_MutexCreate(&card->mutex);
1021         SDC_MutexLock(&card->mutex, OS_WAIT_FOREVER);
1022         card->id = card_id;
1023         printf("card id is %d\n", (unsigned int)card->id);
1024         card->ref = 0;
1025         card->debug_mask = param->debug_mask;
1026         if (param->type < MMC_TYPE_MAX)
1027             card->type = param->type;
1028         card_info[card_id] = card;
1029         SDC_MutexUnlock(&card->mutex);
1030         SD_LOGN("%s card:%p id:%d\n", __func__, card, (unsigned int)card->id);
1031     }
1032 
1033     return ret;
1034 }
1035 
1036 /**
1037  * @brief free for card_info.
1038  * @param card_id:
1039  *        @arg card ID.
1040  * @retval  0 if success or other if failed.
1041  */
mmc_card_delete(uint8_t card_id)1042 int32_t mmc_card_delete(uint8_t card_id)
1043 {
1044     int ret = -1;
1045     struct mmc_card *card = card_info[card_id];
1046 
1047     if (card == NULL || card->id != card_id) {
1048         SD_LOGW_RAW(ROM_WRN_MASK, "%s card not exit! card:%p id:%d del_id:%d\n", \
1049                 __func__, card, (unsigned int)card->id, (unsigned int)card_id);
1050         return -1;
1051     }
1052 
1053     SDC_MutexLock(&card->mutex, OS_WAIT_FOREVER);
1054     if (card->ref != 0) {
1055         SDC_MutexUnlock(&card->mutex);
1056         SD_LOGW("%s fail, ref:%d\n", __func__, (unsigned int)card->ref);
1057         goto out;
1058     }
1059     card_info[card_id] = NULL;
1060     SDC_MutexUnlock(&card->mutex);
1061     SDC_MutexDelete(&card->mutex);
1062     SD_LOGN("%s card:%p id:%d\n", __func__, card, (unsigned int)card->id);
1063     HAL_Free(card);
1064     ret = 0;
1065 
1066 out:
1067 
1068     return ret;
1069 }
1070 
1071 /**
1072  * @brief get pointer of mmc_card.
1073  * @param card_id:
1074  *        @arg card ID.
1075  * @retval  pointer of mmc_card if success or NULL if failed.
1076  */
mmc_card_open(uint8_t card_id)1077 struct mmc_card *mmc_card_open(uint8_t card_id)
1078 {
1079     struct mmc_card *card = card_info[card_id];
1080 
1081     if (card == NULL || card->id != card_id) {
1082         SD_LOGW_RAW(ROM_WRN_MASK, "%s card not exit! id:%d\n",  __func__, card_id);
1083         return NULL;
1084     }
1085 
1086     SDC_MutexLock(&card->mutex, OS_WAIT_FOREVER);
1087     card->ref++;
1088     SDC_MutexUnlock(&card->mutex);
1089     SD_LOGD("%s card:%p id:%d\n", __func__, card, (unsigned int)card->id);
1090 
1091     return card;
1092 }
1093 
1094 /**
1095  * @brief close mmc_card.
1096  * @param card_id:
1097  *        @arg card ID.
1098  * @retval  0 if success or other if failed.
1099  */
mmc_card_close(uint8_t card_id)1100 int32_t mmc_card_close(uint8_t card_id)
1101 {
1102     struct mmc_card *card = card_info[card_id];
1103 
1104     if (card == NULL || card->id != card_id || card->ref <= 0) {
1105         SD_LOGW_RAW(ROM_WRN_MASK, "%s fail! id:%d\n",  __func__, card_id);
1106         return -1;
1107     }
1108 
1109     SDC_MutexLock(&card->mutex, OS_WAIT_FOREVER);
1110     card->ref--;
1111     SDC_MutexUnlock(&card->mutex);
1112     SD_LOGD("%s card:%p id:%d\n", __func__, card, (unsigned int)card->id);
1113 
1114     return 0;
1115 }
1116 
1117 extern struct sdio_func *sdio_alloc_func(struct mmc_card *card);
1118 extern int32_t sdio_add_func(struct sdio_func *func, uint32_t id);
1119 extern int mmc_sdio_remove(struct mmc_host *host);
1120 #define SDIO_DEBUG_SAVE_CARD
1121 /**
1122  * @brief save mmc_card.
1123  * @param card_id:
1124  *        @arg card ID.
1125  * @retval  0 if success or other if failed.
1126  */
mmc_card_save(uint8_t card_id)1127 struct mmc_card_info* mmc_card_save(uint8_t card_id)
1128 {
1129     struct mmc_card_info *s_card_info;
1130     struct mmc_card *card = card_info[card_id];
1131     int32_t funcs;
1132     int32_t i;
1133 
1134     s_card_info = HAL_Malloc(sizeof(struct mmc_card_info));
1135     if (s_card_info == NULL) {
1136         SD_LOGE("%s malloc fail\n", __func__);
1137         goto out;
1138     }
1139     //mmc_card_create(uint8_t card_id, SDCard_InitTypeDef *param);
1140     memcpy(&(s_card_info->card), card, sizeof(struct mmc_card));
1141 #ifdef CONFIG_SDIO_USE_FUNS
1142     for (i = 0; i < card->sdio_funcs; i++) {
1143         memcpy(&s_card_info->sdio_func[i], card->sdio_func[i], sizeof(struct sdio_func));
1144     }
1145 #else
1146     (void)funcs;
1147 #endif
1148     s_card_info->sdc_id = card->host->sdc_id;
1149 
1150 out:
1151     return s_card_info;
1152 }
1153 
sdio_init_func_by_func(struct mmc_card * card,uint32_t fn,struct sdio_func * sdio_func)1154 static int32_t sdio_init_func_by_func(struct mmc_card *card, uint32_t fn, struct sdio_func *sdio_func)
1155 {
1156     int32_t ret;
1157     struct sdio_func *func;
1158 
1159     if (fn > SDIO_MAX_FUNCS)
1160         SD_LOGE("%s,%d wrong fn:%ld!\n", __func__, __LINE__, HAL_PR_SZ_L(fn));
1161 
1162     func = sdio_alloc_func(card);
1163     if (!func)
1164         return -1;
1165 
1166     func->num = fn;
1167     memcpy(func, sdio_func, sizeof(struct sdio_func));
1168 
1169     card->sdio_func[fn - 1] = func;
1170     printf("func address %p\n",func);
1171     return 0;
1172 }
1173 /**
1174  * @brief restore mmc_card.
1175  * @param card_id:
1176  *        @arg card ID.
1177  * @retval  0 if success or other if failed.
1178  */
mmc_card_restore(struct mmc_card_info * s_card_info)1179 int32_t mmc_card_restore(struct mmc_card_info *s_card_info)
1180 {
1181     OS_Mutex_t *tmp_mutex;
1182     uint32_t card_id = s_card_info->card.id;
1183     uint32_t sdc_id = s_card_info->sdc_id;
1184     struct mmc_card *card = card_info[card_id];
1185     int32_t ret;
1186     struct mmc_host *host = hal_sdc_open(sdc_id);
1187     int32_t funcs;
1188     int32_t i;
1189     int err = -1;
1190     SDCard_InitTypeDef card_param;
1191 
1192     card_param.debug_mask = (ROM_INF_MASK | \
1193                          ROM_WRN_MASK | ROM_ERR_MASK | ROM_ANY_MASK);
1194     if (!host) {
1195         SD_LOGE_RAW(ROM_ERR_MASK, "%s init sdc host first!!\n", __func__);
1196         return -1;
1197     }
1198 
1199     /********reinit mmc_card struct*******/
1200     if (mmc_card_create(card_id, &card_param))
1201         goto create_fail;
1202     card = card_info[card_id];
1203     tmp_mutex = &card->mutex;
1204     s_card_info->card.mutex = *tmp_mutex;
1205     s_card_info->card.ref = 0;
1206     memcpy(card, &(s_card_info->card), sizeof(struct mmc_card));
1207     card->host = host;
1208     /********restore host config*********/
1209     mmc_claim_host(host);
1210 
1211     mmc_power_up(host);
1212 
1213     {
1214         /*
1215          * Switch to high-speed (if supported).
1216          * state have been restore
1217          */
1218         //mmc_card_set_highspeed(card);
1219         //SD_LOGN("sdio highspeed \n");
1220 
1221         /*
1222          * Change to the card's maximum speed.
1223          */
1224         HAL_SDC_Update_Clk(card->host, mmc_sdio_get_max_clock(card));
1225 
1226     /*
1227          * Switch to wider bus (if supported).
1228         */
1229     /* sd Host Controller Register SDXC_REG_WIDTH */
1230         HAL_SDC_Set_BusWidth(card->host, MMC_BUS_WIDTH_4);
1231         SD_LOGN("%s bus width type:%d\n", __func__, MMC_BUS_WIDTH_4);
1232     }
1233     card->host->card = card;
1234     funcs = (card->ocr.ocr >> 28) & 0x7;
1235 
1236     SD_LOGD("Number of I/O Functions: %02lx\n", HAL_PR_SZ_L(funcs));
1237     /*
1238      * Initialize (but don't add) all present functions.
1239      */
1240 #ifdef CONFIG_SDIO_USE_FUNS
1241     card->sdio_funcs = 0;
1242     for (i = 0; i < funcs; i++, card->sdio_funcs++) {
1243         err = sdio_init_func_by_func(card, i + 1, &(s_card_info->sdio_func[i]));
1244     if (err)
1245             goto remove;
1246     }
1247 #endif
1248     /*
1249      * First add the card to the driver model...
1250      */
1251     mmc_release_host(host);
1252 
1253     if (host->caps & MMC_CAP_SDIO_IRQ) {
1254         int ret =  OS_SemaphoreCreateBinary(&host->sdio_irq_signal);
1255         SDC_BUG_ON(ret!=OS_OK);
1256     }
1257     mmc_add_card(card_info[card_id]);
1258 #ifdef CONFIG_SDIO_USE_FUNS
1259     for (i = 0; i < funcs; i++) {
1260         err = sdio_add_func(host->card->sdio_func[i], i);
1261         if (err)
1262             goto remove_added;
1263     }
1264 #else
1265     (void)funcs;
1266 #endif
1267 
1268     mmc_claim_host(host);
1269 #ifdef CONFIG_SD_PM
1270     if (!card->suspend) {
1271         mmc_attach_bus(host, &sdio_bus_ops);
1272     }
1273 #endif
1274     return 0;
1275 
1276 #ifdef CONFIG_SDIO_USE_FUNS
1277 remove_added:
1278     /* Remove without lock if the device has been added. */
1279     mmc_sdio_remove(host);
1280 #endif
1281     card->host->card = NULL;
1282     mmc_claim_host(host);
1283 
1284 #ifdef CONFIG_SDIO_USE_FUNS
1285 remove:
1286     /* And with lock if it hasn't been added. */
1287     mmc_release_host(host);
1288     if (host->card) {
1289         mmc_sdio_remove(host);
1290         card->host->card = NULL;
1291     }
1292     mmc_claim_host(host);
1293 #else
1294     card->host->card = NULL;
1295 #endif
1296 create_fail:
1297     SD_LOGE("%s: error %d whilst initialising SDIO card\n", __func__, err);
1298 
1299     return err;
1300 }
1301