diff mbox

[U-Boot,V7,2/4] spi: fsl_qspi: Fix qspi_op_rdid memcpy issue

Message ID 1453792002-25872-2-git-send-email-Qianyu.Gong@nxp.com
State Accepted
Commit 5207014deb1a465d2ac808254902100e7ea69d75
Delegated to: York Sun
Headers show

Commit Message

Gong Qianyu Jan. 26, 2016, 7:06 a.m. UTC
From: Gong Qianyu <Qianyu.Gong@freescale.com>

In current driver everytime we memcpy 4 bytes to the dest memory
regardless of the remaining length.
This patch adds checking the remaining length before memcpy.
If the length is shorter than 4 bytes, memcpy the actual length of data
to the dest memory.

Signed-off-by: Gong Qianyu <Qianyu.Gong@freescale.com>
---
V7:
 - Fix type of "size" and so "size -= 4" is also fixed.
 - Not use min() to avoid a compile warning of type cast.
V6:
 - Use min() to simplify code.
V2-V5:
 - No change.

 drivers/spi/fsl_qspi.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

Comments

York Sun Jan. 27, 2016, 5:15 p.m. UTC | #1
On 01/25/2016 11:15 PM, Gong Qianyu wrote:
> From: Gong Qianyu <Qianyu.Gong@freescale.com>
> 
> In current driver everytime we memcpy 4 bytes to the dest memory
> regardless of the remaining length.
> This patch adds checking the remaining length before memcpy.
> If the length is shorter than 4 bytes, memcpy the actual length of data
> to the dest memory.
> 
> Signed-off-by: Gong Qianyu <Qianyu.Gong@freescale.com>
> ---
> V7:
>  - Fix type of "size" and so "size -= 4" is also fixed.
>  - Not use min() to avoid a compile warning of type cast.
> V6:
>  - Use min() to simplify code.
> V2-V5:
>  - No change.
> 
>  drivers/spi/fsl_qspi.c | 12 ++++++------
>  1 file changed, 6 insertions(+), 6 deletions(-)


Applied to u-boot-fsl-qoriq master. Awaiting upstream.

Thanks.

York
diff mbox

Patch

diff --git a/drivers/spi/fsl_qspi.c b/drivers/spi/fsl_qspi.c
index 38e5900..3553e00 100644
--- a/drivers/spi/fsl_qspi.c
+++ b/drivers/spi/fsl_qspi.c
@@ -477,8 +477,8 @@  static void qspi_op_rdbank(struct fsl_qspi_priv *priv, u8 *rxbuf, u32 len)
 static void qspi_op_rdid(struct fsl_qspi_priv *priv, u32 *rxbuf, u32 len)
 {
 	struct fsl_qspi_regs *regs = priv->regs;
-	u32 mcr_reg, rbsr_reg, data;
-	int i, size;
+	u32 mcr_reg, rbsr_reg, data, size;
+	int i;
 
 	mcr_reg = qspi_read32(priv->flags, &regs->mcr);
 	qspi_write32(priv->flags, &regs->mcr,
@@ -494,15 +494,15 @@  static void qspi_op_rdid(struct fsl_qspi_priv *priv, u32 *rxbuf, u32 len)
 		;
 
 	i = 0;
-	size = len;
-	while ((RX_BUFFER_SIZE >= size) && (size > 0)) {
+	while ((RX_BUFFER_SIZE >= len) && (len > 0)) {
 		rbsr_reg = qspi_read32(priv->flags, &regs->rbsr);
 		if (rbsr_reg & QSPI_RBSR_RDBFL_MASK) {
 			data = qspi_read32(priv->flags, &regs->rbdr[i]);
 			data = qspi_endian_xchg(data);
-			memcpy(rxbuf, &data, 4);
+			size = (len < 4) ? len : 4;
+			memcpy(rxbuf, &data, size);
+			len -= size;
 			rxbuf++;
-			size -= 4;
 			i++;
 		}
 	}