blob: fed76758c68991e33b9b961def830744022e458b [file] [log] [blame]
From 475f064022a716bda4574406d8365627c3bcf131 Mon Sep 17 00:00:00 2001
From: Sam Shih <sam.shih@mediatek.com>
Date: Fri, 2 Jun 2023 13:06:19 +0800
Subject: [PATCH]
[spi-and-storage][999-2366-fix-SPIM-dma-buffer-not-aligned.patch]
---
drivers/spi/spi-mt65xx.c | 33 +++++++++++++++++++++++----------
1 file changed, 23 insertions(+), 10 deletions(-)
diff --git a/drivers/spi/spi-mt65xx.c b/drivers/spi/spi-mt65xx.c
index 05697e1d1..2883dc908 100644
--- a/drivers/spi/spi-mt65xx.c
+++ b/drivers/spi/spi-mt65xx.c
@@ -184,7 +184,7 @@ static const struct mtk_spi_compatible mt8183_compat = {
*/
static const struct mtk_chip_config mtk_default_chip_info = {
.sample_sel = 0,
- .get_tick_dly = 0,
+ .get_tick_dly = 1,
};
static const struct of_device_id mtk_spi_of_match[] = {
@@ -731,8 +731,11 @@ static int mtk_spi_mem_adjust_op_size(struct spi_mem *mem,
if (op->data.dir != SPI_MEM_NO_DATA) {
opcode_len = 1 + op->addr.nbytes + op->dummy.nbytes;
- if (opcode_len + op->data.nbytes > MTK_SPI_IPM_PACKET_SIZE)
+ if (opcode_len + op->data.nbytes > MTK_SPI_IPM_PACKET_SIZE) {
op->data.nbytes = MTK_SPI_IPM_PACKET_SIZE -opcode_len;
+ /* force data buffer dma-aligned. */
+ op->data.nbytes -= op->data.nbytes % 4;
+ }
}
return 0;
@@ -759,10 +762,6 @@ static bool mtk_spi_mem_supports_op(struct spi_mem *mem,
return false;
}
- if (op->data.dir == SPI_MEM_DATA_IN &&
- !IS_ALIGNED((size_t)op->data.buf.in, 4))
- return false;
-
return true;
}
@@ -821,6 +820,7 @@ static int mtk_spi_mem_exec_op(struct spi_mem *mem,
struct mtk_spi *mdata = spi_master_get_devdata(mem->spi->master);
u32 reg_val, nio = 1, tx_size;
char *tx_tmp_buf;
+ char *rx_tmp_buf;
int ret = 0;
mdata->use_spimem = true;
@@ -915,10 +915,18 @@ static int mtk_spi_mem_exec_op(struct spi_mem *mem,
}
if (op->data.dir == SPI_MEM_DATA_IN) {
+ if(!IS_ALIGNED((size_t)op->data.buf.in, 4)) {
+ rx_tmp_buf = kzalloc(op->data.nbytes, GFP_KERNEL | GFP_DMA);
+ if (!rx_tmp_buf)
+ return -ENOMEM;
+ }
+ else
+ rx_tmp_buf = op->data.buf.in;
+
mdata->rx_dma = dma_map_single(mdata->dev,
- op->data.buf.in,
- op->data.nbytes,
- DMA_FROM_DEVICE);
+ rx_tmp_buf,
+ op->data.nbytes,
+ DMA_FROM_DEVICE);
if (dma_mapping_error(mdata->dev, mdata->rx_dma)) {
ret = -ENOMEM;
goto unmap_tx_dma;
@@ -948,9 +956,14 @@ static int mtk_spi_mem_exec_op(struct spi_mem *mem,
writel(reg_val, mdata->base + SPI_CMD_REG);
unmap_rx_dma:
- if (op->data.dir == SPI_MEM_DATA_IN)
+ if (op->data.dir == SPI_MEM_DATA_IN) {
+ if(!IS_ALIGNED((size_t)op->data.buf.in, 4)) {
+ memcpy(op->data.buf.in, rx_tmp_buf, op->data.nbytes);
+ kfree(rx_tmp_buf);
+ }
dma_unmap_single(mdata->dev, mdata->rx_dma,
op->data.nbytes, DMA_FROM_DEVICE);
+ }
unmap_tx_dma:
dma_unmap_single(mdata->dev, mdata->tx_dma,
tx_size, DMA_TO_DEVICE);
--
2.34.1