diff mbox series

[1/2] spi: atmel-quadspi: fix possible MMIO window size overrun

Message ID 20200320093755.921306-1-tudor.ambarus@microchip.com
State Accepted
Commit ba03a6c94493686ec8cc8c4b95dfb3c7a723b99c
Headers show
Series [1/2] spi: atmel-quadspi: fix possible MMIO window size overrun | expand

Commit Message

Tudor Ambarus March 20, 2020, 9:37 a.m. UTC
From: Tudor Ambarus <tudor.ambarus at microchip.com>

The sama5d2 QSPI controller memory space is limited to 128MB:
0x9000_00000-0x9800_00000/0XD000_0000--0XD800_0000.

There are nor flashes that are bigger in size than the memory size
supported by the controller: Micron MT25QL02G (256 MB).

Check if the address exceeds the MMIO window size. An improvement
would be to add support for regular SPI mode and fall back to it
when the flash memories overrun the controller's memory space.

Fixes: 24c8ff4684c5 ("spi: Add Atmel QuadSPI driver")
Signed-off-by: Tudor Ambarus <tudor.ambarus at microchip.com>
---
 drivers/spi/atmel-quadspi.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
diff mbox series

Patch

diff --git a/drivers/spi/atmel-quadspi.c b/drivers/spi/atmel-quadspi.c
index a09bf884e837..4099ee87993d 100644
--- a/drivers/spi/atmel-quadspi.c
+++ b/drivers/spi/atmel-quadspi.c
@@ -146,6 +146,7 @@  struct atmel_qspi_caps {
 struct atmel_qspi {
 	void __iomem *regs;
 	void __iomem *mem;
+	resource_size_t mmap_size;
 	const struct atmel_qspi_caps *caps;
 	ulong bus_clk_rate;
 	u32 mr;
@@ -326,6 +327,14 @@  static int atmel_qspi_exec_op(struct spi_slave *slave,
 	u32 sr, imr, offset;
 	int err;
 
+	/*
+	 * Check if the address exceeds the MMIO window size. An improvement
+	 * would be to add support for regular SPI mode and fall back to it
+	 * when the flash memories overrun the controller's memory space.
+	 */
+	if (op->addr.val + op->data.nbytes > aq->mmap_size)
+		return -ENOTSUPP;
+
 	err = atmel_qspi_set_cfg(aq, op, &offset);
 	if (err)
 		return err;
@@ -490,6 +499,8 @@  static int atmel_qspi_probe(struct udevice *dev)
 	if (IS_ERR(aq->mem))
 		return PTR_ERR(aq->mem);
 
+	aq->mmap_size = resource_size(&res);
+
 	ret = atmel_qspi_enable_clk(dev);
 	if (ret)
 		return ret;