summaryrefslogtreecommitdiff
path: root/bsp/phytium/libraries/drivers/drv_sdio.c
blob: a80ab7acf373fb13014a575955b94f04d57a6694 (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
/*
 * 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
 */

/***************************** Include Files *********************************/
#include <rthw.h>
#include <rtthread.h>

#ifdef BSP_USING_SDIO
#include <rtdevice.h>
#include <rtdbg.h>
#include <drivers/mmcsd_core.h>

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

#include "ftypes.h"
#if defined(TARGET_E2000)
    #include "fparameters.h"
#endif

#include "fsdio.h"
#include "fsdio_hw.h"

#include "drv_sdio.h"
/************************** Constant Definitions *****************************/
#ifdef USING_SDIO0
    #define SDIO_CONTROLLER_ID    FSDIO0_ID
#elif defined (USING_SDIO1)
    #define SDIO_CONTROLLER_ID    FSDIO1_ID
#endif
#define SDIO_TF_CARD_HOST_ID  0x1
#define SDIO_MALLOC_CAP_DESC  256U
#define SDIO_DMA_ALIGN        512U
#define SDIO_DMA_BLK_SZ       512U
#define SDIO_VALID_OCR        0x00FFFF80 /* supported voltage range is 1.65v-3.6v (VDD_165_195-VDD_35_36) */
#define SDIO_MAX_BLK_TRANS    20U

#ifndef CONFIG_SDCARD_OFFSET
#define CONFIG_SDCARD_OFFSET 0x0U
#endif
/**************************** Type Definitions *******************************/
typedef struct
{
    FSdio *mmcsd_instance;
    FSdioIDmaDesc *rw_desc;
    rt_err_t (*transfer)(struct rt_mmcsd_host *host, struct rt_mmcsd_req *req, FSdioCmdData *cmd_data_p);
} mmcsd_info_t;
/************************** Variable Definitions *****************************/

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

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

static rt_err_t fsdio_ctrl_init(struct rt_mmcsd_host *host)
{
    mmcsd_info_t *private_data_t = (mmcsd_info_t *)host->private_data;
    FSdio *mmcsd_instance = RT_NULL;
    const FSdioConfig *default_mmcsd_config = RT_NULL;
    FSdioConfig mmcsd_config;
    FSdioIDmaDesc *rw_desc = RT_NULL;

    mmcsd_instance = rt_malloc(sizeof(FSdio));
    if (!mmcsd_instance)
    {
        LOG_E("Malloc mmcsd_instance failed");
        return RT_ERROR;
    }

    rw_desc = rt_malloc_align(SDIO_MAX_BLK_TRANS * sizeof(FSdioIDmaDesc), SDIO_MALLOC_CAP_DESC);
    if (!rw_desc)
    {
        LOG_E("Malloc rw_desc failed");
        return RT_ERROR;
    }

    rt_memset(mmcsd_instance, 0, sizeof(FSdio));
    rt_memset(rw_desc, 0, SDIO_MAX_BLK_TRANS * sizeof(FSdioIDmaDesc));

    /* SDIO controller init */
    RT_ASSERT((default_mmcsd_config = FSdioLookupConfig(SDIO_CONTROLLER_ID)) != RT_NULL);
    mmcsd_config = *default_mmcsd_config; /* load default config */
#ifdef RT_USING_SMART
    mmcsd_config.base_addr = (uintptr)rt_ioremap((void *)mmcsd_config.base_addr, 0x1000);
#endif
    mmcsd_config.trans_mode = FSDIO_IDMA_TRANS_MODE;
#ifdef USING_EMMC
    mmcsd_config.non_removable = TRUE; /* eMMC is unremovable on board */
#else
    mmcsd_config.non_removable = FALSE; /* TF card is removable on board */
#endif


    if (FSDIO_SUCCESS != FSdioCfgInitialize(mmcsd_instance, &mmcsd_config))
    {
        LOG_E("SDIO controller init failed.");
        return RT_ERROR;
    }

    if (FSDIO_SUCCESS != FSdioSetIDMAList(mmcsd_instance, rw_desc, SDIO_MAX_BLK_TRANS))
    {
        LOG_E("SDIO controller setup DMA failed.");
        return RT_ERROR;
    }
    mmcsd_instance->desc_list.first_desc_p = (uintptr)rw_desc + PV_OFFSET;

    FSdioRegisterRelaxHandler(mmcsd_instance, fsdio_host_relax); /* SDIO delay for a while */

    private_data_t->mmcsd_instance = mmcsd_instance;
    private_data_t->rw_desc = rw_desc;

    rt_kprintf("SDIO controller init success!\r\n");
    return RT_EOK;
}

rt_inline rt_err_t sdio_dma_transfer(struct rt_mmcsd_host *host, struct rt_mmcsd_req *req, FSdioCmdData *req_cmd)
{
    FError ret = FT_SUCCESS;
    mmcsd_info_t *private_data_t = (mmcsd_info_t *)host->private_data;
    FSdio *mmcsd_instance = private_data_t->mmcsd_instance;

    ret = FSdioDMATransfer(mmcsd_instance, req_cmd);
    if (ret != FT_SUCCESS)
    {
        LOG_E("FSdioDMATransfer() fail.");
        return -RT_ERROR;
    }

    ret = FSdioPollWaitDMAEnd(mmcsd_instance, req_cmd);
    if (ret != FT_SUCCESS)
    {
        LOG_E("FSdioPollWaitDMAEnd() fail.");
        return -RT_ERROR;
    }

    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];
        }
    }

    return RT_EOK;
}

