summaryrefslogtreecommitdiff
path: root/bsp/phytium/libraries/drivers/drv_sdif.c
blob: 751b71e22795b6d082122d4d925c43e845f0b6d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
/*
 * Copyright (c) 2006-2023, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Email: [email protected]
 *
 * Change Logs:
 * Date        Author       Notes
 * 2023/7/11   liqiaozhong  init SD card and mount file system
 * 2023/11/8   zhugengyu    add interrupt handling for dma waiting, unify function naming
 * 2024/4/7    zhugengyu    support use two sdif device
 */

/***************************** Include Files *********************************/
#include "rtconfig.h"
#if defined(BSP_USING_SDIF_LAYER)

#include <rthw.h>
#include <rtdef.h>
#include <rtthread.h>
#include <rtdevice.h>
#include <rtdbg.h>
#include <drivers/dev_mmcsd_core.h>

#ifdef RT_USING_SMART
#include "ioremap.h"
#endif
#include "mm_aspace.h"
#include "interrupt.h"

#define LOG_TAG "sdif_drv"
#include "drv_log.h"

#include "ftypes.h"
#include "fparameters.h"
#include "fcpu_info.h"

#include "fsdif_timing.h"

#include "fsdif.h"
#include "fsdif_hw.h"

#include "drv_sdif.h"
/************************** Constant Definitions *****************************/
#define SDIF_CARD_TYPE_MICRO_SD 1
#define SDIF_CARD_TYPE_EMMC 2
#define SDIF_CARD_TYPE_SDIO 3

#define SDIF_DMA_BLK_SZ 512U
#define SDIF_MAX_BLK_TRANS 20U
#define SDIF_DMA_ALIGN SDIF_DMA_BLK_SZ

/* preserve pointer to host instance */
static struct rt_mmcsd_host *mmc_host[FSDIF_NUM] = {RT_NULL};
/**************************** Type Definitions *******************************/

typedef struct
{
    FSdif sdif;
    rt_int32_t sd_type;
    FSdifIDmaDesc *rw_desc;
    uintptr_t rw_desc_dma;
    rt_size_t rw_desc_num;
    struct rt_event event;
#define SDIF_EVENT_CARD_DETECTED (1 << 0)
#define SDIF_EVENT_COMMAND_DONE (1 << 1)
#define SDIF_EVENT_DATA_DONE (1 << 2)
#define SDIF_EVENT_ERROR_OCCUR (1 << 3)
#define SDIF_EVENT_SDIO_IRQ (1 << 4)
    void *aligned_buffer;
    uintptr_t aligned_buffer_dma;
    rt_size_t aligned_buffer_size;
    FSdifCmdData req_cmd;
    FSdifCmdData req_stop;
    FSdifData req_data;
} sdif_info_t;
/************************** Variable Definitions *****************************/

/***************** Macros (Inline Functions) Definitions *********************/

/******************************* Functions *********************************/
static void sdif_host_relax(void)
{
    rt_thread_mdelay(1);
}

void sdif_change(rt_uint32_t id)
{
    RT_ASSERT(id < FSDIF_NUM);
    if (mmc_host[id])
    {
        mmcsd_change(mmc_host[id]);
    }
}

rt_int32_t sdif_card_inserted(rt_uint32_t id)
{
    RT_ASSERT(id < FSDIF_NUM);
    if (mmc_host[id])
    {
        return mmc_host[id]->ops->get_card_status(mmc_host[id]);
    }

    return 0;
}

static void sdif_card_detect_callback(FSdif *const sdif, void *args, u32 status, u32 dmac_status)
{
    struct rt_mmcsd_host *host = (struct rt_mmcsd_host *)args;
    sdif_info_t *host_info = (sdif_info_t *)host->private_data;

    rt_event_send(&host_info->event, SDIF_EVENT_CARD_DETECTED);
    sdif_change(host_info->sdif.config.instance_id);
}

