import RT-Thread@9217865c without bsp, libcpu and components/net
This commit is contained in:
commit
e2376a3709
1414 changed files with 390370 additions and 0 deletions
19
components/drivers/sdio/SConscript
Normal file
19
components/drivers/sdio/SConscript
Normal file
|
@ -0,0 +1,19 @@
|
|||
Import('RTT_ROOT')
|
||||
from building import *
|
||||
|
||||
cwd = GetCurrentDir()
|
||||
src = Split("""
|
||||
block_dev.c
|
||||
mmcsd_core.c
|
||||
sd.c
|
||||
sdio.c
|
||||
gpt.c
|
||||
mmc.c
|
||||
""")
|
||||
|
||||
# The set of source files associated with this SConscript file.
|
||||
path = [cwd + '/../include']
|
||||
|
||||
group = DefineGroup('DeviceDrivers', src, depend = ['RT_USING_SDIO'], CPPPATH = path)
|
||||
|
||||
Return('group')
|
722
components/drivers/sdio/block_dev.c
Normal file
722
components/drivers/sdio/block_dev.c
Normal file
|
@ -0,0 +1,722 @@
|
|||
/*
|
||||
* Copyright (c) 2006-2023, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2011-07-25 weety first version
|
||||
*/
|
||||
|
||||
#include <rtthread.h>
|
||||
#include <dfs_fs.h>
|
||||
|
||||
#include <drivers/mmcsd_core.h>
|
||||
#include <drivers/gpt.h>
|
||||
|
||||
#define DBG_TAG "SDIO"
|
||||
#ifdef RT_SDIO_DEBUG
|
||||
#define DBG_LVL DBG_LOG
|
||||
#else
|
||||
#define DBG_LVL DBG_INFO
|
||||
#endif /* RT_SDIO_DEBUG */
|
||||
#include <rtdbg.h>
|
||||
|
||||
static rt_list_t blk_devices = RT_LIST_OBJECT_INIT(blk_devices);
|
||||
|
||||
#define BLK_MIN(a, b) ((a) < (b) ? (a) : (b))
|
||||
|
||||
struct mmcsd_blk_device
|
||||
{
|
||||
struct rt_mmcsd_card *card;
|
||||
rt_list_t list;
|
||||
struct rt_device dev;
|
||||
struct dfs_partition part;
|
||||
struct rt_device_blk_geometry geometry;
|
||||
rt_size_t max_req_size;
|
||||
};
|
||||
|
||||
#ifndef RT_MMCSD_MAX_PARTITION
|
||||
#define RT_MMCSD_MAX_PARTITION 16
|
||||
#endif
|
||||
#define RT_GPT_PARTITION_MAX 128
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
rt_int32_t mmcsd_num_wr_blocks(struct rt_mmcsd_card *card)
|
||||
{
|
||||
rt_int32_t err;
|
||||
rt_uint32_t blocks;
|
||||
|
||||
struct rt_mmcsd_req req;
|
||||
struct rt_mmcsd_cmd cmd;
|
||||
struct rt_mmcsd_data data;
|
||||
rt_uint32_t timeout_us;
|
||||
|
||||
rt_memset(&cmd, 0, sizeof(struct rt_mmcsd_cmd));
|
||||
|
||||
cmd.cmd_code = APP_CMD;
|
||||
cmd.arg = card->rca << 16;
|
||||
cmd.flags = RESP_SPI_R1 | RESP_R1 | CMD_AC;
|
||||
|
||||
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;
|
||||
|
||||
rt_memset(&cmd, 0, sizeof(struct rt_mmcsd_cmd));
|
||||
|
||||
cmd.cmd_code = SD_APP_SEND_NUM_WR_BLKS;
|
||||
cmd.arg = 0;
|
||||
cmd.flags = RESP_SPI_R1 | RESP_R1 | CMD_ADTC;
|
||||
|
||||
rt_memset(&data, 0, sizeof(struct rt_mmcsd_data));
|
||||
|
||||
data.timeout_ns = card->tacc_ns * 100;
|
||||
data.timeout_clks = card->tacc_clks * 100;
|
||||
|
||||
timeout_us = data.timeout_ns / 1000;
|
||||
timeout_us += data.timeout_clks * 1000 /
|
||||
(card->host->io_cfg.clock / 1000);
|
||||
|
||||
if (timeout_us > 100000)
|
||||
{
|
||||
data.timeout_ns = 100000000;
|
||||
data.timeout_clks = 0;
|
||||
}
|
||||
|
||||
data.blksize = 4;
|
||||
data.blks = 1;
|
||||
data.flags = DATA_DIR_READ;
|
||||
data.buf = &blocks;
|
||||
|
||||
rt_memset(&req, 0, sizeof(struct rt_mmcsd_req));
|
||||
|
||||
req.cmd = &cmd;
|
||||
req.data = &data;
|
||||
|
||||
mmcsd_send_request(card->host, &req);
|
||||
|
||||
if (cmd.err || data.err)
|
||||
return -RT_ERROR;
|
||||
|
||||
return blocks;
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
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;
|
||||
|
||||
cmd.arg = sector;
|
||||
if (!(card->flags & CARD_FLAG_SDHC))
|
||||
{
|
||||
cmd.arg <<= 9;
|
||||
}
|
||||
cmd.flags = RESP_SPI_R1 | RESP_R1 | CMD_ADTC;
|
||||
|
||||
data.blksize = SECTOR_SIZE;
|
||||
data.blks = blks;
|
||||
|
||||
if (blks > 1)
|
||||
{
|
||||
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
|
||||
{
|
||||
req.stop = RT_NULL;
|
||||
r_cmd = READ_SINGLE_BLOCK;
|
||||
w_cmd = WRITE_BLOCK;
|
||||
}
|
||||
|
||||
if (!controller_is_spi(card->host) && (card->flags & 0x8000))
|
||||
{
|
||||
/* last request is WRITE,need check busy */
|
||||
card_busy_detect(card, 10000, RT_NULL);
|
||||
}
|
||||
|
||||
if (!dir)
|
||||
{
|
||||
cmd.cmd_code = r_cmd;
|
||||
data.flags |= DATA_DIR_READ;
|
||||
card->flags &= 0x7fff;
|
||||
}
|
||||
else
|
||||
{
|
||||
cmd.cmd_code = w_cmd;
|
||||
data.flags |= DATA_DIR_WRITE;
|
||||
card->flags |= 0x8000;
|
||||
}
|
||||
|
||||
mmcsd_set_data_timeout(&data, card);
|
||||
data.buf = buf;
|
||||
|
||||
mmcsd_send_request(host, &req);
|
||||
|
||||
mmcsd_host_unlock(host);
|
||||
|
||||
if (cmd.err || data.err || stop.err)
|
||||
{
|
||||
LOG_E("mmcsd request blocks error");
|
||||
LOG_E("%d,%d,%d, 0x%08x,0x%08x",
|
||||
cmd.err, data.err, stop.err, data.flags, sector);
|
||||
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
static rt_err_t rt_mmcsd_init(rt_device_t dev)
|
||||
{
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
static rt_err_t rt_mmcsd_open(rt_device_t dev, rt_uint16_t oflag)
|
||||
{
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
static rt_err_t rt_mmcsd_close(rt_device_t dev)
|
||||
{
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
static rt_err_t rt_mmcsd_control(rt_device_t dev, int cmd, void *args)
|
||||
{
|
||||
struct mmcsd_blk_device *blk_dev = (struct mmcsd_blk_device *)dev->user_data;
|
||||
switch (cmd)
|
||||
{
|
||||
case RT_DEVICE_CTRL_BLK_GETGEOME:
|
||||
rt_memcpy(args, &blk_dev->geometry, sizeof(struct rt_device_blk_geometry));
|
||||
break;
|
||||
case RT_DEVICE_CTRL_BLK_PARTITION:
|
||||
rt_memcpy(args, &blk_dev->part, sizeof(struct dfs_partition));
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
static rt_ssize_t rt_mmcsd_read(rt_device_t dev,
|
||||
rt_off_t pos,
|
||||
void *buffer,
|
||||
rt_size_t size)
|
||||
{
|
||||
rt_err_t err = 0;
|
||||
rt_size_t offset = 0;
|
||||
rt_size_t req_size = 0;
|
||||
rt_size_t remain_size = size;
|
||||
void *rd_ptr = (void *)buffer;
|
||||
struct mmcsd_blk_device *blk_dev = (struct mmcsd_blk_device *)dev->user_data;
|
||||
struct dfs_partition *part = &blk_dev->part;
|
||||
|
||||
if (dev == RT_NULL)
|
||||
{
|
||||
rt_set_errno(-EINVAL);
|
||||
return 0;
|
||||
}
|
||||
|
||||
rt_sem_take(part->lock, RT_WAITING_FOREVER);
|
||||
while (remain_size)
|
||||
{
|
||||
req_size = (remain_size > blk_dev->max_req_size) ? blk_dev->max_req_size : remain_size;
|
||||
err = rt_mmcsd_req_blk(blk_dev->card, part->offset + pos + offset, rd_ptr, req_size, 0);
|
||||
if (err)
|
||||
break;
|
||||
offset += req_size;
|
||||
rd_ptr = (void *)((rt_uint8_t *)rd_ptr + (req_size << 9));
|
||||
remain_size -= req_size;
|
||||
}
|
||||
rt_sem_release(part->lock);
|
||||
|
||||
/* the length of reading must align to SECTOR SIZE */
|
||||
if (err)
|
||||
{
|
||||
rt_set_errno(-EIO);
|
||||
return 0;
|
||||
}
|
||||
return size - remain_size;
|
||||
}
|
||||
|
||||
static rt_ssize_t rt_mmcsd_write(rt_device_t dev,
|
||||
rt_off_t pos,
|
||||
const void *buffer,
|
||||
rt_size_t size)
|
||||
{
|
||||
rt_err_t err = 0;
|
||||
rt_size_t offset = 0;
|
||||
rt_size_t req_size = 0;
|
||||
rt_size_t remain_size = size;
|
||||
void *wr_ptr = (void *)buffer;
|
||||
struct mmcsd_blk_device *blk_dev = (struct mmcsd_blk_device *)dev->user_data;
|
||||
struct dfs_partition *part = &blk_dev->part;
|
||||
|
||||
if (dev == RT_NULL)
|
||||
{
|
||||
rt_set_errno(-EINVAL);
|
||||
return 0;
|
||||
}
|
||||
|
||||
rt_sem_take(part->lock, RT_WAITING_FOREVER);
|
||||
while (remain_size)
|
||||
{
|
||||
req_size = (remain_size > blk_dev->max_req_size) ? blk_dev->max_req_size : remain_size;
|
||||
err = rt_mmcsd_req_blk(blk_dev->card, part->offset + pos + offset, wr_ptr, req_size, 1);
|
||||
if (err)
|
||||
break;
|
||||
offset += req_size;
|
||||
wr_ptr = (void *)((rt_uint8_t *)wr_ptr + (req_size << 9));
|
||||
remain_size -= req_size;
|
||||
}
|
||||
rt_sem_release(part->lock);
|
||||
|
||||
/* the length of reading must align to SECTOR SIZE */
|
||||
if (err)
|
||||
{
|
||||
rt_set_errno(-EIO);
|
||||
|
||||
return 0;
|
||||
}
|
||||
return size - remain_size;
|
||||
}
|
||||
|
||||
static rt_int32_t mmcsd_set_blksize(struct rt_mmcsd_card *card)
|
||||
{
|
||||
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);
|
||||
|
||||
if (err)
|
||||
{
|
||||
LOG_E("MMCSD: unable to set block size to %d: %d", cmd.arg, err);
|
||||
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
rt_int32_t read_lba(struct rt_mmcsd_card *card, size_t lba, uint8_t *buffer, size_t count)
|
||||
{
|
||||
rt_uint8_t status = 0;
|
||||
|
||||
status = mmcsd_set_blksize(card);
|
||||
if (status)
|
||||
{
|
||||
return status;
|
||||
}
|
||||
rt_thread_mdelay(1);
|
||||
status = rt_mmcsd_req_blk(card, lba, buffer, count, 0);
|
||||
return status;
|
||||
}
|
||||
|
||||
#ifdef RT_USING_DEVICE_OPS
|
||||
const static struct rt_device_ops mmcsd_blk_ops =
|
||||
{
|
||||
rt_mmcsd_init,
|
||||
rt_mmcsd_open,
|
||||
rt_mmcsd_close,
|
||||
rt_mmcsd_read,
|
||||
rt_mmcsd_write,
|
||||
rt_mmcsd_control
|
||||
};
|
||||
#endif
|
||||
|
||||
rt_int32_t gpt_device_probe(struct rt_mmcsd_card *card)
|
||||
{
|
||||
rt_int32_t err = RT_EOK;
|
||||
rt_uint8_t i, status;
|
||||
char dname[10];
|
||||
char sname[16];
|
||||
struct mmcsd_blk_device *blk_dev = RT_NULL;
|
||||
|
||||
blk_dev = rt_calloc(1, sizeof(struct mmcsd_blk_device));
|
||||
if (!blk_dev)
|
||||
{
|
||||
LOG_E("mmcsd:malloc memory failed!");
|
||||
return -1;
|
||||
}
|
||||
|
||||
blk_dev->max_req_size = BLK_MIN((card->host->max_dma_segs *
|
||||
card->host->max_seg_size) >> 9,
|
||||
(card->host->max_blk_count *
|
||||
card->host->max_blk_size) >> 9);
|
||||
blk_dev->part.offset = 0;
|
||||
blk_dev->part.size = 0;
|
||||
rt_snprintf(sname, sizeof(sname) - 1, "sem_%s%d", card->host->name, 0);
|
||||
blk_dev->part.lock = rt_sem_create(sname, 1, RT_IPC_FLAG_FIFO);
|
||||
/* register mmcsd device */
|
||||
blk_dev->dev.type = RT_Device_Class_Block;
|
||||
#ifdef RT_USING_DEVICE_OPS
|
||||
blk_dev->dev.ops = &mmcsd_blk_ops;
|
||||
#else
|
||||
blk_dev->dev.init = rt_mmcsd_init;
|
||||
blk_dev->dev.open = rt_mmcsd_open;
|
||||
blk_dev->dev.close = rt_mmcsd_close;
|
||||
blk_dev->dev.read = rt_mmcsd_read;
|
||||
blk_dev->dev.write = rt_mmcsd_write;
|
||||
blk_dev->dev.control = rt_mmcsd_control;
|
||||
#endif
|
||||
blk_dev->card = card;
|
||||
|
||||
blk_dev->geometry.bytes_per_sector = 1 << 9;
|
||||
blk_dev->geometry.block_size = card->card_blksize;
|
||||
blk_dev->geometry.sector_count =
|
||||
card->card_capacity * (1024 / 512);
|
||||
|
||||
blk_dev->dev.user_data = blk_dev;
|
||||
|
||||
rt_device_register(&(blk_dev->dev), card->host->name,
|
||||
RT_DEVICE_FLAG_RDWR);
|
||||
rt_list_insert_after(&blk_devices, &blk_dev->list);
|
||||
|
||||
for (i = 0; i < RT_GPT_PARTITION_MAX; i++)
|
||||
{
|
||||
blk_dev = rt_calloc(1, sizeof(struct mmcsd_blk_device));
|
||||
if (!blk_dev)
|
||||
{
|
||||
LOG_E("mmcsd:malloc memory failed!");
|
||||
break;
|
||||
}
|
||||
blk_dev->max_req_size = BLK_MIN((card->host->max_dma_segs *
|
||||
card->host->max_seg_size) >> 9,
|
||||
(card->host->max_blk_count *
|
||||
card->host->max_blk_size) >> 9);
|
||||
|
||||
/* get the first partition */
|
||||
status = gpt_get_partition_param(card, &blk_dev->part, i);
|
||||
if (status == RT_EOK)
|
||||
{
|
||||
rt_snprintf(dname, sizeof(dname) - 1, "%s%d", card->host->name, i);
|
||||
rt_snprintf(sname, sizeof(sname) - 1, "sem_%s%d", card->host->name, i + 1);
|
||||
blk_dev->part.lock = rt_sem_create(sname, 1, RT_IPC_FLAG_FIFO);
|
||||
|
||||
/* register mmcsd device */
|
||||
blk_dev->dev.type = RT_Device_Class_Block;
|
||||
#ifdef RT_USING_DEVICE_OPS
|
||||
blk_dev->dev.ops = &mmcsd_blk_ops;
|
||||
#else
|
||||
blk_dev->dev.init = rt_mmcsd_init;
|
||||
blk_dev->dev.open = rt_mmcsd_open;
|
||||
blk_dev->dev.close = rt_mmcsd_close;
|
||||
blk_dev->dev.read = rt_mmcsd_read;
|
||||
blk_dev->dev.write = rt_mmcsd_write;
|
||||
blk_dev->dev.control = rt_mmcsd_control;
|
||||
#endif
|
||||
blk_dev->card = card;
|
||||
|
||||
blk_dev->geometry.bytes_per_sector = 1 << 9;
|
||||
blk_dev->geometry.block_size = card->card_blksize;
|
||||
blk_dev->geometry.sector_count = blk_dev->part.size;
|
||||
|
||||
blk_dev->dev.user_data = blk_dev;
|
||||
|
||||
rt_device_register(&(blk_dev->dev), dname,
|
||||
RT_DEVICE_FLAG_RDWR);
|
||||
rt_list_insert_after(&blk_devices, &blk_dev->list);
|
||||
}
|
||||
else
|
||||
{
|
||||
rt_free(blk_dev);
|
||||
blk_dev = RT_NULL;
|
||||
break;
|
||||
}
|
||||
|
||||
#ifdef RT_USING_DFS_MNTTABLE
|
||||
if (blk_dev)
|
||||
{
|
||||
LOG_I("try to mount file system!");
|
||||
/* try to mount file system on this block device */
|
||||
dfs_mount_device(&(blk_dev->dev));
|
||||
}
|
||||
#endif
|
||||
}
|
||||
gpt_free();
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
rt_int32_t mbr_device_probe(struct rt_mmcsd_card *card)
|
||||
{
|
||||
rt_int32_t err = 0;
|
||||
rt_uint8_t i, status;
|
||||
rt_uint8_t *sector;
|
||||
char dname[10];
|
||||
char sname[16];
|
||||
struct mmcsd_blk_device *blk_dev = RT_NULL;
|
||||
|
||||
err = mmcsd_set_blksize(card);
|
||||
if (err)
|
||||
{
|
||||
return err;
|
||||
}
|
||||
rt_thread_mdelay(1);
|
||||
/* get the first sector to read partition table */
|
||||
sector = (rt_uint8_t *)rt_malloc(SECTOR_SIZE);
|
||||
if (sector == RT_NULL)
|
||||
{
|
||||
LOG_E("allocate partition sector buffer failed!");
|
||||
|
||||
return -RT_ENOMEM;
|
||||
}
|
||||
|
||||
status = rt_mmcsd_req_blk(card, 0, sector, 1, 0);
|
||||
if (status == RT_EOK)
|
||||
{
|
||||
blk_dev = rt_calloc(1, sizeof(struct mmcsd_blk_device));
|
||||
if (!blk_dev)
|
||||
{
|
||||
LOG_E("mmcsd:malloc memory failed!");
|
||||
return -1;
|
||||
}
|
||||
blk_dev->max_req_size = BLK_MIN((card->host->max_dma_segs *
|
||||
card->host->max_seg_size) >> 9,
|
||||
(card->host->max_blk_count *
|
||||
card->host->max_blk_size) >> 9);
|
||||
blk_dev->part.offset = 0;
|
||||
blk_dev->part.size = 0;
|
||||
rt_snprintf(sname, sizeof(sname) - 1, "sem_%s%d", card->host->name, 0);
|
||||
blk_dev->part.lock = rt_sem_create(sname, 1, RT_IPC_FLAG_FIFO);
|
||||
/* register mmcsd device */
|
||||
blk_dev->dev.type = RT_Device_Class_Block;
|
||||
#ifdef RT_USING_DEVICE_OPS
|
||||
blk_dev->dev.ops = &mmcsd_blk_ops;
|
||||
#else
|
||||
blk_dev->dev.init = rt_mmcsd_init;
|
||||
blk_dev->dev.open = rt_mmcsd_open;
|
||||
blk_dev->dev.close = rt_mmcsd_close;
|
||||
blk_dev->dev.read = rt_mmcsd_read;
|
||||
blk_dev->dev.write = rt_mmcsd_write;
|
||||
blk_dev->dev.control = rt_mmcsd_control;
|
||||
#endif
|
||||
blk_dev->card = card;
|
||||
|
||||
blk_dev->geometry.bytes_per_sector = 1 << 9;
|
||||
blk_dev->geometry.block_size = card->card_blksize;
|
||||
blk_dev->geometry.sector_count =
|
||||
card->card_capacity * (1024 / 512);
|
||||
|
||||
blk_dev->dev.user_data = blk_dev;
|
||||
|
||||
rt_device_register(&(blk_dev->dev), card->host->name,
|
||||
RT_DEVICE_FLAG_RDWR);
|
||||
rt_list_insert_after(&blk_devices, &blk_dev->list);
|
||||
for (i = 0; i < RT_MMCSD_MAX_PARTITION; i++)
|
||||
{
|
||||
blk_dev = rt_calloc(1, sizeof(struct mmcsd_blk_device));
|
||||
if (!blk_dev)
|
||||
{
|
||||
LOG_E("mmcsd:malloc memory failed!");
|
||||
break;
|
||||
}
|
||||
blk_dev->max_req_size = BLK_MIN((card->host->max_dma_segs *
|
||||
card->host->max_seg_size) >> 9,
|
||||
(card->host->max_blk_count *
|
||||
card->host->max_blk_size) >> 9);
|
||||
|
||||
/* get the first partition */
|
||||
status = dfs_filesystem_get_partition(&blk_dev->part, sector, i);
|
||||
if (status == RT_EOK)
|
||||
{
|
||||
rt_snprintf(dname, sizeof(dname) - 1, "%s%d", card->host->name, i);
|
||||
rt_snprintf(sname, sizeof(sname) - 1, "sem_%s%d", card->host->name, i + 1);
|
||||
blk_dev->part.lock = rt_sem_create(sname, 1, RT_IPC_FLAG_FIFO);
|
||||
|
||||
/* register mmcsd device */
|
||||
blk_dev->dev.type = RT_Device_Class_Block;
|
||||
#ifdef RT_USING_DEVICE_OPS
|
||||
blk_dev->dev.ops = &mmcsd_blk_ops;
|
||||
#else
|
||||
blk_dev->dev.init = rt_mmcsd_init;
|
||||
blk_dev->dev.open = rt_mmcsd_open;
|
||||
blk_dev->dev.close = rt_mmcsd_close;
|
||||
blk_dev->dev.read = rt_mmcsd_read;
|
||||
blk_dev->dev.write = rt_mmcsd_write;
|
||||
blk_dev->dev.control = rt_mmcsd_control;
|
||||
#endif
|
||||
blk_dev->card = card;
|
||||
|
||||
blk_dev->geometry.bytes_per_sector = 1 << 9;
|
||||
blk_dev->geometry.block_size = card->card_blksize;
|
||||
blk_dev->geometry.sector_count = blk_dev->part.size;
|
||||
|
||||
blk_dev->dev.user_data = blk_dev;
|
||||
|
||||
rt_device_register(&(blk_dev->dev), dname,
|
||||
RT_DEVICE_FLAG_RDWR);
|
||||
rt_list_insert_after(&blk_devices, &blk_dev->list);
|
||||
}
|
||||
else
|
||||
{
|
||||
rt_free(blk_dev);
|
||||
blk_dev = RT_NULL;
|
||||
break;
|
||||
}
|
||||
|
||||
#ifdef RT_USING_DFS_MNTTABLE
|
||||
if (blk_dev)
|
||||
{
|
||||
LOG_I("try to mount file system!");
|
||||
/* try to mount file system on this block device */
|
||||
dfs_mount_device(&(blk_dev->dev));
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
LOG_E("read mmcsd first sector failed");
|
||||
err = -RT_ERROR;
|
||||
}
|
||||
|
||||
/* release sector buffer */
|
||||
rt_free(sector);
|
||||
|
||||
return err;
|
||||
|
||||
}
|
||||
|
||||
rt_int32_t rt_mmcsd_blk_probe(struct rt_mmcsd_card *card)
|
||||
{
|
||||
uint32_t err = 0;
|
||||
|
||||
LOG_D("probe mmcsd block device!");
|
||||
if (check_gpt(card) != 0)
|
||||
{
|
||||
err = gpt_device_probe(card);
|
||||
}
|
||||
else
|
||||
{
|
||||
err = mbr_device_probe(card);
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
||||
void rt_mmcsd_blk_remove(struct rt_mmcsd_card *card)
|
||||
{
|
||||
rt_list_t *l, *n;
|
||||
struct mmcsd_blk_device *blk_dev;
|
||||
|
||||
for (l = (&blk_devices)->next, n = l->next; l != &blk_devices; l = n, n = n->next)
|
||||
{
|
||||
blk_dev = (struct mmcsd_blk_device *)rt_list_entry(l, struct mmcsd_blk_device, list);
|
||||
if (blk_dev->card == card)
|
||||
{
|
||||
/* unmount file system */
|
||||
const char *mounted_path = dfs_filesystem_get_mounted_path(&(blk_dev->dev));
|
||||
if (mounted_path)
|
||||
{
|
||||
dfs_unmount(mounted_path);
|
||||
LOG_D("unmount file system %s for device %s.\r\n", mounted_path, blk_dev->dev.parent.name);
|
||||
}
|
||||
rt_sem_delete(blk_dev->part.lock);
|
||||
rt_device_unregister(&blk_dev->dev);
|
||||
rt_list_remove(&blk_dev->list);
|
||||
rt_free(blk_dev);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* This function will initialize block device on the mmc/sd.
|
||||
*
|
||||
* @deprecated since 2.1.0, this function does not need to be invoked
|
||||
* in the system initialization.
|
||||
*/
|
||||
int rt_mmcsd_blk_init(void)
|
||||
{
|
||||
/* nothing */
|
||||
return 0;
|
||||
}
|
561
components/drivers/sdio/gpt.c
Normal file
561
components/drivers/sdio/gpt.c
Normal file
|
@ -0,0 +1,561 @@
|
|||
/*
|
||||
* Copyright (c) 2006-2023, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2022-05-05 linzhenxing first version
|
||||
*/
|
||||
#include <rtthread.h>
|
||||
#include <dfs_fs.h>
|
||||
#include <drivers/gpt.h>
|
||||
#include <drivers/mmcsd_core.h>
|
||||
|
||||
#define DBG_TAG "GPT"
|
||||
#ifdef RT_SDIO_DEBUG
|
||||
#define DBG_LVL DBG_LOG
|
||||
#else
|
||||
#define DBG_LVL DBG_INFO
|
||||
#endif /* RT_SDIO_DEBUG */
|
||||
#include <rtdbg.h>
|
||||
|
||||
#define min(a, b) a < b ? a : b
|
||||
static int force_gpt = 0;
|
||||
static gpt_header *_gpt;
|
||||
static gpt_entry *_ptes;
|
||||
#define GPT_TYPE 1
|
||||
#define MBR_TYPE 0
|
||||
|
||||
static inline int efi_guidcmp (gpt_guid_t left, gpt_guid_t right)
|
||||
{
|
||||
return rt_memcmp(&left, &right, sizeof (gpt_guid_t));
|
||||
}
|
||||
|
||||
static uint32_t last_lba(struct rt_mmcsd_card *card)
|
||||
{
|
||||
RT_ASSERT(card != RT_NULL);
|
||||
return (card->card_sec_cnt) - 1;
|
||||
}
|
||||
|
||||
static inline int pmbr_part_valid(gpt_mbr_record *part)
|
||||
{
|
||||
if (part->os_type != EFI_PMBR_OSTYPE_EFI_GPT)
|
||||
{
|
||||
goto invalid;
|
||||
}
|
||||
|
||||
/* set to 0x00000001 (i.e., the LBA of the GPT Partition Header) */
|
||||
if ((uint32_t)(part->starting_lba) != GPT_PRIMARY_PARTITION_TABLE_LBA)
|
||||
{
|
||||
goto invalid;
|
||||
}
|
||||
|
||||
return GPT_MBR_PROTECTIVE;
|
||||
invalid:
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
*
|
||||
* return ret
|
||||
* ret = 0, invalid mbr
|
||||
* ret = 1, protect mbr
|
||||
* ret = 2, hybrid mbr
|
||||
*/
|
||||
int is_pmbr_valid(legacy_mbr *mbr, uint64_t total_sectors)
|
||||
{
|
||||
uint32_t sz = 0;
|
||||
int i, part = 0, ret = 0; /* invalid by default */
|
||||
|
||||
if (!mbr || (uint16_t)(mbr->signature) != MSDOS_MBR_SIGNATURE)
|
||||
{
|
||||
goto done;
|
||||
}
|
||||
|
||||
for (i = 0; i < 4; i++)
|
||||
{
|
||||
ret = pmbr_part_valid(&mbr->partition_record[i]);
|
||||
if (ret == GPT_MBR_PROTECTIVE)
|
||||
{
|
||||
part = i;
|
||||
/*
|
||||
* Ok, we at least know that there's a protective MBR,
|
||||
* now check if there are other partition types for
|
||||
* hybrid MBR.
|
||||
*/
|
||||
goto check_hybrid;
|
||||
}
|
||||
}
|
||||
|
||||
if (ret != GPT_MBR_PROTECTIVE)
|
||||
{
|
||||
goto done;
|
||||
}
|
||||
|
||||
check_hybrid:
|
||||
for (i = 0; i < 4; i++)
|
||||
{
|
||||
if ((mbr->partition_record[i].os_type !=
|
||||
EFI_PMBR_OSTYPE_EFI_GPT) &&
|
||||
(mbr->partition_record[i].os_type != 0x00))
|
||||
{
|
||||
ret = GPT_MBR_HYBRID;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
* Protective MBRs take up the lesser of the whole disk
|
||||
* or 2 TiB (32bit LBA), ignoring the rest of the disk.
|
||||
* Some partitioning programs, nonetheless, choose to set
|
||||
* the size to the maximum 32-bit limitation, disregarding
|
||||
* the disk size.
|
||||
*
|
||||
* Hybrid MBRs do not necessarily comply with this.
|
||||
*
|
||||
* Consider a bad value here to be a warning to support dd'ing
|
||||
* an image from a smaller disk to a larger disk.
|
||||
*/
|
||||
if (ret == GPT_MBR_PROTECTIVE)
|
||||
{
|
||||
sz = (uint32_t)(mbr->partition_record[part].size_in_lba);
|
||||
if (sz != (uint32_t) total_sectors - 1 && sz != 0xFFFFFFFF)
|
||||
{
|
||||
LOG_I("GPT: mbr size in lba (%u) different than whole disk (%u).",
|
||||
sz, min(total_sectors - 1, 0xFFFFFFFF));
|
||||
}
|
||||
}
|
||||
|
||||
done:
|
||||
return ret;
|
||||
|
||||
}
|
||||
|
||||
static gpt_entry *alloc_read_gpt_entries(struct rt_mmcsd_card *card, gpt_header *gpt)
|
||||
{
|
||||
size_t count;
|
||||
gpt_entry *pte;
|
||||
|
||||
if (!gpt)
|
||||
{
|
||||
return RT_NULL;
|
||||
}
|
||||
|
||||
count = (size_t)(gpt->num_partition_entries) * (gpt->sizeof_partition_entry);
|
||||
if (!count)
|
||||
{
|
||||
return RT_NULL;
|
||||
}
|
||||
|
||||
pte = rt_malloc(count);
|
||||
if (!pte)
|
||||
return RT_NULL;
|
||||
|
||||
if (read_lba(card, (size_t)(gpt->partition_entry_lba),(uint8_t *)pte, count/512) != RT_EOK)
|
||||
{
|
||||
rt_free(pte);
|
||||
return RT_NULL;
|
||||
}
|
||||
return pte;
|
||||
|
||||
}
|
||||
|
||||
static gpt_header *alloc_read_gpt_header(struct rt_mmcsd_card *card, size_t lba)
|
||||
{
|
||||
gpt_header *gpt;
|
||||
void *buf;
|
||||
|
||||
buf = rt_malloc(512);
|
||||
if (!buf)
|
||||
{
|
||||
return RT_NULL;
|
||||
}
|
||||
|
||||
if (read_lba(card, lba, (uint8_t *)buf, 1) != RT_EOK)
|
||||
{
|
||||
rt_free(buf);
|
||||
return RT_NULL;
|
||||
}
|
||||
gpt = (gpt_header *)buf;
|
||||
|
||||
return gpt;
|
||||
}
|
||||
|
||||
static int is_gpt_valid(struct rt_mmcsd_card *card, size_t lba, gpt_header **gpt, gpt_entry **ptes)
|
||||
{
|
||||
size_t lastlba;
|
||||
|
||||
if (!ptes)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!(*gpt = alloc_read_gpt_header(card, lba)))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Check the GUID Partition Table signature */
|
||||
if ((uint64_t)((*gpt)->signature) != GPT_HEADER_SIGNATURE)
|
||||
{
|
||||
LOG_E("GUID Partition Table Header signature is wrong:"
|
||||
"%ld != %ld",(uint64_t)((*gpt)->signature),(uint64_t)GPT_HEADER_SIGNATURE);
|
||||
goto fail;
|
||||
}
|
||||
|
||||
/* Check the GUID Partition Table header size is too small */
|
||||
if ((uint32_t)((*gpt)->header_size) < sizeof(gpt_header))
|
||||
{
|
||||
LOG_E("GUID Partition Table Header size is too small: %u < %zu",
|
||||
(uint32_t)((*gpt)->header_size),sizeof(gpt_header));
|
||||
goto fail;
|
||||
}
|
||||
|
||||
/* Check that the start_lba entry points to the LBA that contains
|
||||
* the GUID Partition Table */
|
||||
if ((uint64_t)((*gpt)->start_lba) != lba)
|
||||
{
|
||||
LOG_E("GPT start_lba incorrect: %ld != %ld",
|
||||
(uint64_t)((*gpt)->start_lba),
|
||||
(uint64_t)lba);
|
||||
goto fail;
|
||||
}
|
||||
|
||||
/* Check the first_usable_lba and last_usable_lba are
|
||||
* within the disk.
|
||||
*/
|
||||
lastlba = last_lba(card);
|
||||
if ((uint64_t)((*gpt)->first_usable_lba) > lastlba)
|
||||
{
|
||||
LOG_E("GPT: first_usable_lba incorrect: %ld > %ld",
|
||||
((uint64_t)((*gpt)->first_usable_lba)),
|
||||
(size_t)lastlba);
|
||||
goto fail;
|
||||
}
|
||||
|
||||
if ((uint64_t)((*gpt)->last_usable_lba) > lastlba)
|
||||
{
|
||||
LOG_E("GPT: last_usable_lba incorrect: %ld > %ld",
|
||||
(uint64_t)((*gpt)->last_usable_lba),
|
||||
(size_t)lastlba);
|
||||
goto fail;
|
||||
}
|
||||
|
||||
if ((uint64_t)((*gpt)->last_usable_lba) < (uint64_t)((*gpt)->first_usable_lba))
|
||||
{
|
||||
LOG_E("GPT: last_usable_lba incorrect: %ld > %ld",
|
||||
(uint64_t)((*gpt)->last_usable_lba),
|
||||
(uint64_t)((*gpt)->first_usable_lba));
|
||||
goto fail;
|
||||
}
|
||||
/* Check that sizeof_partition_entry has the correct value */
|
||||
if ((uint32_t)((*gpt)->sizeof_partition_entry) != sizeof(gpt_entry)) {
|
||||
LOG_E("GUID Partition Entry Size check failed.");
|
||||
goto fail;
|
||||
}
|
||||
|
||||
if (!(*ptes = alloc_read_gpt_entries(card, *gpt)))
|
||||
{
|
||||
goto fail;
|
||||
}
|
||||
|
||||
/* We're done, all's well */
|
||||
return 1;
|
||||
|
||||
fail:
|
||||
rt_free(*gpt);
|
||||
*gpt = RT_NULL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* is_pte_valid() - tests one PTE for validity
|
||||
* pte:pte to check
|
||||
* lastlba: last lba of the disk
|
||||
*
|
||||
* Description: returns 1 if valid, 0 on error.
|
||||
*/
|
||||
static inline int is_pte_valid(const gpt_entry *pte, const size_t lastlba)
|
||||
{
|
||||
if ((!efi_guidcmp(pte->partition_type_guid, NULL_GUID)) ||
|
||||
(uint64_t)(pte->starting_lba) > lastlba ||
|
||||
(uint64_t)(pte->ending_lba) > lastlba)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* compare_gpts() - Search disk for valid GPT headers and PTEs
|
||||
* pgpt: primary GPT header
|
||||
* agpt: alternate GPT header
|
||||
* lastlba: last LBA number
|
||||
*
|
||||
* Description: Returns nothing. Sanity checks pgpt and agpt fields
|
||||
* and prints warnings on discrepancies.
|
||||
*
|
||||
*/
|
||||
static void compare_gpts(gpt_header *pgpt, gpt_header *agpt, size_t lastlba)
|
||||
{
|
||||
int error_found = 0;
|
||||
if (!pgpt || !agpt)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if ((uint64_t)(pgpt->start_lba) != (uint64_t)(agpt->alternate_lba))
|
||||
{
|
||||
LOG_I("GPT:Primary header LBA != Alt. header alternate_lba");
|
||||
LOG_I("GPT:%lld != %lld",
|
||||
(uint64_t)(pgpt->start_lba),
|
||||
(uint64_t)(agpt->alternate_lba));
|
||||
error_found++;
|
||||
}
|
||||
|
||||
if ((uint64_t)(pgpt->alternate_lba) != (uint64_t)(agpt->start_lba))
|
||||
{
|
||||
LOG_I("GPT:Primary header alternate_lba != Alt. header start_lba");
|
||||
LOG_I("GPT:%lld != %lld",
|
||||
(uint64_t)(pgpt->alternate_lba),
|
||||
(uint64_t)(agpt->start_lba));
|
||||
error_found++;
|
||||
}
|
||||
|
||||
if ((uint64_t)(pgpt->first_usable_lba) != (uint64_t)(agpt->first_usable_lba))
|
||||
{
|
||||
LOG_I("GPT:first_usable_lbas don't match.");
|
||||
LOG_I("GPT:%lld != %lld",
|
||||
(uint64_t)(pgpt->first_usable_lba),
|
||||
(uint64_t)(agpt->first_usable_lba));
|
||||
error_found++;
|
||||
}
|
||||
|
||||
if ((uint64_t)(pgpt->last_usable_lba) != (uint64_t)(agpt->last_usable_lba))
|
||||
{
|
||||
LOG_I("GPT:last_usable_lbas don't match.");
|
||||
LOG_I("GPT:%lld != %lld",
|
||||
(uint64_t)(pgpt->last_usable_lba),
|
||||
(uint64_t)(agpt->last_usable_lba));
|
||||
error_found++;
|
||||
}
|
||||
|
||||
if (efi_guidcmp(pgpt->disk_guid, agpt->disk_guid))
|
||||
{
|
||||
LOG_I("GPT:disk_guids don't match.");
|
||||
error_found++;
|
||||
}
|
||||
|
||||
if ((pgpt->num_partition_entries) != (agpt->num_partition_entries))
|
||||
{
|
||||
LOG_I("GPT:num_partition_entries don't match: "
|
||||
"0x%x != 0x%x",
|
||||
(pgpt->num_partition_entries),
|
||||
(agpt->num_partition_entries));
|
||||
error_found++;
|
||||
}
|
||||
|
||||
if ((pgpt->sizeof_partition_entry) != (agpt->sizeof_partition_entry))
|
||||
{
|
||||
LOG_I("GPT:sizeof_partition_entry values don't match: "
|
||||
"0x%x != 0x%x",
|
||||
(pgpt->sizeof_partition_entry),
|
||||
(agpt->sizeof_partition_entry));
|
||||
error_found++;
|
||||
}
|
||||
|
||||
if ((pgpt->partition_entry_array_crc32) != (agpt->partition_entry_array_crc32))
|
||||
{
|
||||
LOG_I("GPT:partition_entry_array_crc32 values don't match: "
|
||||
"0x%x != 0x%x",
|
||||
(pgpt->partition_entry_array_crc32),
|
||||
(agpt->partition_entry_array_crc32));
|
||||
error_found++;
|
||||
}
|
||||
|
||||
if ((pgpt->alternate_lba) != lastlba)
|
||||
{
|
||||
LOG_I("GPT:Primary header thinks Alt. header is not at the end of the disk.");
|
||||
LOG_I("GPT:%lld != %lld",
|
||||
(uint64_t)(pgpt->alternate_lba),
|
||||
(size_t)lastlba);
|
||||
error_found++;
|
||||
}
|
||||
|
||||
if ((agpt->start_lba) != lastlba)
|
||||
{
|
||||
LOG_I("GPT:Alternate GPT header not at the end of the disk.");
|
||||
LOG_I("GPT:%lld != %lld",
|
||||
(uint64_t)(agpt->start_lba),
|
||||
(size_t)lastlba);
|
||||
error_found++;
|
||||
}
|
||||
|
||||
if (error_found)
|
||||
{
|
||||
LOG_I("GPT: Use GNU Parted to correct GPT errors.");
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* find_valid_gpt() - Search disk for valid GPT headers and PTEs
|
||||
* state: disk parsed partitions
|
||||
* gpt: GPT header ptr, filled on return.
|
||||
* ptes: PTEs ptr, filled on return.
|
||||
*
|
||||
* Description: Returns 1 if valid, 0 on error.
|
||||
* If valid, returns pointers to newly allocated GPT header and PTEs.
|
||||
* Validity depends on PMBR being valid (or being overridden by the
|
||||
* 'gpt' kernel command line option) and finding either the Primary
|
||||
* GPT header and PTEs valid, or the Alternate GPT header and PTEs
|
||||
* valid. If the Primary GPT header is not valid, the Alternate GPT header
|
||||
* is not checked unless the 'gpt' kernel command line option is passed.
|
||||
* This protects against devices which misreport their size, and forces
|
||||
* the user to decide to use the Alternate GPT.
|
||||
*/
|
||||
static int find_valid_gpt(struct rt_mmcsd_card *card, gpt_header **gpt,
|
||||
gpt_entry **ptes)
|
||||
{
|
||||
int good_pgpt = 0, good_agpt = 0, good_pmbr = 0;
|
||||
gpt_header *pgpt = RT_NULL, *agpt = RT_NULL;
|
||||
gpt_entry *pptes = RT_NULL, *aptes = RT_NULL;
|
||||
legacy_mbr *legacymbr;
|
||||
size_t total_sectors = last_lba(card) + 1;
|
||||
size_t lastlba;
|
||||
int status = 0;
|
||||
|
||||
if (!ptes)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
lastlba = last_lba(card);
|
||||
if (!force_gpt)
|
||||
{
|
||||
/* This will be added to the EFI Spec. per Intel after v1.02. */
|
||||
legacymbr = rt_malloc(512);
|
||||
if (!legacymbr)
|
||||
{
|
||||
goto fail;
|
||||
}
|
||||
|
||||
status = read_lba(card, 0, (uint8_t *)legacymbr, 1);
|
||||
if (status)
|
||||
{
|
||||
LOG_I("status:%d", status);
|
||||
goto fail;
|
||||
}
|
||||
|
||||
good_pmbr = is_pmbr_valid(legacymbr, total_sectors);
|
||||
rt_free(legacymbr);
|
||||
|
||||
if (!good_pmbr)
|
||||
{
|
||||
goto fail;
|
||||
}
|
||||
|
||||
rt_kprintf("Device has a %s MBR\n",
|
||||
good_pmbr == GPT_MBR_PROTECTIVE ?
|
||||
"protective" : "hybrid");
|
||||
}
|
||||
|
||||
good_pgpt = is_gpt_valid(card, GPT_PRIMARY_PARTITION_TABLE_LBA,
|
||||
&pgpt, &pptes);
|
||||
if (good_pgpt)
|
||||
{
|
||||
good_agpt = is_gpt_valid(card, (pgpt->alternate_lba), &agpt, &aptes);
|
||||
if (!good_agpt && force_gpt)
|
||||
{
|
||||
good_agpt = is_gpt_valid(card, lastlba, &agpt, &aptes);
|
||||
}
|
||||
|
||||
/* The obviously unsuccessful case */
|
||||
if (!good_pgpt && !good_agpt)
|
||||
{
|
||||
goto fail;
|
||||
}
|
||||
|
||||
compare_gpts(pgpt, agpt, lastlba);
|
||||
|
||||
/* The good cases */
|
||||
if (good_pgpt)
|
||||
{
|
||||
*gpt = pgpt;
|
||||
*ptes = pptes;
|
||||
rt_free(agpt);
|
||||
rt_free(aptes);
|
||||
if (!good_agpt)
|
||||
{
|
||||
LOG_D("Alternate GPT is invalid, using primary GPT.");
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
else if (good_agpt)
|
||||
{
|
||||
*gpt = agpt;
|
||||
*ptes = aptes;
|
||||
rt_free(pgpt);
|
||||
rt_free(pptes);
|
||||
LOG_D("Primary GPT is invalid, using alternate GPT.");
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
fail:
|
||||
rt_free(pgpt);
|
||||
rt_free(agpt);
|
||||
rt_free(pptes);
|
||||
rt_free(aptes);
|
||||
*gpt = RT_NULL;
|
||||
*ptes = RT_NULL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int check_gpt(struct rt_mmcsd_card *card)
|
||||
{
|
||||
if (!find_valid_gpt(card, &_gpt, &_ptes) || !_gpt || !_ptes)
|
||||
{
|
||||
rt_free(_gpt);
|
||||
rt_free(_ptes);
|
||||
return MBR_TYPE;
|
||||
}
|
||||
return GPT_TYPE;
|
||||
}
|
||||
|
||||
int gpt_get_partition_param(struct rt_mmcsd_card *card, struct dfs_partition *part, uint32_t pindex)
|
||||
{
|
||||
if (!is_pte_valid(&_ptes[pindex], last_lba(card)))
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
part->offset = (off_t)(_ptes[pindex].starting_lba);
|
||||
part->size = (_ptes[pindex].ending_lba) - (_ptes[pindex].starting_lba) + 1ULL;
|
||||
|
||||
rt_kprintf("found part[%d], begin(sector): %d, end(sector):%d size: ",
|
||||
pindex, _ptes[pindex].starting_lba, _ptes[pindex].ending_lba);
|
||||
|
||||
if ((part->size >> 11) == 0)
|
||||
{
|
||||
rt_kprintf("%d%s", part->size >> 1, "KB\n"); /* KB */
|
||||
}
|
||||
else
|
||||
{
|
||||
unsigned int part_size;
|
||||
part_size = part->size >> 11; /* MB */
|
||||
if ((part_size >> 10) == 0)
|
||||
rt_kprintf("%d.%d%s", part_size, (part->size >> 1) & 0x3FF, "MB\n");
|
||||
else
|
||||
rt_kprintf("%d.%d%s", part_size >> 10, part_size & 0x3FF, "GB\n");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void gpt_free(void)
|
||||
{
|
||||
rt_free(_ptes);
|
||||
rt_free(_gpt);
|
||||
}
|
684
components/drivers/sdio/mmc.c
Normal file
684
components/drivers/sdio/mmc.c
Normal file
|
@ -0,0 +1,684 @@
|
|||
/*
|
||||
* Copyright (c) 2006-2023, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2015-06-15 hichard first version
|
||||
*/
|
||||
|
||||
#include <drivers/mmcsd_core.h>
|
||||
#include <drivers/mmc.h>
|
||||
|
||||
#define DBG_TAG "SDIO"
|
||||
#ifdef RT_SDIO_DEBUG
|
||||
#define DBG_LVL DBG_LOG
|
||||
#else
|
||||
#define DBG_LVL DBG_INFO
|
||||
#endif /* RT_SDIO_DEBUG */
|
||||
#include <rtdbg.h>
|
||||
|
||||
static const rt_uint32_t tran_unit[] =
|
||||
{
|
||||
10000, 100000, 1000000, 10000000,
|
||||
0, 0, 0, 0
|
||||
};
|
||||
|
||||
static const rt_uint8_t tran_value[] =
|
||||
{
|
||||
0, 10, 12, 13, 15, 20, 25, 30,
|
||||
35, 40, 45, 50, 55, 60, 70, 80,
|
||||
};
|
||||
|
||||
static const rt_uint32_t tacc_uint[] =
|
||||
{
|
||||
1, 10, 100, 1000, 10000, 100000, 1000000, 10000000,
|
||||
};
|
||||
|
||||
static const rt_uint8_t tacc_value[] =
|
||||
{
|
||||
0, 10, 12, 13, 15, 20, 25, 30,
|
||||
35, 40, 45, 50, 55, 60, 70, 80,
|
||||
};
|
||||
|
||||
rt_inline rt_uint32_t GET_BITS(rt_uint32_t *resp,
|
||||
rt_uint32_t start,
|
||||
rt_uint32_t size)
|
||||
{
|
||||
const rt_int32_t __size = size;
|
||||
const rt_uint32_t __mask = (__size < 32 ? 1 << __size : 0) - 1;
|
||||
const rt_int32_t __off = 3 - ((start) / 32);
|
||||
const rt_int32_t __shft = (start) & 31;
|
||||
rt_uint32_t __res;
|
||||
|
||||
__res = resp[__off] >> __shft;
|
||||
if (__size + __shft > 32)
|
||||
__res |= resp[__off - 1] << ((32 - __shft) % 32);
|
||||
|
||||
return __res & __mask;
|
||||
}
|
||||
|
||||
/*
|
||||
* Given a 128-bit response, decode to our card CSD structure.
|
||||
*/
|
||||
static rt_int32_t mmcsd_parse_csd(struct rt_mmcsd_card *card)
|
||||
{
|
||||
rt_uint32_t a, b;
|
||||
struct rt_mmcsd_csd *csd = &card->csd;
|
||||
rt_uint32_t *resp = card->resp_csd;
|
||||
|
||||
/*
|
||||
* We only understand CSD structure v1.1 and v1.2.
|
||||
* v1.2 has extra information in bits 15, 11 and 10.
|
||||
* We also support eMMC v4.4 & v4.41.
|
||||
*/
|
||||
csd->csd_structure = GET_BITS(resp, 126, 2);
|
||||
if (csd->csd_structure == 0)
|
||||
{
|
||||
LOG_E("unrecognised CSD structure version %d!", csd->csd_structure);
|
||||
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
csd->taac = GET_BITS(resp, 112, 8);
|
||||
csd->nsac = GET_BITS(resp, 104, 8);
|
||||
csd->tran_speed = GET_BITS(resp, 96, 8);
|
||||
csd->card_cmd_class = GET_BITS(resp, 84, 12);
|
||||
csd->rd_blk_len = GET_BITS(resp, 80, 4);
|
||||
csd->rd_blk_part = GET_BITS(resp, 79, 1);
|
||||
csd->wr_blk_misalign = GET_BITS(resp, 78, 1);
|
||||
csd->rd_blk_misalign = GET_BITS(resp, 77, 1);
|
||||
csd->dsr_imp = GET_BITS(resp, 76, 1);
|
||||
csd->c_size = GET_BITS(resp, 62, 12);
|
||||
csd->c_size_mult = GET_BITS(resp, 47, 3);
|
||||
csd->r2w_factor = GET_BITS(resp, 26, 3);
|
||||
csd->wr_blk_len = GET_BITS(resp, 22, 4);
|
||||
csd->wr_blk_partial = GET_BITS(resp, 21, 1);
|
||||
csd->csd_crc = GET_BITS(resp, 1, 7);
|
||||
|
||||
card->card_blksize = 1 << csd->rd_blk_len;
|
||||
card->tacc_clks = csd->nsac * 100;
|
||||
card->tacc_ns = (tacc_uint[csd->taac & 0x07] * tacc_value[(csd->taac & 0x78) >> 3] + 9) / 10;
|
||||
card->max_data_rate = tran_unit[csd->tran_speed & 0x07] * tran_value[(csd->tran_speed & 0x78) >> 3];
|
||||
if (csd->wr_blk_len >= 9)
|
||||
{
|
||||
a = GET_BITS(resp, 42, 5);
|
||||
b = GET_BITS(resp, 37, 5);
|
||||
card->erase_size = (a + 1) * (b + 1);
|
||||
card->erase_size <<= csd->wr_blk_len - 9;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Read extended CSD.
|
||||
*/
|
||||
static int mmc_get_ext_csd(struct rt_mmcsd_card *card, rt_uint8_t **new_ext_csd)
|
||||
{
|
||||
void *ext_csd;
|
||||
struct rt_mmcsd_req req;
|
||||
struct rt_mmcsd_cmd cmd;
|
||||
struct rt_mmcsd_data data;
|
||||
|
||||
*new_ext_csd = RT_NULL;
|
||||
if (GET_BITS(card->resp_csd, 122, 4) < 4)
|
||||
return 0;
|
||||
|
||||
/*
|
||||
* As the ext_csd is so large and mostly unused, we don't store the
|
||||
* raw block in mmc_card.
|
||||
*/
|
||||
ext_csd = rt_malloc(512);
|
||||
if (!ext_csd)
|
||||
{
|
||||
LOG_E("alloc memory failed when get ext csd!");
|
||||
return -RT_ENOMEM;
|
||||
}
|
||||
|
||||
rt_memset(&req, 0, sizeof(struct rt_mmcsd_req));
|
||||
rt_memset(&cmd, 0, sizeof(struct rt_mmcsd_cmd));
|
||||
rt_memset(&data, 0, sizeof(struct rt_mmcsd_data));
|
||||
|
||||
req.cmd = &cmd;
|
||||
req.data = &data;
|
||||
|
||||
cmd.cmd_code = SEND_EXT_CSD;
|
||||
cmd.arg = 0;
|
||||
|
||||
/* NOTE HACK: the RESP_SPI_R1 is always correct here, but we
|
||||
* rely on callers to never use this with "native" calls for reading
|
||||
* CSD or CID. Native versions of those commands use the R2 type,
|
||||
* not R1 plus a data block.
|
||||
*/
|
||||
cmd.flags = RESP_SPI_R1 | RESP_R1 | CMD_ADTC;
|
||||
|
||||
data.blksize = 512;
|
||||
data.blks = 1;
|
||||
data.flags = DATA_DIR_READ;
|
||||
data.buf = ext_csd;
|
||||
|
||||
/*
|
||||
* Some cards require longer data read timeout than indicated in CSD.
|
||||
* Address this by setting the read timeout to a "reasonably high"
|
||||
* value. For the cards tested, 300ms has proven enough. If necessary,
|
||||
* this value can be increased if other problematic cards require this.
|
||||
*/
|
||||
data.timeout_ns = 300000000;
|
||||
data.timeout_clks = 0;
|
||||
|
||||
mmcsd_send_request(card->host, &req);
|
||||
|
||||
if (cmd.err)
|
||||
return cmd.err;
|
||||
if (data.err)
|
||||
return data.err;
|
||||
|
||||
*new_ext_csd = ext_csd;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Decode extended CSD.
|
||||
*/
|
||||
static int mmc_parse_ext_csd(struct rt_mmcsd_card *card, rt_uint8_t *ext_csd)
|
||||
{
|
||||
rt_uint64_t card_capacity = 0;
|
||||
struct rt_mmcsd_host *host;
|
||||
if (card == RT_NULL || ext_csd == RT_NULL)
|
||||
{
|
||||
LOG_E("emmc parse ext csd fail, invaild args");
|
||||
return -1;
|
||||
}
|
||||
|
||||
host = card->host;
|
||||
if (host->flags & MMCSD_SUP_HS200)
|
||||
{
|
||||
card->flags |= CARD_FLAG_HS200;
|
||||
card->hs_max_data_rate = 200000000;
|
||||
}
|
||||
else if (host->flags & MMCSD_SUP_HIGHSPEED_DDR)
|
||||
{
|
||||
card->flags |= CARD_FLAG_HIGHSPEED_DDR;
|
||||
card->hs_max_data_rate = 52000000;
|
||||
}
|
||||
else
|
||||
{
|
||||
card->flags |= CARD_FLAG_HIGHSPEED;
|
||||
card->hs_max_data_rate = 52000000;
|
||||
}
|
||||
|
||||
card_capacity = *((rt_uint32_t *)&ext_csd[EXT_CSD_SEC_CNT]);
|
||||
card->card_sec_cnt = card_capacity;
|
||||
card_capacity *= card->card_blksize;
|
||||
card_capacity >>= 10; /* unit:KB */
|
||||
card->card_capacity = card_capacity;
|
||||
LOG_I("emmc card capacity %d KB, card sec count:%d.", card->card_capacity, card->card_sec_cnt);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* mmc_switch - modify EXT_CSD register
|
||||
* @card: the MMC card associated with the data transfer
|
||||
* @set: cmd set values
|
||||
* @index: EXT_CSD register index
|
||||
* @value: value to program into EXT_CSD register
|
||||
*
|
||||
* Modifies the EXT_CSD register for selected card.
|
||||
*/
|
||||
static int mmc_switch(struct rt_mmcsd_card *card, rt_uint8_t set,
|
||||
rt_uint8_t index, rt_uint8_t value)
|
||||
{
|
||||
int err;
|
||||
struct rt_mmcsd_host *host = card->host;
|
||||
struct rt_mmcsd_cmd cmd = {0};
|
||||
|
||||
cmd.cmd_code = SWITCH;
|
||||
cmd.arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
|
||||
(index << 16) | (value << 8) | set;
|
||||
cmd.flags = RESP_R1B | CMD_AC;
|
||||
|
||||
err = mmcsd_send_cmd(host, &cmd, 3);
|
||||
if (err)
|
||||
return err;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int mmc_compare_ext_csds(struct rt_mmcsd_card *card,
|
||||
rt_uint8_t *ext_csd, rt_uint32_t bus_width)
|
||||
{
|
||||
rt_uint8_t *bw_ext_csd;
|
||||
int err;
|
||||
|
||||
if (bus_width == MMCSD_BUS_WIDTH_1)
|
||||
return 0;
|
||||
|
||||
err = mmc_get_ext_csd(card, &bw_ext_csd);
|
||||
|
||||
if (err || bw_ext_csd == RT_NULL)
|
||||
{
|
||||
err = -RT_ERROR;
|
||||
goto out;
|
||||
}
|
||||
|
||||
/* only compare read only fields */
|
||||
err = !((ext_csd[EXT_CSD_PARTITION_SUPPORT] == bw_ext_csd[EXT_CSD_PARTITION_SUPPORT]) &&
|
||||
(ext_csd[EXT_CSD_ERASED_MEM_CONT] == bw_ext_csd[EXT_CSD_ERASED_MEM_CONT]) &&
|
||||
(ext_csd[EXT_CSD_REV] == bw_ext_csd[EXT_CSD_REV]) &&
|
||||
(ext_csd[EXT_CSD_STRUCTURE] == bw_ext_csd[EXT_CSD_STRUCTURE]) &&
|
||||
(ext_csd[EXT_CSD_CARD_TYPE] == bw_ext_csd[EXT_CSD_CARD_TYPE]) &&
|
||||
(ext_csd[EXT_CSD_S_A_TIMEOUT] == bw_ext_csd[EXT_CSD_S_A_TIMEOUT]) &&
|
||||
(ext_csd[EXT_CSD_HC_WP_GRP_SIZE] == bw_ext_csd[EXT_CSD_HC_WP_GRP_SIZE]) &&
|
||||
(ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT] == bw_ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT]) &&
|
||||
(ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] == bw_ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]) &&
|
||||
(ext_csd[EXT_CSD_SEC_TRIM_MULT] == bw_ext_csd[EXT_CSD_SEC_TRIM_MULT]) &&
|
||||
(ext_csd[EXT_CSD_SEC_ERASE_MULT] == bw_ext_csd[EXT_CSD_SEC_ERASE_MULT]) &&
|
||||
(ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT] == bw_ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT]) &&
|
||||
(ext_csd[EXT_CSD_TRIM_MULT] == bw_ext_csd[EXT_CSD_TRIM_MULT]) &&
|
||||
(ext_csd[EXT_CSD_SEC_CNT + 0] == bw_ext_csd[EXT_CSD_SEC_CNT + 0]) &&
|
||||
(ext_csd[EXT_CSD_SEC_CNT + 1] == bw_ext_csd[EXT_CSD_SEC_CNT + 1]) &&
|
||||
(ext_csd[EXT_CSD_SEC_CNT + 2] == bw_ext_csd[EXT_CSD_SEC_CNT + 2]) &&
|
||||
(ext_csd[EXT_CSD_SEC_CNT + 3] == bw_ext_csd[EXT_CSD_SEC_CNT + 3]) &&
|
||||
(ext_csd[EXT_CSD_PWR_CL_52_195] == bw_ext_csd[EXT_CSD_PWR_CL_52_195]) &&
|
||||
(ext_csd[EXT_CSD_PWR_CL_26_195] == bw_ext_csd[EXT_CSD_PWR_CL_26_195]) &&
|
||||
(ext_csd[EXT_CSD_PWR_CL_52_360] == bw_ext_csd[EXT_CSD_PWR_CL_52_360]) &&
|
||||
(ext_csd[EXT_CSD_PWR_CL_26_360] == bw_ext_csd[EXT_CSD_PWR_CL_26_360]) &&
|
||||
(ext_csd[EXT_CSD_PWR_CL_200_195] == bw_ext_csd[EXT_CSD_PWR_CL_200_195]) &&
|
||||
(ext_csd[EXT_CSD_PWR_CL_200_360] == bw_ext_csd[EXT_CSD_PWR_CL_200_360]) &&
|
||||
(ext_csd[EXT_CSD_PWR_CL_DDR_52_195] == bw_ext_csd[EXT_CSD_PWR_CL_DDR_52_195]) &&
|
||||
(ext_csd[EXT_CSD_PWR_CL_DDR_52_360] == bw_ext_csd[EXT_CSD_PWR_CL_DDR_52_360]) &&
|
||||
(ext_csd[EXT_CSD_PWR_CL_DDR_200_360] == bw_ext_csd[EXT_CSD_PWR_CL_DDR_200_360]));
|
||||
|
||||
if (err)
|
||||
err = -RT_ERROR;
|
||||
|
||||
out:
|
||||
rt_free(bw_ext_csd);
|
||||
return err;
|
||||
}
|
||||
|
||||
/*
|
||||
* Select the bus width amoung 4-bit and 8-bit(SDR).
|
||||
* If the bus width is changed successfully, return the selected width value.
|
||||
* Zero is returned instead of error value if the wide width is not supported.
|
||||
*/
|
||||
static int mmc_select_bus_width(struct rt_mmcsd_card *card, rt_uint8_t *ext_csd)
|
||||
{
|
||||
rt_uint32_t ext_csd_bits[][2] =
|
||||
{
|
||||
{EXT_CSD_BUS_WIDTH_8, EXT_CSD_DDR_BUS_WIDTH_8},
|
||||
{EXT_CSD_BUS_WIDTH_4, EXT_CSD_DDR_BUS_WIDTH_4},
|
||||
{EXT_CSD_BUS_WIDTH_1, EXT_CSD_BUS_WIDTH_1},
|
||||
};
|
||||
rt_uint32_t bus_widths[] =
|
||||
{
|
||||
MMCSD_BUS_WIDTH_8,
|
||||
MMCSD_BUS_WIDTH_4,
|
||||
MMCSD_BUS_WIDTH_1
|
||||
};
|
||||
struct rt_mmcsd_host *host = card->host;
|
||||
unsigned idx, bus_width = 0;
|
||||
int err = 0, ddr = 0;
|
||||
|
||||
if (GET_BITS(card->resp_csd, 122, 4) < 4)
|
||||
return 0;
|
||||
|
||||
if (card->flags & CARD_FLAG_HIGHSPEED_DDR)
|
||||
{
|
||||
ddr = 2;
|
||||
}
|
||||
/*
|
||||
* Unlike SD, MMC cards dont have a configuration register to notify
|
||||
* supported bus width. So bus test command should be run to identify
|
||||
* the supported bus width or compare the ext csd values of current
|
||||
* bus width and ext csd values of 1 bit mode read earlier.
|
||||
*/
|
||||
for (idx = 0; idx < sizeof(bus_widths) / sizeof(rt_uint32_t); idx++)
|
||||
{
|
||||
/*
|
||||
* Host is capable of 8bit transfer, then switch
|
||||
* the device to work in 8bit transfer mode. If the
|
||||
* mmc switch command returns error then switch to
|
||||
* 4bit transfer mode. On success set the corresponding
|
||||
* bus width on the host. Meanwhile, mmc core would
|
||||
* bail out early if corresponding bus capable wasn't
|
||||
* set by drivers.
|
||||
*/
|
||||
bus_width = bus_widths[idx];
|
||||
if (bus_width == MMCSD_BUS_WIDTH_1)
|
||||
{
|
||||
ddr = 0;
|
||||
}
|
||||
|
||||
err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
|
||||
EXT_CSD_BUS_WIDTH,
|
||||
ext_csd_bits[idx][0]);
|
||||
|
||||
if (err)
|
||||
continue;
|
||||
|
||||
mmcsd_set_bus_width(host, bus_width);
|
||||
err = mmc_compare_ext_csds(card, ext_csd, bus_width);
|
||||
if (!err)
|
||||
{
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
switch (ext_csd_bits[idx][0])
|
||||
{
|
||||
case 0:
|
||||
LOG_E("switch to bus width 1 bit failed!");
|
||||
break;
|
||||
case 1:
|
||||
LOG_E("switch to bus width 4 bit failed!");
|
||||
break;
|
||||
case 2:
|
||||
LOG_E("switch to bus width 8 bit failed!");
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!err && ddr)
|
||||
{
|
||||
err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
|
||||
EXT_CSD_BUS_WIDTH,
|
||||
ext_csd_bits[idx][1]);
|
||||
}
|
||||
|
||||
if (!err)
|
||||
{
|
||||
if (card->flags & (CARD_FLAG_HIGHSPEED | CARD_FLAG_HIGHSPEED_DDR))
|
||||
{
|
||||
|
||||
err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
|
||||
EXT_CSD_HS_TIMING,
|
||||
1);
|
||||
}
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
rt_err_t mmc_send_op_cond(struct rt_mmcsd_host *host,
|
||||
rt_uint32_t ocr, rt_uint32_t *rocr)
|
||||
{
|
||||
struct rt_mmcsd_cmd cmd;
|
||||
rt_uint32_t i;
|
||||
rt_err_t err = RT_EOK;
|
||||
|
||||
rt_memset(&cmd, 0, sizeof(struct rt_mmcsd_cmd));
|
||||
|
||||
cmd.cmd_code = SEND_OP_COND;
|
||||
cmd.arg = controller_is_spi(host) ? 0 : ocr;
|
||||
cmd.flags = RESP_SPI_R1 | RESP_R3 | CMD_BCR;
|
||||
|
||||
for (i = 100; i; i--)
|
||||
{
|
||||
err = mmcsd_send_cmd(host, &cmd, 3);
|
||||
if (err)
|
||||
break;
|
||||
|
||||
/* if we're just probing, do a single pass */
|
||||
if (ocr == 0)
|
||||
break;
|
||||
|
||||
/* otherwise wait until reset completes */
|
||||
if (controller_is_spi(host))
|
||||
{
|
||||
if (!(cmd.resp[0] & R1_SPI_IDLE))
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (cmd.resp[0] & CARD_BUSY)
|
||||
break;
|
||||
}
|
||||
|
||||
err = -RT_ETIMEOUT;
|
||||
|
||||
rt_thread_mdelay(10); //delay 10ms
|
||||
}
|
||||
|
||||
if (rocr && !controller_is_spi(host))
|
||||
*rocr = cmd.resp[0];
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
static rt_err_t mmc_set_card_addr(struct rt_mmcsd_host *host, rt_uint32_t rca)
|
||||
{
|
||||
rt_err_t err;
|
||||
struct rt_mmcsd_cmd cmd;
|
||||
|
||||
rt_memset(&cmd, 0, sizeof(struct rt_mmcsd_cmd));
|
||||
|
||||
cmd.cmd_code = SET_RELATIVE_ADDR;
|
||||
cmd.arg = rca << 16;
|
||||
cmd.flags = RESP_R1 | CMD_AC;
|
||||
|
||||
err = mmcsd_send_cmd(host, &cmd, 3);
|
||||
if (err)
|
||||
return err;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int mmc_select_hs200(struct rt_mmcsd_card *card)
|
||||
{
|
||||
int ret;
|
||||
|
||||
ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
|
||||
EXT_CSD_HS_TIMING, EXT_CSD_TIMING_HS200);
|
||||
if (ret)
|
||||
return ret;
|
||||
|
||||
mmcsd_set_timing(card->host, MMCSD_TIMING_MMC_HS200);
|
||||
mmcsd_set_clock(card->host, 200000000);
|
||||
|
||||
ret = mmcsd_excute_tuning(card);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int mmc_select_timing(struct rt_mmcsd_card *card)
|
||||
{
|
||||
int ret = 0;
|
||||
|
||||
if (card->flags & CARD_FLAG_HS200)
|
||||
{
|
||||
ret = mmc_select_hs200(card);
|
||||
}
|
||||
else if (card->flags & CARD_FLAG_HIGHSPEED_DDR)
|
||||
{
|
||||
mmcsd_set_timing(card->host, MMCSD_TIMING_MMC_DDR52);
|
||||
mmcsd_set_clock(card->host, card->hs_max_data_rate);
|
||||
}
|
||||
else
|
||||
{
|
||||
mmcsd_set_timing(card->host, MMCSD_TIMING_UHS_SDR50);
|
||||
mmcsd_set_clock(card->host, card->hs_max_data_rate);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static rt_int32_t mmcsd_mmc_init_card(struct rt_mmcsd_host *host,
|
||||
rt_uint32_t ocr)
|
||||
{
|
||||
rt_int32_t err;
|
||||
rt_uint32_t resp[4];
|
||||
rt_uint32_t rocr = 0;
|
||||
rt_uint8_t *ext_csd = RT_NULL;
|
||||
struct rt_mmcsd_card *card = RT_NULL;
|
||||
|
||||
mmcsd_go_idle(host);
|
||||
|
||||
/* The extra bit indicates that we support high capacity */
|
||||
err = mmc_send_op_cond(host, ocr | (1 << 30), &rocr);
|
||||
if (err)
|
||||
goto err;
|
||||
|
||||
if (controller_is_spi(host))
|
||||
{
|
||||
err = mmcsd_spi_use_crc(host, 1);
|
||||
if (err)
|
||||
goto err1;
|
||||
}
|
||||
|
||||
if (controller_is_spi(host))
|
||||
err = mmcsd_get_cid(host, resp);
|
||||
else
|
||||
err = mmcsd_all_get_cid(host, resp);
|
||||
if (err)
|
||||
goto err;
|
||||
|
||||
card = rt_malloc(sizeof(struct rt_mmcsd_card));
|
||||
if (!card)
|
||||
{
|
||||
LOG_E("malloc card failed!");
|
||||
err = -RT_ENOMEM;
|
||||
goto err;
|
||||
}
|
||||
rt_memset(card, 0, sizeof(struct rt_mmcsd_card));
|
||||
|
||||
card->card_type = CARD_TYPE_MMC;
|
||||
card->host = host;
|
||||
card->rca = 1;
|
||||
rt_memcpy(card->resp_cid, resp, sizeof(card->resp_cid));
|
||||
|
||||
/*
|
||||
* For native busses: get card RCA and quit open drain mode.
|
||||
*/
|
||||
if (!controller_is_spi(host))
|
||||
{
|
||||
err = mmc_set_card_addr(host, card->rca);
|
||||
if (err)
|
||||
goto err1;
|
||||
|
||||
mmcsd_set_bus_mode(host, MMCSD_BUSMODE_PUSHPULL);
|
||||
}
|
||||
|
||||
err = mmcsd_get_csd(card, card->resp_csd);
|
||||
if (err)
|
||||
goto err1;
|
||||
|
||||
err = mmcsd_parse_csd(card);
|
||||
if (err)
|
||||
goto err1;
|
||||
|
||||
if (!controller_is_spi(host))
|
||||
{
|
||||
err = mmcsd_select_card(card);
|
||||
if (err)
|
||||
goto err1;
|
||||
}
|
||||
|
||||
/*
|
||||
* Fetch and process extended CSD.
|
||||
*/
|
||||
|
||||
err = mmc_get_ext_csd(card, &ext_csd);
|
||||
if (err)
|
||||
goto err1;
|
||||
err = mmc_parse_ext_csd(card, ext_csd);
|
||||
if (err)
|
||||
goto err1;
|
||||
|
||||
/* If doing byte addressing, check if required to do sector
|
||||
* addressing. Handle the case of <2GB cards needing sector
|
||||
* addressing. See section 8.1 JEDEC Standard JED84-A441;
|
||||
* ocr register has bit 30 set for sector addressing.
|
||||
*/
|
||||
if (!(card->flags & CARD_FLAG_SDHC) && (rocr & (1 << 30)))
|
||||
card->flags |= CARD_FLAG_SDHC;
|
||||
|
||||
/*switch bus width and bus mode*/
|
||||
err = mmc_select_bus_width(card, ext_csd);
|
||||
if (err)
|
||||
{
|
||||
LOG_E("mmc select buswidth fail");
|
||||
goto err0;
|
||||
}
|
||||
|
||||
err = mmc_select_timing(card);
|
||||
if (err)
|
||||
{
|
||||
LOG_E("mmc select timing fail");
|
||||
goto err0;
|
||||
}
|
||||
|
||||
host->card = card;
|
||||
|
||||
rt_free(ext_csd);
|
||||
return 0;
|
||||
|
||||
err0:
|
||||
rt_free(ext_csd);
|
||||
err1:
|
||||
rt_free(card);
|
||||
err:
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
/*
|
||||
* Starting point for mmc card init.
|
||||
*/
|
||||
rt_int32_t init_mmc(struct rt_mmcsd_host *host, rt_uint32_t ocr)
|
||||
{
|
||||
rt_int32_t err;
|
||||
rt_uint32_t current_ocr;
|
||||
/*
|
||||
* We need to get OCR a different way for SPI.
|
||||
*/
|
||||
if (controller_is_spi(host))
|
||||
{
|
||||
err = mmcsd_spi_read_ocr(host, 0, &ocr);
|
||||
if (err)
|
||||
goto err;
|
||||
}
|
||||
|
||||
current_ocr = mmcsd_select_voltage(host, ocr);
|
||||
|
||||
/*
|
||||
* Can we support the voltage(s) of the card(s)?
|
||||
*/
|
||||
if (!current_ocr)
|
||||
{
|
||||
err = -RT_ERROR;
|
||||
goto err;
|
||||
}
|
||||
|
||||
/*
|
||||
* Detect and init the card.
|
||||
*/
|
||||
err = mmcsd_mmc_init_card(host, current_ocr);
|
||||
if (err)
|
||||
goto err;
|
||||
|
||||
mmcsd_host_unlock(host);
|
||||
|
||||
err = rt_mmcsd_blk_probe(host->card);
|
||||
if (err)
|
||||
goto remove_card;
|
||||
mmcsd_host_lock(host);
|
||||
|
||||
return 0;
|
||||
|
||||
remove_card:
|
||||
mmcsd_host_lock(host);
|
||||
rt_mmcsd_blk_remove(host->card);
|
||||
rt_free(host->card);
|
||||
host->card = RT_NULL;
|
||||
err:
|
||||
|
||||
LOG_E("init MMC card failed!");
|
||||
|
||||
return err;
|
||||
}
|
781
components/drivers/sdio/mmcsd_core.c
Normal file
781
components/drivers/sdio/mmcsd_core.c
Normal file
|
@ -0,0 +1,781 @@
|
|||
/*
|
||||
* Copyright (c) 2006-2023, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2011-07-25 weety first version
|
||||
*/
|
||||
|
||||
#include <rtthread.h>
|
||||
#include <drivers/mmcsd_core.h>
|
||||
#include <drivers/sd.h>
|
||||
#include <drivers/mmc.h>
|
||||
#include <drivers/sdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#define DBG_TAG "SDIO"
|
||||
#ifdef RT_SDIO_DEBUG
|
||||
#define DBG_LVL DBG_LOG
|
||||
#else
|
||||
#define DBG_LVL DBG_INFO
|
||||
#endif /* RT_SDIO_DEBUG */
|
||||
#include <rtdbg.h>
|
||||
|
||||
#ifndef RT_MMCSD_STACK_SIZE
|
||||
#define RT_MMCSD_STACK_SIZE 1024
|
||||
#endif
|
||||
#ifndef RT_MMCSD_THREAD_PREORITY
|
||||
#if (RT_THREAD_PRIORITY_MAX == 32)
|
||||
#define RT_MMCSD_THREAD_PREORITY 0x16
|
||||
#else
|
||||
#define RT_MMCSD_THREAD_PREORITY 0x40
|
||||
#endif
|
||||
#endif
|
||||
|
||||
//static struct rt_semaphore mmcsd_sem;
|
||||
static struct rt_thread mmcsd_detect_thread;
|
||||
static rt_uint8_t mmcsd_stack[RT_MMCSD_STACK_SIZE];
|
||||
static struct rt_mailbox mmcsd_detect_mb;
|
||||
static rt_uint32_t mmcsd_detect_mb_pool[4];
|
||||
static struct rt_mailbox mmcsd_hotpluge_mb;
|
||||
static rt_uint32_t mmcsd_hotpluge_mb_pool[4];
|
||||
|
||||
void mmcsd_host_lock(struct rt_mmcsd_host *host)
|
||||
{
|
||||
rt_mutex_take(&host->bus_lock, RT_WAITING_FOREVER);
|
||||
}
|
||||
|
||||
void mmcsd_host_unlock(struct rt_mmcsd_host *host)
|
||||
{
|
||||
rt_mutex_release(&host->bus_lock);
|
||||
}
|
||||
|
||||
void mmcsd_req_complete(struct rt_mmcsd_host *host)
|
||||
{
|
||||
rt_sem_release(&host->sem_ack);
|
||||
}
|
||||
|
||||
void mmcsd_send_request(struct rt_mmcsd_host *host, struct rt_mmcsd_req *req)
|
||||
{
|
||||
do
|
||||
{
|
||||
req->cmd->retries--;
|
||||
req->cmd->err = 0;
|
||||
req->cmd->mrq = req;
|
||||
if (req->data)
|
||||
{
|
||||
req->cmd->data = req->data;
|
||||
req->data->err = 0;
|
||||
req->data->mrq = req;
|
||||
if (req->stop)
|
||||
{
|
||||
req->data->stop = req->stop;
|
||||
req->stop->err = 0;
|
||||
req->stop->mrq = req;
|
||||
}
|
||||
}
|
||||
host->ops->request(host, req);
|
||||
|
||||
rt_sem_take(&host->sem_ack, RT_WAITING_FOREVER);
|
||||
|
||||
}
|
||||
while (req->cmd->err && (req->cmd->retries > 0));
|
||||
|
||||
|
||||
}
|
||||
|
||||
rt_int32_t mmcsd_send_cmd(struct rt_mmcsd_host *host,
|
||||
struct rt_mmcsd_cmd *cmd,
|
||||
int retries)
|
||||
{
|
||||
struct rt_mmcsd_req req;
|
||||
|
||||
rt_memset(&req, 0, sizeof(struct rt_mmcsd_req));
|
||||
rt_memset(cmd->resp, 0, sizeof(cmd->resp));
|
||||
cmd->retries = retries;
|
||||
|
||||
req.cmd = cmd;
|
||||
cmd->data = RT_NULL;
|
||||
|
||||
mmcsd_send_request(host, &req);
|
||||
|
||||
return cmd->err;
|
||||
}
|
||||
|
||||
rt_int32_t mmcsd_go_idle(struct rt_mmcsd_host *host)
|
||||
{
|
||||
rt_int32_t err;
|
||||
struct rt_mmcsd_cmd cmd;
|
||||
|
||||
if (!controller_is_spi(host))
|
||||
{
|
||||
mmcsd_set_chip_select(host, MMCSD_CS_HIGH);
|
||||
rt_thread_mdelay(1);
|
||||
}
|
||||
|
||||
rt_memset(&cmd, 0, sizeof(struct rt_mmcsd_cmd));
|
||||
|
||||
cmd.cmd_code = GO_IDLE_STATE;
|
||||
cmd.arg = 0;
|
||||
cmd.flags = RESP_SPI_R1 | RESP_NONE | CMD_BC;
|
||||
|
||||
err = mmcsd_send_cmd(host, &cmd, 0);
|
||||
|
||||
rt_thread_mdelay(1);
|
||||
|
||||
if (!controller_is_spi(host))
|
||||
{
|
||||
mmcsd_set_chip_select(host, MMCSD_CS_IGNORE);
|
||||
rt_thread_mdelay(1);
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
rt_int32_t mmcsd_spi_read_ocr(struct rt_mmcsd_host *host,
|
||||
rt_int32_t high_capacity,
|
||||
rt_uint32_t *ocr)
|
||||
{
|
||||
struct rt_mmcsd_cmd cmd;
|
||||
rt_int32_t err;
|
||||
|
||||
rt_memset(&cmd, 0, sizeof(struct rt_mmcsd_cmd));
|
||||
|
||||
cmd.cmd_code = SPI_READ_OCR;
|
||||
cmd.arg = high_capacity ? (1 << 30) : 0;
|
||||
cmd.flags = RESP_SPI_R3;
|
||||
|
||||
err = mmcsd_send_cmd(host, &cmd, 0);
|
||||
|
||||
*ocr = cmd.resp[1];
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
rt_int32_t mmcsd_all_get_cid(struct rt_mmcsd_host *host, rt_uint32_t *cid)
|
||||
{
|
||||
rt_int32_t err;
|
||||
struct rt_mmcsd_cmd cmd;
|
||||
|
||||
rt_memset(&cmd, 0, sizeof(struct rt_mmcsd_cmd));
|
||||
|
||||
cmd.cmd_code = ALL_SEND_CID;
|
||||
cmd.arg = 0;
|
||||
cmd.flags = RESP_R2 | CMD_BCR;
|
||||
|
||||
err = mmcsd_send_cmd(host, &cmd, 3);
|
||||
if (err)
|
||||
return err;
|
||||
|
||||
rt_memcpy(cid, cmd.resp, sizeof(rt_uint32_t) * 4);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
rt_int32_t mmcsd_get_cid(struct rt_mmcsd_host *host, rt_uint32_t *cid)
|
||||
{
|
||||
rt_int32_t err, i;
|
||||
struct rt_mmcsd_req req;
|
||||
struct rt_mmcsd_cmd cmd;
|
||||
struct rt_mmcsd_data data;
|
||||
rt_uint32_t *buf = RT_NULL;
|
||||
|
||||
if (!controller_is_spi(host))
|
||||
{
|
||||
if (!host->card)
|
||||
return -RT_ERROR;
|
||||
rt_memset(&cmd, 0, sizeof(struct rt_mmcsd_cmd));
|
||||
|
||||
cmd.cmd_code = SEND_CID;
|
||||
cmd.arg = host->card->rca << 16;
|
||||
cmd.flags = RESP_R2 | CMD_AC;
|
||||
err = mmcsd_send_cmd(host, &cmd, 3);
|
||||
if (err)
|
||||
return err;
|
||||
|
||||
rt_memcpy(cid, cmd.resp, sizeof(rt_uint32_t) * 4);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
buf = (rt_uint32_t *)rt_malloc(16);
|
||||
if (!buf)
|
||||
{
|
||||
LOG_E("allocate memory failed!");
|
||||
|
||||
return -RT_ENOMEM;
|
||||
}
|
||||
|
||||
rt_memset(&req, 0, sizeof(struct rt_mmcsd_req));
|
||||
rt_memset(&cmd, 0, sizeof(struct rt_mmcsd_cmd));
|
||||
rt_memset(&data, 0, sizeof(struct rt_mmcsd_data));
|
||||
|
||||
req.cmd = &cmd;
|
||||
req.data = &data;
|
||||
|
||||
cmd.cmd_code = SEND_CID;
|
||||
cmd.arg = 0;
|
||||
|
||||
/* NOTE HACK: the RESP_SPI_R1 is always correct here, but we
|
||||
* rely on callers to never use this with "native" calls for reading
|
||||
* CSD or CID. Native versions of those commands use the R2 type,
|
||||
* not R1 plus a data block.
|
||||
*/
|
||||
cmd.flags = RESP_SPI_R1 | RESP_R1 | CMD_ADTC;
|
||||
|
||||
data.blksize = 16;
|
||||
data.blks = 1;
|
||||
data.flags = DATA_DIR_READ;
|
||||
data.buf = buf;
|
||||
/*
|
||||
* The spec states that CSR and CID accesses have a timeout
|
||||
* of 64 clock cycles.
|
||||
*/
|
||||
data.timeout_ns = 0;
|
||||
data.timeout_clks = 64;
|
||||
|
||||
mmcsd_send_request(host, &req);
|
||||
|
||||
if (cmd.err || data.err)
|
||||
{
|
||||
rt_free(buf);
|
||||
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
for (i = 0; i < 4; i++)
|
||||
cid[i] = buf[i];
|
||||
rt_free(buf);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
rt_int32_t mmcsd_get_csd(struct rt_mmcsd_card *card, rt_uint32_t *csd)
|
||||
{
|
||||
rt_int32_t err, i;
|
||||
struct rt_mmcsd_req req;
|
||||
struct rt_mmcsd_cmd cmd;
|
||||
struct rt_mmcsd_data data;
|
||||
rt_uint32_t *buf = RT_NULL;
|
||||
|
||||
if (!controller_is_spi(card->host))
|
||||
{
|
||||
rt_memset(&cmd, 0, sizeof(struct rt_mmcsd_cmd));
|
||||
|
||||
cmd.cmd_code = SEND_CSD;
|
||||
cmd.arg = card->rca << 16;
|
||||
cmd.flags = RESP_R2 | CMD_AC;
|
||||
err = mmcsd_send_cmd(card->host, &cmd, 3);
|
||||
if (err)
|
||||
return err;
|
||||
|
||||
rt_memcpy(csd, cmd.resp, sizeof(rt_uint32_t) * 4);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
buf = (rt_uint32_t *)rt_malloc(16);
|
||||
if (!buf)
|
||||
{
|
||||
LOG_E("allocate memory failed!");
|
||||
|
||||
return -RT_ENOMEM;
|
||||
}
|
||||
|
||||
rt_memset(&req, 0, sizeof(struct rt_mmcsd_req));
|
||||
rt_memset(&cmd, 0, sizeof(struct rt_mmcsd_cmd));
|
||||
rt_memset(&data, 0, sizeof(struct rt_mmcsd_data));
|
||||
|
||||
req.cmd = &cmd;
|
||||
req.data = &data;
|
||||
|
||||
cmd.cmd_code = SEND_CSD;
|
||||
cmd.arg = 0;
|
||||
|
||||
/* NOTE HACK: the RESP_SPI_R1 is always correct here, but we
|
||||
* rely on callers to never use this with "native" calls for reading
|
||||
* CSD or CID. Native versions of those commands use the R2 type,
|
||||
* not R1 plus a data block.
|
||||
*/
|
||||
cmd.flags = RESP_SPI_R1 | RESP_R1 | CMD_ADTC;
|
||||
|
||||
data.blksize = 16;
|
||||
data.blks = 1;
|
||||
data.flags = DATA_DIR_READ;
|
||||
data.buf = buf;
|
||||
|
||||
/*
|
||||
* The spec states that CSR and CID accesses have a timeout
|
||||
* of 64 clock cycles.
|
||||
*/
|
||||
data.timeout_ns = 0;
|
||||
data.timeout_clks = 64;
|
||||
|
||||
mmcsd_send_request(card->host, &req);
|
||||
|
||||
if (cmd.err || data.err)
|
||||
{
|
||||
rt_free(buf);
|
||||
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
for (i = 0; i < 4; i++)
|
||||
csd[i] = buf[i];
|
||||
rt_free(buf);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static rt_int32_t _mmcsd_select_card(struct rt_mmcsd_host *host,
|
||||
struct rt_mmcsd_card *card)
|
||||
{
|
||||
rt_int32_t err;
|
||||
struct rt_mmcsd_cmd cmd;
|
||||
|
||||
rt_memset(&cmd, 0, sizeof(struct rt_mmcsd_cmd));
|
||||
|
||||
cmd.cmd_code = SELECT_CARD;
|
||||
|
||||
if (card)
|
||||
{
|
||||
cmd.arg = card->rca << 16;
|
||||
cmd.flags = RESP_R1 | CMD_AC;
|
||||
}
|
||||
else
|
||||
{
|
||||
cmd.arg = 0;
|
||||
cmd.flags = RESP_NONE | CMD_AC;
|
||||
}
|
||||
|
||||
err = mmcsd_send_cmd(host, &cmd, 3);
|
||||
if (err)
|
||||
return err;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
rt_int32_t mmcsd_select_card(struct rt_mmcsd_card *card)
|
||||
{
|
||||
return _mmcsd_select_card(card->host, card);
|
||||
}
|
||||
|
||||
rt_int32_t mmcsd_deselect_cards(struct rt_mmcsd_card *card)
|
||||
{
|
||||
return _mmcsd_select_card(card->host, RT_NULL);
|
||||
}
|
||||
|
||||
rt_int32_t mmcsd_spi_use_crc(struct rt_mmcsd_host *host, rt_int32_t use_crc)
|
||||
{
|
||||
struct rt_mmcsd_cmd cmd;
|
||||
rt_int32_t err;
|
||||
|
||||
rt_memset(&cmd, 0, sizeof(struct rt_mmcsd_cmd));
|
||||
|
||||
cmd.cmd_code = SPI_CRC_ON_OFF;
|
||||
cmd.flags = RESP_SPI_R1;
|
||||
cmd.arg = use_crc;
|
||||
|
||||
err = mmcsd_send_cmd(host, &cmd, 0);
|
||||
if (!err)
|
||||
host->spi_use_crc = use_crc;
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
rt_inline void mmcsd_set_iocfg(struct rt_mmcsd_host *host)
|
||||
{
|
||||
struct rt_mmcsd_io_cfg *io_cfg = &host->io_cfg;
|
||||
|
||||
mmcsd_dbg("clock %uHz busmode %u powermode %u cs %u Vdd %u "
|
||||
"width %u \n",
|
||||
io_cfg->clock, io_cfg->bus_mode,
|
||||
io_cfg->power_mode, io_cfg->chip_select, io_cfg->vdd,
|
||||
io_cfg->bus_width);
|
||||
|
||||
host->ops->set_iocfg(host, io_cfg);
|
||||
}
|
||||
|
||||
/*
|
||||
* Control chip select pin on a host.
|
||||
*/
|
||||
void mmcsd_set_chip_select(struct rt_mmcsd_host *host, rt_int32_t mode)
|
||||
{
|
||||
host->io_cfg.chip_select = mode;
|
||||
mmcsd_set_iocfg(host);
|
||||
}
|
||||
|
||||
/*
|
||||
* Sets the host clock to the highest possible frequency that
|
||||
* is below "hz".
|
||||
*/
|
||||
void mmcsd_set_clock(struct rt_mmcsd_host *host, rt_uint32_t clk)
|
||||
{
|
||||
if (clk < host->freq_min)
|
||||
{
|
||||
LOG_W("clock too low!");
|
||||
}
|
||||
|
||||
host->io_cfg.clock = clk;
|
||||
mmcsd_set_iocfg(host);
|
||||
}
|
||||
|
||||
/*
|
||||
* Change the bus mode (open drain/push-pull) of a host.
|
||||
*/
|
||||
void mmcsd_set_bus_mode(struct rt_mmcsd_host *host, rt_uint32_t mode)
|
||||
{
|
||||
host->io_cfg.bus_mode = mode;
|
||||
mmcsd_set_iocfg(host);
|
||||
}
|
||||
|
||||
/*
|
||||
* Change data bus width of a host.
|
||||
*/
|
||||
void mmcsd_set_bus_width(struct rt_mmcsd_host *host, rt_uint32_t width)
|
||||
{
|
||||
host->io_cfg.bus_width = width;
|
||||
mmcsd_set_iocfg(host);
|
||||
}
|
||||
|
||||
void mmcsd_set_timing(struct rt_mmcsd_host *host, rt_uint32_t timing)
|
||||
{
|
||||
host->io_cfg.timing = timing;
|
||||
mmcsd_set_iocfg(host);
|
||||
}
|
||||
|
||||
void mmcsd_set_data_timeout(struct rt_mmcsd_data *data,
|
||||
const struct rt_mmcsd_card *card)
|
||||
{
|
||||
rt_uint32_t mult;
|
||||
|
||||
if (card->card_type == CARD_TYPE_SDIO)
|
||||
{
|
||||
data->timeout_ns = 1000000000; /* SDIO card 1s */
|
||||
data->timeout_clks = 0;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
* SD cards use a 100 multiplier rather than 10
|
||||
*/
|
||||
mult = (card->card_type == CARD_TYPE_SD) ? 100 : 10;
|
||||
|
||||
/*
|
||||
* Scale up the multiplier (and therefore the timeout) by
|
||||
* the r2w factor for writes.
|
||||
*/
|
||||
if (data->flags & DATA_DIR_WRITE)
|
||||
mult <<= card->csd.r2w_factor;
|
||||
|
||||
data->timeout_ns = card->tacc_ns * mult;
|
||||
data->timeout_clks = card->tacc_clks * mult;
|
||||
|
||||
/*
|
||||
* SD cards also have an upper limit on the timeout.
|
||||
*/
|
||||
if (card->card_type == CARD_TYPE_SD)
|
||||
{
|
||||
rt_uint32_t timeout_us, limit_us;
|
||||
|
||||
timeout_us = data->timeout_ns / 1000;
|
||||
timeout_us += data->timeout_clks * 1000 /
|
||||
(card->host->io_cfg.clock / 1000);
|
||||
|
||||
if (data->flags & DATA_DIR_WRITE)
|
||||
/*
|
||||
* The limit is really 250 ms, but that is
|
||||
* insufficient for some crappy cards.
|
||||
*/
|
||||
limit_us = 300000;
|
||||
else
|
||||
limit_us = 100000;
|
||||
|
||||
/*
|
||||
* SDHC cards always use these fixed values.
|
||||
*/
|
||||
if (timeout_us > limit_us || card->flags & CARD_FLAG_SDHC)
|
||||
{
|
||||
data->timeout_ns = limit_us * 1000; /* SDHC card fixed 250ms */
|
||||
data->timeout_clks = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (controller_is_spi(card->host))
|
||||
{
|
||||
if (data->flags & DATA_DIR_WRITE)
|
||||
{
|
||||
if (data->timeout_ns < 1000000000)
|
||||
data->timeout_ns = 1000000000; /* 1s */
|
||||
}
|
||||
else
|
||||
{
|
||||
if (data->timeout_ns < 100000000)
|
||||
data->timeout_ns = 100000000; /* 100ms */
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Mask off any voltages we don't support and select
|
||||
* the lowest voltage
|
||||
*/
|
||||
rt_uint32_t mmcsd_select_voltage(struct rt_mmcsd_host *host, rt_uint32_t ocr)
|
||||
{
|
||||
int bit;
|
||||
extern int __rt_ffs(int value);
|
||||
|
||||
ocr &= host->valid_ocr;
|
||||
|
||||
bit = __rt_ffs(ocr);
|
||||
if (bit)
|
||||
{
|
||||
bit -= 1;
|
||||
|
||||
ocr &= 3 << bit;
|
||||
|
||||
host->io_cfg.vdd = bit;
|
||||
mmcsd_set_iocfg(host);
|
||||
}
|
||||
else
|
||||
{
|
||||
LOG_W("host doesn't support card's voltages!");
|
||||
ocr = 0;
|
||||
}
|
||||
|
||||
return ocr;
|
||||
}
|
||||
|
||||
static void mmcsd_power_up(struct rt_mmcsd_host *host)
|
||||
{
|
||||
int bit = __rt_fls(host->valid_ocr) - 1;
|
||||
|
||||
host->io_cfg.vdd = bit;
|
||||
if (controller_is_spi(host))
|
||||
{
|
||||
host->io_cfg.chip_select = MMCSD_CS_HIGH;
|
||||
host->io_cfg.bus_mode = MMCSD_BUSMODE_PUSHPULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
host->io_cfg.chip_select = MMCSD_CS_IGNORE;
|
||||
host->io_cfg.bus_mode = MMCSD_BUSMODE_OPENDRAIN;
|
||||
}
|
||||
host->io_cfg.power_mode = MMCSD_POWER_UP;
|
||||
host->io_cfg.bus_width = MMCSD_BUS_WIDTH_1;
|
||||
mmcsd_set_iocfg(host);
|
||||
|
||||
/*
|
||||
* This delay should be sufficient to allow the power supply
|
||||
* to reach the minimum voltage.
|
||||
*/
|
||||
rt_thread_mdelay(10);
|
||||
|
||||
host->io_cfg.clock = host->freq_min;
|
||||
host->io_cfg.power_mode = MMCSD_POWER_ON;
|
||||
mmcsd_set_iocfg(host);
|
||||
|
||||
/*
|
||||
* This delay must be at least 74 clock sizes, or 1 ms, or the
|
||||
* time required to reach a stable voltage.
|
||||
*/
|
||||
rt_thread_mdelay(10);
|
||||
}
|
||||
|
||||
static void mmcsd_power_off(struct rt_mmcsd_host *host)
|
||||
{
|
||||
host->io_cfg.clock = 0;
|
||||
host->io_cfg.vdd = 0;
|
||||
if (!controller_is_spi(host))
|
||||
{
|
||||
host->io_cfg.bus_mode = MMCSD_BUSMODE_OPENDRAIN;
|
||||
host->io_cfg.chip_select = MMCSD_CS_IGNORE;
|
||||
}
|
||||
host->io_cfg.power_mode = MMCSD_POWER_OFF;
|
||||
host->io_cfg.bus_width = MMCSD_BUS_WIDTH_1;
|
||||
mmcsd_set_iocfg(host);
|
||||
}
|
||||
|
||||
int mmcsd_wait_cd_changed(rt_int32_t timeout)
|
||||
{
|
||||
struct rt_mmcsd_host *host;
|
||||
if (rt_mb_recv(&mmcsd_hotpluge_mb, (rt_ubase_t *)&host, timeout) == RT_EOK)
|
||||
{
|
||||
if (host->card == RT_NULL)
|
||||
{
|
||||
return MMCSD_HOST_UNPLUGED;
|
||||
}
|
||||
else
|
||||
{
|
||||
return MMCSD_HOST_PLUGED;
|
||||
}
|
||||
}
|
||||
return -RT_ETIMEOUT;
|
||||
}
|
||||
RTM_EXPORT(mmcsd_wait_cd_changed);
|
||||
|
||||
void mmcsd_change(struct rt_mmcsd_host *host)
|
||||
{
|
||||
rt_mb_send(&mmcsd_detect_mb, (rt_ubase_t)host);
|
||||
}
|
||||
|
||||
void mmcsd_detect(void *param)
|
||||
{
|
||||
struct rt_mmcsd_host *host;
|
||||
rt_uint32_t ocr;
|
||||
rt_int32_t err;
|
||||
|
||||
while (1)
|
||||
{
|
||||
if (rt_mb_recv(&mmcsd_detect_mb, (rt_ubase_t *)&host, RT_WAITING_FOREVER) == RT_EOK)
|
||||
{
|
||||
if (host->card == RT_NULL)
|
||||
{
|
||||
mmcsd_host_lock(host);
|
||||
mmcsd_power_up(host);
|
||||
mmcsd_go_idle(host);
|
||||
|
||||
mmcsd_send_if_cond(host, host->valid_ocr);
|
||||
|
||||
err = sdio_io_send_op_cond(host, 0, &ocr);
|
||||
if (!err)
|
||||
{
|
||||
if (init_sdio(host, ocr))
|
||||
mmcsd_power_off(host);
|
||||
mmcsd_host_unlock(host);
|
||||
continue;
|
||||
}
|
||||
|
||||
/*
|
||||
* detect SD card
|
||||
*/
|
||||
err = mmcsd_send_app_op_cond(host, 0, &ocr);
|
||||
if (!err)
|
||||
{
|
||||
if (init_sd(host, ocr))
|
||||
mmcsd_power_off(host);
|
||||
mmcsd_host_unlock(host);
|
||||
rt_mb_send(&mmcsd_hotpluge_mb, (rt_ubase_t)host);
|
||||
continue;
|
||||
}
|
||||
|
||||
/*
|
||||
* detect mmc card
|
||||
*/
|
||||
err = mmc_send_op_cond(host, 0, &ocr);
|
||||
if (!err)
|
||||
{
|
||||
if (init_mmc(host, ocr))
|
||||
mmcsd_power_off(host);
|
||||
mmcsd_host_unlock(host);
|
||||
rt_mb_send(&mmcsd_hotpluge_mb, (rt_ubase_t)host);
|
||||
continue;
|
||||
}
|
||||
mmcsd_host_unlock(host);
|
||||
}
|
||||
else
|
||||
{
|
||||
/* card removed */
|
||||
mmcsd_host_lock(host);
|
||||
if (host->card->sdio_function_num != 0)
|
||||
{
|
||||
LOG_W("unsupport sdio card plug out!");
|
||||
}
|
||||
else
|
||||
{
|
||||
rt_mmcsd_blk_remove(host->card);
|
||||
rt_free(host->card);
|
||||
|
||||
host->card = RT_NULL;
|
||||
}
|
||||
mmcsd_host_unlock(host);
|
||||
rt_mb_send(&mmcsd_hotpluge_mb, (rt_ubase_t)host);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void mmcsd_host_init(struct rt_mmcsd_host *host)
|
||||
{
|
||||
rt_memset(host, 0, sizeof(struct rt_mmcsd_host));
|
||||
strncpy(host->name, "sd", sizeof(host->name) - 1);
|
||||
host->max_seg_size = 65535;
|
||||
host->max_dma_segs = 1;
|
||||
host->max_blk_size = 512;
|
||||
host->max_blk_count = 4096;
|
||||
|
||||
rt_mutex_init(&host->bus_lock, "sd_bus_lock", RT_IPC_FLAG_FIFO);
|
||||
rt_sem_init(&host->sem_ack, "sd_ack", 0, RT_IPC_FLAG_FIFO);
|
||||
}
|
||||
|
||||
struct rt_mmcsd_host *mmcsd_alloc_host(void)
|
||||
{
|
||||
struct rt_mmcsd_host *host;
|
||||
|
||||
host = rt_malloc(sizeof(struct rt_mmcsd_host));
|
||||
if (!host)
|
||||
{
|
||||
LOG_E("alloc host failed");
|
||||
|
||||
return RT_NULL;
|
||||
}
|
||||
|
||||
mmcsd_host_init(host);
|
||||
|
||||
return host;
|
||||
}
|
||||
|
||||
void mmcsd_free_host(struct rt_mmcsd_host *host)
|
||||
{
|
||||
rt_mutex_detach(&host->bus_lock);
|
||||
rt_sem_detach(&host->sem_ack);
|
||||
rt_free(host);
|
||||
}
|
||||
|
||||
rt_int32_t mmcsd_excute_tuning(struct rt_mmcsd_card *card)
|
||||
{
|
||||
struct rt_mmcsd_host *host = card->host;
|
||||
rt_int32_t opcode;
|
||||
|
||||
if (!host->ops->execute_tuning)
|
||||
return RT_EOK;
|
||||
|
||||
if (card->card_type == CARD_TYPE_MMC)
|
||||
opcode = SEND_TUNING_BLOCK_HS200;
|
||||
else
|
||||
opcode = SEND_TUNING_BLOCK;
|
||||
|
||||
return host->ops->execute_tuning(host, opcode);;
|
||||
}
|
||||
|
||||
int rt_mmcsd_core_init(void)
|
||||
{
|
||||
rt_err_t ret;
|
||||
|
||||
/* initialize detect SD cart thread */
|
||||
/* initialize mailbox and create detect SD card thread */
|
||||
ret = rt_mb_init(&mmcsd_detect_mb, "mmcsdmb",
|
||||
&mmcsd_detect_mb_pool[0], sizeof(mmcsd_detect_mb_pool) / sizeof(mmcsd_detect_mb_pool[0]),
|
||||
RT_IPC_FLAG_FIFO);
|
||||
RT_ASSERT(ret == RT_EOK);
|
||||
|
||||
ret = rt_mb_init(&mmcsd_hotpluge_mb, "mmcsdhotplugmb",
|
||||
&mmcsd_hotpluge_mb_pool[0], sizeof(mmcsd_hotpluge_mb_pool) / sizeof(mmcsd_hotpluge_mb_pool[0]),
|
||||
RT_IPC_FLAG_FIFO);
|
||||
RT_ASSERT(ret == RT_EOK);
|
||||
ret = rt_thread_init(&mmcsd_detect_thread, "mmcsd_detect", mmcsd_detect, RT_NULL,
|
||||
&mmcsd_stack[0], RT_MMCSD_STACK_SIZE, RT_MMCSD_THREAD_PREORITY, 20);
|
||||
if (ret == RT_EOK)
|
||||
{
|
||||
rt_thread_startup(&mmcsd_detect_thread);
|
||||
}
|
||||
|
||||
rt_sdio_init();
|
||||
|
||||
return 0;
|
||||
}
|
||||
INIT_PREV_EXPORT(rt_mmcsd_core_init);
|
||||
|
707
components/drivers/sdio/sd.c
Normal file
707
components/drivers/sdio/sd.c
Normal file
|
@ -0,0 +1,707 @@
|
|||
/*
|
||||
* Copyright (c) 2006-2023, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2011-07-25 weety first version
|
||||
*/
|
||||
|
||||
#include <drivers/mmcsd_core.h>
|
||||
#include <drivers/sd.h>
|
||||
|
||||
#define DBG_TAG "SDIO"
|
||||
#ifdef RT_SDIO_DEBUG
|
||||
#define DBG_LVL DBG_LOG
|
||||
#else
|
||||
#define DBG_LVL DBG_INFO
|
||||
#endif /* RT_SDIO_DEBUG */
|
||||
#include <rtdbg.h>
|
||||
|
||||
static const rt_uint32_t tran_unit[] =
|
||||
{
|
||||
10000, 100000, 1000000, 10000000,
|
||||
0, 0, 0, 0
|
||||
};
|
||||
|
||||
static const rt_uint8_t tran_value[] =
|
||||
{
|
||||
0, 10, 12, 13, 15, 20, 25, 30,
|
||||
35, 40, 45, 50, 55, 60, 70, 80,
|
||||
};
|
||||
|
||||
static const rt_uint32_t tacc_uint[] =
|
||||
{
|
||||
1, 10, 100, 1000, 10000, 100000, 1000000, 10000000,
|
||||
};
|
||||
|
||||
static const rt_uint8_t tacc_value[] =
|
||||
{
|
||||
0, 10, 12, 13, 15, 20, 25, 30,
|
||||
35, 40, 45, 50, 55, 60, 70, 80,
|
||||
};
|
||||
|
||||
rt_inline rt_uint32_t GET_BITS(rt_uint32_t *resp,
|
||||
rt_uint32_t start,
|
||||
rt_uint32_t size)
|
||||
{
|
||||
const rt_int32_t __size = size;
|
||||
const rt_uint32_t __mask = (__size < 32 ? 1 << __size : 0) - 1;
|
||||
const rt_int32_t __off = 3 - ((start) / 32);
|
||||
const rt_int32_t __shft = (start) & 31;
|
||||
rt_uint32_t __res;
|
||||
|
||||
__res = resp[__off] >> __shft;
|
||||
if (__size + __shft > 32)
|
||||
__res |= resp[__off-1] << ((32 - __shft) % 32);
|
||||
|
||||
return __res & __mask;
|
||||
}
|
||||
|
||||
static rt_int32_t mmcsd_parse_csd(struct rt_mmcsd_card *card)
|
||||
{
|
||||
struct rt_mmcsd_csd *csd = &card->csd;
|
||||
rt_uint32_t *resp = card->resp_csd;
|
||||
|
||||
csd->csd_structure = GET_BITS(resp, 126, 2);
|
||||
|
||||
switch (csd->csd_structure)
|
||||
{
|
||||
case 0:
|
||||
csd->taac = GET_BITS(resp, 112, 8);
|
||||
csd->nsac = GET_BITS(resp, 104, 8);
|
||||
csd->tran_speed = GET_BITS(resp, 96, 8);
|
||||
csd->card_cmd_class = GET_BITS(resp, 84, 12);
|
||||
csd->rd_blk_len = GET_BITS(resp, 80, 4);
|
||||
csd->rd_blk_part = GET_BITS(resp, 79, 1);
|
||||
csd->wr_blk_misalign = GET_BITS(resp, 78, 1);
|
||||
csd->rd_blk_misalign = GET_BITS(resp, 77, 1);
|
||||
csd->dsr_imp = GET_BITS(resp, 76, 1);
|
||||
csd->c_size = GET_BITS(resp, 62, 12);
|
||||
csd->c_size_mult = GET_BITS(resp, 47, 3);
|
||||
csd->r2w_factor = GET_BITS(resp, 26, 3);
|
||||
csd->wr_blk_len = GET_BITS(resp, 22, 4);
|
||||
csd->wr_blk_partial = GET_BITS(resp, 21, 1);
|
||||
csd->csd_crc = GET_BITS(resp, 1, 7);
|
||||
|
||||
card->card_blksize = 1 << csd->rd_blk_len;
|
||||
card->card_capacity = (csd->c_size + 1) << (csd->c_size_mult + 2);
|
||||
card->card_capacity *= card->card_blksize;
|
||||
card->card_capacity >>= 10; /* unit:KB */
|
||||
card->tacc_clks = csd->nsac * 100;
|
||||
card->tacc_ns = (tacc_uint[csd->taac&0x07] * tacc_value[(csd->taac&0x78)>>3] + 9) / 10;
|
||||
card->max_data_rate = tran_unit[csd->tran_speed&0x07] * tran_value[(csd->tran_speed&0x78)>>3];
|
||||
|
||||
break;
|
||||
case 1:
|
||||
card->flags |= CARD_FLAG_SDHC;
|
||||
|
||||
/*This field is fixed to 0Eh, which indicates 1 ms.
|
||||
The host should not use TAAC, NSAC, and R2W_FACTOR
|
||||
to calculate timeout and should uses fixed timeout
|
||||
values for read and write operations*/
|
||||
csd->taac = GET_BITS(resp, 112, 8);
|
||||
csd->nsac = GET_BITS(resp, 104, 8);
|
||||
csd->tran_speed = GET_BITS(resp, 96, 8);
|
||||
csd->card_cmd_class = GET_BITS(resp, 84, 12);
|
||||
csd->rd_blk_len = GET_BITS(resp, 80, 4);
|
||||
csd->rd_blk_part = GET_BITS(resp, 79, 1);
|
||||
csd->wr_blk_misalign = GET_BITS(resp, 78, 1);
|
||||
csd->rd_blk_misalign = GET_BITS(resp, 77, 1);
|
||||
csd->dsr_imp = GET_BITS(resp, 76, 1);
|
||||
csd->c_size = GET_BITS(resp, 48, 22);
|
||||
|
||||
csd->r2w_factor = GET_BITS(resp, 26, 3);
|
||||
csd->wr_blk_len = GET_BITS(resp, 22, 4);
|
||||
csd->wr_blk_partial = GET_BITS(resp, 21, 1);
|
||||
csd->csd_crc = GET_BITS(resp, 1, 7);
|
||||
|
||||
card->card_blksize = 512;
|
||||
card->card_capacity = (csd->c_size + 1) * 512; /* unit:KB */
|
||||
card->card_sec_cnt = card->card_capacity * 2;
|
||||
card->tacc_clks = 0;
|
||||
card->tacc_ns = 0;
|
||||
card->max_data_rate = tran_unit[csd->tran_speed&0x07] * tran_value[(csd->tran_speed&0x78)>>3];
|
||||
|
||||
break;
|
||||
default:
|
||||
LOG_E("unrecognised CSD structure version %d!", csd->csd_structure);
|
||||
|
||||
return -RT_ERROR;
|
||||
}
|
||||
LOG_I("SD card capacity %d KB.", card->card_capacity);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static rt_int32_t mmcsd_parse_scr(struct rt_mmcsd_card *card)
|
||||
{
|
||||
struct rt_sd_scr *scr = &card->scr;
|
||||
rt_uint32_t resp[4];
|
||||
|
||||
resp[3] = card->resp_scr[1];
|
||||
resp[2] = card->resp_scr[0];
|
||||
scr->sd_version = GET_BITS(resp, 56, 4);
|
||||
scr->sd_bus_widths = GET_BITS(resp, 48, 4);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static rt_int32_t mmcsd_switch(struct rt_mmcsd_card *card)
|
||||
{
|
||||
rt_int32_t err;
|
||||
struct rt_mmcsd_host *host = card->host;
|
||||
struct rt_mmcsd_req req;
|
||||
struct rt_mmcsd_cmd cmd;
|
||||
struct rt_mmcsd_data data;
|
||||
rt_uint8_t *buf;
|
||||
|
||||
buf = (rt_uint8_t*)rt_malloc(64);
|
||||
if (!buf)
|
||||
{
|
||||
LOG_E("alloc memory failed!");
|
||||
|
||||
return -RT_ENOMEM;
|
||||
}
|
||||
|
||||
if (card->card_type != CARD_TYPE_SD)
|
||||
goto err;
|
||||
if (card->scr.sd_version < SCR_SPEC_VER_1)
|
||||
goto err;
|
||||
|
||||
rt_memset(&cmd, 0, sizeof(struct rt_mmcsd_cmd));
|
||||
|
||||
cmd.cmd_code = SD_SWITCH;
|
||||
cmd.arg = 0x00FFFFF1;
|
||||
cmd.flags = RESP_R1 | CMD_ADTC;
|
||||
|
||||
rt_memset(&data, 0, sizeof(struct rt_mmcsd_data));
|
||||
|
||||
mmcsd_set_data_timeout(&data, card);
|
||||
|
||||
data.blksize = 64;
|
||||
data.blks = 1;
|
||||
data.flags = DATA_DIR_READ;
|
||||
data.buf = (rt_uint32_t *)buf;
|
||||
|
||||
rt_memset(&req, 0, sizeof(struct rt_mmcsd_req));
|
||||
|
||||
req.cmd = &cmd;
|
||||
req.data = &data;
|
||||
|
||||
mmcsd_send_request(host, &req);
|
||||
|
||||
if (cmd.err || data.err)
|
||||
{
|
||||
goto err1;
|
||||
}
|
||||
|
||||
if (buf[13] & 0x02)
|
||||
card->hs_max_data_rate = 50000000;
|
||||
|
||||
rt_memset(&cmd, 0, sizeof(struct rt_mmcsd_cmd));
|
||||
|
||||
cmd.cmd_code = SD_SWITCH;
|
||||
cmd.arg = 0x80FFFFF1;
|
||||
cmd.flags = RESP_R1 | CMD_ADTC;
|
||||
|
||||
rt_memset(&data, 0, sizeof(struct rt_mmcsd_data));
|
||||
|
||||
mmcsd_set_data_timeout(&data, card);
|
||||
|
||||
data.blksize = 64;
|
||||
data.blks = 1;
|
||||
data.flags = DATA_DIR_READ;
|
||||
data.buf = (rt_uint32_t *)buf;
|
||||
|
||||
rt_memset(&req, 0, sizeof(struct rt_mmcsd_req));
|
||||
|
||||
req.cmd = &cmd;
|
||||
req.data = &data;
|
||||
|
||||
mmcsd_send_request(host, &req);
|
||||
|
||||
if (cmd.err || data.err)
|
||||
{
|
||||
goto err1;
|
||||
}
|
||||
|
||||
if ((buf[16] & 0xF) != 1)
|
||||
{
|
||||
LOG_I("switching card to high speed failed!");
|
||||
goto err;
|
||||
}
|
||||
|
||||
card->flags |= CARD_FLAG_HIGHSPEED;
|
||||
|
||||
err:
|
||||
rt_free(buf);
|
||||
return 0;
|
||||
|
||||
err1:
|
||||
if (cmd.err)
|
||||
err = cmd.err;
|
||||
if (data.err)
|
||||
err = data.err;
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
static rt_err_t mmcsd_app_cmd(struct rt_mmcsd_host *host,
|
||||
struct rt_mmcsd_card *card)
|
||||
{
|
||||
rt_err_t err;
|
||||
struct rt_mmcsd_cmd cmd = {0};
|
||||
|
||||
cmd.cmd_code = APP_CMD;
|
||||
|
||||
if (card)
|
||||
{
|
||||
cmd.arg = card->rca << 16;
|
||||
cmd.flags = RESP_R1 | CMD_AC;
|
||||
}
|
||||
else
|
||||
{
|
||||
cmd.arg = 0;
|
||||
cmd.flags = RESP_R1 | CMD_BCR;
|
||||
}
|
||||
|
||||
err = mmcsd_send_cmd(host, &cmd, 0);
|
||||
if (err)
|
||||
return err;
|
||||
|
||||
/* Check that card supported application commands */
|
||||
if (!controller_is_spi(host) && !(cmd.resp[0] & R1_APP_CMD))
|
||||
return -RT_ERROR;
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
|
||||
rt_err_t mmcsd_send_app_cmd(struct rt_mmcsd_host *host,
|
||||
struct rt_mmcsd_card *card,
|
||||
struct rt_mmcsd_cmd *cmd,
|
||||
int retry)
|
||||
{
|
||||
struct rt_mmcsd_req req;
|
||||
int i;
|
||||
rt_err_t err;
|
||||
|
||||
err = -RT_ERROR;
|
||||
|
||||
/*
|
||||
* We have to resend MMC_APP_CMD for each attempt so
|
||||
* we cannot use the retries field in mmc_command.
|
||||
*/
|
||||
for (i = 0; i <= retry; i++)
|
||||
{
|
||||
rt_memset(&req, 0, sizeof(struct rt_mmcsd_req));
|
||||
|
||||
err = mmcsd_app_cmd(host, card);
|
||||
if (err)
|
||||
{
|
||||
/* no point in retrying; no APP commands allowed */
|
||||
if (controller_is_spi(host))
|
||||
{
|
||||
if (cmd->resp[0] & R1_SPI_ILLEGAL_COMMAND)
|
||||
break;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
rt_memset(&req, 0, sizeof(struct rt_mmcsd_req));
|
||||
|
||||
rt_memset(cmd->resp, 0, sizeof(cmd->resp));
|
||||
|
||||
req.cmd = cmd;
|
||||
//cmd->data = NULL;
|
||||
|
||||
mmcsd_send_request(host, &req);
|
||||
|
||||
err = cmd->err;
|
||||
if (!cmd->err)
|
||||
break;
|
||||
|
||||
/* no point in retrying illegal APP commands */
|
||||
if (controller_is_spi(host))
|
||||
{
|
||||
if (cmd->resp[0] & R1_SPI_ILLEGAL_COMMAND)
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
rt_err_t mmcsd_app_set_bus_width(struct rt_mmcsd_card *card, rt_int32_t width)
|
||||
{
|
||||
rt_err_t err;
|
||||
struct rt_mmcsd_cmd cmd;
|
||||
|
||||
rt_memset(&cmd, 0, sizeof(struct rt_mmcsd_cmd));
|
||||
|
||||
cmd.cmd_code = SD_APP_SET_BUS_WIDTH;
|
||||
cmd.flags = RESP_R1 | CMD_AC;
|
||||
|
||||
switch (width)
|
||||
{
|
||||
case MMCSD_BUS_WIDTH_1:
|
||||
cmd.arg = MMCSD_BUS_WIDTH_1;
|
||||
break;
|
||||
case MMCSD_BUS_WIDTH_4:
|
||||
cmd.arg = MMCSD_BUS_WIDTH_4;
|
||||
break;
|
||||
default:
|
||||
return -RT_ERROR;
|
||||
}
|
||||
|
||||
err = mmcsd_send_app_cmd(card->host, card, &cmd, 3);
|
||||
if (err)
|
||||
return err;
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
rt_err_t mmcsd_send_app_op_cond(struct rt_mmcsd_host *host,
|
||||
rt_uint32_t ocr,
|
||||
rt_uint32_t *rocr)
|
||||
{
|
||||
struct rt_mmcsd_cmd cmd;
|
||||
rt_uint32_t i;
|
||||
rt_err_t err = RT_EOK;
|
||||
|
||||
rt_memset(&cmd, 0, sizeof(struct rt_mmcsd_cmd));
|
||||
|
||||
cmd.cmd_code = SD_APP_OP_COND;
|
||||
if (controller_is_spi(host))
|
||||
cmd.arg = ocr & (1 << 30); /* SPI only defines one bit */
|
||||
else
|
||||
cmd.arg = ocr;
|
||||
cmd.flags = RESP_SPI_R1 | RESP_R3 | CMD_BCR;
|
||||
|
||||
for (i = 1000; i; i--)
|
||||
{
|
||||
err = mmcsd_send_app_cmd(host, RT_NULL, &cmd, 3);
|
||||
if (err)
|
||||
break;
|
||||
|
||||
/* if we're just probing, do a single pass */
|
||||
if (ocr == 0)
|
||||
break;
|
||||
|
||||
/* otherwise wait until reset completes */
|
||||
if (controller_is_spi(host))
|
||||
{
|
||||
if (!(cmd.resp[0] & R1_SPI_IDLE))
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (cmd.resp[0] & CARD_BUSY)
|
||||
break;
|
||||
}
|
||||
|
||||
err = -RT_ETIMEOUT;
|
||||
|
||||
rt_thread_mdelay(10); //delay 10ms
|
||||
}
|
||||
|
||||
if (rocr && !controller_is_spi(host))
|
||||
*rocr = cmd.resp[0];
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
/*
|
||||
* To support SD 2.0 cards, we must always invoke SD_SEND_IF_COND
|
||||
* before SD_APP_OP_COND. This command will harmlessly fail for
|
||||
* SD 1.0 cards.
|
||||
*/
|
||||
rt_err_t mmcsd_send_if_cond(struct rt_mmcsd_host *host, rt_uint32_t ocr)
|
||||
{
|
||||
struct rt_mmcsd_cmd cmd;
|
||||
rt_err_t err;
|
||||
rt_uint8_t pattern;
|
||||
|
||||
cmd.cmd_code = SD_SEND_IF_COND;
|
||||
cmd.arg = ((ocr & 0xFF8000) != 0) << 8 | 0xAA;
|
||||
cmd.flags = RESP_SPI_R7 | RESP_R7 | CMD_BCR;
|
||||
|
||||
err = mmcsd_send_cmd(host, &cmd, 0);
|
||||
if (err)
|
||||
return err;
|
||||
|
||||
if (controller_is_spi(host))
|
||||
pattern = cmd.resp[1] & 0xFF;
|
||||
else
|
||||
pattern = cmd.resp[0] & 0xFF;
|
||||
|
||||
if (pattern != 0xAA)
|
||||
return -RT_ERROR;
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
rt_err_t mmcsd_get_card_addr(struct rt_mmcsd_host *host, rt_uint32_t *rca)
|
||||
{
|
||||
rt_err_t err;
|
||||
struct rt_mmcsd_cmd cmd;
|
||||
|
||||
rt_memset(&cmd, 0, sizeof(struct rt_mmcsd_cmd));
|
||||
|
||||
cmd.cmd_code = SD_SEND_RELATIVE_ADDR;
|
||||
cmd.arg = 0;
|
||||
cmd.flags = RESP_R6 | CMD_BCR;
|
||||
|
||||
err = mmcsd_send_cmd(host, &cmd, 3);
|
||||
if (err)
|
||||
return err;
|
||||
|
||||
*rca = cmd.resp[0] >> 16;
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
#define be32_to_cpu(x) ((rt_uint32_t)( \
|
||||
(((rt_uint32_t)(x) & (rt_uint32_t)0x000000ffUL) << 24) | \
|
||||
(((rt_uint32_t)(x) & (rt_uint32_t)0x0000ff00UL) << 8) | \
|
||||
(((rt_uint32_t)(x) & (rt_uint32_t)0x00ff0000UL) >> 8) | \
|
||||
(((rt_uint32_t)(x) & (rt_uint32_t)0xff000000UL) >> 24)))
|
||||
|
||||
rt_int32_t mmcsd_get_scr(struct rt_mmcsd_card *card, rt_uint32_t *scr)
|
||||
{
|
||||
rt_int32_t err;
|
||||
struct rt_mmcsd_req req;
|
||||
struct rt_mmcsd_cmd cmd;
|
||||
struct rt_mmcsd_data data;
|
||||
|
||||
err = mmcsd_app_cmd(card->host, card);
|
||||
if (err)
|
||||
return err;
|
||||
|
||||
rt_memset(&req, 0, sizeof(struct rt_mmcsd_req));
|
||||
rt_memset(&cmd, 0, sizeof(struct rt_mmcsd_cmd));
|
||||
rt_memset(&data, 0, sizeof(struct rt_mmcsd_data));
|
||||
|
||||
req.cmd = &cmd;
|
||||
req.data = &data;
|
||||
|
||||
cmd.cmd_code = SD_APP_SEND_SCR;
|
||||
cmd.arg = 0;
|
||||
cmd.flags = RESP_SPI_R1 | RESP_R1 | CMD_ADTC;
|
||||
|
||||
data.blksize = 8;
|
||||
data.blks = 1;
|
||||
data.flags = DATA_DIR_READ;
|
||||
data.buf = scr;
|
||||
|
||||
mmcsd_set_data_timeout(&data, card);
|
||||
|
||||
mmcsd_send_request(card->host, &req);
|
||||
|
||||
if (cmd.err)
|
||||
return cmd.err;
|
||||
if (data.err)
|
||||
return data.err;
|
||||
|
||||
scr[0] = be32_to_cpu(scr[0]);
|
||||
scr[1] = be32_to_cpu(scr[1]);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static rt_int32_t mmcsd_sd_init_card(struct rt_mmcsd_host *host,
|
||||
rt_uint32_t ocr)
|
||||
{
|
||||
struct rt_mmcsd_card *card;
|
||||
rt_int32_t err;
|
||||
rt_uint32_t resp[4];
|
||||
rt_uint32_t max_data_rate;
|
||||
|
||||
mmcsd_go_idle(host);
|
||||
|
||||
/*
|
||||
* If SD_SEND_IF_COND indicates an SD 2.0
|
||||
* compliant card and we should set bit 30
|
||||
* of the ocr to indicate that we can handle
|
||||
* block-addressed SDHC cards.
|
||||
*/
|
||||
err = mmcsd_send_if_cond(host, ocr);
|
||||
if (!err)
|
||||
ocr |= 1 << 30;
|
||||
|
||||
err = mmcsd_send_app_op_cond(host, ocr, RT_NULL);
|
||||
if (err)
|
||||
goto err;
|
||||
|
||||
if (controller_is_spi(host))
|
||||
err = mmcsd_get_cid(host, resp);
|
||||
else
|
||||
err = mmcsd_all_get_cid(host, resp);
|
||||
if (err)
|
||||
goto err;
|
||||
|
||||
card = rt_malloc(sizeof(struct rt_mmcsd_card));
|
||||
if (!card)
|
||||
{
|
||||
LOG_E("malloc card failed!");
|
||||
err = -RT_ENOMEM;
|
||||
goto err;
|
||||
}
|
||||
rt_memset(card, 0, sizeof(struct rt_mmcsd_card));
|
||||
|
||||
card->card_type = CARD_TYPE_SD;
|
||||
card->host = host;
|
||||
rt_memcpy(card->resp_cid, resp, sizeof(card->resp_cid));
|
||||
|
||||
/*
|
||||
* For native busses: get card RCA and quit open drain mode.
|
||||
*/
|
||||
if (!controller_is_spi(host))
|
||||
{
|
||||
err = mmcsd_get_card_addr(host, &card->rca);
|
||||
if (err)
|
||||
goto err1;
|
||||
|
||||
mmcsd_set_bus_mode(host, MMCSD_BUSMODE_PUSHPULL);
|
||||
}
|
||||
|
||||
err = mmcsd_get_csd(card, card->resp_csd);
|
||||
if (err)
|
||||
goto err1;
|
||||
|
||||
err = mmcsd_parse_csd(card);
|
||||
if (err)
|
||||
goto err1;
|
||||
|
||||
if (!controller_is_spi(host))
|
||||
{
|
||||
err = mmcsd_select_card(card);
|
||||
if (err)
|
||||
goto err1;
|
||||
}
|
||||
|
||||
err = mmcsd_get_scr(card, card->resp_scr);
|
||||
if (err)
|
||||
goto err1;
|
||||
|
||||
mmcsd_parse_scr(card);
|
||||
|
||||
if (controller_is_spi(host))
|
||||
{
|
||||
err = mmcsd_spi_use_crc(host, 1);
|
||||
if (err)
|
||||
goto err1;
|
||||
}
|
||||
|
||||
/*
|
||||
* change SD card to high-speed, only SD2.0 spec
|
||||
*/
|
||||
err = mmcsd_switch(card);
|
||||
if (err)
|
||||
goto err1;
|
||||
|
||||
/* set bus speed */
|
||||
max_data_rate = (unsigned int)-1;
|
||||
|
||||
if (card->flags & CARD_FLAG_HIGHSPEED)
|
||||
{
|
||||
if (max_data_rate > card->hs_max_data_rate)
|
||||
max_data_rate = card->hs_max_data_rate;
|
||||
}
|
||||
else if (max_data_rate > card->max_data_rate)
|
||||
{
|
||||
max_data_rate = card->max_data_rate;
|
||||
}
|
||||
|
||||
mmcsd_set_clock(host, max_data_rate);
|
||||
|
||||
/*switch bus width*/
|
||||
if ((host->flags & MMCSD_BUSWIDTH_4) &&
|
||||
(card->scr.sd_bus_widths & SD_SCR_BUS_WIDTH_4))
|
||||
{
|
||||
err = mmcsd_app_set_bus_width(card, MMCSD_BUS_WIDTH_4);
|
||||
if (err)
|
||||
goto err1;
|
||||
|
||||
mmcsd_set_bus_width(host, MMCSD_BUS_WIDTH_4);
|
||||
}
|
||||
|
||||
host->card = card;
|
||||
|
||||
return 0;
|
||||
|
||||
err1:
|
||||
rt_free(card);
|
||||
err:
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
/*
|
||||
* Starting point for SD card init.
|
||||
*/
|
||||
rt_int32_t init_sd(struct rt_mmcsd_host *host, rt_uint32_t ocr)
|
||||
{
|
||||
rt_int32_t err;
|
||||
rt_uint32_t current_ocr;
|
||||
/*
|
||||
* We need to get OCR a different way for SPI.
|
||||
*/
|
||||
if (controller_is_spi(host))
|
||||
{
|
||||
mmcsd_go_idle(host);
|
||||
|
||||
err = mmcsd_spi_read_ocr(host, 0, &ocr);
|
||||
if (err)
|
||||
goto err;
|
||||
}
|
||||
|
||||
if (ocr & VDD_165_195)
|
||||
{
|
||||
LOG_I(" SD card claims to support the "
|
||||
"incompletely defined 'low voltage range'. This "
|
||||
"will be ignored.");
|
||||
ocr &= ~VDD_165_195;
|
||||
}
|
||||
|
||||
current_ocr = mmcsd_select_voltage(host, ocr);
|
||||
|
||||
/*
|
||||
* Can we support the voltage(s) of the card(s)?
|
||||
*/
|
||||
if (!current_ocr)
|
||||
{
|
||||
err = -RT_ERROR;
|
||||
goto err;
|
||||
}
|
||||
|
||||
/*
|
||||
* Detect and init the card.
|
||||
*/
|
||||
err = mmcsd_sd_init_card(host, current_ocr);
|
||||
if (err)
|
||||
goto err;
|
||||
|
||||
mmcsd_host_unlock(host);
|
||||
|
||||
err = rt_mmcsd_blk_probe(host->card);
|
||||
if (err)
|
||||
goto remove_card;
|
||||
mmcsd_host_lock(host);
|
||||
|
||||
return 0;
|
||||
|
||||
remove_card:
|
||||
mmcsd_host_lock(host);
|
||||
rt_mmcsd_blk_remove(host->card);
|
||||
rt_free(host->card);
|
||||
host->card = RT_NULL;
|
||||
err:
|
||||
|
||||
LOG_D("init SD card failed!");
|
||||
|
||||
return err;
|
||||
}
|
1412
components/drivers/sdio/sdio.c
Normal file
1412
components/drivers/sdio/sdio.c
Normal file
File diff suppressed because it is too large
Load diff
Loading…
Add table
Add a link
Reference in a new issue