Skip to contentSkip to conversation

Merge m25p80 into spi-nor

PATCH Superseded Vignesh Raghavendra 2 participants 12 messages Aug 1, 2019 – Aug 5, 2019

Subsystems (2)

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Vignesh Raghavendra

author maintainer
#0 lore raw

v4 cover Merge m25p80 into spi-nor

This is repost of patch 6 and 7 split from from Boris Brezillon's X-X-X mode support series[1]

Background from cover letter for RFC[1]:
m25p80 is just a simple SPI NOR controller driver (a wrapper around the SPI mem API). Not only it shouldn't be named after a specific SPI NOR chip, but it also doesn't deserve a specific driver IMO, especially if the end goal is to get rid of SPI NOR controller drivers found in drivers/mtd/spi-nor/ and replace them by SPI mem drivers (which would be placed in drivers/spi/). With this solution, we declare the SPI NOR driver as a spi_mem_driver, just like the SPI NAND layer is declared as a spi_mem driver (patch 1/2).
This solution also allows us to check at a fined-grain level (thanks to the spi_mem_supports_op() function) which operations are supported and which ones are not, while the original m25p80 logic was basing this decision on the SPI_{RX,TX}_{DUAL,QUAD,OCTO} flags only (patch 2/2).

[1] https://patchwork.ozlabs.org/cover/982926/

Tested on TI' DRA7xx EVM with TI QSPI controller (a spi-mem driver) with DMA (s25fl256) flash. I don't see any performance regression due to bounce buffer copy introduced by this series
Also tested with cadence-quadspi (a spi-nor driver) driver

Boris Brezillon (2):
mtd: spi-nor: Move m25p80 code in spi-nor.c
mtd: spi-nor: Rework hwcaps selection for the spi-mem case

Vignesh Raghavendra (1):
mtd: spi-nor: always use bounce buffer for register read/writes

 drivers/mtd/devices/Kconfig   |  18 -
 drivers/mtd/devices/Makefile  |   1 -
 drivers/mtd/devices/m25p80.c  | 347 ---------------
 drivers/mtd/spi-nor/Kconfig   |   2 +
 drivers/mtd/spi-nor/spi-nor.c | 806 +++++++++++++++++++++++++++++++---
 include/linux/mtd/spi-nor.h   |  24 +-
 6 files changed, 773 insertions(+), 425 deletions(-)
 delete mode 100644 drivers/mtd/devices/m25p80.c
Signature · 1 line
2.22.0
author maintainer
#1 lore raw

PATCH v4 1/3 mtd: spi-nor: always use bounce buffer for register read/writes

spi-mem layer expects all buffers passed to it to be DMA'able. But spi-nor layer mostly allocates buffers on stack for reading/writing to registers and therefore are not DMA'able. Introduce bounce buffer to be used to read/write to registers. This ensures that buffer passed to spi-mem layer during register read/writes is DMA'able. With this change nor->cmd-buf is no longer used, so drop it.

---

v4:
Avoid memcpy during READID

v3: new patch

 drivers/mtd/spi-nor/spi-nor.c | 70 ++++++++++++++++++++---------------
 include/linux/mtd/spi-nor.h   |  7 +++-
 2 files changed, 45 insertions(+), 32 deletions(-)
drivers/mtd/spi-nor/spi-nor.c +40 -30
index 03cc788511d5..e02376e1127b 100644
@@ -296,15 +296,14 @@ struct flash_info {
 static int read_sr(struct spi_nor *nor)
 {
 	int ret;
-	u8 val;
 
-	ret = nor->read_reg(nor, SPINOR_OP_RDSR, &val, 1);
+	ret = nor->read_reg(nor, SPINOR_OP_RDSR, nor->bouncebuf, 1);
 	if (ret < 0) {
 		pr_err("error %d reading SR\n", (int) ret);
 		return ret;
 	}
 
-	return val;
+	return nor->bouncebuf[0];
 }
 
 /*
@@ -315,15 +314,14 @@ static int read_sr(struct spi_nor *nor)
 static int read_fsr(struct spi_nor *nor)
 {
 	int ret;
-	u8 val;
 
-	ret = nor->read_reg(nor, SPINOR_OP_RDFSR, &val, 1);
+	ret = nor->read_reg(nor, SPINOR_OP_RDFSR, nor->bouncebuf, 1);
 	if (ret < 0) {
 		pr_err("error %d reading FSR\n", ret);
 		return ret;
 	}
 
-	return val;
+	return nor->bouncebuf[0];
 }
 
 /*
@@ -334,15 +332,14 @@ static int read_fsr(struct spi_nor *nor)
 static int read_cr(struct spi_nor *nor)
 {
 	int ret;
-	u8 val;
 
-	ret = nor->read_reg(nor, SPINOR_OP_RDCR, &val, 1);
+	ret = nor->read_reg(nor, SPINOR_OP_RDCR, nor->bouncebuf, 1);
 	if (ret < 0) {
 		dev_err(nor->dev, "error %d reading CR\n", ret);
 		return ret;
 	}
 
-	return val;
+	return nor->bouncebuf[0];
 }
 
 /*
@@ -351,8 +348,8 @@ static int read_cr(struct spi_nor *nor)
  */
 static int write_sr(struct spi_nor *nor, u8 val)
 {
-	nor->cmd_buf[0] = val;
-	return nor->write_reg(nor, SPINOR_OP_WRSR, nor->cmd_buf, 1);
+	nor->bouncebuf[0] = val;
+	return nor->write_reg(nor, SPINOR_OP_WRSR, nor->bouncebuf, 1);
 }
 
 /*
@@ -500,31 +497,31 @@ static int set_4byte(struct spi_nor *nor, bool enable)
 			 * We must clear the register to enable normal behavior.
 			 */
 			write_enable(nor);
-			nor->cmd_buf[0] = 0;
-			nor->write_reg(nor, SPINOR_OP_WREAR, nor->cmd_buf, 1);
+			nor->bouncebuf[0] = 0;
+			nor->write_reg(nor, SPINOR_OP_WREAR,
+				       nor->bouncebuf, 1);
 			write_disable(nor);
 		}
 
 		return status;
 	default:
 		/* Spansion style */
-		nor->cmd_buf[0] = enable << 7;
-		return nor->write_reg(nor, SPINOR_OP_BRWR, nor->cmd_buf, 1);
+		nor->bouncebuf[0] = enable << 7;
+		return nor->write_reg(nor, SPINOR_OP_BRWR, nor->bouncebuf, 1);
 	}
 }
 
 static int s3an_sr_ready(struct spi_nor *nor)
 {
 	int ret;
-	u8 val;
 
-	ret = nor->read_reg(nor, SPINOR_OP_XRDSR, &val, 1);
+	ret = nor->read_reg(nor, SPINOR_OP_XRDSR, nor->bouncebuf, 1);
 	if (ret < 0) {
 		dev_err(nor->dev, "error %d reading XRDSR\n", (int) ret);
 		return ret;
 	}
 
-	return !!(val & XSR_RDY);
+	return !!(nor->bouncebuf[0] & XSR_RDY);
 }
 
 static int spi_nor_sr_ready(struct spi_nor *nor)