static void mmc_request_send(struct rt_mmcsd_host *host, struct rt_mmcsd_req *req)
{
    /* ignore some SDIO-ONIY cmd */
    if ((req->cmd->cmd_code == SD_IO_SEND_OP_COND) || (req->cmd->cmd_code == SD_IO_RW_DIRECT))
    {
        req->cmd->err = -1;
        goto skip_cmd;
    }

    mmcsd_info_t *private_data_t = (mmcsd_info_t *)host->private_data;
    FSdioCmdData req_cmd;
    FSdioCmdData req_stop;
    FSdioData req_data;
    rt_uint32_t *data_buf_aligned = RT_NULL;
    rt_uint32_t cmd_flag = resp_type(req->cmd);

    rt_memset(&req_cmd, 0, sizeof(FSdioCmdData));
    rt_memset(&req_stop, 0, sizeof(FSdioCmdData));
    rt_memset(&req_data, 0, sizeof(FSdioData));

    /* convert req into ft driver type */
    if (req->cmd->cmd_code == GO_IDLE_STATE)
    {
        req_cmd.flag |= FSDIO_CMD_FLAG_NEED_INIT;
    }

    if (req->cmd->cmd_code == GO_INACTIVE_STATE)
    {
        req_cmd.flag |= FSDIO_CMD_FLAG_NEED_AUTO_STOP;
    }

    if ((cmd_flag != RESP_R3) && (cmd_flag != RESP_R4) && (cmd_flag != RESP_NONE))
    {
        req_cmd.flag |= FSDIO_CMD_FLAG_NEED_RESP_CRC;
    }

    if (cmd_flag & RESP_MASK)
    {
        req_cmd.flag |= FSDIO_CMD_FLAG_EXP_RESP;

        if (cmd_flag == RESP_R2)
        {
            req_cmd.flag |= FSDIO_CMD_FLAG_EXP_LONG_RESP;
        }
    }

    if (req->data) /* transfer command with data */
    {
        data_buf_aligned = rt_malloc_align(SDIO_DMA_BLK_SZ * req->data->blks, SDIO_DMA_ALIGN);
        if (!data_buf_aligned)
        {
            LOG_E("Malloc data_buf_aligned failed");
            return;
        }
        rt_memset(data_buf_aligned, 0, SDIO_DMA_BLK_SZ * req->data->blks);

        req_cmd.flag |= FSDIO_CMD_FLAG_EXP_DATA;

        req_data.blksz = req->data->blksize;
        req_data.blkcnt = req->data->blks + CONFIG_SDCARD_OFFSET;
        req_data.datalen = req->data->blksize * req->data->blks;
        if ((uintptr)req->data->buf % SDIO_DMA_ALIGN) /* data buffer should be 512-aligned */
        {
            if (req->data->flags & DATA_DIR_WRITE)
            {
                rt_memcpy((void *)data_buf_aligned, (void *)req->data->buf, req_data.datalen);
            }
            req_data.buf = (rt_uint8_t *)data_buf_aligned;
            req_data.buf_p = (uintptr)data_buf_aligned + PV_OFFSET;
        }
        else
        {
            req_data.buf = (rt_uint8_t *)req->data->buf;
            req_data.buf_p = (uintptr)req->data->buf + PV_OFFSET;
        }
        req_cmd.data_p = &req_data;

        if (req->data->flags & DATA_DIR_READ)
        {
            req_cmd.flag |= FSDIO_CMD_FLAG_READ_DATA;
        }
        else if (req->data->flags & DATA_DIR_WRITE)
        {
            req_cmd.flag |= FSDIO_CMD_FLAG_WRITE_DATA;
        }
    }

    req_cmd.cmdidx = req->cmd->cmd_code;
    req_cmd.cmdarg = req->cmd->arg;

    /* do cmd and data transfer */
    req->cmd->err = (private_data_t->transfer)(host, req, &req_cmd);
    if (req->cmd->err != RT_EOK)
    {
        LOG_E("transfer failed in %s", __func__);
    }

    if (req->data && (req->data->flags & DATA_DIR_READ))
    {
        if ((uintptr)req->data->buf % SDIO_DMA_ALIGN) /* data buffer should be 512-aligned */
        {
            rt_memcpy((void *)req->data->buf, (void *)data_buf_aligned, req_data.datalen);
        }
    }

    /* stop cmd */
    if (req->stop)
    {
        req_stop.cmdidx = req->stop->cmd_code;
        req_stop.cmdarg = req->stop->arg;
        if (req->stop->flags & RESP_MASK)
        {
            req_stop.flag |= FSDIO_CMD_FLAG_READ_DATA;
            if (resp_type(req->stop) == RESP_R2)
                req_stop.flag |= FSDIO_CMD_FLAG_EXP_LONG_RESP;
        }
        req->stop->err = (private_data_t->transfer)(host, req, &req_stop);
    }

    if (data_buf_aligned)
        rt_free_align(data_buf_aligned);

skip_cmd:
    mmcsd_req_complete(host);
}

