[PATCH 7/8] spi: mtk_snor: fix zeroed data in DMA read bounce path
David Lechner
dlechner at baylibre.com
Tue Apr 28 18:04:31 CEST 2026
On 4/28/26 10:54 AM, Julien Stephan wrote:
> Le lun. 6 avr. 2026 à 22:14, David Lechner <dlechner at baylibre.com> a écrit :
>>
>> From: Macpaul Lin <macpaul.lin at mediatek.com>
>>
>> Implement proper bounce buffer handling for the read path to fix zeroed
>> data when using DMA. In the bounce path, map the bounce buffer with
>> dma_map_single(), perform DMA using bounce_dma, then copy data from the
>> bounce buffer to the user buffer, and finally unmap with
>> dma_unmap_single().
>>
>> Signed-off-by: Macpaul Lin <macpaul.lin at mediatek.com>
>> Signed-off-by: David Lechner <dlechner at baylibre.com>
>> ---
>> drivers/spi/mtk_snor.c | 17 ++++++++++++++---
>> 1 file changed, 14 insertions(+), 3 deletions(-)
>>
>> diff --git a/drivers/spi/mtk_snor.c b/drivers/spi/mtk_snor.c
>> index 649bca5716c..21ac115e3c3 100644
>> --- a/drivers/spi/mtk_snor.c
>> +++ b/drivers/spi/mtk_snor.c
>> @@ -274,6 +274,7 @@ static int mtk_snor_read_bounce(struct mtk_snor_priv *priv,
>> {
>> unsigned int rdlen;
>> int ret;
>> + dma_addr_t bounce_dma;
>>
>> if (op->data.nbytes & MTK_NOR_DMA_ALIGN_MASK)
>> rdlen = (op->data.nbytes + MTK_NOR_DMA_ALIGN) &
>> @@ -281,11 +282,21 @@ static int mtk_snor_read_bounce(struct mtk_snor_priv *priv,
>> else
>> rdlen = op->data.nbytes;
>>
>> - ret = mtk_snor_dma_exec(priv, op->addr.val, rdlen,
>> - (dma_addr_t)priv->buffer);
>> + /* Map bounce buffer for DMA */
>> + bounce_dma = dma_map_single(priv->buffer, rdlen, DMA_FROM_DEVICE);
>> + if (dma_mapping_error(priv->dev, bounce_dma)) {this
>
> nitpick: do we really need this?
> In u-boot dma_mapping_error is defined as a stub always returning 0...
> unless this patch is a direct port from a kernel patch?
It looks like all other callers of dma_mapping_error() in U-Boot are checking
the return value, so I think I will keep it in case the implementation ever
changes where it can return an error.
>
>
>> + dev_err(priv->dev, "bounce buffer dma map failed\n");
>> + return -EINVAL;
>> + }
>>
>> - if (!ret)
>> + ret = mtk_snor_dma_exec(priv, op->addr.val, rdlen, bounce_dma);
>> + /* Ensure DMA writes are visible to CPU and copy the requested bytes */
>> + if (!ret) {
>> + /* Synchronize cached data to CPU visible memory if needed */
>> memcpy(op->data.buf.in, priv->buffer, op->data.nbytes);
>> + }
>> + /* Unmap bounce buffer regardless of success/failure */
>> + dma_unmap_single(bounce_dma, rdlen, DMA_FROM_DEVICE);
>>
>> return ret;
>> }
>>
>> --
>> 2.43.0
>>
More information about the U-Boot
mailing list