@@ -683,7 +680,6 @@ static loff_t spi_nor_s3an_addr_convert(struct spi_nor *nor, unsigned int addr)
  */
 static int spi_nor_erase_sector(struct spi_nor *nor, u32 addr)
 {
-	u8 buf[SPI_NOR_MAX_ADDR_WIDTH];
 	int i;
 
 	if (nor->flags & SNOR_F_S3AN_ADDR_DEFAULT)
@@ -697,11 +693,12 @@ static int spi_nor_erase_sector(struct spi_nor *nor, u32 addr)
 	 * control
 	 */
 	for (i = nor->addr_width - 1; i >= 0; i--) {
-		buf[i] = addr & 0xff;
+		nor->bouncebuf[i] = addr & 0xff;
 		addr >>= 8;
 	}
 
-	return nor->write_reg(nor, nor->erase_opcode, buf, nor->addr_width);
+	return nor->write_reg(nor, nor->erase_opcode, nor->bouncebuf,
+			      nor->addr_width);
 }
 
 /**
@@ -1404,9 +1401,11 @@ static int write_sr_cr(struct spi_nor *nor, u8 *sr_cr)
 {
 	int ret;
 
+	memcpy(nor->bouncebuf, sr_cr, 2);
+
 	write_enable(nor);
 
-	ret = nor->write_reg(nor, SPINOR_OP_WRSR, sr_cr, 2);
+	ret = nor->write_reg(nor, SPINOR_OP_WRSR, nor->bouncebuf, 2);
 	if (ret < 0) {
 		dev_err(nor->dev,
 			"error while writing configuration register\n");
@@ -1599,22 +1598,22 @@ static int spansion_read_cr_quad_enable(struct spi_nor *nor)
  */
 static int sr2_bit7_quad_enable(struct spi_nor *nor)
 {
-	u8 sr2;
+	u8 *sr2 = nor->bouncebuf;
 	int ret;
 
 	/* Check current Quad Enable bit value. */
-	ret = nor->read_reg(nor, SPINOR_OP_RDSR2, &sr2, 1);
+	ret = nor->read_reg(nor, SPINOR_OP_RDSR2, sr2, 1);
 	if (ret)
 		return ret;
-	if (sr2 & SR2_QUAD_EN_BIT7)
+	if (*sr2 & SR2_QUAD_EN_BIT7)
 		return 0;
 
 	/* Update the Quad Enable bit. */
-	sr2 |= SR2_QUAD_EN_BIT7;
+	*sr2 |= SR2_QUAD_EN_BIT7;
 
 	write_enable(nor);
 
-	ret = nor->write_reg(nor, SPINOR_OP_WRSR2, &sr2, 1);
+	ret = nor->write_reg(nor, SPINOR_OP_WRSR2, sr2, 1);
 	if (ret < 0) {
 		dev_err(nor->dev, "error while writing status register 2\n");
 		return -EINVAL;
@@ -1627,8 +1626,8 @@ static int sr2_bit7_quad_enable(struct spi_nor *nor)
 	}
 
 	/* Read back and check it. */
-	ret = nor->read_reg(nor, SPINOR_OP_RDSR2, &sr2, 1);
-	if (!(ret > 0 && (sr2 & SR2_QUAD_EN_BIT7))) {
+	ret = nor->read_reg(nor, SPINOR_OP_RDSR2, sr2, 1);
+	if (!(ret > 0 && (*sr2 & SR2_QUAD_EN_BIT7))) {
 		dev_err(nor->dev, "SR2 Quad bit not set\n");
 		return -EINVAL;
 	}
@@ -2177,9 +2176,10 @@ static const struct flash_info spi_nor_ids[] = {
 static const struct flash_info *spi_nor_read_id(struct spi_nor *nor)
 {
 	int			tmp;
-	u8			id[SPI_NOR_MAX_ID_LEN];
+	u8			*id;
 	const struct flash_info	*info;
 
+	id = nor->bouncebuf;
 	tmp = nor->read_reg(nor, SPINOR_OP_RDID, id, SPI_NOR_MAX_ID_LEN);
 	if (tmp < 0) {
 		dev_err(nor->dev, "error %d reading JEDEC ID\n", tmp);
@@ -4121,6 +4121,16 @@ int spi_nor_scan(struct spi_nor *nor, const char *name,
 	nor->read_proto = SNOR_PROTO_1_1_1;
 	nor->write_proto = SNOR_PROTO_1_1_1;
 
+	/*
+	 * We need the bounce buffer early to read/write registers when going
+	 * through the spi-mem layer (buffers have to be DMA-able).
+	 */
+	nor->bouncebuf_size = PAGE_SIZE;
+	nor->bouncebuf = devm_kmalloc(dev, nor->bouncebuf_size,
+				      GFP_KERNEL);
+	if (!nor->bouncebuf)
+		return -ENOMEM;
+
 	if (name)
 		info = spi_nor_match_id(name);
 	/* Try to auto-detect if chip name wasn't specified or not found */
include/linux/mtd/spi-nor.h +5 -2
index 9f57cdfcc93d..6b5956a7a65a 100644
@@ -344,6 +344,9 @@ struct flash_info;
  * @mtd:		point to a mtd_info structure
  * @lock:		the lock for the read/write/erase/lock/unlock operations
  * @dev:		point to a spi device, or a spi nor controller device.
+ * @bouncebuf:		bounce buffer used when the buffer passed by the MTD
+ *                      layer is not DMA-able
+ * @bouncebuf_size:	size of the bounce buffer
  * @info:		spi-nor part JDEC MFR id and other info
  * @page_size:		the page size of the SPI NOR
  * @addr_width:		number of address bytes
@@ -356,7 +359,6 @@ struct flash_info;
  * @read_proto:		the SPI protocol for read operations
  * @write_proto:	the SPI protocol for write operations
  * @reg_proto		the SPI protocol for read_reg/write_reg/erase operations
- * @cmd_buf:		used by the write_reg
  * @erase_map:		the erase map of the SPI NOR
  * @prepare:		[OPTIONAL] do some preparations for the
  *			read/write/erase/lock/unlock operations
@@ -382,6 +384,8 @@ struct spi_nor {
 	struct mtd_info		mtd;
 	struct mutex		lock;
 	struct device		*dev;
+	u8			*bouncebuf;
+	size_t			bouncebuf_size;
 	const struct flash_info	*info;
 	u32			page_size;
 	u8			addr_width;
@@ -394,7 +398,6 @@ struct spi_nor {
 	enum spi_nor_protocol	reg_proto;
 	bool			sst_write_second;
 	u32			flags;
-	u8			cmd_buf[SPI_NOR_MAX_CMD_SIZE];
 	struct spi_nor_erase_map	erase_map;
 
 	int (*prepare)(struct spi_nor *nor, enum spi_nor_ops ops);
Signature · 1 line
2.22.0
#2 lore raw

Re: [PATCH v4 1/3] mtd: spi-nor: always use bounce buffer for register read/writes

Quoted 27 lines · Vignesh Raghavendra

External E-Mail

spi-mem layer expects all buffers passed to it to be DMA'able. But spi-nor layer mostly allocates buffers on stack for reading/writing to registers and therefore are not DMA'able. Introduce bounce buffer to be used to read/write to registers. This ensures that buffer passed to spi-mem layer during register read/writes is DMA'able. With this change nor->cmd-buf is no longer used, so drop it.

---

v4:
Avoid memcpy during READID

v3: new patch

 drivers/mtd/spi-nor/spi-nor.c | 70 ++++++++++++++++++++---------------
 include/linux/mtd/spi-nor.h   |  7 +++-
 2 files changed, 45 insertions(+), 32 deletions(-)
drivers/mtd/spi-nor/spi-nor.c +0 -0
index 03cc788511d5..e02376e1127b 100644

cut

drivers/mtd/spi-nor/spi-nor.c static int write_sr_cr(struct spi_nor *nor, u8 *sr_cr) · line 1404

Quoted 6 lines

/**

drivers/mtd/spi-nor/spi-nor.c +1 -0
@@ -1404,9 +1401,11 @@ static int write_sr_cr(struct spi_nor *nor, u8 *sr_cr)
 {
 	int ret;
 
+	memcpy(nor->bouncebuf, sr_cr, 2);

I'm thinking out loud. This can be avoided by forcing all the callers to use nor->bouncebuf. That would result in a:

static int write_sr(struct spi_nor *nor, size_t len)

write_sr_cr() can be removed. Memcopying 2 bytes is a small price to pay, we can keep things as they are, to not be too invasive. But if you think that this idea is worth it, tell.

Quoted 8 lines
drivers/mtd/spi-nor/spi-nor.c +2 -1
+
 	write_enable(nor);
 
-	ret = nor->write_reg(nor, SPINOR_OP_WRSR, sr_cr, 2);
+	ret = nor->write_reg(nor, SPINOR_OP_WRSR, nor->bouncebuf, 2);
 	if (ret < 0) {
 		dev_err(nor->dev,
 			"error while writing configuration register\n");

cut

drivers/mtd/spi-nor/spi-nor.c static const struct flash_info spi_nor_ids[] = { · line 2182

Quoted 9 lines
drivers/mtd/spi-nor/spi-nor.c +2 -1
@@ -2177,9 +2176,10 @@ static const struct flash_info spi_nor_ids[] = {
 static const struct flash_info *spi_nor_read_id(struct spi_nor *nor)
 {
 	int			tmp;
-	u8			id[SPI_NOR_MAX_ID_LEN];
+	u8			*id;
 	const struct flash_info	*info;
 
+	id = nor->bouncebuf;

nit: do init at declaration.

Also, you missed a place in which you can use the bouncebuf, search by "read_reg(":
ret = nor->read_reg(nor, SPINOR_OP_XRDSR, &val, 1);

Cheers,
ta

author maintainer
#3 lore raw

Re: [PATCH v4 1/3] mtd: spi-nor: always use bounce buffer for register read/writes

Quoted 38 lines · On 05/08/19 2:36 PM, [email protected] wrote:
Quoted 27 lines · Vignesh Raghavendra

External E-Mail

spi-mem layer expects all buffers passed to it to be DMA'able. But spi-nor layer mostly allocates buffers on stack for reading/writing to registers and therefore are not DMA'able. Introduce bounce buffer to be used to read/write to registers. This ensures that buffer passed to spi-mem layer during register read/writes is DMA'able. With this change nor->cmd-buf is no longer used, so drop it.

---

v4:
Avoid memcpy during READID

v3: new patch

 drivers/mtd/spi-nor/spi-nor.c | 70 ++++++++++++++++++++---------------
 include/linux/mtd/spi-nor.h   |  7 +++-
 2 files changed, 45 insertions(+), 32 deletions(-)
drivers/mtd/spi-nor/spi-nor.c +0 -0
index 03cc788511d5..e02376e1127b 100644

cut

Quoted 5 lines

/**

drivers/mtd/spi-nor/spi-nor.c +0 -0
@@ -1404,9 +1401,11 @@ static int write_sr_cr(struct spi_nor *nor, u8 *sr_cr)
 {
 	int ret;
 
Quoted 5 lines

drivers/mtd/spi-nor/spi-nor.c static int write_sr_cr(struct spi_nor *nor, u8 *sr_cr) · line 1404

Quoted 1 line
drivers/mtd/spi-nor/spi-nor.c +1 -0
+	memcpy(nor->bouncebuf, sr_cr, 2);

I'm thinking out loud. This can be avoided by forcing all the callers to use nor->bouncebuf. That would result in a:

I can make this change and make all callers use nor->bouncebuf in next version.

Quoted 6 lines

static int write_sr(struct spi_nor *nor, size_t len)

write_sr_cr() can be removed. Memcopying 2 bytes is a small price to pay, we can keep things as they are, to not be too invasive. But if you think that this idea is worth it, tell.

Sounds good to me. But replacing write_sr_cr() with above defintion of write_sr() should be a patch IMO>

Quoted 19 lines
Quoted 8 lines
drivers/mtd/spi-nor/spi-nor.c +2 -1
+
 	write_enable(nor);
 
-	ret = nor->write_reg(nor, SPINOR_OP_WRSR, sr_cr, 2);
+	ret = nor->write_reg(nor, SPINOR_OP_WRSR, nor->bouncebuf, 2);
 	if (ret < 0) {
 		dev_err(nor->dev,
 			"error while writing configuration register\n");

cut

Quoted 8 lines
drivers/mtd/spi-nor/spi-nor.c +1 -1
@@ -2177,9 +2176,10 @@ static const struct flash_info spi_nor_ids[] = {
 static const struct flash_info *spi_nor_read_id(struct spi_nor *nor)
 {
 	int			tmp;
-	u8			id[SPI_NOR_MAX_ID_LEN];
+	u8			*id;
 	const struct flash_info	*info;
 
Quoted 4 lines

drivers/mtd/spi-nor/spi-nor.c static const struct flash_info spi_nor_ids[] = { · line 2182

Quoted 1 line
drivers/mtd/spi-nor/spi-nor.c +1 -0
+	id = nor->bouncebuf;

nit: do init at declaration.

Ok.

Quoted 3 lines

Also, you missed a place in which you can use the bouncebuf, search by "read_reg(":
ret = nor->read_reg(nor, SPINOR_OP_XRDSR, &val, 1);

Indeed, will fix in next version!

Quoted 3 lines

Cheers,
ta

Signature · 2 lines
Regards
Vignesh
#4 lore raw

Re: [PATCH v4 1/3] mtd: spi-nor: always use bounce buffer for register read/writes

Quoted 43 lines · Vignesh Raghavendra

External E-Mail

Quoted 38 lines · On 05/08/19 2:36 PM, [email protected] wrote:
Quoted 27 lines · Vignesh Raghavendra

External E-Mail

spi-mem layer expects all buffers passed to it to be DMA'able. But spi-nor layer mostly allocates buffers on stack for reading/writing to registers and therefore are not DMA'able. Introduce bounce buffer to be used to read/write to registers. This ensures that buffer passed to spi-mem layer during register read/writes is DMA'able. With this change nor->cmd-buf is no longer used, so drop it.

---

v4:
Avoid memcpy during READID

v3: new patch

 drivers/mtd/spi-nor/spi-nor.c | 70 ++++++++++++++++++++---------------
 include/linux/mtd/spi-nor.h   |  7 +++-
 2 files changed, 45 insertions(+), 32 deletions(-)
drivers/mtd/spi-nor/spi-nor.c +0 -0
index 03cc788511d5..e02376e1127b 100644

cut

Quoted 5 lines

/**

drivers/mtd/spi-nor/spi-nor.c +0 -0
@@ -1404,9 +1401,11 @@ static int write_sr_cr(struct spi_nor *nor, u8 *sr_cr)
 {
 	int ret;
 
Quoted 9 lines
Quoted 5 lines

drivers/mtd/spi-nor/spi-nor.c static int write_sr_cr(struct spi_nor *nor, u8 *sr_cr) · line 1404

Quoted 1 line
drivers/mtd/spi-nor/spi-nor.c +1 -0
+	memcpy(nor->bouncebuf, sr_cr, 2);

I'm thinking out loud. This can be avoided by forcing all the callers to use nor->bouncebuf. That would result in a:

I can make this change and make all callers use nor->bouncebuf in next version.

should be ok, it will not change the flow logic.

Quoted 9 lines
Quoted 6 lines

static int write_sr(struct spi_nor *nor, size_t len)

write_sr_cr() can be removed. Memcopying 2 bytes is a small price to pay, we can keep things as they are, to not be too invasive. But if you think that this idea is worth it, tell.

Sounds good to me. But replacing write_sr_cr() with above defintion of write_sr() should be a patch IMO>

I'll do a patch after we finish to integrate the big fat changes.

Thanks,
ta

author maintainer
#5 lore raw

PATCH v4 2/3 mtd: spi-nor: Move m25p80 code in spi-nor.c

In reply to Vignesh Raghavendra

From: Boris Brezillon <[email protected]>

The m25p80 driver is actually a generic wrapper around the spi-mem layer. Not only the driver name is misleading, but we'd expect such a common logic to be directly available in the core. Another reason for moving this code is that SPI NOR controller drivers should progressively be replaced by SPI controller drivers implementing the spi_mem_ops interface, and when the conversion is done, we should have a single spi-nor driver directly interfacing with the spi-mem layer.

While moving the code we also fix a longstanding issue when non-DMA-able buffers are passed by the MTD layer.

---
v4:
Fix a bug in write_sr()
Update comments around allocation of bounce buffer

v3:
Simplify register read/write by dropping spi_nor_exec_op() and using spi_mem_exec_op() directly
Modify spi_nor_spimem_xfer_data() to drop "enum spi_nor_protocol proto" Fix misc coding style comments by Tudor

v2:
Add docs for new functions added
Add spi_nor_ prefix to new functions
Incorporate Andrey's patches https://lkml.org/lkml/2019/4/1/32
to avoid looping spi_nor_spimem_* APIs

 drivers/mtd/devices/Kconfig   |  18 -
 drivers/mtd/devices/Makefile  |   1 -
 drivers/mtd/devices/m25p80.c  | 347 -------------------
 drivers/mtd/spi-nor/Kconfig   |   2 +
 drivers/mtd/spi-nor/spi-nor.c | 631 ++++++++++++++++++++++++++++++++--
 include/linux/mtd/spi-nor.h   |   3 +
 6 files changed, 606 insertions(+), 396 deletions(-)
 delete mode 100644 drivers/mtd/devices/m25p80.c
drivers/mtd/devices/Kconfig +0 -18
index 49abbc52457d..f96287c4b789 100644
@@ -79,24 +79,6 @@ config MTD_DATAFLASH_OTP
 	  other key product data.  The second half is programmed with a
 	  unique-to-each-chip bit pattern at the factory.
 
-config MTD_M25P80
-	tristate "Support most SPI Flash chips (AT26DF, M25P, W25X, ...)"
-	depends on SPI_MASTER && MTD_SPI_NOR
-	select SPI_MEM
-	help
-	  This enables access to most modern SPI flash chips, used for
-	  program and data storage.   Series supported include Atmel AT26DF,
-	  Spansion S25SL, SST 25VF, ST M25P, and Winbond W25X.  Other chips
-	  are supported as well.  See the driver source for the current list,
-	  or to add other chips.
-
-	  Note that the original DataFlash chips (AT45 series, not AT26DF),
-	  need an entirely different driver.
-
-	  Set up your spi devices with the right board-specific platform data,
-	  if you want to specify device partitioning or to use a device which
-	  doesn't support the JEDEC ID instruction.
-
 config MTD_MCHP23K256
 	tristate "Microchip 23K256 SRAM"
 	depends on SPI_MASTER
drivers/mtd/devices/Makefile +0 -1
index 94895eab3066..991c8d12c016 100644
@@ -12,7 +12,6 @@ obj-$(CONFIG_MTD_MTDRAM)	+= mtdram.o
 obj-$(CONFIG_MTD_LART)		+= lart.o
 obj-$(CONFIG_MTD_BLOCK2MTD)	+= block2mtd.o
 obj-$(CONFIG_MTD_DATAFLASH)	+= mtd_dataflash.o
-obj-$(CONFIG_MTD_M25P80)	+= m25p80.o
 obj-$(CONFIG_MTD_MCHP23K256)	+= mchp23k256.o
 obj-$(CONFIG_MTD_SPEAR_SMI)	+= spear_smi.o
 obj-$(CONFIG_MTD_SST25L)	+= sst25l.o
drivers/mtd/devices/m25p80.c deleted +0 -347

347 lines not on this page · raw message

drivers/mtd/spi-nor/Kconfig +2 -0
index 6de83277ce8b..f237fcdf7f86 100644
@@ -2,6 +2,8 @@
 menuconfig MTD_SPI_NOR
 	tristate "SPI-NOR device support"
 	depends on MTD
+	depends on MTD && SPI_MASTER
+	select SPI_MEM
 	help
 	  This is the framework for the SPI NOR which can be used by the SPI
 	  device drivers and the SPI-NOR device driver.
drivers/mtd/spi-nor/spi-nor.c +601 -30

854 lines not on this page · raw message

include/linux/mtd/spi-nor.h +3 -0
index 6b5956a7a65a..4f35b1877889 100644
@@ -9,6 +9,7 @@
 #include <linux/bitops.h>
 #include <linux/mtd/cfi.h>
 #include <linux/mtd/mtd.h>
+#include <linux/spi/spi-mem.h>
 
 /*
  * Manufacturer IDs
@@ -344,6 +345,7 @@ struct flash_info;
  * @mtd:		point to a mtd_info structure
  * @lock:		the lock for the read/write/erase/lock/unlock operations
  * @dev:		point to a spi device, or a spi nor controller device.
+ * @spimem:		point to the spi mem device
  * @bouncebuf:		bounce buffer used when the buffer passed by the MTD
  *                      layer is not DMA-able
  * @bouncebuf_size:	size of the bounce buffer
@@ -384,6 +386,7 @@ struct spi_nor {
 	struct mtd_info		mtd;
 	struct mutex		lock;
 	struct device		*dev;
+	struct spi_mem		*spimem;
 	u8			*bouncebuf;
 	size_t			bouncebuf_size;
 	const struct flash_info	*info;
Signature · 1 line
2.22.0
#6 lore raw

Re: [PATCH v4 2/3] mtd: spi-nor: Move m25p80 code in spi-nor.c

Quoted 5 lines · Vignesh Raghavendra
drivers/mtd/spi-nor/spi-nor.c +0 -0
index e02376e1127b..866962c715b4 100644
@@ -19,6 +19,7 @@

cut

drivers/mtd/spi-nor/spi-nor.c struct flash_info { · line 365

Quoted 12 lines
drivers/mtd/spi-nor/spi-nor.c +12 -0
+static ssize_t spi_nor_spimem_read_data(struct spi_nor *nor, loff_t from,
+					size_t len, u8 *buf)
+{
+	struct spi_mem_op op =
+		SPI_MEM_OP(SPI_MEM_OP_CMD(nor->read_opcode, 1),
+			   SPI_MEM_OP_ADDR(nor->addr_width, from, 1),
+			   SPI_MEM_OP_DUMMY(nor->read_dummy, 1),
+			   SPI_MEM_OP_DATA_IN(len, buf, 1));
+
+	/* get transfer protocols. */
+	op.cmd.buswidth = spi_nor_get_protocol_inst_nbits(nor->read_proto);
+	op.addr.buswidth = spi_nor_get_protocol_addr_nbits(nor->read_proto);

nit: op.dummy.buswidth = op.addr.buswidth;

cut

Quoted 5 lines
drivers/mtd/spi-nor/spi-nor.c +0 -0
@@ -688,6 +1003,16 @@ static int spi_nor_erase_sector(struct spi_nor *nor, u32 addr)
 	if (nor->erase)
 		return nor->erase(nor, addr);
 

drivers/mtd/spi-nor/spi-nor.c static int spi_nor_erase_sector(struct spi_nor *nor, u32 addr) · line 1015

Quoted 10 lines
drivers/mtd/spi-nor/spi-nor.c +10 -0
+	if (nor->spimem) {
+		struct spi_mem_op op =
+			SPI_MEM_OP(SPI_MEM_OP_CMD(nor->erase_opcode, 1),
+				   SPI_MEM_OP_ADDR(nor->addr_width, addr, 1),
+				   SPI_MEM_OP_NO_DUMMY,
+				   SPI_MEM_OP_NO_DATA);
+
+		return spi_mem_exec_op(nor->spimem, &op);
+	}
+

this should be done below in the function, after masking the address.

You add a double space after return in:

drivers/mtd/spi-nor/spi-nor.c static int spansion_read_cr_quad_enable(struct spi_nor *nor) · line 1950

Quoted 1 line
drivers/mtd/spi-nor/spi-nor.c +1 -0
+	return  nor->read_reg(nor, SPINOR_OP_RDSR2, sr2, 1);

There are some checkpatch warnings that we can fix now.

Looks good!
ta

author maintainer
#7 lore raw

Re: [PATCH v4 2/3] mtd: spi-nor: Move m25p80 code in spi-nor.c

Quoted 12 lines · On 05/08/19 3:55 PM, [email protected] wrote:
Quoted 5 lines · Vignesh Raghavendra
drivers/mtd/spi-nor/spi-nor.c +0 -0
index e02376e1127b..866962c715b4 100644
@@ -19,6 +19,7 @@

cut

Quoted 15 lines

drivers/mtd/spi-nor/spi-nor.c struct flash_info { · line 365

Quoted 12 lines
drivers/mtd/spi-nor/spi-nor.c +12 -0
+static ssize_t spi_nor_spimem_read_data(struct spi_nor *nor, loff_t from,
+					size_t len, u8 *buf)
+{
+	struct spi_mem_op op =
+		SPI_MEM_OP(SPI_MEM_OP_CMD(nor->read_opcode, 1),
+			   SPI_MEM_OP_ADDR(nor->addr_width, from, 1),
+			   SPI_MEM_OP_DUMMY(nor->read_dummy, 1),
+			   SPI_MEM_OP_DATA_IN(len, buf, 1));
+
+	/* get transfer protocols. */
+	op.cmd.buswidth = spi_nor_get_protocol_inst_nbits(nor->read_proto);
+	op.addr.buswidth = spi_nor_get_protocol_addr_nbits(nor->read_proto);

nit: op.dummy.buswidth = op.addr.buswidth;

Sure, will change.

Quoted 7 lines

cut

Quoted 5 lines
drivers/mtd/spi-nor/spi-nor.c +0 -0
@@ -688,6 +1003,16 @@ static int spi_nor_erase_sector(struct spi_nor *nor, u32 addr)
 	if (nor->erase)
 		return nor->erase(nor, addr);
 
Quoted 12 lines

drivers/mtd/spi-nor/spi-nor.c static int spi_nor_erase_sector(struct spi_nor *nor, u32 addr) · line 1015

Quoted 10 lines
drivers/mtd/spi-nor/spi-nor.c +10 -0
+	if (nor->spimem) {
+		struct spi_mem_op op =
+			SPI_MEM_OP(SPI_MEM_OP_CMD(nor->erase_opcode, 1),
+				   SPI_MEM_OP_ADDR(nor->addr_width, addr, 1),
+				   SPI_MEM_OP_NO_DUMMY,
+				   SPI_MEM_OP_NO_DATA);
+
+		return spi_mem_exec_op(nor->spimem, &op);
+	}
+

this should be done below in the function, after masking the address.

Nope. It would have been true if addr been sent as part of op.data.buf.out.
But since addr is being send as part of op.addr.val, spi-mem.c takes care of this for spi_transfer(s)

Quoted 4 lines

You add a double space after return in:

drivers/mtd/spi-nor/spi-nor.c static int spansion_read_cr_quad_enable(struct spi_nor *nor) · line 1950

Quoted 1 line
drivers/mtd/spi-nor/spi-nor.c +1 -0
+	return  nor->read_reg(nor, SPINOR_OP_RDSR2, sr2, 1);

Ah, Will fix

Quoted 2 lines

There are some checkpatch warnings that we can fix now.

I did see warnings like:

drivers/mtd/spi-nor/spi-nor.c static const struct flash_info *spi_nor_read_id(struct spi_nor *nor) · line 2554

Quoted 4 lines

WARNING: line over 80 characters
#1023: FILE: drivers/mtd/spi-nor/spi-nor.c:2554:

drivers/mtd/spi-nor/spi-nor.c +1 -0
+				   SPI_MEM_OP_DATA_IN(SPI_NOR_MAX_ID_LEN, id, 1));

I think its okay to overshoot 80 char limit for better readability. Let me know if you think otherwise

Quoted 4 lines

ERROR: space required after that ',' (ctx:VxV)
#1270: FILE: drivers/mtd/spi-nor/spi-nor.c:4846:

drivers/mtd/spi-nor/spi-nor.c +1 -0
+	{"mx25l25635e"},{"mx66l51235l"},
	               ^

This and similar warnings can be fixed, but will throw off alignment wrt previous lines. Therefore I let them be as is.

Quoted 3 lines

Looks good!
ta

Signature · 2 lines
Regards
Vignesh
#8 lore raw

Re: [PATCH v4 2/3] mtd: spi-nor: Move m25p80 code in spi-nor.c

Quoted 34 lines · Vignesh Raghavendra

External E-Mail

Quoted 16 lines · On 05/08/19 3:55 PM, [email protected] wrote:
Quoted 12 lines · Vignesh Raghavendra
drivers/mtd/spi-nor/spi-nor.c +0 -0
index e02376e1127b..866962c715b4 100644
@@ -19,6 +19,7 @@
 
 
 
@@ -688,6 +1003,16 @@ static int spi_nor_erase_sector(struct spi_nor *nor, u32 addr)
 	if (nor->erase)
 		return nor->erase(nor, addr);
 
Quoted 12 lines

drivers/mtd/spi-nor/spi-nor.c static int spi_nor_erase_sector(struct spi_nor *nor, u32 addr) · line 1015

Quoted 10 lines
drivers/mtd/spi-nor/spi-nor.c +10 -0
+	if (nor->spimem) {
+		struct spi_mem_op op =
+			SPI_MEM_OP(SPI_MEM_OP_CMD(nor->erase_opcode, 1),
+				   SPI_MEM_OP_ADDR(nor->addr_width, addr, 1),
+				   SPI_MEM_OP_NO_DUMMY,
+				   SPI_MEM_OP_NO_DATA);
+
+		return spi_mem_exec_op(nor->spimem, &op);
+	}
+

this should be done below in the function, after masking the address.

Quoted 3 lines

Nope. It would have been true if addr been sent as part of op.data.buf.out.
But since addr is being send as part of op.addr.val, spi-mem.c takes care of this for spi_transfer(s)

Is this valid also for the controllers that implement the ctlr->mem_ops?

Quoted 10 lines
Quoted 4 lines

You add a double space after return in:

drivers/mtd/spi-nor/spi-nor.c static int spansion_read_cr_quad_enable(struct spi_nor *nor) · line 1950

Quoted 1 line
drivers/mtd/spi-nor/spi-nor.c +1 -0
+	return  nor->read_reg(nor, SPINOR_OP_RDSR2, sr2, 1);

Ah, Will fix

Quoted 2 lines

There are some checkpatch warnings that we can fix now.

Quoted 7 lines

I did see warnings like:

drivers/mtd/spi-nor/spi-nor.c static const struct flash_info *spi_nor_read_id(struct spi_nor *nor) · line 2554

Quoted 4 lines

WARNING: line over 80 characters
#1023: FILE: drivers/mtd/spi-nor/spi-nor.c:2554:

drivers/mtd/spi-nor/spi-nor.c +1 -0
+				   SPI_MEM_OP_DATA_IN(SPI_NOR_MAX_ID_LEN, id, 1));

I think its okay to overshoot 80 char limit for better readability.

ok

Quoted 8 lines

Let me know if you think otherwise>

Quoted 4 lines

ERROR: space required after that ',' (ctx:VxV)
#1270: FILE: drivers/mtd/spi-nor/spi-nor.c:4846:

drivers/mtd/spi-nor/spi-nor.c +1 -0
+	{"mx25l25635e"},{"mx66l51235l"},
	               ^

This and similar warnings can be fixed, but will throw off alignment wrt previous lines. Therefore I let them be as is.

ok

Cheers,ta

author maintainer
#9 lore raw

Re: [PATCH v4 2/3] mtd: spi-nor: Move m25p80 code in spi-nor.c

Quoted 37 lines · On 05/08/19 5:21 PM, [email protected] wrote:
Quoted 34 lines · Vignesh Raghavendra

External E-Mail

Quoted 28 lines · On 05/08/19 3:55 PM, [email protected] wrote:
Quoted 12 lines · Vignesh Raghavendra
drivers/mtd/spi-nor/spi-nor.c +0 -0
index e02376e1127b..866962c715b4 100644
@@ -19,6 +19,7 @@
 
 
 
@@ -688,6 +1003,16 @@ static int spi_nor_erase_sector(struct spi_nor *nor, u32 addr)
 	if (nor->erase)
 		return nor->erase(nor, addr);
 

drivers/mtd/spi-nor/spi-nor.c static int spi_nor_erase_sector(struct spi_nor *nor, u32 addr) · line 1015

Quoted 10 lines
drivers/mtd/spi-nor/spi-nor.c +10 -0
+	if (nor->spimem) {
+		struct spi_mem_op op =
+			SPI_MEM_OP(SPI_MEM_OP_CMD(nor->erase_opcode, 1),
+				   SPI_MEM_OP_ADDR(nor->addr_width, addr, 1),
+				   SPI_MEM_OP_NO_DUMMY,
+				   SPI_MEM_OP_NO_DATA);
+
+		return spi_mem_exec_op(nor->spimem, &op);
+	}
+

this should be done below in the function, after masking the address.

Quoted 5 lines
Quoted 3 lines

Nope. It would have been true if addr been sent as part of op.data.buf.out.
But since addr is being send as part of op.addr.val, spi-mem.c takes care of this for spi_transfer(s)

Is this valid also for the controllers that implement the ctlr->mem_ops?

Nope, address conversion logic is not required if ctlr->mem_ops is implemented. spi-mem drivers should be able to handle address field as is and there is no need to be converted to SPI bus order.

Regards
Vignesh

Quoted 34 lines
Quoted 11 lines
Quoted 5 lines

You add a double space after return in:

drivers/mtd/spi-nor/spi-nor.c static int spansion_read_cr_quad_enable(struct spi_nor *nor) · line 1950

Quoted 1 line
drivers/mtd/spi-nor/spi-nor.c +1 -0
+	return  nor->read_reg(nor, SPINOR_OP_RDSR2, sr2, 1);

Ah, Will fix

Quoted 2 lines

There are some checkpatch warnings that we can fix now.

Quoted 7 lines

I did see warnings like:

drivers/mtd/spi-nor/spi-nor.c static const struct flash_info *spi_nor_read_id(struct spi_nor *nor) · line 2554

Quoted 4 lines

WARNING: line over 80 characters
#1023: FILE: drivers/mtd/spi-nor/spi-nor.c:2554:

drivers/mtd/spi-nor/spi-nor.c +1 -0
+				   SPI_MEM_OP_DATA_IN(SPI_NOR_MAX_ID_LEN, id, 1));

I think its okay to overshoot 80 char limit for better readability.

ok

Quoted 8 lines

Let me know if you think otherwise>

Quoted 4 lines

ERROR: space required after that ',' (ctx:VxV)
#1270: FILE: drivers/mtd/spi-nor/spi-nor.c:4846:

drivers/mtd/spi-nor/spi-nor.c +1 -0
+	{"mx25l25635e"},{"mx66l51235l"},
	               ^

This and similar warnings can be fixed, but will throw off alignment wrt previous lines. Therefore I let them be as is.

ok

Cheers,ta

Signature · 2 lines
Regards
Vignesh
author maintainer
#10 lore raw

PATCH v4 3/3 mtd: spi-nor: Rework hwcaps selection for the spi-mem case

In reply to Vignesh Raghavendra

From: Boris Brezillon <[email protected]>

The spi-mem layer provides a spi_mem_supports_op() function to check whether a specific operation is supported by the controller or not. This is much more accurate than the hwcaps selection logic based on SPI_{RX,TX}_ flags.

Rework the hwcaps selection logic to use spi_mem_supports_op() when nor->spimem != NULL.

---
 drivers/mtd/spi-nor/spi-nor.c | 183 +++++++++++++++++++++++++++-------
 include/linux/mtd/spi-nor.h   |  14 +++
 2 files changed, 161 insertions(+), 36 deletions(-)
drivers/mtd/spi-nor/spi-nor.c +147 -36
index 866962c715b4..02b40f08ac36 100644
@@ -2952,6 +2952,129 @@ static int spi_nor_read_sfdp(struct spi_nor *nor, u32 addr,
 	return ret;
 }
 
+/**
+ * spi_nor_spimem_check_op - check if the operation is supported
+ *                           by controller
+ *@nor:        pointer to a 'struct spi_nor'
+ *@op:         pointer to op template to be checked
+ *
+ * Returns 0 if operation is supported, -ENOTSUPP otherwise.
+ */
+static int spi_nor_spimem_check_op(struct spi_nor *nor,
+				   struct spi_mem_op *op)
+{
+	/*
+	 * First test with 4 address bytes. The opcode itself might
+	 * be a 3B addressing opcode but we don't care, because
+	 * SPI controller implementation should not check the opcode,
+	 * but just the sequence.
+	 */
+	op->addr.nbytes = 4;
+	if (!spi_mem_supports_op(nor->spimem, op)) {
+		/* If flash size <16MB, 3 address bytes are sufficient */
+		if (nor->mtd.size <= SZ_16M) {
+			op->addr.nbytes = 3;
+			if (!spi_mem_supports_op(nor->spimem, op))
+				return -ENOTSUPP;
+		}
+	}
+
+	return 0;
+}
+
+/**
+ * spi_nor_spimem_check_readop - check if the read op is supported
+ *                               by controller
+ *@nor:         pointer to a 'struct spi_nor'
+ *@read:        pointer to op template to be checked
+ *
+ * Returns 0 if operation is supported, -ENOTSUPP otherwise.
+ */
+static int spi_nor_spimem_check_readop(struct spi_nor *nor,
+				       const struct spi_nor_read_command *read)
+{
+	struct spi_mem_op op = SPI_MEM_OP(SPI_MEM_OP_CMD(read->opcode, 1),
+					  SPI_MEM_OP_ADDR(3, 0, 1),
+					  SPI_MEM_OP_DUMMY(0, 1),
+					  SPI_MEM_OP_DATA_IN(0, NULL, 1));
+
+	op.cmd.buswidth = spi_nor_get_protocol_inst_nbits(read->proto);
+	op.addr.buswidth = spi_nor_get_protocol_addr_nbits(read->proto);
+	op.data.buswidth = spi_nor_get_protocol_data_nbits(read->proto);
+	op.dummy.buswidth = op.addr.buswidth;
+	op.dummy.nbytes = (read->num_mode_clocks + read->num_wait_states) *
+			  op.dummy.buswidth / 8;
+
+	return spi_nor_spimem_check_op(nor, &op);
+}
+
+/**
+ * spi_nor_spimem_check_pp - check if the page program op is supported
+ *                           by controller
+ *@nor:         pointer to a 'struct spi_nor'
+ *@pp:          pointer to op template to be checked
+ *
+ * Returns 0 if operation is supported, -ENOTSUPP otherwise.
+ */
+static int spi_nor_spimem_check_pp(struct spi_nor *nor,
+				   const struct spi_nor_pp_command *pp)
+{
+	struct spi_mem_op op = SPI_MEM_OP(SPI_MEM_OP_CMD(pp->opcode, 1),
+					  SPI_MEM_OP_ADDR(3, 0, 1),
+					  SPI_MEM_OP_NO_DUMMY,
+					  SPI_MEM_OP_DATA_OUT(0, NULL, 1));
+
+	op.cmd.buswidth = spi_nor_get_protocol_inst_nbits(pp->proto);
+	op.addr.buswidth = spi_nor_get_protocol_addr_nbits(pp->proto);
+	op.data.buswidth = spi_nor_get_protocol_data_nbits(pp->proto);
+
+	return spi_nor_spimem_check_op(nor, &op);
+}
+
+/**
+ * spi_nor_spimem_adjust_hwcaps - Find optimal Read/Write protocol
+ *                                based on SPI controller capabilities
+ * @nor:        pointer to a 'struct spi_nor'
+ * @params:     pointer to the 'struct spi_nor_flash_parameter'
+ *              representing SPI NOR flash capabilities
+ * @hwcaps:     pointer to resulting capabilities after adjusting
+ *              according to controller and flash's capability
+ */
+static void
+spi_nor_spimem_adjust_hwcaps(struct spi_nor *nor,
+			     const struct spi_nor_flash_parameter *params,
+			     u32 *hwcaps)
+{
+	unsigned int cap;
+
+	/* DTR modes are not supported yet, mask them all. */
+	*hwcaps &= ~SNOR_HWCAPS_DTR;
+
+	/* X-X-X modes are not supported yet, mask them all. */
+	*hwcaps &= ~SNOR_HWCAPS_X_X_X;
+
+	/* Start with read commands. */
+	for (cap = 0; cap < sizeof(*hwcaps) * BITS_PER_BYTE; cap++) {
+		int rdidx, ppidx;
+
+		if (!(*hwcaps & BIT(cap)))
+			continue;
+
+		rdidx = spi_nor_hwcaps_read2cmd(BIT(cap));
+		if (rdidx >= 0 &&
+		    spi_nor_spimem_check_readop(nor, &params->reads[rdidx]))
+			*hwcaps &= ~BIT(cap);
+
+		ppidx = spi_nor_hwcaps_pp2cmd(BIT(cap));
+		if (ppidx < 0)
+			continue;
+
+		if (spi_nor_spimem_check_pp(nor,
+					    &params->page_programs[ppidx]))
+			*hwcaps &= ~BIT(cap);
+	}
+}
+
 /**
  * spi_nor_read_sfdp_dma_unsafe() - read Serial Flash Discoverable Parameters.
  * @nor:	pointer to a 'struct spi_nor'
@@ -4361,16 +4484,25 @@ static int spi_nor_setup(struct spi_nor *nor,
 	 */
 	shared_mask = hwcaps->mask & params->hwcaps.mask;
 
-	/* SPI n-n-n protocols are not supported yet. */
-	ignored_mask = (SNOR_HWCAPS_READ_2_2_2 |
-			SNOR_HWCAPS_READ_4_4_4 |
-			SNOR_HWCAPS_READ_8_8_8 |
-			SNOR_HWCAPS_PP_4_4_4 |
-			SNOR_HWCAPS_PP_8_8_8);
-	if (shared_mask & ignored_mask) {
-		dev_dbg(nor->dev,
-			"SPI n-n-n protocols are not supported yet.\n");
-		shared_mask &= ~ignored_mask;
+	if (nor->spimem) {
+		/*
+		 * When called from spi_nor_probe(), all caps are set and we
+		 * need to discard some of them based on what the SPI
+		 * controller actually supports (using spi_mem_supports_op()).
+		 */
+		spi_nor_spimem_adjust_hwcaps(nor, params, &shared_mask);
+	} else {
+		/*
+		 * SPI n-n-n protocols are not supported when the SPI
+		 * controller directly implements the spi_nor interface.
+		 * Yet another reason to switch to spi-mem.
+		 */
+		ignored_mask = SNOR_HWCAPS_X_X_X;
+		if (shared_mask & ignored_mask) {
+			dev_dbg(nor->dev,
+				"SPI n-n-n protocols are not supported.\n");
+			shared_mask &= ~ignored_mask;
+		}
 	}
 
 	/* Select the (Fast) Read command. */
@@ -4713,11 +4845,11 @@ static int spi_nor_probe(struct spi_mem *spimem)
 	struct spi_device *spi = spimem->spi;
 	struct flash_platform_data *data = dev_get_platdata(&spi->dev);
 	struct spi_nor *nor;
-	struct spi_nor_hwcaps hwcaps = {
-		.mask = SNOR_HWCAPS_READ |
-			SNOR_HWCAPS_READ_FAST |
-			SNOR_HWCAPS_PP,
-	};
+	/*
+	 * Enable all caps by default. The core will mask them after
+	 * checking what's really supported using spi_mem_supports_op().
+	 */
+	const struct spi_nor_hwcaps hwcaps = { .mask = SNOR_HWCAPS_ALL };
 	char *flash_name;
 	int ret;
 
@@ -4731,27 +4863,6 @@ static int spi_nor_probe(struct spi_mem *spimem)
 
 	spi_mem_set_drvdata(spimem, nor);
 
-	if (spi->mode & SPI_RX_OCTAL) {
-		hwcaps.mask |= SNOR_HWCAPS_READ_1_1_8;
-
-		if (spi->mode & SPI_TX_OCTAL)
-			hwcaps.mask |= (SNOR_HWCAPS_READ_1_8_8 |
-					SNOR_HWCAPS_PP_1_1_8 |
-					SNOR_HWCAPS_PP_1_8_8);
-	} else if (spi->mode & SPI_RX_QUAD) {
-		hwcaps.mask |= SNOR_HWCAPS_READ_1_1_4;
-
-		if (spi->mode & SPI_TX_QUAD)
-			hwcaps.mask |= (SNOR_HWCAPS_READ_1_4_4 |
-					SNOR_HWCAPS_PP_1_1_4 |
-					SNOR_HWCAPS_PP_1_4_4);
-	} else if (spi->mode & SPI_RX_DUAL) {
-		hwcaps.mask |= SNOR_HWCAPS_READ_1_1_2;
-
-		if (spi->mode & SPI_TX_DUAL)
-			hwcaps.mask |= SNOR_HWCAPS_READ_1_2_2;
-	}
-
 	if (data && data->name)
 		nor->mtd.name = data->name;
 
include/linux/mtd/spi-nor.h +14 -0
index 4f35b1877889..5f1acb1867dd 100644
@@ -524,6 +524,20 @@ struct spi_nor_hwcaps {
 #define SNOR_HWCAPS_PP_1_8_8	BIT(21)
 #define SNOR_HWCAPS_PP_8_8_8	BIT(22)
 
+#define SNOR_HWCAPS_X_X_X	(SNOR_HWCAPS_READ_2_2_2 |	\
+				 SNOR_HWCAPS_READ_4_4_4 |	\
+				 SNOR_HWCAPS_READ_8_8_8 |	\
+				 SNOR_HWCAPS_PP_4_4_4 |		\
+				 SNOR_HWCAPS_PP_8_8_8)
+
+#define SNOR_HWCAPS_DTR		(SNOR_HWCAPS_READ_1_1_1_DTR |	\
+				 SNOR_HWCAPS_READ_1_2_2_DTR |	\
+				 SNOR_HWCAPS_READ_1_4_4_DTR |	\
+				 SNOR_HWCAPS_READ_1_8_8_DTR)
+
+#define SNOR_HWCAPS_ALL		(SNOR_HWCAPS_READ_MASK |	\
+				 SNOR_HWCAPS_PP_MASK)
+
 /**
  * spi_nor_scan() - scan the SPI NOR
  * @nor:	the spi_nor structure
Signature · 1 line
2.22.0
#11 lore raw

Re: [PATCH v4 3/3] mtd: spi-nor: Rework hwcaps selection for the spi-mem case

Quoted 7 lines · Vignesh Raghavendra
drivers/mtd/spi-nor/spi-nor.c +7 -0
+static int spi_nor_spimem_check_op(struct spi_nor *nor,
+				   struct spi_mem_op *op)
+{
+	/*
+	 * First test with 4 address bytes. The opcode itself might
+	 * be a 3B addressing opcode but we don't care, because
+	 * SPI controller implementation should not check the opcode,

drivers/mtd/spi-nor/spi-nor.c static int spi_nor_read_sfdp(struct spi_nor *nor, u32 addr, · line 2979

Quoted 10 lines
drivers/mtd/spi-nor/spi-nor.c +10 -0
+	 * but just the sequence.
+	 */
+	op->addr.nbytes = 4;
+	if (!spi_mem_supports_op(nor->spimem, op)) {
+		/* If flash size <16MB, 3 address bytes are sufficient */
+		if (nor->mtd.size <= SZ_16M) {
+			op->addr.nbytes = 3;
+			if (!spi_mem_supports_op(nor->spimem, op))
+				return -ENOTSUPP;
+		}

this returns success when:

	op->addr.nbytes == 4 && !spi_mem_supports_op(nor->spimem, op) &&
	!(nor->mtd.size <= SZ_16M))

which is wrong.

The patch looks good otherwise!

Quoted 4 lines
drivers/mtd/spi-nor/spi-nor.c +4 -0
+	}
+
+	return 0;
+}