static void sdif_command_done_callback(FSdif *const sdif, void *args, u32 status, u32 dmac_status)
{
    struct rt_mmcsd_host *host = (struct rt_mmcsd_host *)args;
    sdif_info_t *host_info = (sdif_info_t *)host->private_data;

    rt_event_send(&host_info->event, SDIF_EVENT_COMMAND_DONE);
}

static void sdif_data_done_callback(FSdif *const sdif, void *args, u32 status, u32 dmac_status)
{
    struct rt_mmcsd_host *host = (struct rt_mmcsd_host *)args;
    sdif_info_t *host_info = (sdif_info_t *)host->private_data;

    rt_event_send(&host_info->event, SDIF_EVENT_DATA_DONE);
}

static void sdif_sdio_irq_callback(FSdif *const sdif, void *args, u32 status, u32 dmac_status)
{
    struct rt_mmcsd_host *host = (struct rt_mmcsd_host *)args;
    sdif_info_t *host_info = (sdif_info_t *)host->private_data;

    rt_event_send(&host_info->event, SDIF_EVENT_SDIO_IRQ);
}

static void sdif_error_occur_callback(FSdif *const sdif, void *args, u32 status, u32 dmac_status)
{
    struct rt_mmcsd_host *host = (struct rt_mmcsd_host *)args;
    sdif_info_t *host_info = (sdif_info_t *)host->private_data;
    LOG_E("Error occur !!!");
    LOG_E("Status: 0x%x, dmac status: 0x%x.", status, dmac_status);

    if (status & FSDIF_INT_RE_BIT)
        LOG_E("Response err. 0x%x", FSDIF_INT_RE_BIT);

    if (status & FSDIF_INT_RTO_BIT)
        LOG_E("Response timeout. 0x%x", FSDIF_INT_RTO_BIT);

    if (dmac_status & FSDIF_DMAC_STATUS_DU)
        LOG_E("Descriptor un-readable. 0x%x", FSDIF_DMAC_STATUS_DU);

    if (status & FSDIF_INT_DCRC_BIT)
        LOG_E("Data CRC error. 0x%x", FSDIF_INT_DCRC_BIT);

    if (status & FSDIF_INT_RCRC_BIT)
        LOG_E("Data CRC error. 0x%x", FSDIF_INT_RCRC_BIT);

    rt_event_send(&host_info->event, SDIF_EVENT_ERROR_OCCUR);
}

static rt_err_t sdif_pre_request(struct rt_mmcsd_host *host, struct rt_mmcsd_req *req)
{
    rt_err_t err = RT_EOK;
    sdif_info_t *host_info = (sdif_info_t *)host->private_data;

    if (host_info->sd_type != SDIF_CARD_TYPE_SDIO)
    {
        /* ignore SDIO detect command */
        if ((req->cmd->cmd_code == SD_IO_SEND_OP_COND) ||
            (req->cmd->cmd_code == SD_IO_RW_DIRECT))
        {
            req->cmd->err = -1;
            mmcsd_req_complete(host);
            err = RT_EEMPTY;
        }
    }

    if (host_info->sd_type == SDIF_CARD_TYPE_EMMC)
    {
        /* ignore micro SD detect command, not in eMMC spec. */
        if ((req->cmd->cmd_code == SD_APP_OP_COND) ||
            (req->cmd->cmd_code == APP_CMD))
        {
            req->cmd->err = -1;
            mmcsd_req_complete(host);
            err = RT_EEMPTY;
        }

        /* ignore mmcsd_send_if_cond(CMD-8) which will failed for eMMC
           but check cmd arg to let SEND_EXT_CSD (CMD-8) run */
        if ((req->cmd->cmd_code == SD_SEND_IF_COND) &&
            (req->cmd->arg == 0x1AA)) /* 0x1AA is the send_if_cond pattern, use it by care */
        {
            req->cmd->err = -1;
            mmcsd_req_complete(host);
            err = RT_EEMPTY;
        }
    }

    if ((req->cmd->cmd_code == READ_MULTIPLE_BLOCK) ||
        (req->cmd->cmd_code == WRITE_MULTIPLE_BLOCK)) /* set block count */
    {
        struct rt_mmcsd_req sbc;
        struct rt_mmcsd_cmd sbc_cmd;

        rt_memset(&sbc, 0, sizeof(sbc));
        rt_memset(&sbc_cmd, 0, sizeof(sbc_cmd));

        sbc_cmd.cmd_code = SET_BLOCK_COUNT;
        RT_ASSERT(req->data);
        sbc_cmd.arg = req->data->blks;
        sbc_cmd.flags = RESP_R1;

        LOG_I("set block_count = %d", req->data->blks);

        sbc.data = RT_NULL;
        sbc.cmd = &sbc_cmd;
        sbc.stop = RT_NULL;
        sbc.sbc = RT_NULL;
        mmcsd_send_request(host, &sbc);

        err = sbc_cmd.err;
        if (req->cmd->busy_timeout < 1000) /* in case rt-thread do not give wait timeout */
        {
            req->cmd->busy_timeout = 5000;
        }
    }

    return err;
}

