Skip to content

Commit

Permalink
mmc: meson-gx: use an intermediate buffer for dram_access_quirk
Browse files Browse the repository at this point in the history
Signed-off-by: Neil Armstrong <narmstrong@baylibre.com>
  • Loading branch information
superna9999 committed Nov 2, 2021
1 parent 3906fe9 commit 832f662
Showing 1 changed file with 24 additions and 77 deletions.
101 changes: 24 additions & 77 deletions drivers/mmc/host/meson-gx-mmc.c
Expand Up @@ -747,50 +747,29 @@ static void meson_mmc_desc_chain_transfer(struct mmc_host *mmc, u32 cmd_cfg)
}

/* local sg copy for dram_access_quirk */
static void meson_mmc_copy_buffer(struct meson_host *host, struct mmc_data *data,
size_t buflen, bool to_buffer)
static void meson_mmc_copy_buffer(struct meson_host *host, size_t buflen, bool to_buffer)
{
unsigned int sg_flags = SG_MITER_ATOMIC;
struct scatterlist *sgl = data->sg;
unsigned int nents = data->sg_len;
struct sg_mapping_iter miter;
unsigned int offset = 0;

if (to_buffer)
sg_flags |= SG_MITER_FROM_SG;
else
sg_flags |= SG_MITER_TO_SG;

sg_miter_start(&miter, sgl, nents, sg_flags);
unsigned int buf_offset = 0;
unsigned int left;
u32 *buf = host->bounce_buf;

while ((offset < buflen) && sg_miter_next(&miter)) {
unsigned int buf_offset = 0;
unsigned int len, left;
u32 *buf = miter.addr;
left = buflen;

len = min(miter.length, buflen - offset);
left = len;
if (to_buffer) {
do {
writel(*buf++, host->bounce_iomem_buf + buf_offset);

if (to_buffer) {
do {
writel(*buf++, host->bounce_iomem_buf + offset + buf_offset);

buf_offset += 4;
left -= 4;
} while (left);
} else {
do {
*buf++ = readl(host->bounce_iomem_buf + offset + buf_offset);

buf_offset += 4;
left -= 4;
} while (left);
}
buf_offset += 4;
left -= 4;
} while (left);
} else {
do {
*buf++ = readl(host->bounce_iomem_buf + buf_offset);

offset += len;
buf_offset += 4;
left -= 4;
} while (left);
}

sg_miter_stop(&miter);
}

static void meson_mmc_start_cmd(struct mmc_host *mmc, struct mmc_command *cmd)
Expand Down Expand Up @@ -836,11 +815,10 @@ static void meson_mmc_start_cmd(struct mmc_host *mmc, struct mmc_command *cmd)
if (data->flags & MMC_DATA_WRITE) {
cmd_cfg |= CMD_CFG_DATA_WR;
WARN_ON(xfer_bytes > host->bounce_buf_size);
sg_copy_to_buffer(data->sg, data->sg_len,
host->bounce_buf, xfer_bytes);
if (host->dram_access_quirk)
meson_mmc_copy_buffer(host, data, xfer_bytes, true);
else
sg_copy_to_buffer(data->sg, data->sg_len,
host->bounce_buf, xfer_bytes);
meson_mmc_copy_buffer(host, xfer_bytes, true);
dma_wmb();
}

Expand All @@ -859,43 +837,12 @@ static void meson_mmc_start_cmd(struct mmc_host *mmc, struct mmc_command *cmd)
writel(cmd->arg, host->regs + SD_EMMC_CMD_ARG);
}

static int meson_mmc_validate_dram_access(struct mmc_host *mmc, struct mmc_data *data)
{
struct scatterlist *sg;
int i;

/* Reject request if any element offset or size is not 32bit aligned */
for_each_sg(data->sg, sg, data->sg_len, i) {
if (!IS_ALIGNED(sg->offset, sizeof(u32)) ||
!IS_ALIGNED(sg->length, sizeof(u32))) {
dev_err(mmc_dev(mmc), "unaligned sg offset %u len %u\n",
data->sg->offset, data->sg->length);
return -EINVAL;
}
}

return 0;
}

static void meson_mmc_request(struct mmc_host *mmc, struct mmc_request *mrq)
{
struct meson_host *host = mmc_priv(mmc);
bool needs_pre_post_req = mrq->data &&
!(mrq->data->host_cookie & SD_EMMC_PRE_REQ_DONE);

/*
* The memory at the end of the controller used as bounce buffer for
* the dram_access_quirk only accepts 32bit read/write access,
* check the aligment and length of the data before starting the request.
*/
if (host->dram_access_quirk && mrq->data) {
mrq->cmd->error = meson_mmc_validate_dram_access(mmc, mrq->data);
if (mrq->cmd->error) {
mmc_request_done(mmc, mrq);
return;
}
}

if (needs_pre_post_req) {
meson_mmc_get_transfer_mode(mmc, mrq);
if (!meson_mmc_desc_chain_mode(mrq->data))
Expand Down Expand Up @@ -1040,11 +987,10 @@ static irqreturn_t meson_mmc_irq_thread(int irq, void *dev_id)
if (meson_mmc_bounce_buf_read(data)) {
xfer_bytes = data->blksz * data->blocks;
WARN_ON(xfer_bytes > host->bounce_buf_size);
sg_copy_from_buffer(data->sg, data->sg_len,
host->bounce_buf, xfer_bytes);
if (host->dram_access_quirk)
meson_mmc_copy_buffer(host, data, xfer_bytes, false);
else
sg_copy_from_buffer(data->sg, data->sg_len,
host->bounce_buf, xfer_bytes);
meson_mmc_copy_buffer(host, xfer_bytes, false);
}

next_cmd = meson_mmc_get_next_command(cmd);
Expand Down Expand Up @@ -1266,6 +1212,7 @@ static int meson_mmc_probe(struct platform_device *pdev)
host->bounce_buf_size = SD_EMMC_SRAM_DATA_BUF_LEN;
host->bounce_iomem_buf = host->regs + SD_EMMC_SRAM_DATA_BUF_OFF;
host->bounce_dma_addr = res->start + SD_EMMC_SRAM_DATA_BUF_OFF;
host->bounce_buf = devm_kmalloc(host->dev, host->bounce_buf_size, GFP_KERNEL);
} else {
/* data bounce buffer */
host->bounce_buf_size = mmc->max_req_size;
Expand Down

0 comments on commit 832f662

Please sign in to comment.