static void mmc_set_iocfg(struct rt_mmcsd_host *host, struct rt_mmcsd_io_cfg *io_cfg)
{
    FError ret = FT_SUCCESS;
    mmcsd_info_t *private_data_t = (mmcsd_info_t *)host->private_data;
    FSdio *mmcsd_instance = private_data_t->mmcsd_instance;
    uintptr base_addr = mmcsd_instance->config.base_addr;

    if (0 != io_cfg->clock)
    {
        ret = FSdioSetClkFreq(mmcsd_instance, io_cfg->clock);
        if (ret != FT_SUCCESS)
        {
            LOG_E("FSdioSetClkFreq fail.");
        }
    }

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

static const struct rt_mmcsd_host_ops ops =
{
    mmc_request_send,
    mmc_set_iocfg,
    RT_NULL,
    RT_NULL,
    RT_NULL,
};

int ft_mmcsd_init(void)
{
    /* variables init */
    struct rt_mmcsd_host *host = RT_NULL;
    mmcsd_info_t *private_data = RT_NULL;

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

    private_data = rt_malloc(sizeof(mmcsd_info_t));
    if (!private_data)
    {
        LOG_E("Malloc private_data failed");
        goto err_free;
    }

    rt_memset(private_data, 0, sizeof(mmcsd_info_t));
    private_data->transfer = sdio_dma_transfer;

    /* host data init */
    host->ops = &ops;
    host->freq_min = 400000;
    host->freq_max = 50000000;
    host->valid_ocr = SDIO_VALID_OCR; /* the voltage range supported is 1.65v-3.6v */
    host->flags = MMCSD_MUTBLKWRITE | MMCSD_BUSWIDTH_4;
    host->max_seg_size = SDIO_DMA_BLK_SZ; /* used in block_dev.c */
    host->max_dma_segs = SDIO_MAX_BLK_TRANS; /* physical segment number */
    host->max_blk_size = SDIO_DMA_BLK_SZ; /* all the 4 para limits size of one blk tran */
    host->max_blk_count = SDIO_MAX_BLK_TRANS;
    host->private_data = private_data;

    if (RT_EOK != fsdio_ctrl_init(host))
    {
        LOG_E("fsdio_ctrl_init() failed");
        goto err_free;
    }

    mmcsd_change(host);

    return RT_EOK;

err_free:
    if (host)
        rt_free(host);
    if (private_data->mmcsd_instance)
        rt_free(private_data->mmcsd_instance);
    if (private_data->rw_desc)
        rt_free_align(private_data->rw_desc);
    if (private_data)
        rt_free(private_data);

    return -RT_EOK;
}
INIT_DEVICE_EXPORT(ft_mmcsd_init);
#endif // #ifdef RT_USING_SDIO