static void sdif_convert_command_info(struct rt_mmcsd_host *host, struct rt_mmcsd_cmd *in_cmd, struct rt_mmcsd_data *in_data, FSdifCmdData *out_req)
{
    FSdifCmdData *out_cmd = out_req;
    FSdifData *out_data = out_req->data_p;
    sdif_info_t *host_info = (sdif_info_t *)host->private_data;

    uint32_t opcode = in_cmd->cmd_code;
    out_cmd->rawcmd = FSDIF_CMD_INDX_SET(opcode);

    if (in_cmd->cmd_code == GO_IDLE_STATE)
    {
        out_cmd->rawcmd |= FSDIF_CMD_INIT;
    }

    if (in_cmd->cmd_code == GO_INACTIVE_STATE)
    {
        out_cmd->rawcmd |= FSDIF_CMD_STOP_ABORT;
    }

    if (in_cmd->cmd_code == VOLTAGE_SWITCH)
    {
        out_cmd->rawcmd |= FSDIF_CMD_VOLT_SWITCH;
    }

    if (resp_type(in_cmd) != RESP_NONE)
    {
        out_cmd->rawcmd |= FSDIF_CMD_RESP_EXP;
        if (resp_type(in_cmd) == RESP_R2)
        {
            /* need 136 bits long response */
            out_cmd->rawcmd |= FSDIF_CMD_RESP_LONG;
        }

        if ((resp_type(in_cmd) != RESP_R3) &&
            (resp_type(in_cmd) != RESP_R4))
        {
            /* most cmds need CRC */
            out_cmd->rawcmd |= FSDIF_CMD_RESP_CRC;
        }
    }

    if (in_data)
    {
        RT_ASSERT(out_data);
        out_cmd->rawcmd |= FSDIF_CMD_DAT_EXP;

        if (in_data->flags & DATA_DIR_READ)
        {
            out_data->buf = (void *)in_data->buf;
            out_data->buf_dma = (uintptr_t)in_data->buf + PV_OFFSET;
        }
        else if (in_data->flags & DATA_DIR_WRITE)
        {
            out_cmd->rawcmd |= FSDIF_CMD_DAT_WRITE;
            out_data->buf = (void *)in_data->buf;
            out_data->buf_dma = (uintptr_t)in_data->buf + PV_OFFSET;
        }
        else
        {
            RT_ASSERT(0);
        }

        out_data->blksz = in_data->blksize;
        out_data->blkcnt = in_data->blks;
        out_data->datalen = in_data->blksize * in_data->blks;

        /* handle unaligned input buffer */
        if (out_data->buf_dma % SDIF_DMA_ALIGN)
        {
            RT_ASSERT(out_data->datalen <= host_info->aligned_buffer_size);
            out_data->buf = host_info->aligned_buffer;
            out_data->buf_dma = (uintptr_t)host_info->aligned_buffer + PV_OFFSET;

            if (in_data->flags & DATA_DIR_WRITE)
            {
                /* copy the data need to write to sd card */
                memcpy(out_data->buf, in_data->buf, out_data->datalen);
            }
        }

        LOG_D("buf@%p, blksz: %d, datalen: %ld",
              out_data->buf,
              out_data->blksz,
              out_data->datalen);
    }

    out_cmd->cmdidx = in_cmd->cmd_code;
    out_cmd->cmdarg = in_cmd->arg;
    LOG_D("cmdarg: 0x%x", out_cmd->cmdarg);
}

