2011-10-23 17:46:20 +08:00
|
|
|
/*
|
2023-04-18 10:26:23 +08:00
|
|
|
* Copyright (c) 2006-2023, RT-Thread Development Team
|
2011-10-23 17:46:20 +08:00
|
|
|
*
|
2018-10-14 19:37:18 +08:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2011-10-23 17:46:20 +08:00
|
|
|
*
|
|
|
|
* Change Logs:
|
2013-06-28 00:36:54 +08:00
|
|
|
* Date Author Notes
|
2024-10-30 11:41:07 +08:00
|
|
|
* 2011-07-25 weety first version
|
|
|
|
* 2023-08-08 GuEe-GUI port to the block
|
2011-10-23 17:46:20 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <rtthread.h>
|
2024-10-30 11:41:07 +08:00
|
|
|
#include <drivers/blk.h>
|
|
|
|
#include <drivers/misc.h>
|
2024-09-14 05:40:40 +08:00
|
|
|
#include <drivers/dev_mmcsd_core.h>
|
2011-10-23 17:46:20 +08:00
|
|
|
|
2019-04-12 10:18:57 +08:00
|
|
|
#define DBG_TAG "SDIO"
|
2018-09-05 14:50:43 +08:00
|
|
|
#ifdef RT_SDIO_DEBUG
|
2019-04-12 10:18:57 +08:00
|
|
|
#define DBG_LVL DBG_LOG
|
2018-09-05 14:50:43 +08:00
|
|
|
#else
|
2019-04-12 10:18:57 +08:00
|
|
|
#define DBG_LVL DBG_INFO
|
2018-09-05 14:50:43 +08:00
|
|
|
#endif /* RT_SDIO_DEBUG */
|
|
|
|
#include <rtdbg.h>
|
|
|
|
|
2024-10-30 11:41:07 +08:00
|
|
|
#ifndef RT_MMCSD_MAX_PARTITION
|
|
|
|
#define RT_MMCSD_MAX_PARTITION 16
|
|
|
|
#endif
|
2018-09-08 23:21:13 +08:00
|
|
|
|
2012-02-13 19:12:57 +08:00
|
|
|
struct mmcsd_blk_device
|
|
|
|
{
|
2024-10-30 11:41:07 +08:00
|
|
|
struct rt_blk_disk parent;
|
2013-06-28 00:36:54 +08:00
|
|
|
struct rt_mmcsd_card *card;
|
2024-10-30 11:41:07 +08:00
|
|
|
|
2018-09-08 23:21:13 +08:00
|
|
|
rt_size_t max_req_size;
|
2024-10-30 11:41:07 +08:00
|
|
|
struct rt_device_blk_geometry geometry;
|
2011-10-23 17:46:20 +08:00
|
|
|
};
|
|
|
|
|
2024-10-30 11:41:07 +08:00
|
|
|
#define raw_to_mmcsd_blk(raw) rt_container_of(raw, struct mmcsd_blk_device, parent)
|
|
|
|
|
|
|
|
#ifdef RT_USING_DM
|
|
|
|
static struct rt_dm_ida sdio_ida = RT_DM_IDA_INIT(SDIO);
|
2011-10-23 17:46:20 +08:00
|
|
|
#endif
|
2022-12-03 12:07:44 +08:00
|
|
|
|
|
|
|
static int __send_status(struct rt_mmcsd_card *card, rt_uint32_t *status, unsigned retries)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
struct rt_mmcsd_cmd cmd;
|
|
|
|
|
|
|
|
cmd.busy_timeout = 0;
|
|
|
|
cmd.cmd_code = SEND_STATUS;
|
|
|
|
cmd.arg = card->rca << 16;
|
|
|
|
cmd.flags = RESP_R1 | CMD_AC;
|
|
|
|
err = mmcsd_send_cmd(card->host, &cmd, retries);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
if (status)
|
|
|
|
*status = cmd.resp[0];
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int card_busy_detect(struct rt_mmcsd_card *card, unsigned int timeout_ms,
|
|
|
|
rt_uint32_t *resp_errs)
|
|
|
|
{
|
|
|
|
int timeout = rt_tick_from_millisecond(timeout_ms);
|
|
|
|
int err = 0;
|
|
|
|
rt_uint32_t status;
|
|
|
|
rt_tick_t start;
|
|
|
|
|
|
|
|
start = rt_tick_get();
|
|
|
|
do
|
|
|
|
{
|
|
|
|
rt_bool_t out = (int)(rt_tick_get() - start) > timeout;
|
|
|
|
|
|
|
|
err = __send_status(card, &status, 5);
|
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
LOG_E("error %d requesting status", err);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Accumulate any response error bits seen */
|
|
|
|
if (resp_errs)
|
|
|
|
*resp_errs |= status;
|
|
|
|
|
|
|
|
if (out)
|
|
|
|
{
|
|
|
|
LOG_E("wait card busy timeout");
|
|
|
|
return -RT_ETIMEOUT;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Some cards mishandle the status bits,
|
|
|
|
* so make sure to check both the busy
|
|
|
|
* indication and the card state.
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
while (!(status & R1_READY_FOR_DATA) ||
|
|
|
|
(R1_CURRENT_STATE(status) == 7));
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
2011-10-23 17:46:20 +08:00
|
|
|
|
2017-12-23 23:56:46 +08:00
|
|
|
rt_int32_t mmcsd_num_wr_blocks(struct rt_mmcsd_card *card)
|
2011-10-23 17:46:20 +08:00
|
|
|
{
|
2013-06-28 00:36:54 +08:00
|
|
|
rt_int32_t err;
|
|
|
|
rt_uint32_t blocks;
|
2011-10-23 17:46:20 +08:00
|
|
|
|
2013-06-28 00:36:54 +08:00
|
|
|
struct rt_mmcsd_req req;
|
|
|
|
struct rt_mmcsd_cmd cmd;
|
|
|
|
struct rt_mmcsd_data data;
|
|
|
|
rt_uint32_t timeout_us;
|
2011-10-23 17:46:20 +08:00
|
|
|
|
2013-06-28 00:36:54 +08:00
|
|
|
rt_memset(&cmd, 0, sizeof(struct rt_mmcsd_cmd));
|
2011-10-23 17:46:20 +08:00
|
|
|
|
2013-06-28 00:36:54 +08:00
|
|
|
cmd.cmd_code = APP_CMD;
|
|
|
|
cmd.arg = card->rca << 16;
|
|
|
|
cmd.flags = RESP_SPI_R1 | RESP_R1 | CMD_AC;
|
2011-10-23 17:46:20 +08:00
|
|
|
|
2013-06-28 00:36:54 +08:00
|
|
|
err = mmcsd_send_cmd(card->host, &cmd, 0);
|
|
|
|
if (err)
|
|
|
|
return -RT_ERROR;
|
|
|
|
if (!controller_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
|
|
|
|
return -RT_ERROR;
|
2011-10-23 17:46:20 +08:00
|
|
|
|
2013-06-28 00:36:54 +08:00
|
|
|
rt_memset(&cmd, 0, sizeof(struct rt_mmcsd_cmd));
|
2011-10-23 17:46:20 +08:00
|
|
|
|
2013-06-28 00:36:54 +08:00
|
|
|
cmd.cmd_code = SD_APP_SEND_NUM_WR_BLKS;
|
|
|
|
cmd.arg = 0;
|
|
|
|
cmd.flags = RESP_SPI_R1 | RESP_R1 | CMD_ADTC;
|
2011-10-23 17:46:20 +08:00
|
|
|
|
2013-06-28 00:36:54 +08:00
|
|
|
rt_memset(&data, 0, sizeof(struct rt_mmcsd_data));
|
2011-10-23 17:46:20 +08:00
|
|
|
|
2013-06-28 00:36:54 +08:00
|
|
|
data.timeout_ns = card->tacc_ns * 100;
|
|
|
|
data.timeout_clks = card->tacc_clks * 100;
|
2011-10-23 17:46:20 +08:00
|
|
|
|
2013-06-28 00:36:54 +08:00
|
|
|
timeout_us = data.timeout_ns / 1000;
|
|
|
|
timeout_us += data.timeout_clks * 1000 /
|
2022-12-03 12:07:44 +08:00
|
|
|
(card->host->io_cfg.clock / 1000);
|
2011-10-23 17:46:20 +08:00
|
|
|
|
2021-03-08 18:19:04 +08:00
|
|
|
if (timeout_us > 100000)
|
2013-06-28 00:36:54 +08:00
|
|
|
{
|
|
|
|
data.timeout_ns = 100000000;
|
|
|
|
data.timeout_clks = 0;
|
|
|
|
}
|
2011-10-23 17:46:20 +08:00
|
|
|
|
2013-06-28 00:36:54 +08:00
|
|
|
data.blksize = 4;
|
|
|
|
data.blks = 1;
|
|
|
|
data.flags = DATA_DIR_READ;
|
|
|
|
data.buf = &blocks;
|
2011-10-23 17:46:20 +08:00
|
|
|
|
2013-06-28 00:36:54 +08:00
|
|
|
rt_memset(&req, 0, sizeof(struct rt_mmcsd_req));
|
2011-10-23 17:46:20 +08:00
|
|
|
|
2013-06-28 00:36:54 +08:00
|
|
|
req.cmd = &cmd;
|
|
|
|
req.data = &data;
|
2011-10-23 17:46:20 +08:00
|
|
|
|
2013-06-28 00:36:54 +08:00
|
|
|
mmcsd_send_request(card->host, &req);
|
2011-10-23 17:46:20 +08:00
|
|
|
|
2013-06-28 00:36:54 +08:00
|
|
|
if (cmd.err || data.err)
|
|
|
|
return -RT_ERROR;
|
2011-10-23 17:46:20 +08:00
|
|
|
|
2013-06-28 00:36:54 +08:00
|
|
|
return blocks;
|
2011-10-23 17:46:20 +08:00
|
|
|
}
|
|
|
|
|
2013-06-28 00:36:54 +08:00
|
|
|
static rt_err_t rt_mmcsd_req_blk(struct rt_mmcsd_card *card,
|
|
|
|
rt_uint32_t sector,
|
|
|
|
void *buf,
|
|
|
|
rt_size_t blks,
|
|
|
|
rt_uint8_t dir)
|
2011-10-23 17:46:20 +08:00
|
|
|
{
|
2013-06-28 00:36:54 +08:00
|
|
|
struct rt_mmcsd_cmd cmd, stop;
|
|
|
|
struct rt_mmcsd_data data;
|
|
|
|
struct rt_mmcsd_req req;
|
|
|
|
struct rt_mmcsd_host *host = card->host;
|
|
|
|
rt_uint32_t r_cmd, w_cmd;
|
|
|
|
|
|
|
|
mmcsd_host_lock(host);
|
|
|
|
rt_memset(&req, 0, sizeof(struct rt_mmcsd_req));
|
|
|
|
rt_memset(&cmd, 0, sizeof(struct rt_mmcsd_cmd));
|
|
|
|
rt_memset(&stop, 0, sizeof(struct rt_mmcsd_cmd));
|
|
|
|
rt_memset(&data, 0, sizeof(struct rt_mmcsd_data));
|
|
|
|
req.cmd = &cmd;
|
|
|
|
req.data = &data;
|
2021-03-08 18:19:04 +08:00
|
|
|
|
2013-06-28 00:36:54 +08:00
|
|
|
cmd.arg = sector;
|
2021-03-08 18:19:04 +08:00
|
|
|
if (!(card->flags & CARD_FLAG_SDHC))
|
2013-06-28 00:36:54 +08:00
|
|
|
{
|
|
|
|
cmd.arg <<= 9;
|
|
|
|
}
|
|
|
|
cmd.flags = RESP_SPI_R1 | RESP_R1 | CMD_ADTC;
|
|
|
|
|
|
|
|
data.blksize = SECTOR_SIZE;
|
|
|
|
data.blks = blks;
|
|
|
|
|
2021-03-08 18:19:04 +08:00
|
|
|
if (blks > 1)
|
2013-06-28 00:36:54 +08:00
|
|
|
{
|
|
|
|
if (!controller_is_spi(card->host) || !dir)
|
|
|
|
{
|
|
|
|
req.stop = &stop;
|
|
|
|
stop.cmd_code = STOP_TRANSMISSION;
|
|
|
|
stop.arg = 0;
|
|
|
|
stop.flags = RESP_SPI_R1B | RESP_R1B | CMD_AC;
|
|
|
|
}
|
|
|
|
r_cmd = READ_MULTIPLE_BLOCK;
|
|
|
|
w_cmd = WRITE_MULTIPLE_BLOCK;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-06-20 23:23:32 +08:00
|
|
|
req.stop = RT_NULL;
|
2013-06-28 00:36:54 +08:00
|
|
|
r_cmd = READ_SINGLE_BLOCK;
|
|
|
|
w_cmd = WRITE_BLOCK;
|
|
|
|
}
|
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
if (!controller_is_spi(card->host) && (card->flags & 0x8000))
|
|
|
|
{
|
|
|
|
/* last request is WRITE,need check busy */
|
|
|
|
card_busy_detect(card, 10000, RT_NULL);
|
|
|
|
}
|
|
|
|
|
2021-03-08 18:19:04 +08:00
|
|
|
if (!dir)
|
2013-06-28 00:36:54 +08:00
|
|
|
{
|
|
|
|
cmd.cmd_code = r_cmd;
|
|
|
|
data.flags |= DATA_DIR_READ;
|
2022-12-03 12:07:44 +08:00
|
|
|
card->flags &= 0x7fff;
|
2013-06-28 00:36:54 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cmd.cmd_code = w_cmd;
|
|
|
|
data.flags |= DATA_DIR_WRITE;
|
2022-12-03 12:07:44 +08:00
|
|
|
card->flags |= 0x8000;
|
2013-06-28 00:36:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
mmcsd_set_data_timeout(&data, card);
|
|
|
|
data.buf = buf;
|
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
mmcsd_send_request(host, &req);
|
2013-06-28 00:36:54 +08:00
|
|
|
|
|
|
|
mmcsd_host_unlock(host);
|
|
|
|
|
2021-03-08 18:19:04 +08:00
|
|
|
if (cmd.err || data.err || stop.err)
|
2013-06-28 00:36:54 +08:00
|
|
|
{
|
2018-09-05 14:50:43 +08:00
|
|
|
LOG_E("mmcsd request blocks error");
|
|
|
|
LOG_E("%d,%d,%d, 0x%08x,0x%08x",
|
2022-12-03 12:07:44 +08:00
|
|
|
cmd.err, data.err, stop.err, data.flags, sector);
|
2013-06-28 00:36:54 +08:00
|
|
|
|
|
|
|
return -RT_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return RT_EOK;
|
2011-10-23 17:46:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static rt_int32_t mmcsd_set_blksize(struct rt_mmcsd_card *card)
|
|
|
|
{
|
2013-06-28 00:36:54 +08:00
|
|
|
struct rt_mmcsd_cmd cmd;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
/* Block-addressed cards ignore MMC_SET_BLOCKLEN. */
|
|
|
|
if (card->flags & CARD_FLAG_SDHC)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
mmcsd_host_lock(card->host);
|
|
|
|
cmd.cmd_code = SET_BLOCKLEN;
|
|
|
|
cmd.arg = 512;
|
|
|
|
cmd.flags = RESP_SPI_R1 | RESP_R1 | CMD_AC;
|
|
|
|
err = mmcsd_send_cmd(card->host, &cmd, 5);
|
|
|
|
mmcsd_host_unlock(card->host);
|
|
|
|
|
2021-03-08 18:19:04 +08:00
|
|
|
if (err)
|
2013-06-28 00:36:54 +08:00
|
|
|
{
|
2018-09-05 14:50:43 +08:00
|
|
|
LOG_E("MMCSD: unable to set block size to %d: %d", cmd.arg, err);
|
2013-06-28 00:36:54 +08:00
|
|
|
|
|
|
|
return -RT_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2011-10-23 17:46:20 +08:00
|
|
|
}
|
2023-08-08 00:22:14 +08:00
|
|
|
|
2024-10-30 11:41:07 +08:00
|
|
|
static rt_ssize_t mmcsd_blk_read(struct rt_blk_disk *disk, rt_off_t sector,
|
|
|
|
void *buffer, rt_size_t sector_count)
|
2023-08-08 00:22:14 +08:00
|
|
|
{
|
2024-10-30 11:41:07 +08:00
|
|
|
rt_err_t err;
|
|
|
|
rt_size_t offset = 0;
|
|
|
|
rt_size_t req_size = 0;
|
|
|
|
rt_size_t remain_size = sector_count;
|
|
|
|
void *rd_ptr = (void *)buffer;
|
|
|
|
struct mmcsd_blk_device *blk_dev = raw_to_mmcsd_blk(disk);
|
2023-08-08 00:22:14 +08:00
|
|
|
|
2024-10-30 11:41:07 +08:00
|
|
|
while (remain_size)
|
2023-08-08 00:22:14 +08:00
|
|
|
{
|
2024-10-30 11:41:07 +08:00
|
|
|
req_size = rt_min_t(rt_size_t, remain_size, blk_dev->max_req_size);
|
2023-08-08 00:22:14 +08:00
|
|
|
|
2024-10-30 11:41:07 +08:00
|
|
|
err = rt_mmcsd_req_blk(blk_dev->card, sector + offset, rd_ptr, req_size, 0);
|
2023-08-08 00:22:14 +08:00
|
|
|
|
2024-10-30 11:41:07 +08:00
|
|
|
if (err)
|
2023-08-08 00:22:14 +08:00
|
|
|
{
|
2024-10-30 11:41:07 +08:00
|
|
|
return err;
|
2023-08-08 00:22:14 +08:00
|
|
|
}
|
|
|
|
|
2024-10-30 11:41:07 +08:00
|
|
|
offset += req_size;
|
|
|
|
rd_ptr = (void *)((rt_uint8_t *)rd_ptr + (req_size << 9));
|
|
|
|
remain_size -= req_size;
|
2023-08-08 00:22:14 +08:00
|
|
|
}
|
|
|
|
|
2024-10-30 11:41:07 +08:00
|
|
|
return sector_count - remain_size;
|
2023-08-08 00:22:14 +08:00
|
|
|
}
|
|
|
|
|
2024-10-30 11:41:07 +08:00
|
|
|
static rt_ssize_t mmcsd_blk_write(struct rt_blk_disk *disk, rt_off_t sector,
|
|
|
|
const void *buffer, rt_size_t sector_count)
|
2023-08-08 00:22:14 +08:00
|
|
|
{
|
2024-10-30 11:41:07 +08:00
|
|
|
rt_err_t err;
|
|
|
|
rt_size_t offset = 0;
|
|
|
|
rt_size_t req_size = 0;
|
|
|
|
rt_size_t remain_size = sector_count;
|
|
|
|
void *wr_ptr = (void *)buffer;
|
|
|
|
struct mmcsd_blk_device *blk_dev = raw_to_mmcsd_blk(disk);
|
2023-08-08 00:22:14 +08:00
|
|
|
|
2024-10-30 11:41:07 +08:00
|
|
|
while (remain_size)
|
2023-08-08 00:22:14 +08:00
|
|
|
{
|
2024-10-30 11:41:07 +08:00
|
|
|
req_size = rt_min_t(rt_size_t, remain_size, blk_dev->max_req_size);
|
2023-08-08 00:22:14 +08:00
|
|
|
|
2024-10-30 11:41:07 +08:00
|
|
|
err = rt_mmcsd_req_blk(blk_dev->card, sector + offset, wr_ptr, req_size, 1);
|
2023-08-08 00:22:14 +08:00
|
|
|
|
2024-10-30 11:41:07 +08:00
|
|
|
if (err)
|
2023-08-08 00:22:14 +08:00
|
|
|
{
|
2024-10-30 11:41:07 +08:00
|
|
|
return err;
|
2023-08-08 00:22:14 +08:00
|
|
|
}
|
|
|
|
|
2024-10-30 11:41:07 +08:00
|
|
|
offset += req_size;
|
|
|
|
wr_ptr = (void *)((rt_uint8_t *)wr_ptr + (req_size << 9));
|
|
|
|
remain_size -= req_size;
|
2023-08-08 00:22:14 +08:00
|
|
|
}
|
|
|
|
|
2024-10-30 11:41:07 +08:00
|
|
|
return sector_count - remain_size;
|
2023-08-08 00:22:14 +08:00
|
|
|
}
|
|
|
|
|
2024-10-30 11:41:07 +08:00
|
|
|
static rt_err_t mmcsd_blk_getgeome(struct rt_blk_disk *disk,
|
|
|
|
struct rt_device_blk_geometry *geometry)
|
2023-08-08 00:22:14 +08:00
|
|
|
{
|
2024-10-30 11:41:07 +08:00
|
|
|
struct mmcsd_blk_device *blk_dev = raw_to_mmcsd_blk(disk);
|
2023-08-08 00:22:14 +08:00
|
|
|
|
2024-10-30 11:41:07 +08:00
|
|
|
rt_memcpy(geometry, &blk_dev->geometry, sizeof(*geometry));
|
2023-08-08 00:22:14 +08:00
|
|
|
|
2023-11-10 17:50:20 +08:00
|
|
|
return RT_EOK;
|
|
|
|
}
|
|
|
|
|
2024-10-30 11:41:07 +08:00
|
|
|
static const struct rt_blk_disk_ops mmcsd_blk_ops =
|
2023-08-08 00:22:14 +08:00
|
|
|
{
|
2024-10-30 11:41:07 +08:00
|
|
|
.read = mmcsd_blk_read,
|
|
|
|
.write = mmcsd_blk_write,
|
|
|
|
.getgeome = mmcsd_blk_getgeome,
|
2023-08-08 00:22:14 +08:00
|
|
|
};
|
|
|
|
|
2024-10-30 11:41:07 +08:00
|
|
|
rt_int32_t rt_mmcsd_blk_probe(struct rt_mmcsd_card *card)
|
2021-12-07 13:04:41 +08:00
|
|
|
{
|
2024-10-30 11:41:07 +08:00
|
|
|
rt_err_t err;
|
|
|
|
struct rt_mmcsd_host *host = card->host;
|
|
|
|
struct mmcsd_blk_device *blk_dev = rt_calloc(1, sizeof(*blk_dev));
|
2021-12-07 13:04:41 +08:00
|
|
|
|
|
|
|
if (!blk_dev)
|
|
|
|
{
|
2024-10-30 11:41:07 +08:00
|
|
|
return -RT_ENOMEM;
|
2021-12-07 13:04:41 +08:00
|
|
|
}
|
2024-10-30 11:41:07 +08:00
|
|
|
card->blk_dev = blk_dev;
|
2021-12-07 13:04:41 +08:00
|
|
|
|
2024-10-30 11:41:07 +08:00
|
|
|
#ifdef RT_USING_DM
|
|
|
|
blk_dev->parent.ida = &sdio_ida;
|
2021-12-07 13:04:41 +08:00
|
|
|
#endif
|
2024-10-30 11:41:07 +08:00
|
|
|
blk_dev->parent.parallel_io = RT_FALSE;
|
|
|
|
blk_dev->parent.removable = controller_is_removable(host);
|
|
|
|
blk_dev->parent.ops = &mmcsd_blk_ops;
|
|
|
|
blk_dev->parent.max_partitions = RT_MMCSD_MAX_PARTITION;
|
2022-12-03 12:07:44 +08:00
|
|
|
|
2024-10-30 11:41:07 +08:00
|
|
|
blk_dev->card = card;
|
|
|
|
blk_dev->max_req_size = rt_min_t(rt_size_t,
|
|
|
|
host->max_dma_segs * host->max_seg_size,
|
|
|
|
host->max_blk_count * host->max_blk_size) >> 9;
|
2022-12-03 12:07:44 +08:00
|
|
|
blk_dev->geometry.bytes_per_sector = 1 << 9;
|
|
|
|
blk_dev->geometry.block_size = card->card_blksize;
|
2024-10-30 11:41:07 +08:00
|
|
|
blk_dev->geometry.sector_count = card->card_capacity * (1024 / 512);
|
2022-12-03 12:07:44 +08:00
|
|
|
|
2024-10-30 11:41:07 +08:00
|
|
|
/* Set blk size before partitions probe, Why? */
|
|
|
|
if ((err = mmcsd_set_blksize(card)))
|
2013-06-28 00:36:54 +08:00
|
|
|
{
|
2024-10-30 11:41:07 +08:00
|
|
|
goto _fail;
|
2013-06-28 00:36:54 +08:00
|
|
|
}
|
2022-12-03 12:07:44 +08:00
|
|
|
rt_thread_mdelay(1);
|
2013-06-28 00:36:54 +08:00
|
|
|
|
2024-10-30 11:41:07 +08:00
|
|
|
#ifdef RT_USING_DM
|
|
|
|
rt_dm_dev_set_name(&blk_dev->parent.parent, host->name);
|
2022-12-03 12:07:44 +08:00
|
|
|
#else
|
2024-10-30 11:41:07 +08:00
|
|
|
rt_strncpy(blk_dev->parent.parent.parent.name, host->name, RT_NAME_MAX);
|
2022-12-03 12:07:44 +08:00
|
|
|
#endif
|
2021-11-29 14:48:56 +08:00
|
|
|
|
2024-10-30 11:41:07 +08:00
|
|
|
if ((err = rt_hw_blk_disk_register(&blk_dev->parent)))
|
2013-06-28 00:36:54 +08:00
|
|
|
{
|
2024-10-30 11:41:07 +08:00
|
|
|
goto _fail;
|
2013-06-28 00:36:54 +08:00
|
|
|
}
|
2021-03-08 18:19:04 +08:00
|
|
|
|
2024-10-30 11:41:07 +08:00
|
|
|
return RT_EOK;
|
2022-12-03 12:07:44 +08:00
|
|
|
|
2024-10-30 11:41:07 +08:00
|
|
|
_fail:
|
|
|
|
card->blk_dev = RT_NULL;
|
|
|
|
free(blk_dev);
|
2011-10-23 17:46:20 +08:00
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
return err;
|
|
|
|
}
|
2022-11-02 14:27:41 +08:00
|
|
|
|
2022-12-03 12:07:44 +08:00
|
|
|
void rt_mmcsd_blk_remove(struct rt_mmcsd_card *card)
|
|
|
|
{
|
2024-10-30 11:41:07 +08:00
|
|
|
struct mmcsd_blk_device *blk_dev = card->blk_dev;
|
2022-12-03 12:07:44 +08:00
|
|
|
|
2024-10-30 11:41:07 +08:00
|
|
|
if (!blk_dev)
|
2013-06-28 00:36:54 +08:00
|
|
|
{
|
2024-10-30 11:41:07 +08:00
|
|
|
return;
|
2013-06-28 00:36:54 +08:00
|
|
|
}
|
2022-12-03 12:07:44 +08:00
|
|
|
|
2024-10-30 11:41:07 +08:00
|
|
|
if (!rt_hw_blk_disk_unregister(&blk_dev->parent))
|
|
|
|
{
|
|
|
|
card->blk_dev = RT_NULL;
|
|
|
|
rt_free(blk_dev);
|
|
|
|
}
|
2022-12-03 12:07:44 +08:00
|
|
|
}
|