2023-11-21 17:42:23 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2006-2023, RT-Thread Development Team
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
|
|
|
* Email: opensource_embedded@phytium.com.cn
|
|
|
|
*
|
|
|
|
* 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-04-19 08:52:09 +08:00
|
|
|
* 2024/4/7 zhugengyu support use two sdif device
|
2023-11-21 17:42:23 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
/***************************** Include Files *********************************/
|
2024-04-19 08:52:09 +08:00
|
|
|
#include "rtconfig.h"
|
2023-11-21 17:42:23 +08:00
|
|
|
|
|
|
|
#ifdef BSP_USING_SDIF
|
|
|
|
#include <rthw.h>
|
|
|
|
#include <rtdef.h>
|
|
|
|
#include <rtthread.h>
|
|
|
|
#include <rtdevice.h>
|
|
|
|
#include <rtdbg.h>
|
|
|
|
#include <drivers/mmcsd_core.h>
|
|
|
|
|
|
|
|
#ifdef RT_USING_SMART
|
2024-04-19 08:52:09 +08:00
|
|
|
#include "ioremap.h"
|
2023-11-21 17:42:23 +08:00
|
|
|
#endif
|
|
|
|
#include "mm_aspace.h"
|
|
|
|
#include "interrupt.h"
|
|
|
|
|
2024-04-19 08:52:09 +08:00
|
|
|
#define LOG_TAG "sdif_drv"
|
2023-11-21 17:42:23 +08:00
|
|
|
#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 *****************************/
|
2024-04-19 08:52:09 +08:00
|
|
|
#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
|
2023-11-21 17:42:23 +08:00
|
|
|
|
|
|
|
/* preserve pointer to host instance */
|
|
|
|
static struct rt_mmcsd_host *mmc_host[FSDIF_NUM] = {RT_NULL};
|
|
|
|
/**************************** Type Definitions *******************************/
|
2024-04-19 08:52:09 +08:00
|
|
|
|
2023-11-21 17:42:23 +08:00
|
|
|
typedef struct
|
|
|
|
{
|
2024-04-19 08:52:09 +08:00
|
|
|
FSdif sdif;
|
|
|
|
rt_int32_t sd_type;
|
2023-11-21 17:42:23 +08:00
|
|
|
FSdifIDmaDesc *rw_desc;
|
2024-04-19 08:52:09 +08:00
|
|
|
uintptr_t rw_desc_dma;
|
|
|
|
rt_size_t rw_desc_num;
|
2023-11-21 17:42:23 +08:00
|
|
|
struct rt_event event;
|
2024-04-19 08:52:09 +08:00
|
|
|
#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;
|
2023-11-21 17:42:23 +08:00
|
|
|
/************************** Variable Definitions *****************************/
|
|
|
|
|
|
|
|
/***************** Macros (Inline Functions) Definitions *********************/
|
|
|
|
|
2024-04-19 08:52:09 +08:00
|
|
|
/******************************* Functions *********************************/
|
|
|
|
static void sdif_host_relax(void)
|
2023-11-21 17:42:23 +08:00
|
|
|
{
|
|
|
|
rt_thread_mdelay(1);
|
|
|
|
}
|
|
|
|
|
2024-04-19 08:52:09 +08:00
|
|
|
void sdif_change(rt_uint32_t id)
|
2023-11-21 17:42:23 +08:00
|
|
|
{
|
2024-04-19 08:52:09 +08:00
|
|
|
RT_ASSERT(id < FSDIF_NUM);
|
|
|
|
if (mmc_host[id])
|
|
|
|
{
|
|
|
|
mmcsd_change(mmc_host[id]);
|
|
|
|
}
|
|
|
|
}
|
2023-11-21 17:42:23 +08:00
|
|
|
|
2024-04-19 08:52:09 +08:00
|
|
|
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;
|
2023-11-21 17:42:23 +08:00
|
|
|
}
|
|
|
|
|
2024-04-19 08:52:09 +08:00
|
|
|
static void sdif_card_detect_callback(FSdif *const sdif, void *args, u32 status, u32 dmac_status)
|
2023-11-21 17:42:23 +08:00
|
|
|
{
|
|
|
|
struct rt_mmcsd_host *host = (struct rt_mmcsd_host *)args;
|
2024-04-19 08:52:09 +08:00
|
|
|
sdif_info_t *host_info = (sdif_info_t *)host->private_data;
|
2023-11-21 17:42:23 +08:00
|
|
|
|
2024-04-19 08:52:09 +08:00
|
|
|
rt_event_send(&host_info->event, SDIF_EVENT_CARD_DETECTED);
|
|
|
|
sdif_change(host_info->sdif.config.instance_id);
|
2023-11-21 17:42:23 +08:00
|
|
|
}
|
|
|
|
|
2024-04-19 08:52:09 +08:00
|
|
|
static void sdif_command_done_callback(FSdif *const sdif, void *args, u32 status, u32 dmac_status)
|
2023-11-21 17:42:23 +08:00
|
|
|
{
|
|
|
|
struct rt_mmcsd_host *host = (struct rt_mmcsd_host *)args;
|
2024-04-19 08:52:09 +08:00
|
|
|
sdif_info_t *host_info = (sdif_info_t *)host->private_data;
|
2023-11-21 17:42:23 +08:00
|
|
|
|
2024-04-19 08:52:09 +08:00
|
|
|
rt_event_send(&host_info->event, SDIF_EVENT_COMMAND_DONE);
|
2023-11-21 17:42:23 +08:00
|
|
|
}
|
|
|
|
|
2024-04-19 08:52:09 +08:00
|
|
|
static void sdif_data_done_callback(FSdif *const sdif, void *args, u32 status, u32 dmac_status)
|
2023-11-21 17:42:23 +08:00
|
|
|
{
|
|
|
|
struct rt_mmcsd_host *host = (struct rt_mmcsd_host *)args;
|
2024-04-19 08:52:09 +08:00
|
|
|
sdif_info_t *host_info = (sdif_info_t *)host->private_data;
|
2023-11-21 17:42:23 +08:00
|
|
|
|
2024-04-19 08:52:09 +08:00
|
|
|
rt_event_send(&host_info->event, SDIF_EVENT_DATA_DONE);
|
2023-11-21 17:42:23 +08:00
|
|
|
}
|
|
|
|
|
2024-04-19 08:52:09 +08:00
|
|
|
static void sdif_sdio_irq_callback(FSdif *const sdif, void *args, u32 status, u32 dmac_status)
|
2023-11-21 17:42:23 +08:00
|
|
|
{
|
|
|
|
struct rt_mmcsd_host *host = (struct rt_mmcsd_host *)args;
|
2024-04-19 08:52:09 +08:00
|
|
|
sdif_info_t *host_info = (sdif_info_t *)host->private_data;
|
2023-11-21 17:42:23 +08:00
|
|
|
|
2024-04-19 08:52:09 +08:00
|
|
|
rt_event_send(&host_info->event, SDIF_EVENT_SDIO_IRQ);
|
2023-11-21 17:42:23 +08:00
|
|
|
}
|
|
|
|
|
2024-04-19 08:52:09 +08:00
|
|
|
static void sdif_error_occur_callback(FSdif *const sdif, void *args, u32 status, u32 dmac_status)
|
2023-11-21 17:42:23 +08:00
|
|
|
{
|
2024-04-19 08:52:09 +08:00
|
|
|
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);
|
2023-11-21 17:42:23 +08:00
|
|
|
|
2024-04-19 08:52:09 +08:00
|
|
|
if (status & FSDIF_INT_RE_BIT)
|
|
|
|
LOG_E("Response err. 0x%x", FSDIF_INT_RE_BIT);
|
2023-11-21 17:42:23 +08:00
|
|
|
|
2024-04-19 08:52:09 +08:00
|
|
|
if (status & FSDIF_INT_RTO_BIT)
|
|
|
|
LOG_E("Response timeout. 0x%x", FSDIF_INT_RTO_BIT);
|
2023-11-21 17:42:23 +08:00
|
|
|
|
2024-04-19 08:52:09 +08:00
|
|
|
if (dmac_status & FSDIF_DMAC_STATUS_DU)
|
|
|
|
LOG_E("Descriptor un-readable. 0x%x", FSDIF_DMAC_STATUS_DU);
|
2023-11-21 17:42:23 +08:00
|
|
|
|
2024-04-19 08:52:09 +08:00
|
|
|
if (status & FSDIF_INT_DCRC_BIT)
|
|
|
|
LOG_E("Data CRC error. 0x%x", FSDIF_INT_DCRC_BIT);
|
2023-11-21 17:42:23 +08:00
|
|
|
|
2024-04-19 08:52:09 +08:00
|
|
|
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);
|
2023-11-21 17:42:23 +08:00
|
|
|
}
|
|
|
|
|
2024-04-19 08:52:09 +08:00
|
|
|
static rt_err_t sdif_pre_request(struct rt_mmcsd_host *host, struct rt_mmcsd_req *req)
|
2023-11-21 17:42:23 +08:00
|
|
|
{
|
2024-04-19 08:52:09 +08:00
|
|
|
rt_err_t err = RT_EOK;
|
|
|
|
sdif_info_t *host_info = (sdif_info_t *)host->private_data;
|
2023-11-21 17:42:23 +08:00
|
|
|
|
2024-04-19 08:52:09 +08:00
|
|
|
if (host_info->sd_type != SDIF_CARD_TYPE_SDIO)
|
2023-11-21 17:42:23 +08:00
|
|
|
{
|
2024-04-19 08:52:09 +08:00
|
|
|
/* 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;
|
|
|
|
}
|
2023-11-21 17:42:23 +08:00
|
|
|
}
|
|
|
|
|
2024-04-19 08:52:09 +08:00
|
|
|
if (host_info->sd_type == SDIF_CARD_TYPE_EMMC)
|
2023-11-21 17:42:23 +08:00
|
|
|
{
|
2024-04-19 08:52:09 +08:00
|
|
|
/* 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;
|
|
|
|
}
|
2023-11-21 17:42:23 +08:00
|
|
|
}
|
|
|
|
|
2024-04-19 08:52:09 +08:00
|
|
|
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;
|
2023-11-21 17:42:23 +08:00
|
|
|
|
2024-04-19 08:52:09 +08:00
|
|
|
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);
|
2023-11-21 17:42:23 +08:00
|
|
|
|
2024-04-19 08:52:09 +08:00
|
|
|
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;
|
|
|
|
|
|
|
|
out_cmd->flag = 0U;
|
|
|
|
|
|
|
|
if (in_cmd->cmd_code == GO_IDLE_STATE)
|
2023-11-21 17:42:23 +08:00
|
|
|
{
|
2024-04-19 08:52:09 +08:00
|
|
|
out_cmd->flag |= FSDIF_CMD_FLAG_NEED_INIT;
|
2023-11-21 17:42:23 +08:00
|
|
|
}
|
|
|
|
|
2024-04-19 08:52:09 +08:00
|
|
|
if (in_cmd->cmd_code == GO_INACTIVE_STATE)
|
2023-11-21 17:42:23 +08:00
|
|
|
{
|
2024-04-19 08:52:09 +08:00
|
|
|
out_cmd->flag |= FSDIF_CMD_FLAG_NEED_AUTO_STOP | FSDIF_CMD_FLAG_ABORT;
|
2023-11-21 17:42:23 +08:00
|
|
|
}
|
|
|
|
|
2024-04-19 08:52:09 +08:00
|
|
|
if (resp_type(in_cmd) != RESP_NONE)
|
|
|
|
{
|
|
|
|
out_cmd->flag |= FSDIF_CMD_FLAG_EXP_RESP;
|
2023-11-21 17:42:23 +08:00
|
|
|
|
2024-04-19 08:52:09 +08:00
|
|
|
if (resp_type(in_cmd) == RESP_R2)
|
|
|
|
{
|
|
|
|
/* need 136 bits long response */
|
|
|
|
out_cmd->flag |= FSDIF_CMD_FLAG_EXP_LONG_RESP;
|
|
|
|
}
|
2023-11-21 17:42:23 +08:00
|
|
|
|
2024-04-19 08:52:09 +08:00
|
|
|
if ((resp_type(in_cmd) != RESP_R3) &&
|
|
|
|
(resp_type(in_cmd) != RESP_R4))
|
|
|
|
{
|
|
|
|
/* most cmds need CRC */
|
|
|
|
out_cmd->flag |= FSDIF_CMD_FLAG_NEED_RESP_CRC;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (in_data)
|
|
|
|
{
|
|
|
|
RT_ASSERT(out_data);
|
|
|
|
out_cmd->flag |= FSDIF_CMD_FLAG_EXP_DATA;
|
|
|
|
|
|
|
|
if (in_data->flags & DATA_DIR_READ)
|
|
|
|
{
|
|
|
|
out_cmd->flag |= FSDIF_CMD_FLAG_READ_DATA;
|
|
|
|
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->flag |= FSDIF_CMD_FLAG_WRITE_DATA;
|
|
|
|
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);
|
2023-11-21 17:42:23 +08:00
|
|
|
}
|
|
|
|
|
2024-04-19 08:52:09 +08:00
|
|
|
static rt_err_t sdif_do_transfer(struct rt_mmcsd_host *host, FSdifCmdData *req_cmd, rt_int32_t timeout_ms)
|
2023-11-21 17:42:23 +08:00
|
|
|
{
|
|
|
|
FError ret = FT_SUCCESS;
|
2024-04-19 08:52:09 +08:00
|
|
|
sdif_info_t *host_info = (sdif_info_t *)host->private_data;
|
2023-11-21 17:42:23 +08:00
|
|
|
rt_uint32_t event = 0U;
|
|
|
|
rt_uint32_t wait_event = 0U;
|
2024-04-19 08:52:09 +08:00
|
|
|
|
|
|
|
LOG_I("cmd-%d sending", req_cmd->cmdidx);
|
2023-11-21 17:42:23 +08:00
|
|
|
|
|
|
|
if (req_cmd->data_p == RT_NULL)
|
|
|
|
{
|
|
|
|
wait_event = SDIF_EVENT_COMMAND_DONE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
wait_event = SDIF_EVENT_COMMAND_DONE | SDIF_EVENT_DATA_DONE;
|
|
|
|
}
|
|
|
|
|
2024-04-19 08:52:09 +08:00
|
|
|
ret = FSdifDMATransfer(&host_info->sdif, req_cmd);
|
2023-11-21 17:42:23 +08:00
|
|
|
if (ret != FT_SUCCESS)
|
|
|
|
{
|
|
|
|
LOG_E("FSdifDMATransfer() fail.");
|
|
|
|
return -RT_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (TRUE)
|
|
|
|
{
|
2024-04-19 08:52:09 +08:00
|
|
|
/*
|
|
|
|
* transfer without data: wait COMMAND_DONE event
|
|
|
|
* transfer with data: wait COMMAND_DONE and DATA_DONE event
|
|
|
|
*/
|
|
|
|
if (rt_event_recv(&host_info->event,
|
2023-11-21 17:42:23 +08:00
|
|
|
(wait_event),
|
2024-04-19 08:52:09 +08:00
|
|
|
(RT_EVENT_FLAG_AND | RT_EVENT_FLAG_CLEAR),
|
|
|
|
rt_tick_from_millisecond(1000),
|
2023-11-21 17:42:23 +08:00
|
|
|
&event) == RT_EOK)
|
|
|
|
{
|
2024-04-19 08:52:09 +08:00
|
|
|
(void)FSdifGetCmdResponse(&host_info->sdif, req_cmd);
|
2023-11-21 17:42:23 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2024-04-19 08:52:09 +08:00
|
|
|
/*
|
|
|
|
* 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)
|
2023-11-21 17:42:23 +08:00
|
|
|
{
|
2024-04-19 08:52:09 +08:00
|
|
|
LOG_E("Sdif DMA transfer endup with error !!!");
|
|
|
|
return -RT_EIO;
|
2023-11-21 17:42:23 +08:00
|
|
|
}
|
2024-04-19 08:52:09 +08:00
|
|
|
|
|
|
|
timeout_ms -= 1000;
|
|
|
|
if (timeout_ms <= 0)
|
2023-11-21 17:42:23 +08:00
|
|
|
{
|
2024-04-19 08:52:09 +08:00
|
|
|
LOG_E("Sdif DMA transfer endup with timeout !!!");
|
|
|
|
return -RT_EIO;
|
2023-11-21 17:42:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
2024-04-19 08:52:09 +08:00
|
|
|
static void sdif_send_request(struct rt_mmcsd_host *host, struct rt_mmcsd_req *req)
|
2023-11-21 17:42:23 +08:00
|
|
|
{
|
2024-04-19 08:52:09 +08:00
|
|
|
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;
|
2023-11-21 17:42:23 +08:00
|
|
|
|
2024-04-19 08:52:09 +08:00
|
|
|
rt_err_t err = sdif_pre_request(host, req);
|
|
|
|
if (err != RT_EOK)
|
2023-11-21 17:42:23 +08:00
|
|
|
{
|
2024-04-19 08:52:09 +08:00
|
|
|
return;
|
2023-11-21 17:42:23 +08:00
|
|
|
}
|
|
|
|
|
2024-07-22 10:33:39 +08:00
|
|
|
rt_memset(req_cmd, 0, sizeof(FSdifCmdData));
|
2023-11-21 17:42:23 +08:00
|
|
|
|
2024-04-19 08:52:09 +08:00
|
|
|
if (req->data)
|
2023-11-21 17:42:23 +08:00
|
|
|
{
|
2024-07-22 10:33:39 +08:00
|
|
|
rt_memset(req_data, 0, sizeof(FSdifData));
|
2024-04-19 08:52:09 +08:00
|
|
|
req_cmd->data_p = req_data;
|
2023-11-21 17:42:23 +08:00
|
|
|
}
|
2024-04-19 08:52:09 +08:00
|
|
|
else
|
2023-11-21 17:42:23 +08:00
|
|
|
{
|
2024-04-19 08:52:09 +08:00
|
|
|
req_cmd->data_p = RT_NULL;
|
2023-11-21 17:42:23 +08:00
|
|
|
}
|
|
|
|
|
2024-04-19 08:52:09 +08:00
|
|
|
sdif_convert_command_info(host, req->cmd, req->data, req_cmd);
|
2023-11-21 17:42:23 +08:00
|
|
|
|
2024-04-19 08:52:09 +08:00
|
|
|
req->cmd->err = sdif_do_transfer(host, req_cmd, req->cmd->busy_timeout);
|
2023-11-21 17:42:23 +08:00
|
|
|
|
2024-04-19 08:52:09 +08:00
|
|
|
if (resp_type(req->cmd) & RESP_MASK)
|
|
|
|
{
|
|
|
|
if (resp_type(req->cmd) == RESP_R2)
|
2023-11-21 17:42:23 +08:00
|
|
|
{
|
2024-04-19 08:52:09 +08:00
|
|
|
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];
|
2023-11-21 17:42:23 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-04-19 08:52:09 +08:00
|
|
|
req->cmd->resp[0] = req_cmd->response[0];
|
2023-11-21 17:42:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (req->data && (req->data->flags & DATA_DIR_READ))
|
|
|
|
{
|
2024-04-19 08:52:09 +08:00
|
|
|
/* if it is read sd card, copy data to unaligned buffer and return */
|
|
|
|
if ((uintptr)req->data->buf % SDIF_DMA_ALIGN)
|
2023-11-21 17:42:23 +08:00
|
|
|
{
|
2024-04-19 08:52:09 +08:00
|
|
|
rt_memcpy((void *)req->data->buf,
|
|
|
|
(void *)host_info->aligned_buffer,
|
|
|
|
req_cmd->data_p->datalen);
|
2023-11-21 17:42:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mmcsd_req_complete(host);
|
|
|
|
}
|
|
|
|
|
2024-04-19 08:52:09 +08:00
|
|
|
static void sdif_set_iocfg(struct rt_mmcsd_host *host, struct rt_mmcsd_io_cfg *io_cfg)
|
2023-11-21 17:42:23 +08:00
|
|
|
{
|
|
|
|
FError ret = FT_SUCCESS;
|
2024-04-19 08:52:09 +08:00
|
|
|
sdif_info_t *host_info = (sdif_info_t *)host->private_data;
|
|
|
|
FSdif *sdif = &host_info->sdif;
|
|
|
|
uintptr base_addr = sdif->config.base_addr;
|
2023-11-21 17:42:23 +08:00
|
|
|
|
|
|
|
if (0 != io_cfg->clock)
|
|
|
|
{
|
2024-04-19 08:52:09 +08:00
|
|
|
ret = FSdifSetClkFreq(sdif, io_cfg->clock);
|
2023-11-21 17:42:23 +08:00
|
|
|
if (ret != FT_SUCCESS)
|
|
|
|
{
|
|
|
|
LOG_E("FSdifSetClkFreq fail.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (io_cfg->bus_width)
|
|
|
|
{
|
2024-04-19 08:52:09 +08:00
|
|
|
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;
|
2023-11-21 17:42:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-19 08:52:09 +08:00
|
|
|
static rt_int32_t sdif_card_status(struct rt_mmcsd_host *host)
|
2023-11-21 17:42:23 +08:00
|
|
|
{
|
2024-04-19 08:52:09 +08:00
|
|
|
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,
|
2023-11-21 17:42:23 +08:00
|
|
|
};
|
|
|
|
|
2024-04-19 08:52:09 +08:00
|
|
|
static void sdif_ctrl_setup_interrupt(struct rt_mmcsd_host *host)
|
2023-11-21 17:42:23 +08:00
|
|
|
{
|
2024-04-19 08:52:09 +08:00
|
|
|
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;
|
2023-11-21 17:42:23 +08:00
|
|
|
}
|
|
|
|
|
2024-04-19 08:52:09 +08:00
|
|
|
static rt_err_t sdif_host_init(rt_uint32_t id, rt_uint32_t type)
|
2023-11-21 17:42:23 +08:00
|
|
|
{
|
|
|
|
struct rt_mmcsd_host *host = RT_NULL;
|
2024-04-19 08:52:09 +08:00
|
|
|
sdif_info_t *host_info = RT_NULL;
|
|
|
|
const FSdifConfig *default_sdif_config = RT_NULL;
|
|
|
|
FSdifConfig sdif_config;
|
2023-11-21 17:42:23 +08:00
|
|
|
rt_err_t result = RT_EOK;
|
|
|
|
|
|
|
|
host = mmcsd_alloc_host();
|
|
|
|
if (!host)
|
|
|
|
{
|
|
|
|
LOG_E("Alloc host failed");
|
2024-04-19 08:52:09 +08:00
|
|
|
result = RT_ENOMEM;
|
2023-11-21 17:42:23 +08:00
|
|
|
goto err_free;
|
|
|
|
}
|
|
|
|
|
2024-04-19 08:52:09 +08:00
|
|
|
host_info = rt_malloc(sizeof(sdif_info_t));
|
|
|
|
if (!host_info)
|
2023-11-21 17:42:23 +08:00
|
|
|
{
|
2024-04-19 08:52:09 +08:00
|
|
|
LOG_E("Malloc host_info failed");
|
|
|
|
result = RT_ENOMEM;
|
2023-11-21 17:42:23 +08:00
|
|
|
goto err_free;
|
|
|
|
}
|
2024-04-19 08:52:09 +08:00
|
|
|
rt_memset(host_info, 0, sizeof(*host_info));
|
2023-11-21 17:42:23 +08:00
|
|
|
|
2024-04-19 08:52:09 +08:00
|
|
|
result = rt_event_init(&host_info->event, "sdif_event", RT_IPC_FLAG_FIFO);
|
2023-11-21 17:42:23 +08:00
|
|
|
RT_ASSERT(RT_EOK == result);
|
|
|
|
|
2024-04-19 08:52:09 +08:00
|
|
|
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));
|
|
|
|
|
2023-11-21 17:42:23 +08:00
|
|
|
/* host data init */
|
|
|
|
host->ops = &ops;
|
2024-04-19 08:52:09 +08:00
|
|
|
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 */
|
2023-11-21 17:42:23 +08:00
|
|
|
host->flags = MMCSD_MUTBLKWRITE | MMCSD_BUSWIDTH_4;
|
2024-04-19 08:52:09 +08:00
|
|
|
host->max_seg_size = SDIF_DMA_BLK_SZ; /* used in block_dev.c */
|
2023-11-21 17:42:23 +08:00
|
|
|
host->max_dma_segs = SDIF_MAX_BLK_TRANS; /* physical segment number */
|
2024-04-19 08:52:09 +08:00
|
|
|
host->max_blk_size = SDIF_DMA_BLK_SZ; /* all the 4 para limits size of one blk tran */
|
2023-11-21 17:42:23 +08:00
|
|
|
host->max_blk_count = SDIF_MAX_BLK_TRANS;
|
2024-04-19 08:52:09 +08:00
|
|
|
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;
|
2023-11-21 17:42:23 +08:00
|
|
|
|
2024-04-19 08:52:09 +08:00
|
|
|
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 */
|
|
|
|
}
|
2023-11-21 17:42:23 +08:00
|
|
|
|
2024-04-19 08:52:09 +08:00
|
|
|
sdif_config.get_tuning = FSdifGetTimingSetting;
|
|
|
|
|
|
|
|
if (FSDIF_SUCCESS != FSdifCfgInitialize(&host_info->sdif, &sdif_config))
|
2023-11-21 17:42:23 +08:00
|
|
|
{
|
2024-04-19 08:52:09 +08:00
|
|
|
LOG_E("SDIF controller init failed.");
|
|
|
|
result = RT_EIO;
|
2023-11-21 17:42:23 +08:00
|
|
|
goto err_free;
|
|
|
|
}
|
|
|
|
|
2024-04-19 08:52:09 +08:00
|
|
|
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;
|
2023-11-21 17:42:23 +08:00
|
|
|
|
|
|
|
err_free:
|
|
|
|
if (host)
|
|
|
|
{
|
2024-04-19 08:52:09 +08:00
|
|
|
mmcsd_free_host(host);
|
2023-11-21 17:42:23 +08:00
|
|
|
}
|
2024-04-19 08:52:09 +08:00
|
|
|
|
|
|
|
if (host_info)
|
2023-11-21 17:42:23 +08:00
|
|
|
{
|
2024-04-19 08:52:09 +08:00
|
|
|
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);
|
2023-11-21 17:42:23 +08:00
|
|
|
}
|
2024-04-19 08:52:09 +08:00
|
|
|
|
|
|
|
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)
|
2023-11-21 17:42:23 +08:00
|
|
|
{
|
2024-04-19 08:52:09 +08:00
|
|
|
LOG_E("SDIF0 init failed, status = %d", status);
|
|
|
|
return status;
|
2023-11-21 17:42:23 +08:00
|
|
|
}
|
2024-04-19 08:52:09 +08:00
|
|
|
#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)
|
2023-11-21 17:42:23 +08:00
|
|
|
{
|
2024-04-19 08:52:09 +08:00
|
|
|
LOG_E("SDIF0 init failed, status = %d", status);
|
|
|
|
return status;
|
2023-11-21 17:42:23 +08:00
|
|
|
}
|
2024-04-19 08:52:09 +08:00
|
|
|
#endif
|
2023-11-21 17:42:23 +08:00
|
|
|
|
2024-04-19 08:52:09 +08:00
|
|
|
return status;
|
2023-11-21 17:42:23 +08:00
|
|
|
}
|
2024-04-19 08:52:09 +08:00
|
|
|
INIT_DEVICE_EXPORT(rt_hw_sdif_init);
|
|
|
|
#endif // #ifdef BSP_USING_SDIF
|