static rt_err_t sdif_do_transfer(struct rt_mmcsd_host *host, FSdifCmdData *req_cmd, rt_int32_t timeout_ms)
{
    FError ret = FT_SUCCESS;
    sdif_info_t *host_info = (sdif_info_t *)host->private_data;
    rt_uint32_t event = 0U;
    rt_uint32_t wait_event = 0U;

    LOG_I("cmd-%d sending", req_cmd->cmdidx);

    if (req_cmd->data_p == RT_NULL)
    {
        wait_event = SDIF_EVENT_COMMAND_DONE;
    }
    else
    {
        wait_event = SDIF_EVENT_COMMAND_DONE | SDIF_EVENT_DATA_DONE;
    }

    if (req_cmd->data_p)
    {
        ret = FSdifSetupDMADescriptor(&host_info->sdif, req_cmd->data_p);
        if (ret != FT_SUCCESS)
        {
            LOG_E("FSdifSetupDMADescriptor fail.");
            return -RT_ERROR;
        }
    }

    ret = FSdifDMATransfer(&host_info->sdif, req_cmd);
    if (ret != FT_SUCCESS)
    {
        LOG_E("FSdifDMATransfer() fail. ret = 0x%x", ret);
        return -RT_ERROR;
    }

    while (TRUE)
    {
        /*
         * transfer without data: wait COMMAND_DONE event
         * transfer with data: wait COMMAND_DONE and DATA_DONE event
         */
        if (rt_event_recv(&host_info->event,
                          (wait_event),
                          (RT_EVENT_FLAG_AND | RT_EVENT_FLAG_CLEAR),
                          rt_tick_from_millisecond(1000),
                          &event) == RT_EOK)
        {
            (void)FSdifGetCmdResponse(&host_info->sdif, req_cmd);
            break;
        }

        /*
         * transfer with error: check if ERROR_OCCUR event exists, no wait
         */
        if (rt_event_recv(&host_info->event,
                          (SDIF_EVENT_ERROR_OCCUR),
                          (RT_EVENT_FLAG_AND | RT_WAITING_NO),
                          0,
                          &event) == RT_EOK)
        {
            LOG_E("Sdif DMA transfer endup with error !!!");
            return -RT_EIO;
        }

        timeout_ms -= 1000;
        if (timeout_ms <= 0)
        {
            LOG_E("Sdif DMA transfer endup with timeout !!!");
            return -RT_EIO;
        }
    }

    return RT_EOK;
}

static void sdif_send_request(struct rt_mmcsd_host *host, struct rt_mmcsd_req *req)
{
    sdif_info_t *host_info = (sdif_info_t *)host->private_data;
    FSdifCmdData *req_cmd = &host_info->req_cmd;
    FSdifData *req_data = &host_info->req_data;

    rt_err_t err = sdif_pre_request(host, req);
    if (err != RT_EOK)
    {
        return;
    }

    rt_memset(req_cmd, 0, sizeof(FSdifCmdData));

    if (req->data)
    {
        rt_memset(req_data, 0, sizeof(FSdifData));
        req_cmd->data_p = req_data;
    }
    else
    {
        req_cmd->data_p = RT_NULL;
    }

    sdif_convert_command_info(host, req->cmd, req->data, req_cmd);

    req->cmd->err = sdif_do_transfer(host, req_cmd, req->cmd->busy_timeout);

    if (resp_type(req->cmd) & RESP_MASK)
    {
        if (resp_type(req->cmd) == RESP_R2)
        {
            req->cmd->resp[3] = req_cmd->response[0];
            req->cmd->resp[2] = req_cmd->response[1];
            req->cmd->resp[1] = req_cmd->response[2];
            req->cmd->resp[0] = req_cmd->response[3];
        }
        else
        {
            req->cmd->resp[0] = req_cmd->response[0];
        }
    }

    if (req->data && (req->data->flags & DATA_DIR_READ))
    {
        /* if it is read sd card, copy data to unaligned buffer and return */
        if ((uintptr)req->data->buf % SDIF_DMA_ALIGN)
        {
            rt_memcpy((void *)req->data->buf,
                      (void *)host_info->aligned_buffer,
                      req_cmd->data_p->datalen);
        }
    }

    mmcsd_req_complete(host);
}

