2011-10-23 17:46:20 +08:00
|
|
|
/*
|
2018-10-14 19:37:18 +08:00
|
|
|
* Copyright (c) 2006-2018, RT-Thread Development Team
|
2011-10-23 17:46:20 +08:00
|
|
|
*
|
2018-10-14 19:37:18 +08:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2011-10-23 17:46:20 +08:00
|
|
|
*
|
|
|
|
* Change Logs:
|
2013-06-28 00:36:54 +08:00
|
|
|
* Date Author Notes
|
|
|
|
* 2011-07-25 weety first version
|
2011-10-23 17:46:20 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <rtthread.h>
|
2012-07-08 22:43:08 +08:00
|
|
|
#include <drivers/mmcsd_core.h>
|
|
|
|
#include <drivers/sd.h>
|
2015-06-20 23:23:32 +08:00
|
|
|
#include <drivers/mmc.h>
|
2016-03-01 10:55:16 +08:00
|
|
|
#include <drivers/sdio.h>
|
2011-10-23 17:46:20 +08:00
|
|
|
|
2018-09-05 14:50:43 +08:00
|
|
|
#define DBG_ENABLE
|
|
|
|
#define DBG_SECTION_NAME "[SDIO]"
|
|
|
|
#ifdef RT_SDIO_DEBUG
|
|
|
|
#define DBG_LEVEL DBG_LOG
|
|
|
|
#else
|
|
|
|
#define DBG_LEVEL DBG_INFO
|
|
|
|
#endif /* RT_SDIO_DEBUG */
|
|
|
|
#define DBG_COLOR
|
|
|
|
#include <rtdbg.h>
|
|
|
|
|
2011-10-23 17:46:20 +08:00
|
|
|
#ifndef RT_MMCSD_STACK_SIZE
|
|
|
|
#define RT_MMCSD_STACK_SIZE 1024
|
|
|
|
#endif
|
|
|
|
#ifndef RT_MMCSD_THREAD_PREORITY
|
2012-08-26 19:39:14 +08:00
|
|
|
#if (RT_THREAD_PRIORITY_MAX == 32)
|
|
|
|
#define RT_MMCSD_THREAD_PREORITY 0x16
|
|
|
|
#else
|
2011-10-23 17:46:20 +08:00
|
|
|
#define RT_MMCSD_THREAD_PREORITY 0x40
|
|
|
|
#endif
|
2012-08-26 19:39:14 +08:00
|
|
|
#endif
|
2011-10-23 17:46:20 +08:00
|
|
|
|
|
|
|
//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];
|
2016-05-20 12:20:35 +08:00
|
|
|
static struct rt_mailbox mmcsd_hotpluge_mb;
|
|
|
|
static rt_uint32_t mmcsd_hotpluge_mb_pool[4];
|
2011-10-23 17:46:20 +08:00
|
|
|
|
|
|
|
void mmcsd_host_lock(struct rt_mmcsd_host *host)
|
|
|
|
{
|
2017-07-04 00:50:25 +08:00
|
|
|
rt_mutex_take(&host->bus_lock, RT_WAITING_FOREVER);
|
2011-10-23 17:46:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void mmcsd_host_unlock(struct rt_mmcsd_host *host)
|
|
|
|
{
|
2017-07-04 00:50:25 +08:00
|
|
|
rt_mutex_release(&host->bus_lock);
|
2011-10-23 17:46:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void mmcsd_req_complete(struct rt_mmcsd_host *host)
|
|
|
|
{
|
2013-06-28 00:36:54 +08:00
|
|
|
rt_sem_release(&host->sem_ack);
|
2011-10-23 17:46:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void mmcsd_send_request(struct rt_mmcsd_host *host, struct rt_mmcsd_req *req)
|
|
|
|
{
|
2015-06-20 23:23:32 +08:00
|
|
|
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);
|
|
|
|
|
2015-06-22 21:28:42 +08:00
|
|
|
} while(req->cmd->err && (req->cmd->retries > 0));
|
2015-06-20 23:23:32 +08:00
|
|
|
|
|
|
|
|
2011-10-23 17:46:20 +08:00
|
|
|
}
|
|
|
|
|
2013-06-28 00:36:54 +08:00
|
|
|
rt_int32_t mmcsd_send_cmd(struct rt_mmcsd_host *host,
|
|
|
|
struct rt_mmcsd_cmd *cmd,
|
|
|
|
int retries)
|
2011-10-23 17:46:20 +08:00
|
|
|
{
|
2013-06-28 00:36:54 +08:00
|
|
|
struct rt_mmcsd_req req;
|
2011-10-23 17:46:20 +08:00
|
|
|
|
2013-06-28 00:36:54 +08:00
|
|
|
rt_memset(&req, 0, sizeof(struct rt_mmcsd_req));
|
|
|
|
rt_memset(cmd->resp, 0, sizeof(cmd->resp));
|
2015-06-20 23:23:32 +08:00
|
|
|
cmd->retries = retries;
|
2011-10-23 17:46:20 +08:00
|
|
|
|
2013-06-28 00:36:54 +08:00
|
|
|
req.cmd = cmd;
|
|
|
|
cmd->data = RT_NULL;
|
2011-10-23 17:46:20 +08:00
|
|
|
|
2013-06-28 00:36:54 +08:00
|
|
|
mmcsd_send_request(host, &req);
|
2011-10-23 17:46:20 +08:00
|
|
|
|
2013-06-28 00:36:54 +08:00
|
|
|
return cmd->err;
|
2011-10-23 17:46:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
rt_int32_t mmcsd_go_idle(struct rt_mmcsd_host *host)
|
|
|
|
{
|
2013-06-28 00:36:54 +08:00
|
|
|
rt_int32_t err;
|
|
|
|
struct rt_mmcsd_cmd cmd;
|
2011-10-23 17:46:20 +08:00
|
|
|
|
2013-06-28 00:36:54 +08:00
|
|
|
if (!controller_is_spi(host))
|
|
|
|
{
|
|
|
|
mmcsd_set_chip_select(host, MMCSD_CS_HIGH);
|
|
|
|
mmcsd_delay_ms(1);
|
|
|
|
}
|
2011-10-23 17:46:20 +08:00
|
|
|
|
2013-06-28 00:36:54 +08:00
|
|
|
rt_memset(&cmd, 0, sizeof(struct rt_mmcsd_cmd));
|
2011-10-23 17:46:20 +08:00
|
|
|
|
2013-06-28 00:36:54 +08:00
|
|
|
cmd.cmd_code = GO_IDLE_STATE;
|
|
|
|
cmd.arg = 0;
|
|
|
|
cmd.flags = RESP_SPI_R1 | RESP_NONE | CMD_BC;
|
2011-10-23 17:46:20 +08:00
|
|
|
|
2013-06-28 00:36:54 +08:00
|
|
|
err = mmcsd_send_cmd(host, &cmd, 0);
|
2011-10-23 17:46:20 +08:00
|
|
|
|
2013-06-28 00:36:54 +08:00
|
|
|
mmcsd_delay_ms(1);
|
2011-10-23 17:46:20 +08:00
|
|
|
|
2013-06-28 00:36:54 +08:00
|
|
|
if (!controller_is_spi(host))
|
|
|
|
{
|
|
|
|
mmcsd_set_chip_select(host, MMCSD_CS_IGNORE);
|
|
|
|
mmcsd_delay_ms(1);
|
|
|
|
}
|
2011-10-23 17:46:20 +08:00
|
|
|
|
2013-06-28 00:36:54 +08:00
|
|
|
return err;
|
2011-10-23 17:46:20 +08:00
|
|
|
}
|
|
|
|
|
2013-06-28 00:36:54 +08:00
|
|
|
rt_int32_t mmcsd_spi_read_ocr(struct rt_mmcsd_host *host,
|
|
|
|
rt_int32_t high_capacity,
|
|
|
|
rt_uint32_t *ocr)
|
2011-10-23 17:46:20 +08:00
|
|
|
{
|
2013-06-28 00:36:54 +08:00
|
|
|
struct rt_mmcsd_cmd cmd;
|
|
|
|
rt_int32_t err;
|
2011-10-23 17:46:20 +08:00
|
|
|
|
2013-06-28 00:36:54 +08:00
|
|
|
rt_memset(&cmd, 0, sizeof(struct rt_mmcsd_cmd));
|
2011-10-23 17:46:20 +08:00
|
|
|
|
2013-06-28 00:36:54 +08:00
|
|
|
cmd.cmd_code = SPI_READ_OCR;
|
|
|
|
cmd.arg = high_capacity ? (1 << 30) : 0;
|
|
|
|
cmd.flags = RESP_SPI_R3;
|
2011-10-23 17:46:20 +08:00
|
|
|
|
2013-06-28 00:36:54 +08:00
|
|
|
err = mmcsd_send_cmd(host, &cmd, 0);
|
2011-10-23 17:46:20 +08:00
|
|
|
|
2013-06-28 00:36:54 +08:00
|
|
|
*ocr = cmd.resp[1];
|
2011-10-23 17:46:20 +08:00
|
|
|
|
2013-06-28 00:36:54 +08:00
|
|
|
return err;
|
|
|
|
}
|
2011-10-23 17:46:20 +08:00
|
|
|
|
|
|
|
rt_int32_t mmcsd_all_get_cid(struct rt_mmcsd_host *host, rt_uint32_t *cid)
|
|
|
|
{
|
2013-06-28 00:36:54 +08:00
|
|
|
rt_int32_t err;
|
|
|
|
struct rt_mmcsd_cmd cmd;
|
2011-10-23 17:46:20 +08:00
|
|
|
|
2013-06-28 00:36:54 +08:00
|
|
|
rt_memset(&cmd, 0, sizeof(struct rt_mmcsd_cmd));
|
2011-10-23 17:46:20 +08:00
|
|
|
|
2013-06-28 00:36:54 +08:00
|
|
|
cmd.cmd_code = ALL_SEND_CID;
|
|
|
|
cmd.arg = 0;
|
|
|
|
cmd.flags = RESP_R2 | CMD_BCR;
|
2011-10-23 17:46:20 +08:00
|
|
|
|
2013-06-28 00:36:54 +08:00
|
|
|
err = mmcsd_send_cmd(host, &cmd, 3);
|
|
|
|
if (err)
|
|
|
|
return err;
|
2011-10-23 17:46:20 +08:00
|
|
|
|
2013-06-28 00:36:54 +08:00
|
|
|
rt_memcpy(cid, cmd.resp, sizeof(rt_uint32_t) * 4);
|
2011-10-23 17:46:20 +08:00
|
|
|
|
2013-06-28 00:36:54 +08:00
|
|
|
return 0;
|
2011-10-23 17:46:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
rt_int32_t mmcsd_get_cid(struct rt_mmcsd_host *host, rt_uint32_t *cid)
|
|
|
|
{
|
2013-06-28 00:36:54 +08:00
|
|
|
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)
|
|
|
|
{
|
2018-09-05 14:50:43 +08:00
|
|
|
LOG_E("allocate memory failed!");
|
2013-06-28 00:36:54 +08:00
|
|
|
|
|
|
|
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;
|
2011-10-23 17:46:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
rt_int32_t mmcsd_get_csd(struct rt_mmcsd_card *card, rt_uint32_t *csd)
|
|
|
|
{
|
2013-06-28 00:36:54 +08:00
|
|
|
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)
|
|
|
|
{
|
2018-09-05 14:50:43 +08:00
|
|
|
LOG_E("allocate memory failed!");
|
2013-06-28 00:36:54 +08:00
|
|
|
|
|
|
|
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;
|
2011-10-23 17:46:20 +08:00
|
|
|
}
|
|
|
|
|
2013-06-28 00:36:54 +08:00
|
|
|
static rt_int32_t _mmcsd_select_card(struct rt_mmcsd_host *host,
|
|
|
|
struct rt_mmcsd_card *card)
|
2011-10-23 17:46:20 +08:00
|
|
|
{
|
2013-06-28 00:36:54 +08:00
|
|
|
rt_int32_t err;
|
|
|
|
struct rt_mmcsd_cmd cmd;
|
2011-10-23 17:46:20 +08:00
|
|
|
|
2013-06-28 00:36:54 +08:00
|
|
|
rt_memset(&cmd, 0, sizeof(struct rt_mmcsd_cmd));
|
2011-10-23 17:46:20 +08:00
|
|
|
|
2013-06-28 00:36:54 +08:00
|
|
|
cmd.cmd_code = SELECT_CARD;
|
2011-10-23 17:46:20 +08:00
|
|
|
|
2013-06-28 00:36:54 +08:00
|
|
|
if (card)
|
|
|
|
{
|
|
|
|
cmd.arg = card->rca << 16;
|
|
|
|
cmd.flags = RESP_R1 | CMD_AC;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cmd.arg = 0;
|
|
|
|
cmd.flags = RESP_NONE | CMD_AC;
|
|
|
|
}
|
2011-10-23 17:46:20 +08:00
|
|
|
|
2013-06-28 00:36:54 +08:00
|
|
|
err = mmcsd_send_cmd(host, &cmd, 3);
|
|
|
|
if (err)
|
|
|
|
return err;
|
2011-10-23 17:46:20 +08:00
|
|
|
|
2013-06-28 00:36:54 +08:00
|
|
|
return 0;
|
2011-10-23 17:46:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
rt_int32_t mmcsd_select_card(struct rt_mmcsd_card *card)
|
|
|
|
{
|
2013-06-28 00:36:54 +08:00
|
|
|
return _mmcsd_select_card(card->host, card);
|
2011-10-23 17:46:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
rt_int32_t mmcsd_deselect_cards(struct rt_mmcsd_card *card)
|
|
|
|
{
|
2013-06-28 00:36:54 +08:00
|
|
|
return _mmcsd_select_card(card->host, RT_NULL);
|
2011-10-23 17:46:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
rt_int32_t mmcsd_spi_use_crc(struct rt_mmcsd_host *host, rt_int32_t use_crc)
|
|
|
|
{
|
2013-06-28 00:36:54 +08:00
|
|
|
struct rt_mmcsd_cmd cmd;
|
|
|
|
rt_int32_t err;
|
2011-10-23 17:46:20 +08:00
|
|
|
|
2013-06-28 00:36:54 +08:00
|
|
|
rt_memset(&cmd, 0, sizeof(struct rt_mmcsd_cmd));
|
2011-10-23 17:46:20 +08:00
|
|
|
|
2013-06-28 00:36:54 +08:00
|
|
|
cmd.cmd_code = SPI_CRC_ON_OFF;
|
|
|
|
cmd.flags = RESP_SPI_R1;
|
|
|
|
cmd.arg = use_crc;
|
2011-10-23 17:46:20 +08:00
|
|
|
|
2013-06-28 00:36:54 +08:00
|
|
|
err = mmcsd_send_cmd(host, &cmd, 0);
|
|
|
|
if (!err)
|
|
|
|
host->spi_use_crc = use_crc;
|
2011-10-23 17:46:20 +08:00
|
|
|
|
2013-06-28 00:36:54 +08:00
|
|
|
return err;
|
|
|
|
}
|
2011-10-23 17:46:20 +08:00
|
|
|
|
|
|
|
rt_inline void mmcsd_set_iocfg(struct rt_mmcsd_host *host)
|
|
|
|
{
|
2013-06-28 00:36:54 +08:00
|
|
|
struct rt_mmcsd_io_cfg *io_cfg = &host->io_cfg;
|
2011-10-23 17:46:20 +08:00
|
|
|
|
2013-06-28 00:36:54 +08:00
|
|
|
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);
|
2011-10-23 17:46:20 +08:00
|
|
|
|
2013-06-28 00:36:54 +08:00
|
|
|
host->ops->set_iocfg(host, io_cfg);
|
2011-10-23 17:46:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Control chip select pin on a host.
|
|
|
|
*/
|
|
|
|
void mmcsd_set_chip_select(struct rt_mmcsd_host *host, rt_int32_t mode)
|
|
|
|
{
|
2013-06-28 00:36:54 +08:00
|
|
|
host->io_cfg.chip_select = mode;
|
|
|
|
mmcsd_set_iocfg(host);
|
2011-10-23 17:46:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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)
|
|
|
|
{
|
2013-06-28 00:36:54 +08:00
|
|
|
if (clk < host->freq_min)
|
|
|
|
{
|
2018-09-05 14:50:43 +08:00
|
|
|
LOG_W("clock too low!");
|
2013-06-28 00:36:54 +08:00
|
|
|
}
|
2011-10-23 17:46:20 +08:00
|
|
|
|
2013-06-28 00:36:54 +08:00
|
|
|
host->io_cfg.clock = clk;
|
|
|
|
mmcsd_set_iocfg(host);
|
2011-10-23 17:46:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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)
|
|
|
|
{
|
2013-06-28 00:36:54 +08:00
|
|
|
host->io_cfg.bus_mode = mode;
|
|
|
|
mmcsd_set_iocfg(host);
|
2011-10-23 17:46:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Change data bus width of a host.
|
|
|
|
*/
|
|
|
|
void mmcsd_set_bus_width(struct rt_mmcsd_host *host, rt_uint32_t width)
|
|
|
|
{
|
2013-06-28 00:36:54 +08:00
|
|
|
host->io_cfg.bus_width = width;
|
|
|
|
mmcsd_set_iocfg(host);
|
2011-10-23 17:46:20 +08:00
|
|
|
}
|
|
|
|
|
2013-06-28 00:36:54 +08:00
|
|
|
void mmcsd_set_data_timeout(struct rt_mmcsd_data *data,
|
|
|
|
const struct rt_mmcsd_card *card)
|
2011-10-23 17:46:20 +08:00
|
|
|
{
|
2013-06-28 00:36:54 +08:00
|
|
|
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 */
|
|
|
|
}
|
|
|
|
}
|
2011-10-23 17:46:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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)
|
|
|
|
{
|
2013-06-28 00:36:54 +08:00
|
|
|
int bit;
|
2016-03-01 10:55:16 +08:00
|
|
|
extern int __rt_ffs(int value);
|
2011-10-23 17:46:20 +08:00
|
|
|
|
2013-06-28 00:36:54 +08:00
|
|
|
ocr &= host->valid_ocr;
|
2011-10-23 17:46:20 +08:00
|
|
|
|
2013-06-28 00:36:54 +08:00
|
|
|
bit = __rt_ffs(ocr);
|
|
|
|
if (bit)
|
|
|
|
{
|
|
|
|
bit -= 1;
|
2011-10-23 17:46:20 +08:00
|
|
|
|
2013-06-28 00:36:54 +08:00
|
|
|
ocr &= 3 << bit;
|
2011-10-23 17:46:20 +08:00
|
|
|
|
2013-06-28 00:36:54 +08:00
|
|
|
host->io_cfg.vdd = bit;
|
|
|
|
mmcsd_set_iocfg(host);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-09-05 14:50:43 +08:00
|
|
|
LOG_W("host doesn't support card's voltages!");
|
2013-06-28 00:36:54 +08:00
|
|
|
ocr = 0;
|
|
|
|
}
|
2011-10-23 17:46:20 +08:00
|
|
|
|
2013-06-28 00:36:54 +08:00
|
|
|
return ocr;
|
2011-10-23 17:46:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void mmcsd_power_up(struct rt_mmcsd_host *host)
|
|
|
|
{
|
2018-03-04 17:25:59 +08:00
|
|
|
int bit = __rt_fls(host->valid_ocr) - 1;
|
2013-06-28 00:36:54 +08:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
mmcsd_delay_ms(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.
|
|
|
|
*/
|
|
|
|
mmcsd_delay_ms(10);
|
2011-10-23 17:46:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void mmcsd_power_off(struct rt_mmcsd_host *host)
|
|
|
|
{
|
2013-06-28 00:36:54 +08:00
|
|
|
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);
|
2011-10-23 17:46:20 +08:00
|
|
|
}
|
|
|
|
|
2016-05-20 12:20:35 +08:00
|
|
|
int mmcsd_wait_cd_changed(rt_int32_t timeout)
|
|
|
|
{
|
|
|
|
struct rt_mmcsd_host *host;
|
|
|
|
if (rt_mb_recv(&mmcsd_hotpluge_mb, (rt_uint32_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);
|
|
|
|
|
2011-10-23 17:46:20 +08:00
|
|
|
void mmcsd_change(struct rt_mmcsd_host *host)
|
|
|
|
{
|
2013-06-28 00:36:54 +08:00
|
|
|
rt_mb_send(&mmcsd_detect_mb, (rt_uint32_t)host);
|
2011-10-23 17:46:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void mmcsd_detect(void *param)
|
|
|
|
{
|
2013-06-28 00:36:54 +08:00
|
|
|
struct rt_mmcsd_host *host;
|
|
|
|
rt_uint32_t ocr;
|
|
|
|
rt_int32_t err;
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
if (rt_mb_recv(&mmcsd_detect_mb, (rt_uint32_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);
|
2017-12-07 14:41:09 +08:00
|
|
|
rt_mb_send(&mmcsd_hotpluge_mb, (rt_uint32_t)host);
|
2013-06-28 00:36:54 +08:00
|
|
|
continue;
|
|
|
|
}
|
2015-06-20 23:23:32 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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);
|
2016-05-20 12:20:35 +08:00
|
|
|
rt_mb_send(&mmcsd_hotpluge_mb, (rt_uint32_t)host);
|
2015-06-20 23:23:32 +08:00
|
|
|
continue;
|
|
|
|
}
|
2013-06-28 00:36:54 +08:00
|
|
|
mmcsd_host_unlock(host);
|
|
|
|
}
|
2016-04-05 11:01:49 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* card removed */
|
|
|
|
mmcsd_host_lock(host);
|
|
|
|
if (host->card->sdio_function_num != 0)
|
|
|
|
{
|
2018-09-05 14:50:43 +08:00
|
|
|
LOG_W("unsupport sdio card plug out!");
|
2016-04-05 11:01:49 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rt_mmcsd_blk_remove(host->card);
|
|
|
|
rt_free(host->card);
|
|
|
|
|
|
|
|
host->card = RT_NULL;
|
|
|
|
}
|
|
|
|
mmcsd_host_unlock(host);
|
2016-05-20 12:20:35 +08:00
|
|
|
rt_mb_send(&mmcsd_hotpluge_mb, (rt_uint32_t)host);
|
2016-04-05 11:01:49 +08:00
|
|
|
}
|
2013-06-28 00:36:54 +08:00
|
|
|
}
|
|
|
|
}
|
2011-10-23 17:46:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
struct rt_mmcsd_host *mmcsd_alloc_host(void)
|
|
|
|
{
|
2013-06-28 00:36:54 +08:00
|
|
|
struct rt_mmcsd_host *host;
|
|
|
|
|
|
|
|
host = rt_malloc(sizeof(struct rt_mmcsd_host));
|
|
|
|
if (!host)
|
|
|
|
{
|
2018-09-05 14:50:43 +08:00
|
|
|
LOG_E("alloc host failed");
|
2011-10-23 17:46:20 +08:00
|
|
|
|
2013-06-28 00:36:54 +08:00
|
|
|
return RT_NULL;
|
|
|
|
}
|
2011-10-23 17:46:20 +08:00
|
|
|
|
2013-06-28 00:36:54 +08:00
|
|
|
rt_memset(host, 0, sizeof(struct rt_mmcsd_host));
|
2011-10-23 17:46:20 +08:00
|
|
|
|
2013-06-28 00:36:54 +08:00
|
|
|
host->max_seg_size = 65535;
|
|
|
|
host->max_dma_segs = 1;
|
|
|
|
host->max_blk_size = 512;
|
|
|
|
host->max_blk_count = 4096;
|
2012-04-08 23:20:36 +08:00
|
|
|
|
2017-07-04 00:50:25 +08:00
|
|
|
rt_mutex_init(&host->bus_lock, "sd_bus_lock", RT_IPC_FLAG_FIFO);
|
2013-06-28 00:36:54 +08:00
|
|
|
rt_sem_init(&host->sem_ack, "sd_ack", 0, RT_IPC_FLAG_FIFO);
|
2011-10-23 17:46:20 +08:00
|
|
|
|
2013-06-28 00:36:54 +08:00
|
|
|
return host;
|
2011-10-23 17:46:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void mmcsd_free_host(struct rt_mmcsd_host *host)
|
|
|
|
{
|
2017-07-04 00:50:25 +08:00
|
|
|
rt_mutex_detach(&host->bus_lock);
|
2013-06-28 00:36:54 +08:00
|
|
|
rt_sem_detach(&host->sem_ack);
|
|
|
|
rt_free(host);
|
2011-10-23 17:46:20 +08:00
|
|
|
}
|
|
|
|
|
2017-11-05 21:41:43 +08:00
|
|
|
int rt_mmcsd_core_init(void)
|
2011-10-23 17:46:20 +08:00
|
|
|
{
|
2013-06-28 00:36:54 +08:00
|
|
|
rt_err_t ret;
|
|
|
|
|
2016-04-05 11:01:49 +08:00
|
|
|
/* initialize detect SD cart thread */
|
|
|
|
/* initialize mailbox and create detect SD card thread */
|
2013-06-28 00:36:54 +08:00
|
|
|
ret = rt_mb_init(&mmcsd_detect_mb, "mmcsdmb",
|
2016-05-20 12:20:35 +08:00
|
|
|
&mmcsd_detect_mb_pool[0], sizeof(mmcsd_detect_mb_pool) / sizeof(mmcsd_detect_mb_pool[0]),
|
2013-06-28 00:36:54 +08:00
|
|
|
RT_IPC_FLAG_FIFO);
|
|
|
|
RT_ASSERT(ret == RT_EOK);
|
|
|
|
|
2016-05-20 12:20:35 +08:00
|
|
|
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,
|
2013-06-28 00:36:54 +08:00
|
|
|
&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();
|
2016-04-05 11:01:49 +08:00
|
|
|
|
2017-11-06 20:11:25 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2017-11-05 21:41:43 +08:00
|
|
|
INIT_PREV_EXPORT(rt_mmcsd_core_init);
|
|
|
|
|