spi-nor: intel-spi: Check transfer length in the HW/SW cycle
authorBin Meng <bmeng.cn@gmail.com>
Mon, 11 Sep 2017 09:41:54 +0000 (02:41 -0700)
committerCyrille Pitchen <cyrille.pitchen@wedev4u.fr>
Wed, 11 Oct 2017 07:43:13 +0000 (09:43 +0200)
Intel SPI controller only has a 64 bytes FIFO. This adds a sanity
check before triggering any HW/SW sequencer work.

Additionally for the SW sequencer, if given data length is zero,
we should not mark the 'Data Cycle' bit.

Signed-off-by: Bin Meng <bmeng.cn@gmail.com>
Acked-by: Mika Westerberg <mika.westerberg@linux.intel.com>
Signed-off-by: Cyrille Pitchen <cyrille.pitchen@wedev4u.fr>
drivers/mtd/spi-nor/intel-spi.c

index 263c6ab..c4a9de6 100644 (file)
@@ -399,6 +399,9 @@ static int intel_spi_hw_cycle(struct intel_spi *ispi, u8 opcode, int len)
                return -EINVAL;
        }
 
+       if (len > INTEL_SPI_FIFO_SZ)
+               return -EINVAL;
+
        val |= (len - 1) << HSFSTS_CTL_FDBC_SHIFT;
        val |= HSFSTS_CTL_FCERR | HSFSTS_CTL_FDONE;
        val |= HSFSTS_CTL_FGO;
@@ -419,14 +422,19 @@ static int intel_spi_hw_cycle(struct intel_spi *ispi, u8 opcode, int len)
 
 static int intel_spi_sw_cycle(struct intel_spi *ispi, u8 opcode, int len)
 {
-       u32 val, status;
+       u32 val = 0, status;
        int ret;
 
        ret = intel_spi_opcode_index(ispi, opcode);
        if (ret < 0)
                return ret;
 
-       val = ((len - 1) << SSFSTS_CTL_DBC_SHIFT) | SSFSTS_CTL_DS;
+       if (len > INTEL_SPI_FIFO_SZ)
+               return -EINVAL;
+
+       /* Only mark 'Data Cycle' bit when there is data to be transferred */
+       if (len > 0)
+               val = ((len - 1) << SSFSTS_CTL_DBC_SHIFT) | SSFSTS_CTL_DS;
        val |= ret << SSFSTS_CTL_COP_SHIFT;
        val |= SSFSTS_CTL_FCERR | SSFSTS_CTL_FDONE;
        val |= SSFSTS_CTL_SCGO;