static void sdif_set_iocfg(struct rt_mmcsd_host *host, struct rt_mmcsd_io_cfg *io_cfg)
{
    sdif_info_t *host_info = (sdif_info_t *)host->private_data;
    FSdif *sdif = &host_info->sdif;
    uintptr base_addr = sdif->config.base_addr;

    if (0 != io_cfg->clock)
    {
        // boolean is_ddr = FALSE;
        // if (host->card->type == CARD_TYPE_MMC)
        // {
        //     if (io_cfg->timing == MMCSD_TIMING_MMC_HS400 ||
        //         io_cfg->timing ==  MMCSD_TIMING_MMC_HS400_ENH_DS)
        //     {
        //         is_ddr = TRUE;
        //     }
        // }
        // else if (host->card->type == CARD_TYPE_SD)
        // {
        //     if (io_cfg->timing == MMCSD_TIMING_UHS_DDR50)
        //     {
        //         is_ddr = TRUE;
        //     }
        // }

        // if (FSDIF_SUCCESS != FSdifSetClkFreqByCalc(&dev->hc, is_ddr, io_cfg->clock))
        // {
        //     LOG_E("FSdifSetClkFreqByCalc fail.")
        // }

        FSdifTiming timing;
        FError ret;
        boolean is_ddr = FALSE;
        memset(&timing, 0U, sizeof(timing));
        /* Get the timing setting based on the clock frequency and device removability */
        ret = FSdifGetTimingSetting(io_cfg->clock, sdif->config.non_removable, &timing);
        if (ret != FT_SUCCESS)
        {
            LOG_E("Failed to find timing for clock-%d", io_cfg->clock);
        }
        /* Set the clock frequency using the obtained timing setting */
        ret = FSdifSetClkFreqByDict(sdif, FALSE, &timing, io_cfg->clock);
        if (ret != FT_SUCCESS)
        {
            LOG_E("FSdifSetClkFreq fail.");
        }
    }

    switch (io_cfg->bus_width)
    {
    case MMCSD_BUS_WIDTH_1:
        FSdifSetBusWidth(base_addr, 1U);
        break;
    case MMCSD_BUS_WIDTH_4:
        FSdifSetBusWidth(base_addr, 4U);
        break;
    case MMCSD_BUS_WIDTH_8:
        FSdifSetBusWidth(base_addr, 8U);
        break;
    default:
        LOG_E("Invalid bus width %d", io_cfg->bus_width);
        break;
    }
}

static rt_int32_t sdif_card_status(struct rt_mmcsd_host *host)
{
    sdif_info_t *host_info = (sdif_info_t *)host->private_data;
    FSdif *sdif = &host_info->sdif;
    uintptr base_addr = sdif->config.base_addr;

    return FSdifCheckIfCardExists(base_addr) ? 1 : 0;
}

static const struct rt_mmcsd_host_ops ops =
    {
        .request = sdif_send_request,
        .set_iocfg = sdif_set_iocfg,
        .get_card_status = sdif_card_status,
        .enable_sdio_irq = RT_NULL,
        .execute_tuning = RT_NULL,
};

