CARPLAY版本整理
This commit is contained in:
@ -0,0 +1,197 @@
|
||||
#include <string.h>
|
||||
|
||||
#include "FreeRTOS.h"
|
||||
#include "trace.h"
|
||||
|
||||
#include "mmcsd_core.h"
|
||||
|
||||
#define SECTOR_SIZE 512
|
||||
|
||||
int32_t mmcsd_num_wr_blocks(struct mmcsd_card *card)
|
||||
{
|
||||
int32_t err;
|
||||
uint32_t blocks;
|
||||
|
||||
struct mmcsd_req req;
|
||||
struct mmcsd_cmd cmd;
|
||||
struct mmcsd_data data;
|
||||
uint32_t timeout_us;
|
||||
|
||||
memset(&cmd, 0, sizeof(struct 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 -1;
|
||||
if (!controller_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
|
||||
return -1;
|
||||
|
||||
memset(&cmd, 0, sizeof(struct mmcsd_cmd));
|
||||
|
||||
cmd.cmd_code = SD_APP_SEND_NUM_WR_BLKS;
|
||||
cmd.arg = 0;
|
||||
cmd.flags = RESP_SPI_R1 | RESP_R1 | CMD_ADTC;
|
||||
|
||||
memset(&data, 0, sizeof(struct 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;
|
||||
|
||||
memset(&req, 0, sizeof(struct mmcsd_req));
|
||||
|
||||
req.cmd = &cmd;
|
||||
req.data = &data;
|
||||
|
||||
mmcsd_send_request(card->host, &req);
|
||||
|
||||
if (cmd.err || data.err)
|
||||
return -1;
|
||||
|
||||
return blocks;
|
||||
}
|
||||
|
||||
int mmcsd_req_blk(struct mmcsd_card *card,
|
||||
uint32_t sector,
|
||||
void *buf,
|
||||
size_t blks,
|
||||
uint8_t dir)
|
||||
{
|
||||
struct mmcsd_cmd cmd, stop;
|
||||
struct mmcsd_data data;
|
||||
struct mmcsd_req req;
|
||||
struct mmcsd_host *host = card->host;
|
||||
uint32_t r_cmd, w_cmd;
|
||||
|
||||
mmcsd_host_lock(host);
|
||||
memset(&req, 0, sizeof(struct mmcsd_req));
|
||||
memset(&cmd, 0, sizeof(struct mmcsd_cmd));
|
||||
memset(&stop, 0, sizeof(struct mmcsd_cmd));
|
||||
memset(&data, 0, sizeof(struct 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 = NULL;
|
||||
r_cmd = READ_SINGLE_BLOCK;
|
||||
w_cmd = WRITE_BLOCK;
|
||||
}
|
||||
|
||||
if (!dir)
|
||||
{
|
||||
cmd.cmd_code = r_cmd;
|
||||
data.flags |= DATA_DIR_READ;
|
||||
}
|
||||
else
|
||||
{
|
||||
cmd.cmd_code = w_cmd;
|
||||
data.flags |= DATA_DIR_WRITE;
|
||||
}
|
||||
|
||||
mmcsd_set_data_timeout(&data, card);
|
||||
data.buf = buf;
|
||||
mmcsd_send_request(host, &req);
|
||||
|
||||
if (!controller_is_spi(card->host) && dir != 0)
|
||||
{
|
||||
do
|
||||
{
|
||||
int32_t err;
|
||||
|
||||
cmd.cmd_code = SEND_STATUS;
|
||||
cmd.arg = card->rca << 16;
|
||||
cmd.flags = RESP_R1 | CMD_AC;
|
||||
err = mmcsd_send_cmd(card->host, &cmd, 5);
|
||||
if (err)
|
||||
{
|
||||
TRACE_ERROR("error %d requesting status", err);
|
||||
break;
|
||||
}
|
||||
/*
|
||||
* Some cards mishandle the status bits,
|
||||
* so make sure to check both the busy
|
||||
* indication and the card state.
|
||||
*/
|
||||
} while (!(cmd.resp[0] & R1_READY_FOR_DATA) ||
|
||||
(R1_CURRENT_STATE(cmd.resp[0]) == 7));
|
||||
}
|
||||
|
||||
mmcsd_host_unlock(host);
|
||||
|
||||
if (cmd.err || data.err || stop.err)
|
||||
{
|
||||
TRACE_ERROR("mmcsd request blocks error");
|
||||
TRACE_ERROR("%d,%d,%d, 0x%08x,0x%08x",
|
||||
cmd.err, data.err, stop.err, data.flags, sector);
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int32_t mmcsd_set_blksize(struct mmcsd_card *card)
|
||||
{
|
||||
struct 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)
|
||||
{
|
||||
TRACE_ERROR("MMCSD: unable to set block size to %d: %d", cmd.arg, err);
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
@ -0,0 +1,575 @@
|
||||
#include <string.h>
|
||||
|
||||
#include "FreeRTOS.h"
|
||||
#include "trace.h"
|
||||
#include "errno.h"
|
||||
|
||||
#include "mmcsd_core.h"
|
||||
#include "mmc.h"
|
||||
|
||||
static const uint32_t tran_unit[] =
|
||||
{
|
||||
10000, 100000, 1000000, 10000000,
|
||||
0, 0, 0, 0
|
||||
};
|
||||
|
||||
static const uint8_t tran_value[] =
|
||||
{
|
||||
0, 10, 12, 13, 15, 20, 25, 30,
|
||||
35, 40, 45, 50, 55, 60, 70, 80,
|
||||
};
|
||||
|
||||
static const uint32_t tacc_uint[] =
|
||||
{
|
||||
1, 10, 100, 1000, 10000, 100000, 1000000, 10000000,
|
||||
};
|
||||
|
||||
static const uint8_t tacc_value[] =
|
||||
{
|
||||
0, 10, 12, 13, 15, 20, 25, 30,
|
||||
35, 40, 45, 50, 55, 60, 70, 80,
|
||||
};
|
||||
|
||||
static inline uint32_t GET_BITS(uint32_t *resp,
|
||||
uint32_t start,
|
||||
uint32_t size)
|
||||
{
|
||||
const int32_t __size = size;
|
||||
const uint32_t __mask = (__size < 32 ? 1 << __size : 0) - 1;
|
||||
const int32_t __off = 3 - ((start) / 32);
|
||||
const int32_t __shft = (start) & 31;
|
||||
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 int32_t mmcsd_parse_csd(struct mmcsd_card *card)
|
||||
{
|
||||
uint32_t a, b;
|
||||
struct mmcsd_csd *csd = &card->csd;
|
||||
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) {
|
||||
TRACE_ERROR("unrecognised CSD structure version %d!", csd->csd_structure);
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
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_blknr = (csd->c_size + 1) << (csd->c_size_mult + 2);
|
||||
card->card_capacity = card->card_blknr * 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];
|
||||
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 mmcsd_card *card, uint8_t **new_ext_csd)
|
||||
{
|
||||
void *ext_csd;
|
||||
struct mmcsd_req req;
|
||||
struct mmcsd_cmd cmd;
|
||||
struct mmcsd_data data;
|
||||
|
||||
*new_ext_csd = NULL;
|
||||
|
||||
if (GET_BITS(card->resp_cid, 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 = pvPortMalloc(512);
|
||||
if (!ext_csd) {
|
||||
TRACE_ERROR("alloc memory failed when get ext csd!");
|
||||
return -ENOMEM;
|
||||
}
|
||||
|
||||
memset(&req, 0, sizeof(struct mmcsd_req));
|
||||
memset(&cmd, 0, sizeof(struct mmcsd_cmd));
|
||||
memset(&data, 0, sizeof(struct 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 mmcsd_card *card, uint8_t *ext_csd)
|
||||
{
|
||||
uint64_t card_capacity = 0;
|
||||
|
||||
if(card == NULL || ext_csd == NULL)
|
||||
{
|
||||
TRACE_ERROR("emmc parse ext csd fail, invaild args");
|
||||
return -1;
|
||||
}
|
||||
|
||||
card->flags |= CARD_FLAG_HIGHSPEED;
|
||||
card->hs_max_data_rate = 52000000;
|
||||
|
||||
card_capacity = *((uint32_t *)&ext_csd[EXT_CSD_SEC_CNT]);
|
||||
card_capacity *= card->card_blksize;
|
||||
card_capacity >>= 10; /* unit:KB */
|
||||
card->card_capacity = card_capacity;
|
||||
TRACE_INFO("emmc card capacity %d KB.", card->card_capacity);
|
||||
|
||||
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 mmcsd_card *card, uint8_t set,
|
||||
uint8_t index, uint8_t value)
|
||||
{
|
||||
int err;
|
||||
struct mmcsd_host *host = card->host;
|
||||
struct mmcsd_cmd cmd = {0};
|
||||
|
||||
cmd.cmd_code = SWITCH;
|
||||
cmd.arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
|
||||
(index << 16) | (value << 8) | set;
|
||||
cmd.flags = RESP_SPI_R1 | RESP_R1 | CMD_AC;
|
||||
|
||||
err = mmcsd_send_cmd(host, &cmd, 3);
|
||||
if (err)
|
||||
return err;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int mmc_compare_ext_csds(struct mmcsd_card *card,
|
||||
uint8_t *ext_csd, uint32_t bus_width)
|
||||
{
|
||||
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 == NULL) {
|
||||
err = -1;
|
||||
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 = -1;
|
||||
|
||||
out:
|
||||
vPortFree(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 mmcsd_card *card, uint8_t *ext_csd)
|
||||
{
|
||||
uint32_t ext_csd_bits[] = {
|
||||
EXT_CSD_BUS_WIDTH_8,
|
||||
EXT_CSD_BUS_WIDTH_4,
|
||||
EXT_CSD_BUS_WIDTH_1
|
||||
};
|
||||
uint32_t bus_widths[] = {
|
||||
MMCSD_BUS_WIDTH_8,
|
||||
MMCSD_BUS_WIDTH_4,
|
||||
MMCSD_BUS_WIDTH_1
|
||||
};
|
||||
struct mmcsd_host *host = card->host;
|
||||
unsigned idx, bus_width = 0;
|
||||
int err = 0;
|
||||
|
||||
if (GET_BITS(card->resp_cid, 122, 4) < 4)
|
||||
return 0;
|
||||
|
||||
/*
|
||||
* 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(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.
|
||||
*/
|
||||
if ((!(host->flags & MMCSD_BUSWIDTH_8) &&
|
||||
ext_csd_bits[idx] == EXT_CSD_BUS_WIDTH_8) ||
|
||||
(!(host->flags & MMCSD_BUSWIDTH_4) &&
|
||||
(ext_csd_bits[idx] == EXT_CSD_BUS_WIDTH_4 ||
|
||||
ext_csd_bits[idx] == EXT_CSD_BUS_WIDTH_8)))
|
||||
continue;
|
||||
|
||||
err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
|
||||
EXT_CSD_BUS_WIDTH,
|
||||
ext_csd_bits[idx]);
|
||||
if (err)
|
||||
continue;
|
||||
|
||||
bus_width = bus_widths[idx];
|
||||
mmcsd_set_bus_width(host, bus_width);
|
||||
mmcsd_delay_ms(20); //delay 10ms
|
||||
err = mmc_compare_ext_csds(card, ext_csd, bus_width);
|
||||
if (!err) {
|
||||
err = bus_width;
|
||||
break;
|
||||
} else {
|
||||
switch(ext_csd_bits[idx]){
|
||||
case 0:
|
||||
TRACE_ERROR("switch to bus width 1 bit failed!");
|
||||
break;
|
||||
case 1:
|
||||
TRACE_ERROR("switch to bus width 4 bit failed!");
|
||||
break;
|
||||
case 2:
|
||||
TRACE_ERROR("switch to bus width 8 bit failed!");
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
int mmc_send_op_cond(struct mmcsd_host *host,
|
||||
uint32_t ocr, uint32_t *rocr)
|
||||
{
|
||||
struct mmcsd_cmd cmd;
|
||||
uint32_t i;
|
||||
int err = 0;
|
||||
|
||||
memset(&cmd, 0, sizeof(struct 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 = -1;
|
||||
|
||||
mmcsd_delay_ms(10); //delay 10ms
|
||||
}
|
||||
|
||||
if (rocr && !controller_is_spi(host))
|
||||
*rocr = cmd.resp[0];
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
static int mmc_set_card_addr(struct mmcsd_host *host, uint32_t rca)
|
||||
{
|
||||
int err;
|
||||
struct mmcsd_cmd cmd;
|
||||
|
||||
memset(&cmd, 0, sizeof(struct 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 int32_t mmcsd_mmc_init_card(struct mmcsd_host *host,
|
||||
uint32_t ocr)
|
||||
{
|
||||
int32_t err;
|
||||
uint32_t resp[4];
|
||||
uint32_t rocr = 0;
|
||||
uint32_t max_data_rate;
|
||||
uint8_t *ext_csd = NULL;
|
||||
struct mmcsd_card *card = 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 = pvPortMalloc(sizeof(struct mmcsd_card));
|
||||
if (!card)
|
||||
{
|
||||
TRACE_ERROR("malloc card failed!");
|
||||
err = -ENOMEM;
|
||||
goto err;
|
||||
}
|
||||
memset(card, 0, sizeof(struct mmcsd_card));
|
||||
|
||||
card->card_type = CARD_TYPE_MMC;
|
||||
card->host = host;
|
||||
card->rca = 1;
|
||||
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;
|
||||
|
||||
/* set bus speed */
|
||||
if (card->flags & CARD_FLAG_HIGHSPEED)
|
||||
max_data_rate = card->hs_max_data_rate;
|
||||
else
|
||||
max_data_rate = card->max_data_rate;
|
||||
|
||||
mmcsd_set_clock(host, max_data_rate);
|
||||
|
||||
/*switch bus width*/
|
||||
mmc_select_bus_width(card, ext_csd);
|
||||
|
||||
host->card = card;
|
||||
|
||||
vPortFree(ext_csd);
|
||||
return 0;
|
||||
|
||||
err1:
|
||||
vPortFree(card);
|
||||
err:
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
/*
|
||||
* Starting point for mmc card init.
|
||||
*/
|
||||
int32_t init_mmc(struct mmcsd_host *host, uint32_t ocr)
|
||||
{
|
||||
int32_t err;
|
||||
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 = -1;
|
||||
goto err;
|
||||
}
|
||||
|
||||
/*
|
||||
* Detect and init the card.
|
||||
*/
|
||||
err = mmcsd_mmc_init_card(host, current_ocr);
|
||||
if (err)
|
||||
goto err;
|
||||
|
||||
mmcsd_host_unlock(host);
|
||||
|
||||
mmcsd_host_lock(host);
|
||||
|
||||
return 0;
|
||||
err:
|
||||
|
||||
TRACE_ERROR("init MMC card failed!");
|
||||
|
||||
return err;
|
||||
}
|
@ -0,0 +1,771 @@
|
||||
#include "FreeRTOS.h"
|
||||
#include "os_adapt.h"
|
||||
#include "trace.h"
|
||||
#include "errno.h"
|
||||
#include "ff_sddisk.h"
|
||||
|
||||
#include "mmcsd_core.h"
|
||||
#include "sd.h"
|
||||
#include "mmc.h"
|
||||
#include "sdio.h"
|
||||
|
||||
#define MMCSD_STACK_SIZE 2048
|
||||
#define MMCSD_THREAD_PREORITY 0x16
|
||||
|
||||
#define SDMMC_MOUNT_PATH "/sd"
|
||||
|
||||
struct mmcsd_card *sdmmc_cardinfo = NULL;
|
||||
FF_Disk_t *sdmmc_disk;
|
||||
|
||||
static TaskHandle_t mmcsd_detect_thread;
|
||||
static QueueHandle_t mmcsd_detect_mb;
|
||||
static QueueHandle_t mmcsd_hotpluge_mb;
|
||||
static QueueHandle_t mmcsd_sdio_ready_mb;
|
||||
|
||||
static QueueHandle_t mmcsd_mmc_ready_mb;
|
||||
|
||||
void mmcsd_host_lock(struct mmcsd_host *host)
|
||||
{
|
||||
xSemaphoreTake(host->bus_lock, portMAX_DELAY);
|
||||
}
|
||||
|
||||
void mmcsd_host_unlock(struct mmcsd_host *host)
|
||||
{
|
||||
xSemaphoreGive(host->bus_lock);
|
||||
}
|
||||
|
||||
void mmcsd_req_complete(struct mmcsd_host *host)
|
||||
{
|
||||
xSemaphoreGive(host->sem_ack);
|
||||
}
|
||||
|
||||
void mmcsd_send_request(struct mmcsd_host *host, struct 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);
|
||||
|
||||
xSemaphoreTake(host->sem_ack, portMAX_DELAY);
|
||||
|
||||
} while(req->cmd->err && (req->cmd->retries > 0));
|
||||
|
||||
|
||||
}
|
||||
|
||||
int32_t mmcsd_send_cmd(struct mmcsd_host *host,
|
||||
struct mmcsd_cmd *cmd,
|
||||
int retries)
|
||||
{
|
||||
struct mmcsd_req req;
|
||||
|
||||
memset(&req, 0, sizeof(struct mmcsd_req));
|
||||
memset(cmd->resp, 0, sizeof(cmd->resp));
|
||||
cmd->retries = retries;
|
||||
|
||||
req.cmd = cmd;
|
||||
cmd->data = NULL;
|
||||
|
||||
mmcsd_send_request(host, &req);
|
||||
|
||||
return cmd->err;
|
||||
}
|
||||
|
||||
int32_t mmcsd_go_idle(struct mmcsd_host *host)
|
||||
{
|
||||
int32_t err;
|
||||
struct mmcsd_cmd cmd;
|
||||
|
||||
if (!controller_is_spi(host))
|
||||
{
|
||||
mmcsd_set_chip_select(host, MMCSD_CS_HIGH);
|
||||
mmcsd_delay_ms(1);
|
||||
}
|
||||
|
||||
memset(&cmd, 0, sizeof(struct 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);
|
||||
|
||||
mmcsd_delay_ms(1);
|
||||
|
||||
if (!controller_is_spi(host))
|
||||
{
|
||||
mmcsd_set_chip_select(host, MMCSD_CS_IGNORE);
|
||||
mmcsd_delay_ms(1);
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
int32_t mmcsd_spi_read_ocr(struct mmcsd_host *host,
|
||||
int32_t high_capacity,
|
||||
uint32_t *ocr)
|
||||
{
|
||||
struct mmcsd_cmd cmd;
|
||||
int32_t err;
|
||||
|
||||
memset(&cmd, 0, sizeof(struct 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;
|
||||
}
|
||||
|
||||
int32_t mmcsd_all_get_cid(struct mmcsd_host *host, uint32_t *cid)
|
||||
{
|
||||
int32_t err;
|
||||
struct mmcsd_cmd cmd;
|
||||
|
||||
memset(&cmd, 0, sizeof(struct 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;
|
||||
|
||||
memcpy(cid, cmd.resp, sizeof(uint32_t) * 4);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int32_t mmcsd_get_cid(struct mmcsd_host *host, uint32_t *cid)
|
||||
{
|
||||
int32_t err, i;
|
||||
struct mmcsd_req req;
|
||||
struct mmcsd_cmd cmd;
|
||||
struct mmcsd_data data;
|
||||
uint32_t *buf = NULL;
|
||||
|
||||
if (!controller_is_spi(host))
|
||||
{
|
||||
if (!host->card)
|
||||
return -1;
|
||||
memset(&cmd, 0, sizeof(struct 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;
|
||||
|
||||
memcpy(cid, cmd.resp, sizeof(uint32_t) * 4);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
buf = (uint32_t *)pvPortMalloc(16);
|
||||
if (!buf)
|
||||
{
|
||||
TRACE_ERROR("allocate memory failed!");
|
||||
|
||||
return -ENOMEM;
|
||||
}
|
||||
|
||||
memset(&req, 0, sizeof(struct mmcsd_req));
|
||||
memset(&cmd, 0, sizeof(struct mmcsd_cmd));
|
||||
memset(&data, 0, sizeof(struct 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)
|
||||
{
|
||||
vPortFree(buf);
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
for (i = 0;i < 4;i++)
|
||||
cid[i] = buf[i];
|
||||
vPortFree(buf);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int32_t mmcsd_get_csd(struct mmcsd_card *card, uint32_t *csd)
|
||||
{
|
||||
int32_t err, i;
|
||||
struct mmcsd_req req;
|
||||
struct mmcsd_cmd cmd;
|
||||
struct mmcsd_data data;
|
||||
uint32_t *buf = NULL;
|
||||
|
||||
if (!controller_is_spi(card->host))
|
||||
{
|
||||
memset(&cmd, 0, sizeof(struct 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;
|
||||
|
||||
memcpy(csd, cmd.resp, sizeof(uint32_t) * 4);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
buf = (uint32_t*)pvPortMalloc(16);
|
||||
if (!buf)
|
||||
{
|
||||
TRACE_ERROR("allocate memory failed!");
|
||||
|
||||
return -ENOMEM;
|
||||
}
|
||||
|
||||
memset(&req, 0, sizeof(struct mmcsd_req));
|
||||
memset(&cmd, 0, sizeof(struct mmcsd_cmd));
|
||||
memset(&data, 0, sizeof(struct 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)
|
||||
{
|
||||
vPortFree(buf);
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
for (i = 0;i < 4;i++)
|
||||
csd[i] = buf[i];
|
||||
vPortFree(buf);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int32_t _mmcsd_select_card(struct mmcsd_host *host,
|
||||
struct mmcsd_card *card)
|
||||
{
|
||||
int32_t err;
|
||||
struct mmcsd_cmd cmd;
|
||||
|
||||
memset(&cmd, 0, sizeof(struct 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;
|
||||
}
|
||||
|
||||
int32_t mmcsd_select_card(struct mmcsd_card *card)
|
||||
{
|
||||
return _mmcsd_select_card(card->host, card);
|
||||
}
|
||||
|
||||
int32_t mmcsd_deselect_cards(struct mmcsd_card *card)
|
||||
{
|
||||
return _mmcsd_select_card(card->host, NULL);
|
||||
}
|
||||
|
||||
int32_t mmcsd_spi_use_crc(struct mmcsd_host *host, int32_t use_crc)
|
||||
{
|
||||
struct mmcsd_cmd cmd;
|
||||
int32_t err;
|
||||
|
||||
memset(&cmd, 0, sizeof(struct 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;
|
||||
}
|
||||
|
||||
static inline void mmcsd_set_iocfg(struct mmcsd_host *host)
|
||||
{
|
||||
struct 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 mmcsd_host *host, 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 mmcsd_host *host, uint32_t clk)
|
||||
{
|
||||
if (clk < host->freq_min)
|
||||
{
|
||||
TRACE_WARNING("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 mmcsd_host *host, 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 mmcsd_host *host, uint32_t width)
|
||||
{
|
||||
host->io_cfg.bus_width = width;
|
||||
mmcsd_set_iocfg(host);
|
||||
}
|
||||
|
||||
void mmcsd_set_data_timeout(struct mmcsd_data *data,
|
||||
const struct mmcsd_card *card)
|
||||
{
|
||||
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)
|
||||
{
|
||||
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
|
||||
*/
|
||||
uint32_t mmcsd_select_voltage(struct mmcsd_host *host, uint32_t ocr)
|
||||
{
|
||||
int bit;
|
||||
|
||||
ocr &= host->valid_ocr;
|
||||
|
||||
bit = ffs(ocr);
|
||||
if (bit)
|
||||
{
|
||||
bit -= 1;
|
||||
|
||||
ocr &= 3 << bit;
|
||||
|
||||
host->io_cfg.vdd = bit;
|
||||
mmcsd_set_iocfg(host);
|
||||
}
|
||||
else
|
||||
{
|
||||
TRACE_WARNING("host doesn't support card's voltages!");
|
||||
ocr = 0;
|
||||
}
|
||||
|
||||
return ocr;
|
||||
}
|
||||
|
||||
static void mmcsd_power_up(struct mmcsd_host *host)
|
||||
{
|
||||
int bit = 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.
|
||||
*/
|
||||
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);
|
||||
}
|
||||
|
||||
static void mmcsd_power_off(struct 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(uint32_t timeout)
|
||||
{
|
||||
struct mmcsd_host *host;
|
||||
if (xQueueReceive(mmcsd_hotpluge_mb, &host, timeout) == pdPASS)
|
||||
{
|
||||
if(host->card == NULL)
|
||||
{
|
||||
return MMCSD_HOST_UNPLUGED;
|
||||
}
|
||||
else
|
||||
{
|
||||
return MMCSD_HOST_PLUGED;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int mmcsd_wait_sdio_ready(int32_t timeout)
|
||||
{
|
||||
struct mmcsd_host *host;
|
||||
if (xQueueReceive(mmcsd_sdio_ready_mb, &host, timeout) == pdPASS)
|
||||
return MMCSD_HOST_PLUGED;
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
int mmcsd_wait_mmc_ready(uint32_t timeout)
|
||||
{
|
||||
struct mmcsd_host *host;
|
||||
if (xQueueReceive(mmcsd_mmc_ready_mb, &host, timeout) == pdPASS)
|
||||
return MMCSD_HOST_PLUGED;
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
void mmcsd_change(struct mmcsd_host *host)
|
||||
{
|
||||
xQueueSend(mmcsd_detect_mb, &host, 0);
|
||||
}
|
||||
|
||||
void mmcsd_change_from_isr(struct mmcsd_host *host)
|
||||
{
|
||||
xQueueSendFromISR(mmcsd_detect_mb, &host, 0);
|
||||
}
|
||||
|
||||
void mmcsd_detect(void *param)
|
||||
{
|
||||
struct mmcsd_host *host;
|
||||
uint32_t ocr;
|
||||
int32_t err;
|
||||
|
||||
while (1)
|
||||
{
|
||||
if (xQueueReceive(mmcsd_detect_mb, &host, portMAX_DELAY) == pdPASS)
|
||||
{
|
||||
if (host->card == NULL)
|
||||
{
|
||||
mmcsd_host_lock(host);
|
||||
mmcsd_power_up(host);
|
||||
mmcsd_go_idle(host);
|
||||
|
||||
mmcsd_send_if_cond(host, host->valid_ocr);
|
||||
|
||||
#if DEVICE_TYPE_SELECT != EMMC_FLASH
|
||||
err = sdio_io_send_op_cond(host, 0, &ocr);
|
||||
if (!err)
|
||||
{
|
||||
if (init_sdio(host, ocr))
|
||||
mmcsd_power_off(host);
|
||||
else
|
||||
xQueueSend(mmcsd_sdio_ready_mb, &host, 0);
|
||||
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);
|
||||
} else {
|
||||
sdmmc_cardinfo = host->card;
|
||||
mmcsd_host_unlock(host);
|
||||
sdmmc_disk = FF_SDDiskInit(SDMMC_MOUNT_PATH);
|
||||
xQueueSend(mmcsd_hotpluge_mb, &host, 0);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
#else
|
||||
/*
|
||||
* detect mmc card
|
||||
*/
|
||||
err = mmc_send_op_cond(host, 0, &ocr);
|
||||
if (!err)
|
||||
{
|
||||
if (init_mmc(host, ocr)) {
|
||||
mmcsd_power_off(host);
|
||||
} else {
|
||||
sdmmc_cardinfo = host->card;
|
||||
mmcsd_host_unlock(host);
|
||||
sdmmc_disk = FF_SDDiskInit(SDMMC_MOUNT_PATH);
|
||||
xQueueSend(mmcsd_mmc_ready_mb, &host, 0);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
#endif
|
||||
mmcsd_host_unlock(host);
|
||||
}
|
||||
else
|
||||
{
|
||||
/* card removed */
|
||||
mmcsd_host_lock(host);
|
||||
if (host->card->sdio_function_num != 0)
|
||||
{
|
||||
TRACE_WARNING("unsupport sdio card plug out!\r\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
if (host->card->card_type == CARD_TYPE_SD || host->card->card_type == CARD_TYPE_MMC) {
|
||||
FF_SDDiskDelete(sdmmc_disk);
|
||||
sdmmc_cardinfo = NULL;
|
||||
}
|
||||
vPortFree(host->card);
|
||||
|
||||
host->card = NULL;
|
||||
}
|
||||
mmcsd_host_unlock(host);
|
||||
xQueueSend(mmcsd_hotpluge_mb, &host, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct mmcsd_host *mmcsd_alloc_host(void)
|
||||
{
|
||||
struct mmcsd_host *host;
|
||||
|
||||
host = pvPortMalloc(sizeof(struct mmcsd_host));
|
||||
if (!host)
|
||||
{
|
||||
TRACE_ERROR("alloc host failed");
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memset(host, 0, sizeof(struct mmcsd_host));
|
||||
|
||||
host->max_seg_size = 65535;
|
||||
host->max_dma_segs = 1;
|
||||
host->max_blk_size = 512;
|
||||
host->max_blk_count = 4096;
|
||||
|
||||
host->bus_lock = xSemaphoreCreateMutex();
|
||||
host->sem_ack= xSemaphoreCreateBinary();
|
||||
|
||||
return host;
|
||||
}
|
||||
|
||||
void mmcsd_free_host(struct mmcsd_host *host)
|
||||
{
|
||||
vSemaphoreDelete(host->bus_lock);
|
||||
vSemaphoreDelete(host->sem_ack);
|
||||
vPortFree(host);
|
||||
}
|
||||
|
||||
int mmcsd_core_init(void)
|
||||
{
|
||||
/* initialize detect SD cart thread */
|
||||
/* initialize mailbox and create detect SD card thread */
|
||||
mmcsd_detect_mb = xQueueCreate(4, sizeof(uint32_t));
|
||||
configASSERT(mmcsd_detect_mb);
|
||||
|
||||
mmcsd_hotpluge_mb = xQueueCreate(4, sizeof(uint32_t));
|
||||
configASSERT(mmcsd_hotpluge_mb);
|
||||
|
||||
mmcsd_sdio_ready_mb = xQueueCreate(4, sizeof(uint32_t));
|
||||
configASSERT(mmcsd_sdio_ready_mb);
|
||||
|
||||
mmcsd_mmc_ready_mb = xQueueCreate(4, sizeof(uint32_t));
|
||||
configASSERT(mmcsd_mmc_ready_mb);
|
||||
|
||||
xTaskCreate(mmcsd_detect, "mmcsd_detect", MMCSD_STACK_SIZE,
|
||||
NULL, MMCSD_THREAD_PREORITY, &mmcsd_detect_thread);
|
||||
#if DEVICE_TYPE_SELECT != EMMC_FLASH
|
||||
sdio_init();
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct mmcsd_card *mmcsd_get_sdmmc_card_info(void)
|
||||
{
|
||||
return sdmmc_cardinfo;
|
||||
}
|
@ -0,0 +1,687 @@
|
||||
#include <string.h>
|
||||
|
||||
#include "FreeRTOS.h"
|
||||
#include "trace.h"
|
||||
#include "errno.h"
|
||||
|
||||
#include "mmcsd_core.h"
|
||||
#include "sd.h"
|
||||
|
||||
static const uint32_t tran_unit[] =
|
||||
{
|
||||
10000, 100000, 1000000, 10000000,
|
||||
0, 0, 0, 0
|
||||
};
|
||||
|
||||
static const uint8_t tran_value[] =
|
||||
{
|
||||
0, 10, 12, 13, 15, 20, 25, 30,
|
||||
35, 40, 45, 50, 55, 60, 70, 80,
|
||||
};
|
||||
|
||||
static const uint32_t tacc_uint[] =
|
||||
{
|
||||
1, 10, 100, 1000, 10000, 100000, 1000000, 10000000,
|
||||
};
|
||||
|
||||
static const uint8_t tacc_value[] =
|
||||
{
|
||||
0, 10, 12, 13, 15, 20, 25, 30,
|
||||
35, 40, 45, 50, 55, 60, 70, 80,
|
||||
};
|
||||
|
||||
static inline uint32_t GET_BITS(uint32_t *resp,
|
||||
uint32_t start,
|
||||
uint32_t size)
|
||||
{
|
||||
const int32_t __size = size;
|
||||
const uint32_t __mask = (__size < 32 ? 1 << __size : 0) - 1;
|
||||
const int32_t __off = 3 - ((start) / 32);
|
||||
const int32_t __shft = (start) & 31;
|
||||
uint32_t __res;
|
||||
|
||||
__res = resp[__off] >> __shft;
|
||||
if (__size + __shft > 32)
|
||||
__res |= resp[__off-1] << ((32 - __shft) % 32);
|
||||
|
||||
return __res & __mask;
|
||||
}
|
||||
|
||||
static int32_t mmcsd_parse_csd(struct mmcsd_card *card)
|
||||
{
|
||||
struct mmcsd_csd *csd = &card->csd;
|
||||
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_blknr = (csd->c_size + 1) << (csd->c_size_mult + 2);
|
||||
card->card_capacity = card->card_blknr * 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_blknr = (csd->c_size + 1) << 10;
|
||||
card->card_blksize = 512;
|
||||
card->card_capacity = (csd->c_size + 1) * 512; /* unit:KB */
|
||||
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:
|
||||
TRACE_ERROR("unrecognised CSD structure version %d!", csd->csd_structure);
|
||||
|
||||
return -1;
|
||||
}
|
||||
TRACE_INFO("SD card capacity %d KB.", card->card_capacity);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int32_t mmcsd_parse_scr(struct mmcsd_card *card)
|
||||
{
|
||||
struct sd_scr *scr = &card->scr;
|
||||
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 int32_t mmcsd_switch(struct mmcsd_card *card)
|
||||
{
|
||||
int32_t err;
|
||||
struct mmcsd_host *host = card->host;
|
||||
struct mmcsd_req req;
|
||||
struct mmcsd_cmd cmd;
|
||||
struct mmcsd_data data;
|
||||
uint8_t *buf;
|
||||
|
||||
buf = (uint8_t*)pvPortMalloc(64);
|
||||
if (!buf)
|
||||
{
|
||||
TRACE_ERROR("alloc memory failed!");
|
||||
|
||||
return -ENOMEM;
|
||||
}
|
||||
|
||||
if (card->card_type != CARD_TYPE_SD)
|
||||
goto err;
|
||||
if (card->scr.sd_version < SCR_SPEC_VER_1)
|
||||
goto err;
|
||||
|
||||
memset(&cmd, 0, sizeof(struct mmcsd_cmd));
|
||||
|
||||
cmd.cmd_code = SD_SWITCH;
|
||||
cmd.arg = 0x00FFFFF1;
|
||||
cmd.flags = RESP_R1 | CMD_ADTC;
|
||||
|
||||
memset(&data, 0, sizeof(struct mmcsd_data));
|
||||
|
||||
mmcsd_set_data_timeout(&data, card);
|
||||
|
||||
data.blksize = 64;
|
||||
data.blks = 1;
|
||||
data.flags = DATA_DIR_READ;
|
||||
data.buf = (uint32_t *)buf;
|
||||
|
||||
memset(&req, 0, sizeof(struct 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;
|
||||
|
||||
memset(&cmd, 0, sizeof(struct mmcsd_cmd));
|
||||
|
||||
cmd.cmd_code = SD_SWITCH;
|
||||
cmd.arg = 0x80FFFFF1;
|
||||
cmd.flags = RESP_R1 | CMD_ADTC;
|
||||
|
||||
memset(&data, 0, sizeof(struct mmcsd_data));
|
||||
|
||||
mmcsd_set_data_timeout(&data, card);
|
||||
|
||||
data.blksize = 64;
|
||||
data.blks = 1;
|
||||
data.flags = DATA_DIR_READ;
|
||||
data.buf = (uint32_t *)buf;
|
||||
|
||||
memset(&req, 0, sizeof(struct 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)
|
||||
{
|
||||
TRACE_INFO("switching card to high speed failed!");
|
||||
goto err;
|
||||
}
|
||||
|
||||
card->flags |= CARD_FLAG_HIGHSPEED;
|
||||
|
||||
err:
|
||||
vPortFree(buf);
|
||||
return 0;
|
||||
|
||||
err1:
|
||||
if (cmd.err)
|
||||
err = cmd.err;
|
||||
if (data.err)
|
||||
err = data.err;
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
static int mmcsd_app_cmd(struct mmcsd_host *host,
|
||||
struct mmcsd_card *card)
|
||||
{
|
||||
int err;
|
||||
struct 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 -1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int mmcsd_send_app_cmd(struct mmcsd_host *host,
|
||||
struct mmcsd_card *card,
|
||||
struct mmcsd_cmd *cmd,
|
||||
int retry)
|
||||
{
|
||||
struct mmcsd_req req;
|
||||
|
||||
uint32_t i;
|
||||
int err;
|
||||
|
||||
err = -1;
|
||||
|
||||
/*
|
||||
* 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++)
|
||||
{
|
||||
memset(&req, 0, sizeof(struct 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;
|
||||
}
|
||||
|
||||
memset(&req, 0, sizeof(struct mmcsd_req));
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
int mmcsd_app_set_bus_width(struct mmcsd_card *card, int32_t width)
|
||||
{
|
||||
int err;
|
||||
struct mmcsd_cmd cmd;
|
||||
|
||||
memset(&cmd, 0, sizeof(struct 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 -1;
|
||||
}
|
||||
|
||||
err = mmcsd_send_app_cmd(card->host, card, &cmd, 3);
|
||||
if (err)
|
||||
return err;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int mmcsd_send_app_op_cond(struct mmcsd_host *host,
|
||||
uint32_t ocr,
|
||||
uint32_t *rocr)
|
||||
{
|
||||
struct mmcsd_cmd cmd;
|
||||
uint32_t i;
|
||||
int err = 0;
|
||||
|
||||
memset(&cmd, 0, sizeof(struct 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 = 100; i; i--)
|
||||
{
|
||||
err = mmcsd_send_app_cmd(host, 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 = -1;
|
||||
|
||||
mmcsd_delay_ms(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.
|
||||
*/
|
||||
int mmcsd_send_if_cond(struct mmcsd_host *host, uint32_t ocr)
|
||||
{
|
||||
struct mmcsd_cmd cmd;
|
||||
int err;
|
||||
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 -1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int mmcsd_get_card_addr(struct mmcsd_host *host, uint32_t *rca)
|
||||
{
|
||||
int err;
|
||||
struct mmcsd_cmd cmd;
|
||||
|
||||
memset(&cmd, 0, sizeof(struct 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 0;
|
||||
}
|
||||
|
||||
#define be32_to_cpu(x) ((uint32_t)( \
|
||||
(((uint32_t)(x) & (uint32_t)0x000000ffUL) << 24) | \
|
||||
(((uint32_t)(x) & (uint32_t)0x0000ff00UL) << 8) | \
|
||||
(((uint32_t)(x) & (uint32_t)0x00ff0000UL) >> 8) | \
|
||||
(((uint32_t)(x) & (uint32_t)0xff000000UL) >> 24)))
|
||||
|
||||
int32_t mmcsd_get_scr(struct mmcsd_card *card, uint32_t *scr)
|
||||
{
|
||||
int32_t err;
|
||||
struct mmcsd_req req;
|
||||
struct mmcsd_cmd cmd;
|
||||
struct mmcsd_data data;
|
||||
|
||||
err = mmcsd_app_cmd(card->host, card);
|
||||
if (err)
|
||||
return err;
|
||||
|
||||
memset(&req, 0, sizeof(struct mmcsd_req));
|
||||
memset(&cmd, 0, sizeof(struct mmcsd_cmd));
|
||||
memset(&data, 0, sizeof(struct 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 int32_t mmcsd_sd_init_card(struct mmcsd_host *host,
|
||||
uint32_t ocr)
|
||||
{
|
||||
struct mmcsd_card *card;
|
||||
int32_t err;
|
||||
uint32_t resp[4];
|
||||
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, 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 = pvPortMalloc(sizeof(struct mmcsd_card));
|
||||
if (!card)
|
||||
{
|
||||
TRACE_ERROR("malloc card failed!");
|
||||
err = -ENOMEM;
|
||||
goto err;
|
||||
}
|
||||
memset(card, 0, sizeof(struct mmcsd_card));
|
||||
|
||||
card->card_type = CARD_TYPE_SD;
|
||||
card->host = host;
|
||||
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:
|
||||
vPortFree(card);
|
||||
err:
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
/*
|
||||
* Starting point for SD card init.
|
||||
*/
|
||||
int32_t init_sd(struct mmcsd_host *host, uint32_t ocr)
|
||||
{
|
||||
int32_t err;
|
||||
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)
|
||||
{
|
||||
TRACE_INFO(" 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 = -1;
|
||||
goto err;
|
||||
}
|
||||
|
||||
/*
|
||||
* Detect and init the card.
|
||||
*/
|
||||
err = mmcsd_sd_init_card(host, current_ocr);
|
||||
if (err)
|
||||
goto err;
|
||||
|
||||
mmcsd_host_unlock(host);
|
||||
|
||||
mmcsd_host_lock(host);
|
||||
|
||||
return 0;
|
||||
err:
|
||||
|
||||
TRACE_DEBUG("init SD card failed!");
|
||||
|
||||
return err;
|
||||
}
|
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user