static void sdif_ctrl_setup_interrupt(struct rt_mmcsd_host *host)
{
    sdif_info_t *host_info = (sdif_info_t *)host->private_data;
    FSdif *sdif = &(host_info->sdif);
    FSdifConfig *config_p = &sdif->config;
    rt_uint32_t cpu_id = rt_hw_cpu_id();

    rt_hw_interrupt_set_target_cpus(config_p->irq_num, cpu_id);
    rt_hw_interrupt_set_priority(config_p->irq_num, 0xd0);

    /* register intr callback */
    rt_hw_interrupt_install(config_p->irq_num,
                            FSdifInterruptHandler,
                            sdif,
                            NULL);

    /* enable irq */
    rt_hw_interrupt_umask(config_p->irq_num);

    FSdifRegisterEvtHandler(sdif, FSDIF_EVT_CARD_DETECTED, sdif_card_detect_callback, host);
    FSdifRegisterEvtHandler(sdif, FSDIF_EVT_ERR_OCCURE, sdif_error_occur_callback, host);
    FSdifRegisterEvtHandler(sdif, FSDIF_EVT_CMD_DONE, sdif_command_done_callback, host);
    FSdifRegisterEvtHandler(sdif, FSDIF_EVT_DATA_DONE, sdif_data_done_callback, host);
    FSdifRegisterEvtHandler(sdif, FSDIF_EVT_SDIO_IRQ, sdif_sdio_irq_callback, host);

    return;
}

static rt_err_t sdif_host_init(rt_uint32_t id, rt_uint32_t type)
{
    struct rt_mmcsd_host *host = RT_NULL;
    sdif_info_t *host_info = RT_NULL;
    const FSdifConfig *default_sdif_config = RT_NULL;
    FSdifConfig sdif_config;
    rt_err_t result = RT_EOK;

    host = mmcsd_alloc_host();
    if (!host)
    {
        LOG_E("Alloc host failed");
        result = RT_ENOMEM;
        goto err_free;
    }

    host_info = rt_malloc(sizeof(sdif_info_t));
    if (!host_info)
    {
        LOG_E("Malloc host_info failed");
        result = RT_ENOMEM;
        goto err_free;
    }
    rt_memset(host_info, 0, sizeof(*host_info));

    result = rt_event_init(&host_info->event, "sdif_event", RT_IPC_FLAG_FIFO);
    RT_ASSERT(RT_EOK == result);

    host_info->aligned_buffer_size = SDIF_DMA_BLK_SZ * SDIF_MAX_BLK_TRANS;
    host_info->aligned_buffer = rt_malloc_align(host_info->aligned_buffer_size,
                                                SDIF_DMA_ALIGN);
    if (!host_info->aligned_buffer)
    {
        LOG_E("Malloc aligned buffer failed");
        result = RT_ENOMEM;
        goto err_free;
    }

    host_info->aligned_buffer_dma = (uintptr_t)host_info->aligned_buffer + PV_OFFSET;
    rt_memset(host_info->aligned_buffer, 0, host_info->aligned_buffer_size);

    host_info->rw_desc_num = (SDIF_DMA_BLK_SZ * SDIF_MAX_BLK_TRANS) / FSDIF_IDMAC_MAX_BUF_SIZE + 1;
    host_info->rw_desc = rt_malloc_align(host_info->rw_desc_num * sizeof(FSdifIDmaDesc),
                                         SDIF_DMA_ALIGN);
    if (!host_info->rw_desc)
    {
        LOG_E("Malloc rw_desc failed");
        result = RT_ENOMEM;
        goto err_free;
    }

    host_info->rw_desc_dma = (uintptr_t)host_info->rw_desc + PV_OFFSET;
    rt_memset(host_info->rw_desc, 0, host_info->rw_desc_num * sizeof(FSdifIDmaDesc));

    /* host data init */
    host->ops = &ops;
    host->freq_min = FSDIF_CLK_SPEED_400KHZ;
    if (type == SDIF_CARD_TYPE_MICRO_SD)
    {
        host->freq_max = FSDIF_CLK_SPEED_50_MHZ;
    }
    else
    {
        host->freq_max = FSDIF_CLK_SPEED_52_MHZ;
    }

    host->valid_ocr = VDD_32_33 | VDD_33_34; /* voltage 3.3v */
    host->flags = MMCSD_MUTBLKWRITE | MMCSD_BUSWIDTH_4;
    host->max_seg_size = SDIF_DMA_BLK_SZ;    /* used in block_dev.c */
    host->max_dma_segs = SDIF_MAX_BLK_TRANS; /* physical segment number */
    host->max_blk_size = SDIF_DMA_BLK_SZ;    /* all the 4 para limits size of one blk tran */
    host->max_blk_count = SDIF_MAX_BLK_TRANS;
    host->private_data = host_info;
    host->name[0] = 's';
    host->name[1] = 'd';
    host->name[2] = '0' + id;
    host->name[3] = '\0';

    mmc_host[id] = host;

    RT_ASSERT((default_sdif_config = FSdifLookupConfig(id)) != RT_NULL);
    sdif_config = *default_sdif_config;
#ifdef RT_USING_SMART
    sdif_config.base_addr = (uintptr)rt_ioremap((void *)sdif_config.base_addr, 0x1000);
#endif
    sdif_config.trans_mode = FSDIF_IDMA_TRANS_MODE;

    if (type == SDIF_CARD_TYPE_MICRO_SD)
    {
        sdif_config.non_removable = FALSE; /* TF card is removable on board */
    }
    else if (type == SDIF_CARD_TYPE_EMMC)
    {
        sdif_config.non_removable = TRUE; /* eMMC is unremovable on board */
    }

    if (FSDIF_SUCCESS != FSdifCfgInitialize(&host_info->sdif, &sdif_config))
    {
        LOG_E("SDIF controller init failed.");
        result = RT_EIO;
        goto err_free;
    }

    if (FSDIF_SUCCESS != FSdifSetIDMAList(&host_info->sdif,
                                          host_info->rw_desc,
                                          host_info->rw_desc_dma,
                                          host_info->rw_desc_num))
    {
        LOG_E("SDIF controller setup DMA failed.");
        result = RT_EIO;
        goto err_free;
    }

    FSdifRegisterRelaxHandler(&host_info->sdif, sdif_host_relax); /* SDIF delay for a while */

    host_info->sd_type = type;
    LOG_I("Init sdif-%d as %d", id, type);

    /* setup interrupt */
    sdif_ctrl_setup_interrupt(host);
    return result;

err_free:
    if (host)
    {
        mmcsd_free_host(host);
    }

    if (host_info)
    {
        if (host_info->aligned_buffer)
        {
            rt_free(host_info->aligned_buffer);
            host_info->aligned_buffer = RT_NULL;
            host_info->aligned_buffer_size = 0U;
        }

        if (host_info->rw_desc)
        {
            rt_free(host_info->rw_desc);
            host_info->rw_desc = RT_NULL;
            host_info->rw_desc_num = 0;
        }

        rt_free(host_info);
    }

    return result;
}

int rt_hw_sdif_init(void)
{
    int status = RT_EOK;
    rt_uint32_t sd_type;

    FSdifTimingInit();

#ifdef USING_SDIF0
#if defined(USE_SDIF0_TF)
    sd_type = SDIF_CARD_TYPE_MICRO_SD;
#elif defined(USE_SDIF0_EMMC)
    sd_type = SDIF_CARD_TYPE_EMMC;
#endif
    status = sdif_host_init(FSDIF0_ID, sd_type);

    if (status != RT_EOK)
    {
        LOG_E("SDIF0 init failed, status = %d", status);
        return status;
    }
#endif

#ifdef USING_SDIF1
#if defined(USE_SDIF1_TF)
    sd_type = SDIF_CARD_TYPE_MICRO_SD;
#elif defined(USE_SDIF1_EMMC)
    sd_type = SDIF_CARD_TYPE_EMMC;
#endif
    status = sdif_host_init(FSDIF1_ID, sd_type);

    if (status != RT_EOK)
    {
        LOG_E("SDIF0 init failed, status = %d", status);
        return status;
    }
#endif

    return status;
}
INIT_DEVICE_EXPORT(rt_hw_sdif_init);
#endif /* BSP_USING_SDIF_LAYER */