Skip to contentSkip to conversation

mtd: spi-nor: improve support of QSPI nor

RFC In review Cyrille Pitchen 2 participants 9 messages Sep 15, 2015 – Sep 16, 2015

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

v7 cover mtd: spi-nor: improve support of QSPI nor

Hi all,

this series is still a work in progress though it is almost finished. The support of Macronix dummy code to configure the number of dummy cycles is still missing...

However I would like some feedbacks to synchronize this work with other mtd / spi-nor improvements as I see lots of patches on the mailing list.

Best Regards,

Cyrille

Cyrille Pitchen (6):
mtd: spi-nor: remove unused read_xfer/write_xfer hooks
mtd: spi-nor: read JEDEC ID with multiple I/O protocols
mtd: spi-nor: set the read op code and protocol based on the
manufacturer
mtd: m25p80: add support of dual and quad spi protocols to all
commands
Documentation: atmel-quadspi: add binding file for Atmel QSPI driver
mtd: atmel-quadspi: add driver for Atmel QSPI controller

 .../devicetree/bindings/mtd/atmel-quadspi.txt      |  29 +
 drivers/mtd/devices/m25p80.c                       | 262 ++++--
 drivers/mtd/spi-nor/Kconfig                        |   7 +
 drivers/mtd/spi-nor/Makefile                       |   1 +
 drivers/mtd/spi-nor/atmel-quadspi.c                | 880 +++++++++++++++++++++
 drivers/mtd/spi-nor/fsl-quadspi.c                  |   2 +-
 drivers/mtd/spi-nor/nxp-spifi.c                    |  13 +-
 drivers/mtd/spi-nor/spi-nor.c                      | 603 ++++++++++++--
 include/linux/mtd/spi-nor.h                        |  68 +-
 9 files changed, 1689 insertions(+), 176 deletions(-)
 create mode 100644 Documentation/devicetree/bindings/mtd/atmel-quadspi.txt
 create mode 100644 drivers/mtd/spi-nor/atmel-quadspi.c
Signature · 1 line
1.8.2.2
#1 lore raw

RFC v7 1/6 mtd: spi-nor: remove unused read_xfer/write_xfer hooks

struct spi_nor_xfer_cfg and read_xfer/write_xfer hooks were never used by any driver. Do some cleanup by removing them.

---
 include/linux/mtd/spi-nor.h | 35 -----------------------------------
 1 file changed, 35 deletions(-)
include/linux/mtd/spi-nor.h +0 -35
index e9c912d73141..672595a381c5 100644
@@ -87,33 +87,6 @@ enum read_mode {
 	SPI_NOR_QUAD,
 };
 
-/**
- * struct spi_nor_xfer_cfg - Structure for defining a Serial Flash transfer
- * @wren:		command for "Write Enable", or 0x00 for not required
- * @cmd:		command for operation
- * @cmd_pins:		number of pins to send @cmd (1, 2, 4)
- * @addr:		address for operation
- * @addr_pins:		number of pins to send @addr (1, 2, 4)
- * @addr_width:		number of address bytes
- *			(3,4, or 0 for address not required)
- * @mode:		mode data
- * @mode_pins:		number of pins to send @mode (1, 2, 4)
- * @mode_cycles:	number of mode cycles (0 for mode not required)
- * @dummy_cycles:	number of dummy cycles (0 for dummy not required)
- */
-struct spi_nor_xfer_cfg {
-	u8		wren;
-	u8		cmd;
-	u8		cmd_pins;
-	u32		addr;
-	u8		addr_pins;
-	u8		addr_width;
-	u8		mode;
-	u8		mode_pins;
-	u8		mode_cycles;
-	u8		dummy_cycles;
-};
-
 #define SPI_NOR_MAX_CMD_SIZE	8
 enum spi_nor_ops {
 	SPI_NOR_OPS_READ = 0,
@@ -144,14 +117,11 @@ struct mtd_info;
  * @flash_read:		the mode of the read
  * @sst_write_second:	used by the SST write operation
  * @flags:		flag options for the current SPI-NOR (SNOR_F_*)
- * @cfg:		used by the read_xfer/write_xfer
  * @cmd_buf:		used by the write_reg
  * @prepare:		[OPTIONAL] do some preparations for the
  *			read/write/erase/lock/unlock operations
  * @unprepare:		[OPTIONAL] do some post work after the
  *			read/write/erase/lock/unlock operations
- * @read_xfer:		[OPTIONAL] the read fundamental primitive
- * @write_xfer:		[OPTIONAL] the writefundamental primitive
  * @read_reg:		[DRIVER-SPECIFIC] read out the register
  * @write_reg:		[DRIVER-SPECIFIC] write data to the register
  * @read:		[DRIVER-SPECIFIC] read data from the SPI NOR
@@ -176,15 +146,10 @@ struct spi_nor {
 	enum read_mode		flash_read;
 	bool			sst_write_second;
 	u32			flags;
-	struct spi_nor_xfer_cfg	cfg;
 	u8			cmd_buf[SPI_NOR_MAX_CMD_SIZE];
 
 	int (*prepare)(struct spi_nor *nor, enum spi_nor_ops ops);
 	void (*unprepare)(struct spi_nor *nor, enum spi_nor_ops ops);
-	int (*read_xfer)(struct spi_nor *nor, struct spi_nor_xfer_cfg *cfg,
-			 u8 *buf, size_t len);
-	int (*write_xfer)(struct spi_nor *nor, struct spi_nor_xfer_cfg *cfg,
-			  u8 *buf, size_t len);
 	int (*read_reg)(struct spi_nor *nor, u8 opcode, u8 *buf, int len);
 	int (*write_reg)(struct spi_nor *nor, u8 opcode, u8 *buf, int len);
 
Signature · 1 line
1.8.2.2
#2 lore raw

RFC v7 2/6 mtd: spi-nor: read JEDEC ID with multiple I/O protocols

In reply to Cyrille Pitchen

When their quad or dual I/O mode is enabled, Micron and Macronix spi-nor memories don't reply to the regular Read ID (0x9f) command. Instead they reply to a new dedicated command Read ID Multiple I/O (0xaf).

If the Read ID (0x9f) command fails (the read ID is all 1's or all 0's), then the Read ID Multiple I/O (0xaf) is used, first with SPI 4-4-4 protocol (supported by both Micron and Macronix memories), lately with SPI-2-2-2 protocol (supported only by Micron memories).

---
 drivers/mtd/devices/m25p80.c      |  8 +-----
 drivers/mtd/spi-nor/fsl-quadspi.c |  2 +-
 drivers/mtd/spi-nor/nxp-spifi.c   | 13 +++------
 drivers/mtd/spi-nor/spi-nor.c     | 59 ++++++++++++++++++++++++++++++++++-----
 include/linux/mtd/spi-nor.h       | 27 +++++++++++++++---
 5 files changed, 81 insertions(+), 28 deletions(-)
drivers/mtd/devices/m25p80.c +1 -7
index 4b5d7a4655fd..1457866a4930 100644
@@ -179,7 +179,6 @@ static int m25p_probe(struct spi_device *spi)
 	struct flash_platform_data	*data;
 	struct m25p *flash;
 	struct spi_nor *nor;
-	enum read_mode mode = SPI_NOR_NORMAL;
 	char *flash_name = NULL;
 	int ret;
 
@@ -205,11 +204,6 @@ static int m25p_probe(struct spi_device *spi)
 	spi_set_drvdata(spi, flash);
 	flash->spi = spi;
 
-	if (spi->mode & SPI_RX_QUAD)
-		mode = SPI_NOR_QUAD;
-	else if (spi->mode & SPI_RX_DUAL)
-		mode = SPI_NOR_DUAL;
-
 	if (data && data->name)
 		nor->mtd.name = data->name;
 
@@ -223,7 +217,7 @@ static int m25p_probe(struct spi_device *spi)
 	else
 		flash_name = spi->modalias;
 
-	ret = spi_nor_scan(nor, flash_name, mode);
+	ret = spi_nor_scan(nor, flash_name, spi->mode);
 	if (ret)
 		return ret;
 
drivers/mtd/spi-nor/fsl-quadspi.c +1 -1
index 2954f89fc8be..1ded5dbe2240 100644
@@ -1033,7 +1033,7 @@ static int fsl_qspi_probe(struct platform_device *pdev)
 		/* set the chip address for READID */
 		fsl_qspi_set_base_addr(q, nor);
 
-		ret = spi_nor_scan(nor, NULL, SPI_NOR_QUAD);
+		ret = spi_nor_scan(nor, NULL, SPI_RX_QUAD);
 		if (ret)
 			goto mutex_failed;
 
drivers/mtd/spi-nor/nxp-spifi.c +4 -9
index 9e82098ae644..c499f3258245 100644
@@ -272,7 +272,6 @@ static int nxp_spifi_setup_flash(struct nxp_spifi *spifi,
 				 struct device_node *np)
 {
 	struct mtd_part_parser_data ppdata;
-	enum read_mode flash_read;
 	u32 ctrl, property;
 	u16 mode = 0;
 	int ret;
@@ -304,16 +303,12 @@ static int nxp_spifi_setup_flash(struct nxp_spifi *spifi,
 	       SPIFI_CTRL_CSHIGH(15) |
 	       SPIFI_CTRL_FBCLK;
 
-	if (mode & SPI_RX_DUAL) {
+	if (mode & SPI_RX_DUAL)
 		ctrl |= SPIFI_CTRL_DUAL;
-		flash_read = SPI_NOR_DUAL;
-	} else if (mode & SPI_RX_QUAD) {
+	else if (mode & SPI_RX_QUAD)
 		ctrl &= ~SPIFI_CTRL_DUAL;
-		flash_read = SPI_NOR_QUAD;
-	} else {
+	else
 		ctrl |= SPIFI_CTRL_DUAL;
-		flash_read = SPI_NOR_NORMAL;
-	}
 
 	switch (mode & (SPI_CPHA | SPI_CPOL)) {
 	case SPI_MODE_0:
@@ -349,7 +344,7 @@ static int nxp_spifi_setup_flash(struct nxp_spifi *spifi,
 	 */
 	nxp_spifi_dummy_id_read(&spifi->nor);
 
-	ret = spi_nor_scan(&spifi->nor, NULL, flash_read);
+	ret = spi_nor_scan(&spifi->nor, NULL, mode);
 	if (ret) {
 		dev_err(spifi->dev, "device scan failed\n");
 		return ret;
drivers/mtd/spi-nor/spi-nor.c +52 -7

120 lines not on this page · raw message

include/linux/mtd/spi-nor.h +23 -4
index 672595a381c5..32339e82cb9d 100644
@@ -57,8 +57,9 @@
 #define SPINOR_OP_BRWR		0x17	/* Bank register write */
 
 /* Used for Micron flashes only. */
-#define SPINOR_OP_RD_EVCR      0x65    /* Read EVCR register */
-#define SPINOR_OP_WD_EVCR      0x61    /* Write EVCR register */
+#define SPINOR_OP_MIO_RDID	0xaf	/* Multiple I/O Read JEDEC ID */
+#define SPINOR_OP_RD_EVCR	0x65    /* Read EVCR register */
+#define SPINOR_OP_WD_EVCR	0x61    /* Write EVCR register */
 
 /* Status Register bits. */
 #define SR_WIP			1	/* Write in progress */
@@ -87,6 +88,16 @@ enum read_mode {
 	SPI_NOR_QUAD,
 };
 
+enum spi_protocol {
+	SPI_PROTO_1_1_1,	/* SPI */
+	SPI_PROTO_1_1_2,	/* Dual Output */
+	SPI_PROTO_1_1_4,	/* Quad Output */
+	SPI_PROTO_1_2_2,	/* Dual IO */
+	SPI_PROTO_1_4_4,	/* Quad IO */
+	SPI_PROTO_2_2_2,	/* Dual Command */
+	SPI_PROTO_4_4_4,	/* Quad Command */
+};
+
 #define SPI_NOR_MAX_CMD_SIZE	8
 enum spi_nor_ops {
 	SPI_NOR_OPS_READ = 0,
@@ -117,6 +128,10 @@ struct mtd_info;
  * @flash_read:		the mode of the read
  * @sst_write_second:	used by the SST write operation
  * @flags:		flag options for the current SPI-NOR (SNOR_F_*)
+ * @erase_proto:	the SPI protocol used by erase operations
+ * @read_proto:		the SPI protocol used by read operations
+ * @write_proto:	the SPI protocol used by write operations
+ * @reg_proto		the SPI protocol used by read_reg/write_reg operations
  * @cmd_buf:		used by the write_reg
  * @prepare:		[OPTIONAL] do some preparations for the
  *			read/write/erase/lock/unlock operations
@@ -143,6 +158,10 @@ struct spi_nor {
 	u8			read_opcode;
 	u8			read_dummy;
 	u8			program_opcode;
+	enum spi_protocol	erase_proto;
+	enum spi_protocol	read_proto;
+	enum spi_protocol	write_proto;
+	enum spi_protocol	reg_proto;
 	enum read_mode		flash_read;
 	bool			sst_write_second;
 	u32			flags;
@@ -169,7 +188,7 @@ struct spi_nor {
  * spi_nor_scan() - scan the SPI NOR
  * @nor:	the spi_nor structure
  * @name:	the chip type name
- * @mode:	the read mode supported by the driver
+ * @mode:	the bitmask or TX/RX modes supported by the driver
  *
  * The drivers can use this fuction to scan the SPI NOR.
  * In the scanning, it will try to get all the necessary information to
@@ -179,6 +198,6 @@ struct spi_nor {
  *
  * Return: 0 for success, others for failure.
  */
-int spi_nor_scan(struct spi_nor *nor, const char *name, enum read_mode mode);
+int spi_nor_scan(struct spi_nor *nor, const char *name, u16 mode);
 
 #endif
Signature · 1 line
1.8.2.2
#5 lore raw

RFC v7 3/6 mtd: spi-nor: set the read op code and protocol based on the manufacturer

In reply to Cyrille Pitchen

Micron:
Once their Quad SPI protocol enabled, Micron spi-nor memories expect all commands to use the SPI 4-4-4 protocol. Also when the Dual SPI protocol is enabled, all commands must use the SPI 2-2-2 protocol.

Macronix:
When the QPI mode is enabled, all commands must use the SPI 4-4-4 protocol. If the QPI mode is disabled, the Fast Read Dual Output (0x3b) command uses the SPI 1-1-2 protocol whereas other commands use the SPI 1-1-1 protocol.

Spansion:
When Quad I/O operations are enabled, the Fast Read Quad Output (0x6b / 0x6c) commands use the SPI 1-1-4 protocol.
Also when using the Fast Read Dual Output (0x3b / 0x3c) commands, the SPI 1-1-2 protocol must be used. Other commands use the SPI 1-1-1 protocol.

---
 drivers/mtd/spi-nor/spi-nor.c | 544 ++++++++++++++++++++++++++++++++++++------
 include/linux/mtd/spi-nor.h   |  10 +-
 2 files changed, 481 insertions(+), 73 deletions(-)
drivers/mtd/spi-nor/spi-nor.c +473 -71
index 1908038c8f2e..7fbcccb3d02e 100644
@@ -900,27 +900,110 @@ write_err:
 	return ret;
 }
 
-static int macronix_quad_enable(struct spi_nor *nor)
+static int macronix_set_qpi_mode(struct spi_nor *nor, bool enable)
 {
-	int ret, val;
+	int sr, mask, qpi_bit;
+
+	mask = SR_QUAD_EN_MX;
+	qpi_bit = (enable) ? SR_QUAD_EN_MX : 0;
+
+	sr = read_sr(nor);
+	if ((sr & mask) == qpi_bit)
+		return 0;
 
-	val = read_sr(nor);
 	write_enable(nor);
+	write_sr(nor, (sr & ~mask) | qpi_bit);
 
-	write_sr(nor, val | SR_QUAD_EN_MX);
+	/* Set the reg protocol now before accessing any other register. */
+	nor->reg_proto = (enable) ? SPI_PROTO_4_4_4 : SPI_PROTO_1_1_1;
 
-	if (spi_nor_wait_till_ready(nor))
+	if (spi_nor_wait_till_ready(nor)) {
+		dev_err(nor->dev, "Failed to %s the QPI mode.\n",
+			enable ? "enable" : "disable");
 		return 1;
+	}
 
-	ret = read_sr(nor);
-	if (!(ret > 0 && (ret & SR_QUAD_EN_MX))) {
-		dev_err(nor->dev, "Macronix Quad bit not set\n");
+	sr = read_sr(nor);
+	if (!(sr > 0 && ((sr & mask) != qpi_bit))) {
+		dev_err(nor->dev, "Macronix Quad bit was not %s.\n",
+			enable ? "set" : "cleared");
 		return -EINVAL;
 	}
 
 	return 0;
 }
 
+static int macronix_set_quad_io(struct spi_nor *nor)
+{
+	/* Enable the QPI mode if not done yet. */
+	if (macronix_set_qpi_mode(nor, true))
+		return -EINVAL;
+
+	/* Use SPI 4-4-4 protocol for all commands. */
+	nor->read_proto = SPI_PROTO_4_4_4;
+	nor->write_proto = SPI_PROTO_4_4_4;
+	nor->erase_proto = SPI_PROTO_4_4_4;
+
+	/*
+	 * The Fast Read Quad Output 1-1-4 command (0x6b) command is not
+	 * supported in QPI mode, use the Fast Read Quad I/O 1-4-4 (0xeb)
+	 * instead.
+	 */
+	nor->read_opcode = SPINOR_OP_READ_1_4_4;
+
+	return 0;
+}
+
+static int macronix_set_quad_output(struct spi_nor *nor)
+{
+	/* Disable the QPI mode if not done yet. */
+	if (macronix_set_qpi_mode(nor, false))
+		return -EINVAL;
+
+	/* Use the Fast Read Quad Output 1-1-4 command. */
+	nor->read_proto = SPI_PROTO_1_1_4;
+	nor->read_opcode = SPINOR_OP_READ_1_1_4;
+
+	return 0;
+}
+
+static int macronix_set_dual_io(struct spi_nor *nor)
+{
+	/* Disable the QPI mode if not done yet. */
+	if (macronix_set_qpi_mode(nor, false))
+		return -EINVAL;
+
+	/* Use the Fast Read Dual I/O 1-2-2 command. */
+	nor->read_proto = SPI_PROTO_1_2_2;
+	nor->read_opcode = SPINOR_OP_READ_1_2_2;
+
+	return 0;
+}
+
+static int macronix_set_dual_output(struct spi_nor *nor)
+{
+	/* Disable the QPI mode if not done yet. */
+	if (macronix_set_qpi_mode(nor, false))
+		return -EINVAL;
+
+	/* Use the Fast Read Dual Output 1-1-2 command. */
+	nor->read_proto = SPI_PROTO_1_1_2;
+	nor->read_opcode = SPINOR_OP_READ_1_1_2;
+
+	return 0;
+}
+
+static int macronix_set_single(struct spi_nor *nor)
+{
+	/* Disable the QPI mode if not done yet. */
+	if (macronix_set_qpi_mode(nor, false))
+		return -EINVAL;
+
+	nor->read_proto = SPI_PROTO_1_1_1;
+
+	return 0;
+}
+
 /*
  * Write status Register and configuration register with 2 bytes
  * The first byte will be written to the status register, while the
@@ -935,96 +1018,416 @@ static int write_sr_cr(struct spi_nor *nor, u16 val)
 	return nor->write_reg(nor, SPINOR_OP_WRSR, nor->cmd_buf, 2);
 }
 
-static int spansion_quad_enable(struct spi_nor *nor)
+static int spansion_write_cr(struct spi_nor *nor, bool quad_mode, u8 lc)
 {
-	int ret;
-	int quad_en = CR_QUAD_EN_SPAN << 8;
+	int cr, mask, value;
 
-	write_enable(nor);
+	mask = CR_QUAD_EN_SPAN | CR_LC_MASK;
+	value = (lc << 6) & CR_LC_MASK;
+	if (quad_mode)
+		value |= CR_QUAD_EN_SPAN;
 
-	ret = write_sr_cr(nor, quad_en);
-	if (ret < 0) {
+	/* Read the control register */
+	cr = read_cr(nor);
+	if (cr < 0) {
+		dev_err(nor->dev,
+			"error while reading configuration register\n");
+		return -EINVAL;
+	}
+
+	/* Check whether the Quad bit and the Latency Code need to be updated */
+	if ((cr & mask) == value)
+		return 0;
+
+	/* Update the configuration register. */
+	cr = (cr & ~mask) | value;
+	write_enable(nor);
+	if (write_sr_cr(nor, cr << 8) < 0) {
 		dev_err(nor->dev,
 			"error while writing configuration register\n");
 		return -EINVAL;
 	}
 
 	/* read back and check it */
-	ret = read_cr(nor);
-	if (!(ret > 0 && (ret & CR_QUAD_EN_SPAN))) {
-		dev_err(nor->dev, "Spansion Quad bit not set\n");
+	cr = read_cr(nor);
+	if (!(cr > 0 && ((cr & mask) == value))) {
+		dev_err(nor->dev,
+			"error while updating configuration register\n");
 		return -EINVAL;
 	}
 
 	return 0;
 }
 
-static int micron_quad_enable(struct spi_nor *nor)
+static int spansion_set_quad_output(struct spi_nor *nor)
+{
+	int ret;
+
+	/*
+	 * Set the Latency Code to 0 so the memory expects 8 dummy cycles when
+	 * processing a Fast Read Quad Output 1-1-4 command.
+	 */
+	ret = spansion_write_cr(nor, true, 0);
+	if (ret)
+		return ret;
+
+	/* Use the Fast Read Quad Output 1-1-4 command. */
+	nor->read_proto = SPI_PROTO_1_1_4;
+	nor->read_opcode = SPINOR_OP_READ_1_1_4;
+
+	return 0;
+}
+
+static int spansion_set_dual_output(struct spi_nor *nor)
+{
+	int ret;
+
+	/*
+	 * Set the Latency Code to 0 so the memory expects 8 dummy cycles when
+	 * processing a Fast Read Dual Output 1-1-2 command.
+	 */
+	ret = spansion_write_cr(nor, false, 0);
+	if (ret)
+		return ret;
+
+	/* Use the Fast Read Dual Output 1-1-2 command. */
+	nor->read_proto = SPI_PROTO_1_1_2;
+	nor->read_opcode = SPINOR_OP_READ_1_1_2;
+
+	return 0;
+}
+
+static int spansion_set_single(struct spi_nor *nor)
 {
 	int ret;
-	u8 val;
 
-	ret = nor->read_reg(nor, SPINOR_OP_RD_EVCR, &val, 1);
+	/*
+	 * Set the Latency Code to 0 so the memory expects 8 dummy cycles when
+	 * processing a Fast Read 1-1-1 command. The Latency Code is not
+	 * relevant for Read command since no dummy cycle is expected.
+	 */
+	ret = spansion_write_cr(nor, false, 0);
+	if (ret)
+		return ret;
+
+	nor->read_proto = SPI_PROTO_1_1_1;
+
+	return 0;
+}
+
+static int micron_set_dummy_cycles(struct spi_nor *nor, u8 num_dummy_cycles)
+{
+	u8 vcr, val, mask;
+	int ret;
+
+	mask = GENMASK(7, 4);
+	val = (num_dummy_cycles << 4) & mask;
+
+	/* Read the Volatile Configuration Register (VCR). */
+	ret = nor->read_reg(nor, SPINOR_OP_RD_VCR, &vcr, 1);
 	if (ret < 0) {
-		dev_err(nor->dev, "error %d reading EVCR\n", ret);
+		dev_err(nor->dev, "error while reading VCR register\n");
 		return ret;
 	}
 
+	/* Check whether we need to update the number of dummy cycles. */
+	if ((vcr & mask) == val)
+		return 0;
+
+	/* Update the number of dummy into the VCR. */
 	write_enable(nor);
+	vcr = (vcr & ~mask) | val;
+	ret = nor->write_reg(nor, SPINOR_OP_WR_VCR, &vcr, 1);
+	if (ret < 0) {
+		dev_err(nor->dev, "error while writing VCR register\n");
+		return ret;
+	}
+
+	ret = spi_nor_wait_till_ready(nor);
+	if (ret)
+		return ret;
 
-	/* set EVCR, enable quad I/O */
-	nor->cmd_buf[0] = val & ~EVCR_QUAD_EN_MICRON;
-	ret = nor->write_reg(nor, SPINOR_OP_WD_EVCR, nor->cmd_buf, 1);
+	/* Read VCR and check it. */
+	ret = nor->read_reg(nor, SPINOR_OP_RD_VCR, &vcr, 1);
+	if (ret < 0) {
+		dev_err(nor->dev, "error while reading VCR\n");
+		return ret;
+	}
+	if ((vcr & mask) != val) {
+		dev_err(nor->dev, "Micron VCR dummy cycles not updated\n");
+		return -EINVAL;
+	}
+
+	return 0;
+}
+
+static int micron_set_protocol(struct spi_nor *nor, u8 val,
+			       enum spi_protocol proto)
+{
+	u8 evcr, mask;
+	int ret;
+
+	mask = EVCR_QUAD_EN_MICRON | EVCR_DUAL_EN_MICRON;
+
+	/* Read the Exhanced Volatile Configuration Register (EVCR). */
+	ret = nor->read_reg(nor, SPINOR_OP_RD_EVCR, &evcr, 1);
+	if (ret < 0) {
+		dev_err(nor->dev, "error while reading EVCR register\n");
+		return ret;
+	}
+
+	/* Check whether we need to update the protocol bits. */
+	if ((evcr & mask) == val)
+		return 0;
+
+	/* Set EVCR protocol bits. */
+	write_enable(nor);
+	evcr = (evcr & ~mask) | val;
+	ret = nor->write_reg(nor, SPINOR_OP_WD_EVCR, &evcr, 1);
 	if (ret < 0) {
 		dev_err(nor->dev, "error while writing EVCR register\n");
 		return ret;
 	}
 
+	/* Switch reg protocol now before accessing any other registers. */
+	nor->reg_proto = proto;
+
 	ret = spi_nor_wait_till_ready(nor);
 	if (ret)
 		return ret;
 
-	/* read EVCR and check it */
-	ret = nor->read_reg(nor, SPINOR_OP_RD_EVCR, &val, 1);
+	/* Read EVCR and check it. */
+	ret = nor->read_reg(nor, SPINOR_OP_RD_EVCR, &evcr, 1);
 	if (ret < 0) {
-		dev_err(nor->dev, "error %d reading EVCR\n", ret);
+		dev_err(nor->dev, "error while reading EVCR\n");
 		return ret;
 	}
-	if (val & EVCR_QUAD_EN_MICRON) {
-		dev_err(nor->dev, "Micron EVCR Quad bit not clear\n");
+	if ((evcr & mask) != val) {
+		dev_err(nor->dev, "Micron EVCR protocol bits not updated\n");
 		return -EINVAL;
 	}
 
 	return 0;
 }
 
-static int set_quad_mode(struct spi_nor *nor, const struct flash_info *info)
+static inline int micron_set_extended_spi_protocol(struct spi_nor *nor)
 {
-	int status;
+	int ret;
 
+	/* Set both the Quad and Dual bits to select the Extended SPI mode */
+	ret = micron_set_protocol(nor,
+				  EVCR_QUAD_EN_MICRON | EVCR_DUAL_EN_MICRON,
+				  SPI_PROTO_1_1_1);
+	if (ret) {
+		dev_err(nor->dev, "Failed to set Micron Extended SPI mode\n");
+		return ret;
+	}
+
+	return 0;
+}
+
+static int micron_set_quad_io(struct spi_nor *nor)
+{
+	int ret;
+
+	/* Clear at least the Quad bit to enable quad mode */
+	ret = micron_set_protocol(nor,
+				  EVCR_DUAL_EN_MICRON,
+				  SPI_PROTO_4_4_4);
+	if (ret) {
+		dev_err(nor->dev, "Failed to set Micron Quad mode\n");
+		return ret;
+	}
+
+	/* Force the number of dummy cycles to 8 */
+	ret = micron_set_dummy_cycles(nor, 8);
+	if (ret)
+		return ret;
+
+	/* Use SPI 4-4-4 protocol for all commands. */
+	nor->read_proto = SPI_PROTO_4_4_4;
+	nor->write_proto = SPI_PROTO_4_4_4;
+	nor->erase_proto = SPI_PROTO_4_4_4;
+
+	/*
+	 * The Fast Read Quad Output 1-1-4 command (0x6b) is processed with
+	 * SPI 4-4-4 protocol.
+	 */
+	nor->read_opcode = SPINOR_OP_READ_1_1_4;
+
+	return 0;
+}
+
+static int micron_set_quad_output(struct spi_nor *nor)
+{
+	int ret;
+
+	ret = micron_set_extended_spi_protocol(nor);
+	if (ret)
+		return ret;
+
+	/* Force the number of dummy cycles to 8 */
+	ret = micron_set_dummy_cycles(nor, 8);
+	if (ret)
+		return ret;
+
+	/* Use the Fast Read Quad Output 1-1-4 command. */
+	nor->read_proto = SPI_PROTO_1_1_4;
+	nor->read_opcode = SPINOR_OP_READ_1_1_4;
+
+	return 0;
+}
+
+static int micron_set_dual_io(struct spi_nor *nor)
+{
+	int ret;
+
+	/* Clear Dual bit but keep Quad bit set to enable dual mode */
+	ret = micron_set_protocol(nor,
+				  EVCR_QUAD_EN_MICRON,
+				  SPI_PROTO_2_2_2);
+	if (ret) {
+		dev_err(nor->dev, "Failed to set Micron Dual mode\n");
+		return ret;
+	}
+
+	/* Force the number of dummy cycles to 8 */
+	ret = micron_set_dummy_cycles(nor, 8);
+	if (ret)
+		return ret;
+
+	/* Use SPI 2-2-2 protocol for all commands. */
+	nor->read_proto = SPI_PROTO_2_2_2;
+	nor->write_proto = SPI_PROTO_2_2_2;
+	nor->erase_proto = SPI_PROTO_2_2_2;
+
+	/*
+	 * The Fast Read Dual Output 1-1-2 command (0x3b) is processed with
+	 * SPI 2-2-2 protocol.
+	 */
+	nor->read_opcode = SPINOR_OP_READ_1_1_2;
+
+	return 0;
+}
+
+static int micron_set_dual_output(struct spi_nor *nor)
+{
+	int ret;
+
+	ret = micron_set_extended_spi_protocol(nor);
+	if (ret)
+		return ret;
+
+	/* Force the number of dummy cycles to 8 */
+	ret = micron_set_dummy_cycles(nor, 8);
+	if (ret)
+		return ret;
+
+	/* Use the Fast Read Dual Output 1-1-4 command. */
+	nor->read_proto = SPI_PROTO_1_1_2;
+	nor->read_opcode = SPINOR_OP_READ_1_1_2;
+
+	return 0;
+}
+
+static int micron_set_single(struct spi_nor *nor)
+{
+	int ret;
+
+	ret = micron_set_extended_spi_protocol(nor);
+	if (ret)
+		return ret;
+
+	/* Force the number of dummy cycles to 8 (Fast Read only) */
+	ret = micron_set_dummy_cycles(nor, 8);
+	if (ret)
+		return ret;
+
+	nor->read_proto = SPI_PROTO_1_1_1;
+
+	return 0;
+}
+
+static int set_quad_mode(struct spi_nor *nor, const struct flash_info *info,
+			 u16 mode)
+{
 	switch (JEDEC_MFR(info)) {
 	case CFI_MFR_MACRONIX:
-		status = macronix_quad_enable(nor);
-		if (status) {
-			dev_err(nor->dev, "Macronix quad-read not enabled\n");
-			return -EINVAL;
-		}
-		return status;
+		if (mode & SPI_TX_QUAD)
+			return macronix_set_quad_io(nor);
+		return macronix_set_quad_output(nor);
+
 	case CFI_MFR_ST:
-		status = micron_quad_enable(nor);
-		if (status) {
-			dev_err(nor->dev, "Micron quad-read not enabled\n");
-			return -EINVAL;
-		}
-		return status;
+		if (mode & SPI_TX_QUAD)
+			return micron_set_quad_io(nor);
+		return micron_set_quad_output(nor);
+
+	case CFI_MFR_AMD:
+		/*
+		 * Don't use the Fast Read Quad I/O (0xeb / 0xec) commands as
+		 * their number of dummy cycles is not a multiple of 8. Some
+		 * SPI controllers, especially those relying on the m25p80
+		 * driver, expect the number of dummy cycles to be a multiple
+		 * of 8.
+		 */
+		return spansion_set_quad_output(nor);
+
 	default:
-		status = spansion_quad_enable(nor);
-		if (status) {
-			dev_err(nor->dev, "Spansion quad-read not enabled\n");
-			return -EINVAL;
-		}
-		return status;
+		break;
+	}
+
+	return -EINVAL;
+}
+
+static int set_dual_mode(struct spi_nor *nor, const struct flash_info *info,
+			 u16 mode)
+{
+	switch (JEDEC_MFR(info)) {
+	case CFI_MFR_MACRONIX:
+		if (mode & SPI_TX_DUAL)
+			return macronix_set_dual_io(nor);
+		return macronix_set_dual_output(nor);
+
+	case CFI_MFR_ST:
+		if (mode & SPI_TX_DUAL)
+			return micron_set_dual_io(nor);
+		return micron_set_dual_output(nor);
+
+	case CFI_MFR_AMD:
+		/*
+		 * Don't use the Fast Read Dual I/O (0xbb / 0xbc) commands as
+		 * their number of dummy cycles is not a multiple of 8. Some
+		 * SPI controllers, especially those relying on the m25p80
+		 * driver, expect the number of dummy cycles to be a multiple
+		 * of 8.
+		 */
+		return spansion_set_dual_output(nor);
+
+	default:
+		break;
+	}
+
+	return -EINVAL;
+}
+
+static int set_single_mode(struct spi_nor *nor, const struct flash_info *info,
+			   u16 mode)
+{
+	switch (JEDEC_MFR(info)) {
+	case CFI_MFR_MACRONIX:
+		return macronix_set_single(nor);
+
+	case CFI_MFR_ST:
+		return micron_set_single(nor);
+
+	case CFI_MFR_AMD:
+		return spansion_set_single(nor);
+
+	default:
+		break;
 	}
+
+	return -EINVAL;
 }
 
 static int spi_nor_check(struct spi_nor *nor)
@@ -1170,39 +1573,38 @@ int spi_nor_scan(struct spi_nor *nor, const char *name, u16 mode)
 	if (info->flags & SPI_NOR_NO_FR)
 		nor->flash_read = SPI_NOR_NORMAL;
 
+	/* Default commands */
+	nor->program_opcode = SPINOR_OP_PP;
+	if (nor->flash_read == SPI_NOR_NORMAL)
+		nor->read_opcode = SPINOR_OP_READ;
+	else
+		nor->read_opcode = SPINOR_OP_READ_FAST;
+
 	/* Quad/Dual-read mode takes precedence over fast/normal */
 	if (mode & SPI_RX_QUAD && info->flags & SPI_NOR_QUAD_READ) {
-		ret = set_quad_mode(nor, info);
+		/* At least SPI 1-1-4 should be supported */
+		ret = set_quad_mode(nor, info, mode);
 		if (ret) {
 			dev_err(dev, "quad mode not supported\n");
 			return ret;
 		}
 		nor->flash_read = SPI_NOR_QUAD;
 	} else if (mode & SPI_RX_DUAL && info->flags & SPI_NOR_DUAL_READ) {
+		/* At lest SPI 1-1-2 should be supported */
+		ret = set_dual_mode(nor, info, mode);
+		if (ret) {
+			dev_err(dev, "dual mode not supported\n");
+			return ret;
+		}
 		nor->flash_read = SPI_NOR_DUAL;
+	} else if (info->flags & (SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ)) {
+		ret = set_single_mode(nor, info, mode);
+		if (ret) {
+			dev_err(dev, "failed to switch back to single mode\n");
+			return ret;
+		}
 	}
 
-	/* Default commands */
-	switch (nor->flash_read) {
-	case SPI_NOR_QUAD:
-		nor->read_opcode = SPINOR_OP_READ_1_1_4;
-		break;
-	case SPI_NOR_DUAL:
-		nor->read_opcode = SPINOR_OP_READ_1_1_2;
-		break;
-	case SPI_NOR_FAST:
-		nor->read_opcode = SPINOR_OP_READ_FAST;
-		break;
-	case SPI_NOR_NORMAL:
-		nor->read_opcode = SPINOR_OP_READ;
-		break;
-	default:
-		dev_err(dev, "No Read opcode defined\n");
-		return -EINVAL;
-	}
-
-	nor->program_opcode = SPINOR_OP_PP;
-
 	if (info->addr_width)
 		nor->addr_width = info->addr_width;
 	else if (mtd->size > 0x1000000) {
include/linux/mtd/spi-nor.h +8 -2
index 32339e82cb9d..1cdc905a3077 100644
@@ -24,8 +24,10 @@
 #define SPINOR_OP_WRSR		0x01	/* Write status register 1 byte */
 #define SPINOR_OP_READ		0x03	/* Read data bytes (low frequency) */
 #define SPINOR_OP_READ_FAST	0x0b	/* Read data bytes (high frequency) */
-#define SPINOR_OP_READ_1_1_2	0x3b	/* Read data bytes (Dual SPI) */
-#define SPINOR_OP_READ_1_1_4	0x6b	/* Read data bytes (Quad SPI) */
+#define SPINOR_OP_READ_1_1_2	0x3b	/* Read data bytes (Dual Output SPI) */
+#define SPINOR_OP_READ_1_2_2	0xbb	/* Read data bytes (Dual I/O SPI) */
+#define SPINOR_OP_READ_1_1_4	0x6b	/* Read data bytes (Quad Out SPI) */
+#define SPINOR_OP_READ_1_4_4	0xeb	/* Read data bytes (Quad I/O SPI) */
 #define SPINOR_OP_PP		0x02	/* Page program (up to 256 bytes) */
 #define SPINOR_OP_BE_4K		0x20	/* Erase 4KiB block */
 #define SPINOR_OP_BE_4K_PMC	0xd7	/* Erase 4KiB block on PMC chips */
@@ -58,6 +60,8 @@
 
 /* Used for Micron flashes only. */
 #define SPINOR_OP_MIO_RDID	0xaf	/* Multiple I/O Read JEDEC ID */
+#define SPINOR_OP_RD_VCR	0x85	/* Read VCR register */
+#define SPINOR_OP_WR_VCR	0x81	/* Write VCR register */
 #define SPINOR_OP_RD_EVCR	0x65    /* Read EVCR register */
 #define SPINOR_OP_WD_EVCR	0x61    /* Write EVCR register */
 
@@ -74,12 +78,14 @@
 
 /* Enhanced Volatile Configuration Register bits */
 #define EVCR_QUAD_EN_MICRON    0x80    /* Micron Quad I/O */
+#define EVCR_DUAL_EN_MICRON    0x40    /* Micron Dual I/O */
 
 /* Flag Status Register bits */
 #define FSR_READY		0x80
 
 /* Configuration Register bits. */
 #define CR_QUAD_EN_SPAN		0x2	/* Spansion Quad I/O */
+#define CR_LC_MASK		0xc0	/* Spansion Latency Code */
 
 enum read_mode {
 	SPI_NOR_NORMAL = 0,
Signature · 1 line
1.8.2.2
#6 lore raw

RFC v7 4/6 mtd: m25p80: add support of dual and quad spi protocols to all commands

In reply to Cyrille Pitchen

Before this patch, m25p80_read() supported few SPI protocols:
- regular SPI 1-1-1
- SPI Dual Output 1-1-2
- SPI Quad Output 1-1-4
On the other hand, all other m25p80_*() hooks only supported SPI 1-1-1.

However once their Quad mode enabled, Micron and Macronix spi-nor memories expect all commands to use the SPI 4-4-4 protocol.

Also, once their Dual mode enabled, Micron spi-nor memories expect all commands to use the SPI-2-2-2 protocol.

So this patch adds support to all currently existing SPI protocols to cover as many protocols as possible.

---
 drivers/mtd/devices/m25p80.c | 254 ++++++++++++++++++++++++++++++++++++-------
 1 file changed, 212 insertions(+), 42 deletions(-)
drivers/mtd/devices/m25p80.c +212 -42
index 1457866a4930..96072b33b29f 100644
@@ -27,22 +27,110 @@
 #include <linux/spi/flash.h>
 #include <linux/mtd/spi-nor.h>
 
-#define	MAX_CMD_SIZE		6
+#define	MAX_CMD_SIZE		8
 struct m25p {
 	struct spi_device	*spi;
 	struct spi_nor		spi_nor;
 	u8			command[MAX_CMD_SIZE];
 };
 
+static inline int m25p80_proto2nbits(enum spi_protocol proto,
+				     unsigned *code_nbits,
+				     unsigned *addr_nbits,
+				     unsigned *data_nbits)
+{
+	unsigned code, addr, data;
+
+	switch (proto) {
+	case SPI_PROTO_1_1_1:
+		code = SPI_NBITS_SINGLE;
+		addr = SPI_NBITS_SINGLE;
+		data = SPI_NBITS_SINGLE;
+		break;
+
+	case SPI_PROTO_1_1_2:
+		code = SPI_NBITS_SINGLE;
+		addr = SPI_NBITS_SINGLE;
+		data = SPI_NBITS_DUAL;
+		break;
+
+	case SPI_PROTO_1_1_4:
+		code = SPI_NBITS_SINGLE;
+		addr = SPI_NBITS_SINGLE;
+		data = SPI_NBITS_QUAD;
+		break;
+
+	case SPI_PROTO_1_2_2:
+		code = SPI_NBITS_SINGLE;
+		addr = SPI_NBITS_DUAL;
+		data = SPI_NBITS_DUAL;
+		break;
+
+	case SPI_PROTO_1_4_4:
+		code = SPI_NBITS_SINGLE;
+		addr = SPI_NBITS_QUAD;
+		data = SPI_NBITS_QUAD;
+		break;
+
+	case SPI_PROTO_2_2_2:
+		code = SPI_NBITS_DUAL;
+		addr = SPI_NBITS_DUAL;
+		data = SPI_NBITS_DUAL;
+		break;
+
+	case SPI_PROTO_4_4_4:
+		code = SPI_NBITS_QUAD;
+		addr = SPI_NBITS_QUAD;
+		data = SPI_NBITS_QUAD;
+		break;
+
+	default:
+		return -EINVAL;
+
+	}
+
+	if (code_nbits)
+		*code_nbits = code;
+	if (addr_nbits)
+		*addr_nbits = addr;
+	if (data_nbits)
+		*data_nbits = data;
+
+	return 0;
+}
+
 static int m25p80_read_reg(struct spi_nor *nor, u8 code, u8 *val, int len)
 {
 	struct m25p *flash = nor->priv;
 	struct spi_device *spi = flash->spi;
+	unsigned code_nbits, data_nbits;
+	struct spi_transfer xfers[2];
 	int ret;
 
-	ret = spi_write_then_read(spi, &code, 1, val, len);
+	/* Get transfer protocols (addr_nbits is not relevant here). */
+	ret = m25p80_proto2nbits(nor->reg_proto,
+				 &code_nbits, NULL, &data_nbits);
+	if (ret < 0)
+		return ret;
+
+	/* Set up transfers. */
+	memset(xfers, 0, sizeof(xfers));
+
+	flash->command[0] = code;
+	xfers[0].len = 1;
+	xfers[0].tx_buf = flash->command;
+	xfers[0].tx_nbits = code_nbits;
+
+	xfers[1].len = len;
+	xfers[1].rx_buf = &flash->command[1];
+	xfers[1].rx_nbits = data_nbits;
+
+	/* Process command. */
+	ret = spi_sync_transfer(spi, xfers, 2);
 	if (ret < 0)
 		dev_err(&spi->dev, "error %d reading %x\n", ret, code);
+	else
+		memcpy(val, &flash->command[1], len);
 
 	return ret;
 }
@@ -65,12 +153,38 @@ static int m25p80_write_reg(struct spi_nor *nor, u8 opcode, u8 *buf, int len)
 {
 	struct m25p *flash = nor->priv;
 	struct spi_device *spi = flash->spi;
+	unsigned code_nbits, data_nbits, num_xfers = 1;
+	struct spi_transfer xfers[2];
+	int ret;
+
+	/* Get transfer protocols (addr_nbits is not relevant here). */
+	ret = m25p80_proto2nbits(nor->reg_proto,
+				 &code_nbits, NULL, &data_nbits);
+	if (ret < 0)
+		return ret;
+
+	/* Set up transfer(s). */
+	memset(xfers, 0, sizeof(xfers));
 
 	flash->command[0] = opcode;
-	if (buf)
+	xfers[0].len = 1;
+	xfers[0].tx_buf = flash->command;
+	xfers[0].tx_nbits = code_nbits;
+
+	if (buf) {
 		memcpy(&flash->command[1], buf, len);
+		if (data_nbits == code_nbits) {
+			xfers[0].len += len;
+		} else {
+			xfers[1].len = len;
+			xfers[1].tx_buf = &flash->command[1];
+			xfers[1].tx_nbits = data_nbits;
+			num_xfers++;
+		}
+	}
 
-	return spi_write(spi, flash->command, len + 1);
+	/* Process command. */
+	return spi_sync_transfer(spi, xfers, num_xfers);
 }
 
 static void m25p80_write(struct spi_nor *nor, loff_t to, size_t len,
@@ -78,43 +192,54 @@ static void m25p80_write(struct spi_nor *nor, loff_t to, size_t len,
 {
 	struct m25p *flash = nor->priv;
 	struct spi_device *spi = flash->spi;
-	struct spi_transfer t[2] = {};
+	unsigned code_nbits, addr_nbits, data_nbits, num_xfers = 1;
+	struct spi_transfer xfers[3];
 	struct spi_message m;
-	int cmd_sz = m25p_cmdsz(nor);
-
-	spi_message_init(&m);
+	int ret, cmd_sz = m25p_cmdsz(nor);
 
 	if (nor->program_opcode == SPINOR_OP_AAI_WP && nor->sst_write_second)
 		cmd_sz = 1;
 
-	flash->command[0] = nor->program_opcode;
-	m25p_addr2cmd(nor, to, flash->command);
+	/* Get transfer protocols. */
+	ret = m25p80_proto2nbits(nor->write_proto,
+				 &code_nbits, &addr_nbits, &data_nbits);
+	if (ret < 0) {
+		*retlen = 0;
+		return;
+	}
+
+	/* Set up transfers. */
+	memset(xfers, 0, sizeof(xfers));
 
-	t[0].tx_buf = flash->command;
-	t[0].len = cmd_sz;
-	spi_message_add_tail(&t[0], &m);
+	flash->command[0] = nor->program_opcode;
+	xfers[0].len = 1;
+	xfers[0].tx_buf = flash->command;
+	xfers[0].tx_nbits = code_nbits;
+
+	if (cmd_sz > 1) {
+		m25p_addr2cmd(nor, to, flash->command);
+		if (addr_nbits == code_nbits) {
+			xfers[0].len += nor->addr_width;
+		} else {
+			xfers[1].len = nor->addr_width;
+			xfers[1].tx_buf = &flash->command[1];
+			xfers[1].tx_nbits = addr_nbits;
+			num_xfers++;
+		}
+	}
 
-	t[1].tx_buf = buf;
-	t[1].len = len;
-	spi_message_add_tail(&t[1], &m);
+	xfers[num_xfers].len = len;
+	xfers[num_xfers].tx_buf = buf;
+	xfers[num_xfers].tx_nbits = data_nbits;
+	num_xfers++;
 
+	/* Process command. */
+	spi_message_init_with_transfers(&m, xfers, num_xfers);
 	spi_sync(spi, &m);
 
 	*retlen += m.actual_length - cmd_sz;
 }
 
-static inline unsigned int m25p80_rx_nbits(struct spi_nor *nor)
-{
-	switch (nor->flash_read) {
-	case SPI_NOR_DUAL:
-		return 2;
-	case SPI_NOR_QUAD:
-		return 4;
-	default:
-		return 0;
-	}
-}
-
 /*
  * Read an address range from the nor chip.  The address range
  * may be any size provided it is within the physical boundaries.
@@ -124,28 +249,48 @@ static int m25p80_read(struct spi_nor *nor, loff_t from, size_t len,
 {
 	struct m25p *flash = nor->priv;
 	struct spi_device *spi = flash->spi;
-	struct spi_transfer t[2];
-	struct spi_message m;
+	unsigned code_nbits, addr_nbits, data_nbits, num_xfers = 1;
 	unsigned int dummy = nor->read_dummy;
+	struct spi_transfer xfers[3];
+	struct spi_message m;
+	int ret;
+
+	/* Get transfer protocols. */
+	ret = m25p80_proto2nbits(nor->read_proto,
+				 &code_nbits, &addr_nbits, &data_nbits);
+	if (ret < 0) {
+		*retlen = 0;
+		return ret;
+	}
 
 	/* convert the dummy cycles to the number of bytes */
 	dummy /= 8;
 
-	spi_message_init(&m);
-	memset(t, 0, (sizeof t));
+	/* Set up transfers. */
+	memset(xfers, 0, sizeof(xfers));
 
 	flash->command[0] = nor->read_opcode;
-	m25p_addr2cmd(nor, from, flash->command);
+	xfers[0].len = 1;
+	xfers[0].tx_buf = flash->command;
+	xfers[0].tx_nbits = code_nbits;
 
-	t[0].tx_buf = flash->command;
-	t[0].len = m25p_cmdsz(nor) + dummy;
-	spi_message_add_tail(&t[0], &m);
+	m25p_addr2cmd(nor, from, flash->command);
+	if (addr_nbits == code_nbits) {
+		xfers[0].len += nor->addr_width + dummy;
+	} else {
+		xfers[1].len = nor->addr_width + dummy;
+		xfers[1].tx_buf = &flash->command[1];
+		xfers[1].tx_nbits = addr_nbits;
+		num_xfers++;
+	}
 
-	t[1].rx_buf = buf;
-	t[1].rx_nbits = m25p80_rx_nbits(nor);
-	t[1].len = len;
-	spi_message_add_tail(&t[1], &m);
+	xfers[num_xfers].len = len;
+	xfers[num_xfers].rx_buf = buf;
+	xfers[num_xfers].rx_nbits = data_nbits;
+	num_xfers++;
 
+	/* Process command. */
+	spi_message_init_with_transfers(&m, xfers, num_xfers);
 	spi_sync(spi, &m);
 
 	*retlen = m.actual_length - m25p_cmdsz(nor) - dummy;
@@ -155,15 +300,40 @@ static int m25p80_read(struct spi_nor *nor, loff_t from, size_t len,
 static int m25p80_erase(struct spi_nor *nor, loff_t offset)
 {
 	struct m25p *flash = nor->priv;
+	struct spi_device *spi = flash->spi;
+	unsigned code_nbits, addr_nbits, num_xfers = 1;
+	struct spi_transfer xfers[2];
+	int ret;
 
 	dev_dbg(nor->dev, "%dKiB at 0x%08x\n",
 		flash->spi_nor.mtd.erasesize / 1024, (u32)offset);
 
-	/* Set up command buffer. */
+	/* Get transfer protocols (data_nbits is not relevant here). */
+	ret = m25p80_proto2nbits(nor->erase_proto,
+				 &code_nbits, &addr_nbits, NULL);
+	if (ret < 0)
+		return ret;
+
+	/* Set up transfers. */
+	memset(xfers, 0, sizeof(xfers));
+
 	flash->command[0] = nor->erase_opcode;
+	xfers[0].len = 1;
+	xfers[0].tx_buf = flash->command;
+	xfers[0].tx_nbits = code_nbits;
+
 	m25p_addr2cmd(nor, offset, flash->command);
+	if (code_nbits == addr_nbits) {
+		xfers[0].len += nor->addr_width;
+	} else {
+		xfers[1].len = nor->addr_width;
+		xfers[1].tx_buf = &flash->command[1];
+		xfers[1].tx_nbits = addr_nbits;
+		num_xfers++;
+	}
 
-	spi_write(flash->spi, flash->command, m25p_cmdsz(nor));
+	/* Process command. */
+	spi_sync_transfer(spi, xfers, num_xfers);
 
 	return 0;
 }
Signature · 1 line
1.8.2.2
#7 lore raw

RFC v7 5/6 Documentation: atmel-quadspi: add binding file for Atmel QSPI driver

In reply to Cyrille Pitchen

This patch documents the DT bindings for the driver of the Atmel QSPI controller embedded inside sama5d2x SoCs.

---
 .../devicetree/bindings/mtd/atmel-quadspi.txt      | 29 ++++++++++++++++++++++
 1 file changed, 29 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/mtd/atmel-quadspi.txt
Documentation/devicetree/bindings/mtd/atmel-quadspi.txt new file +29 -0
new file mode 100644
index 000000000000..0b8d545bb198
@@ -0,0 +1,29 @@
+* Atmel Quad Serial Peripheral Interface (QSPI)
+
+Required properties:
+- compatible:     should be "atmel,sama5d2-qspi"
+- reg:            the first contains the register location and length,
+                  the second contains the memory mapping address and length
+- interrupts:     should contain the interrupt for the device
+- clocks:         the phandle of the clock needed by the QSPI controller
+- #address-cells: should be <1>
+- #size-cells:    should be <0>
+
+Example:
+
+spi at f0020000 {
+	compatible = "atmel,sama5d2-qspi";
+	reg = <0xf0020000 0x100>,
+	      <0xd0000000 0x8000000>;
+	interrupts = <52 IRQ_TYPE_LEVEL_HIGH 7>;
+	clocks = <&spi0_clk>;
+	#address-cells = <1>;
+	#size-cells = <0>;
+	pinctrl-names = "default";
+	pinctrl-0 = <&pinctrl_spi0_default>;
+	status = "okay";
+
+	m25p80 at 0 {
+		...
+	};
+};
Signature · 1 line
1.8.2.2
#8 lore raw

RFC v7 6/6 mtd: atmel-quadspi: add driver for Atmel QSPI controller

In reply to Cyrille Pitchen

This driver add support to the new Atmel QSPI controller embedded into sama5d2x SoCs. It expects a NOR memory to be connected to the QSPI controller.

---
 drivers/mtd/spi-nor/Kconfig         |   7 +
 drivers/mtd/spi-nor/Makefile        |   1 +
 drivers/mtd/spi-nor/atmel-quadspi.c | 880 ++++++++++++++++++++++++++++++++++++
 3 files changed, 888 insertions(+)
 create mode 100644 drivers/mtd/spi-nor/atmel-quadspi.c
drivers/mtd/spi-nor/Kconfig +7 -0
index 89bf4c1faa2b..7a3d55429550 100644
@@ -29,6 +29,13 @@ config SPI_FSL_QUADSPI
 	  This controller does not support generic SPI. It only supports
 	  SPI NOR.
 
+config SPI_ATMEL_QUADSPI
+	tristate "Atmel Quad SPI Controller"
+	depends on OF && HAS_DMA && (ARCH_AT91 || COMPILE_TEST)
+	help
+	  This enables support for the Quad SPI controller in master mode.
+	  We only connect the NOR to this controller now.
+
 config SPI_NXP_SPIFI
 	tristate "NXP SPI Flash Interface (SPIFI)"
 	depends on OF && (ARCH_LPC18XX || COMPILE_TEST)
drivers/mtd/spi-nor/Makefile +1 -0
index e53333ef8582..f5d23d7379bb 100644
@@ -1,3 +1,4 @@
 obj-$(CONFIG_MTD_SPI_NOR)	+= spi-nor.o
 obj-$(CONFIG_SPI_FSL_QUADSPI)	+= fsl-quadspi.o
+obj-$(CONFIG_SPI_ATMEL_QUADSPI)	+= atmel-quadspi.o
 obj-$(CONFIG_SPI_NXP_SPIFI)	+= nxp-spifi.o
drivers/mtd/spi-nor/atmel-quadspi.c new file +880 -0

880 lines not on this page · raw message

Signature · 1 line
1.8.2.2
#3 lore raw

Re: [linux-next RFC v7 2/6] mtd: spi-nor: read JEDEC ID with multiple I/O protocols

In reply to Cyrille Pitchen
Quoted 43 lines · Cyrille Pitchen

When their quad or dual I/O mode is enabled, Micron and Macronix spi-nor memories don't reply to the regular Read ID (0x9f) command. Instead they reply to a new dedicated command Read ID Multiple I/O (0xaf).

If the Read ID (0x9f) command fails (the read ID is all 1's or all 0's), then the Read ID Multiple I/O (0xaf) is used, first with SPI 4-4-4 protocol (supported by both Micron and Macronix memories), lately with SPI-2-2-2 protocol (supported only by Micron memories).

---
 drivers/mtd/devices/m25p80.c      |  8 +-----
 drivers/mtd/spi-nor/fsl-quadspi.c |  2 +-
 drivers/mtd/spi-nor/nxp-spifi.c   | 13 +++------
 drivers/mtd/spi-nor/spi-nor.c     | 59 ++++++++++++++++++++++++++++++++++-----
 include/linux/mtd/spi-nor.h       | 27 +++++++++++++++---
 5 files changed, 81 insertions(+), 28 deletions(-)
drivers/mtd/devices/m25p80.c +0 -6
index 4b5d7a4655fd..1457866a4930 100644
@@ -179,7 +179,6 @@ static int m25p_probe(struct spi_device *spi)
        struct flash_platform_data      *data;
        struct m25p *flash;
        struct spi_nor *nor;
-       enum read_mode mode = SPI_NOR_NORMAL;
        char *flash_name = NULL;
        int ret;
 
@@ -205,11 +204,6 @@ static int m25p_probe(struct spi_device *spi)
        spi_set_drvdata(spi, flash);
        flash->spi = spi;
 
-       if (spi->mode & SPI_RX_QUAD)
-               mode = SPI_NOR_QUAD;
-       else if (spi->mode & SPI_RX_DUAL)
-               mode = SPI_NOR_DUAL;
-
        if (data && data->name)
                nor->mtd.name = data->name;
 

drivers/mtd/devices/m25p80.c static int m25p_probe(struct spi_device *spi) · line 220

Quoted 5 lines
drivers/mtd/devices/m25p80.c +1 -1
@@ -223,7 +217,7 @@ static int m25p_probe(struct spi_device *spi)
        else
                flash_name = spi->modalias;
 
-       ret = spi_nor_scan(nor, flash_name, mode);
+       ret = spi_nor_scan(nor, flash_name, spi->mode);

IMHO, this is certainly incorrect because spi-nor never know anything about spi (Linux) that is why this framework got into picture.

Quoted 62 lines
drivers/mtd/devices/m25p80.c +0 -0
        if (ret)
                return ret;
 
drivers/mtd/spi-nor/fsl-quadspi.c +1 -1
index 2954f89fc8be..1ded5dbe2240 100644
@@ -1033,7 +1033,7 @@ static int fsl_qspi_probe(struct platform_device *pdev)
                /* set the chip address for READID */
                fsl_qspi_set_base_addr(q, nor);
 
-               ret = spi_nor_scan(nor, NULL, SPI_NOR_QUAD);
+               ret = spi_nor_scan(nor, NULL, SPI_RX_QUAD);
                if (ret)
                        goto mutex_failed;
 
drivers/mtd/spi-nor/nxp-spifi.c +4 -9
index 9e82098ae644..c499f3258245 100644
@@ -272,7 +272,6 @@ static int nxp_spifi_setup_flash(struct nxp_spifi *spifi,
                                 struct device_node *np)
 {
        struct mtd_part_parser_data ppdata;
-       enum read_mode flash_read;
        u32 ctrl, property;
        u16 mode = 0;
        int ret;
@@ -304,16 +303,12 @@ static int nxp_spifi_setup_flash(struct nxp_spifi *spifi,
               SPIFI_CTRL_CSHIGH(15) |
               SPIFI_CTRL_FBCLK;
 
-       if (mode & SPI_RX_DUAL) {
+       if (mode & SPI_RX_DUAL)
                ctrl |= SPIFI_CTRL_DUAL;
-               flash_read = SPI_NOR_DUAL;
-       } else if (mode & SPI_RX_QUAD) {
+       else if (mode & SPI_RX_QUAD)
                ctrl &= ~SPIFI_CTRL_DUAL;
-               flash_read = SPI_NOR_QUAD;
-       } else {
+       else
                ctrl |= SPIFI_CTRL_DUAL;
-               flash_read = SPI_NOR_NORMAL;
-       }
 
        switch (mode & (SPI_CPHA | SPI_CPOL)) {
        case SPI_MODE_0:
@@ -349,7 +344,7 @@ static int nxp_spifi_setup_flash(struct nxp_spifi *spifi,
         */
        nxp_spifi_dummy_id_read(&spifi->nor);
 
-       ret = spi_nor_scan(&spifi->nor, NULL, flash_read);
+       ret = spi_nor_scan(&spifi->nor, NULL, mode);
        if (ret) {
                dev_err(spifi->dev, "device scan failed\n");
                return ret;
drivers/mtd/spi-nor/spi-nor.c +0 -0
index 8818d4325d20..1908038c8f2e 100644

drivers/mtd/spi-nor/spi-nor.c line 23

Quoted 4 lines
drivers/mtd/spi-nor/spi-nor.c +1 -0
@@ -20,6 +20,7 @@
 #include <linux/mtd/cfi.h>
 #include <linux/mtd/mtd.h>
 #include <linux/of_platform.h>
+#include <linux/spi/spi.h>

Same comment as above - don't use spi on spi-nor.

Quoted 199 lines
drivers/mtd/spi-nor/spi-nor.c +51 -7
 #include <linux/spi/flash.h>
 #include <linux/mtd/spi-nor.h>
 
@@ -61,6 +62,11 @@ struct flash_info {
 
 #define JEDEC_MFR(info)        ((info)->id[0])
 
+struct read_id_proto {
+       enum spi_protocol       proto;  /* SPI protocol to read the JEDEC ID */
+       u16                     mode;   /* SPI controller required caps */
+};
+
 static const struct flash_info *spi_nor_match_id(const char *name);
 
 /*
@@ -701,11 +707,15 @@ static const struct flash_info spi_nor_ids[] = {
        { },
 };
 
-static const struct flash_info *spi_nor_read_id(struct spi_nor *nor)
+static const struct flash_info *spi_nor_read_id(struct spi_nor *nor, u16 mode)
 {
-       int                     tmp;
+       int                     i, tmp;
        u8                      id[SPI_NOR_MAX_ID_LEN];
        const struct flash_info *info;
+       static const struct read_id_proto proto[] = {
+               { SPI_PROTO_4_4_4, SPI_RX_QUAD | SPI_TX_QUAD },
+               { SPI_PROTO_2_2_2, SPI_RX_DUAL | SPI_TX_DUAL }
+       };
 
        tmp = nor->read_reg(nor, SPINOR_OP_RDID, id, SPI_NOR_MAX_ID_LEN);
        if (tmp < 0) {
@@ -713,6 +723,35 @@ static const struct flash_info *spi_nor_read_id(struct spi_nor *nor)
                return ERR_PTR(tmp);
        }
 
+       /* Special case for Micron/Macronix qspi nor. */
+       for (i = 0; i < ARRAY_SIZE(proto); ++i) {
+               if (!((id[0] == 0xff && id[1] == 0xff && id[2] == 0xff) ||
+                     (id[0] == 0x00 && id[1] == 0x00 && id[2] == 0x00)))
+                       break;
+
+               /* Check whether the SPI controller supports this protocol. */
+               if ((mode & proto[i].mode) != proto[i].mode)
+                       continue;
+
+               nor->erase_proto = proto[i].proto;
+               nor->read_proto = proto[i].proto;
+               nor->write_proto = proto[i].proto;
+               nor->reg_proto = proto[i].proto;
+
+               /*
+                * Multiple I/O Read ID only returns the Manufacturer ID
+                * (1 byte) and the Device ID (2 bytes). So we reset the
+                * remaining bytes.
+                */
+               memset(id, 0, sizeof(id));
+               tmp = nor->read_reg(nor, SPINOR_OP_MIO_RDID, id, 3);
+               if (tmp < 0) {
+                       dev_dbg(nor->dev,
+                               " error %d reading JEDEC ID (MULTI IO)\n", tmp);
+                       return ERR_PTR(tmp);
+               }
+       }
+
        for (tmp = 0; tmp < ARRAY_SIZE(spi_nor_ids) - 1; tmp++) {
                info = &spi_nor_ids[tmp];
                if (info->id_len) {
@@ -999,7 +1038,7 @@ static int spi_nor_check(struct spi_nor *nor)
        return 0;
 }
 
-int spi_nor_scan(struct spi_nor *nor, const char *name, enum read_mode mode)
+int spi_nor_scan(struct spi_nor *nor, const char *name, u16 mode)
 {
        const struct flash_info *info = NULL;
        struct device *dev = nor->dev;
@@ -1012,11 +1051,17 @@ int spi_nor_scan(struct spi_nor *nor, const char *name, enum read_mode mode)
        if (ret)
                return ret;
 
+       /* Reset SPI protocol for all commands */
+       nor->erase_proto = SPI_PROTO_1_1_1;
+       nor->read_proto = SPI_PROTO_1_1_1;
+       nor->write_proto = SPI_PROTO_1_1_1;
+       nor->reg_proto = SPI_PROTO_1_1_1;
+
        if (name)
                info = spi_nor_match_id(name);
        /* Try to auto-detect if chip name wasn't specified or not found */
        if (!info)
-               info = spi_nor_read_id(nor);
+               info = spi_nor_read_id(nor, mode);
        if (IS_ERR_OR_NULL(info))
                return -ENOENT;
 
@@ -1027,7 +1072,7 @@ int spi_nor_scan(struct spi_nor *nor, const char *name, enum read_mode mode)
        if (name && info->id_len) {
                const struct flash_info *jinfo;
 
-               jinfo = spi_nor_read_id(nor);
+               jinfo = spi_nor_read_id(nor, mode);
                if (IS_ERR(jinfo)) {
                        return PTR_ERR(jinfo);
                } else if (jinfo != info) {
@@ -1126,14 +1171,14 @@ int spi_nor_scan(struct spi_nor *nor, const char *name, enum read_mode mode)
                nor->flash_read = SPI_NOR_NORMAL;
 
        /* Quad/Dual-read mode takes precedence over fast/normal */
-       if (mode == SPI_NOR_QUAD && info->flags & SPI_NOR_QUAD_READ) {
+       if (mode & SPI_RX_QUAD && info->flags & SPI_NOR_QUAD_READ) {
                ret = set_quad_mode(nor, info);
                if (ret) {
                        dev_err(dev, "quad mode not supported\n");
                        return ret;
                }
                nor->flash_read = SPI_NOR_QUAD;
-       } else if (mode == SPI_NOR_DUAL && info->flags & SPI_NOR_DUAL_READ) {
+       } else if (mode & SPI_RX_DUAL && info->flags & SPI_NOR_DUAL_READ) {
                nor->flash_read = SPI_NOR_DUAL;
        }
 
include/linux/mtd/spi-nor.h +23 -5
index 672595a381c5..32339e82cb9d 100644
@@ -57,8 +57,9 @@
 #define SPINOR_OP_BRWR         0x17    /* Bank register write */
 
 /* Used for Micron flashes only. */
-#define SPINOR_OP_RD_EVCR      0x65    /* Read EVCR register */
-#define SPINOR_OP_WD_EVCR      0x61    /* Write EVCR register */
+#define SPINOR_OP_MIO_RDID     0xaf    /* Multiple I/O Read JEDEC ID */
+#define SPINOR_OP_RD_EVCR      0x65    /* Read EVCR register */
+#define SPINOR_OP_WD_EVCR      0x61    /* Write EVCR register */
 
 /* Status Register bits. */
 #define SR_WIP                 1       /* Write in progress */
@@ -87,6 +88,16 @@ enum read_mode {
        SPI_NOR_QUAD,
 };
 
+enum spi_protocol {
+       SPI_PROTO_1_1_1,        /* SPI */
+       SPI_PROTO_1_1_2,        /* Dual Output */
+       SPI_PROTO_1_1_4,        /* Quad Output */
+       SPI_PROTO_1_2_2,        /* Dual IO */
+       SPI_PROTO_1_4_4,        /* Quad IO */
+       SPI_PROTO_2_2_2,        /* Dual Command */
+       SPI_PROTO_4_4_4,        /* Quad Command */
+};
+
 #define SPI_NOR_MAX_CMD_SIZE   8
 enum spi_nor_ops {
        SPI_NOR_OPS_READ = 0,
@@ -117,6 +128,10 @@ struct mtd_info;
  * @flash_read:                the mode of the read
  * @sst_write_second:  used by the SST write operation
  * @flags:             flag options for the current SPI-NOR (SNOR_F_*)
+ * @erase_proto:       the SPI protocol used by erase operations
+ * @read_proto:                the SPI protocol used by read operations
+ * @write_proto:       the SPI protocol used by write operations
+ * @reg_proto          the SPI protocol used by read_reg/write_reg operations
  * @cmd_buf:           used by the write_reg
  * @prepare:           [OPTIONAL] do some preparations for the
  *                     read/write/erase/lock/unlock operations
@@ -143,6 +158,10 @@ struct spi_nor {
        u8                      read_opcode;
        u8                      read_dummy;
        u8                      program_opcode;
+       enum spi_protocol       erase_proto;
+       enum spi_protocol       read_proto;
+       enum spi_protocol       write_proto;
+       enum spi_protocol       reg_proto;
        enum read_mode          flash_read;
        bool                    sst_write_second;
        u32                     flags;
@@ -169,7 +188,7 @@ struct spi_nor {
  * spi_nor_scan() - scan the SPI NOR
  * @nor:       the spi_nor structure
  * @name:      the chip type name
- * @mode:      the read mode supported by the driver
+ * @mode:      the bitmask or TX/RX modes supported by the driver
  *
  * The drivers can use this fuction to scan the SPI NOR.
  * In the scanning, it will try to get all the necessary information to
@@ -179,6 +198,6 @@ struct spi_nor {
  *
  * Return: 0 for success, others for failure.
  */
-int spi_nor_scan(struct spi_nor *nor, const char *name, enum read_mode mode);
+int spi_nor_scan(struct spi_nor *nor, const char *name, u16 mode);
 
 #endif
--

1.8.2.2

Quoted 4 lines

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

thanks!

Signature · 1 line
Jagan | openedev.
#4 lore raw

Re: [linux-next RFC v7 2/6] mtd: spi-nor: read JEDEC ID with multiple I/O protocols

Hi Jagan,

Le 15/09/2015 19:53, Jagan Teki a ?crit :

Quoted 48 lines
Quoted 46 lines · Cyrille Pitchen

When their quad or dual I/O mode is enabled, Micron and Macronix spi-nor memories don't reply to the regular Read ID (0x9f) command. Instead they reply to a new dedicated command Read ID Multiple I/O (0xaf).

If the Read ID (0x9f) command fails (the read ID is all 1's or all 0's), then the Read ID Multiple I/O (0xaf) is used, first with SPI 4-4-4 protocol (supported by both Micron and Macronix memories), lately with SPI-2-2-2 protocol (supported only by Micron memories).

---
 drivers/mtd/devices/m25p80.c      |  8 +-----
 drivers/mtd/spi-nor/fsl-quadspi.c |  2 +-
 drivers/mtd/spi-nor/nxp-spifi.c   | 13 +++------
 drivers/mtd/spi-nor/spi-nor.c     | 59 ++++++++++++++++++++++++++++++++++-----
 include/linux/mtd/spi-nor.h       | 27 +++++++++++++++---
 5 files changed, 81 insertions(+), 28 deletions(-)
drivers/mtd/devices/m25p80.c +0 -6
index 4b5d7a4655fd..1457866a4930 100644
@@ -179,7 +179,6 @@ static int m25p_probe(struct spi_device *spi)
        struct flash_platform_data      *data;
        struct m25p *flash;
        struct spi_nor *nor;
-       enum read_mode mode = SPI_NOR_NORMAL;
        char *flash_name = NULL;
        int ret;
 
@@ -205,11 +204,6 @@ static int m25p_probe(struct spi_device *spi)
        spi_set_drvdata(spi, flash);
        flash->spi = spi;
 
-       if (spi->mode & SPI_RX_QUAD)
-               mode = SPI_NOR_QUAD;
-       else if (spi->mode & SPI_RX_DUAL)
-               mode = SPI_NOR_DUAL;
-
        if (data && data->name)
                nor->mtd.name = data->name;
 
@@ -223,7 +217,7 @@ static int m25p_probe(struct spi_device *spi)
        else
                flash_name = spi->modalias;
 
Quoted 6 lines

drivers/mtd/devices/m25p80.c static int m25p_probe(struct spi_device *spi) · line 220

Quoted 2 lines
drivers/mtd/devices/m25p80.c +1 -1
-       ret = spi_nor_scan(nor, flash_name, mode);
+       ret = spi_nor_scan(nor, flash_name, spi->mode);

IMHO, this is certainly incorrect because spi-nor never know anything about spi (Linux) that is why this framework got into picture.

OK but what to use instead? Because we need to know the SPI controller capabilities as compared to what the SPI NOR memory expects.

SPI_NOR_QUAD is just not enough as it doesn't allow us to make the difference between SPI 1-1-4, 1-4-4 or 4-4-4 protocols.
Some manufacturer specific commands like Multiple I/O Read ID (0xaf) only work with the SPI 4-4-4 protocol.

Also patch 3 (mtd: spi-nor: set the read op code and protocol based on the manufacturer) makes use of the SPI controller capabilities to select the right Fast Read op code and SPI protocol to match the SPI NOR memory interface.

Currently the framework always uses the Fast Read Quad Output 1-1-4 (0x6b). Then for Macronix QSPI memories, the framework also enables their QPI mode. However the datasheet of Macronix MX66L1G claims this command (0x6b) is only supported in in SPI mode but not in QPI mode.

Also for Micron QSPI memories, the framework turns their Quad mode on but once enabled the spi-nor memories expected ALL commands to use the SPI 4-4-4 protocol. SPI controllers have no mean to be notified about that protocol change.

Besides, the m25p80 driver only checks the SPI_RX_QUAD flag before asking the spi-nor framework to use some Quad SPI mode but in many cases it's not enough; the SPI_TX_QUAD flag is also needed.

Of course there are other ways to provide the spi-nor framework with the SPI controller. Maybe we can still use a bitmask for its simplicity of use by changing a little bit the definition of the spi_protocol constants:

#define SPI_PROTO_1_1_1    0x0001
#define SPI_PROTO_1_1_2    0x0002
#define SPI_PROTO_1_1_4    0x0004
#define SPI_PROTO_1_2_2    0x0008
#define SPI_PROTO_1_4_4    0x0010
#define SPI_PROTO_2_2_2    0x0020
#define SPI_PROTO_4_4_4    0x0040

Then for instance in m25p80.c, we do something like:

unsigned int supported_protocols = SPI_PROTO_1_1_1;

if (spi->mode & SPI_RX_QUAD) {
        supported_protocols |= SPI_PROTO_1_1_4;

        if (spi->mode & SPI_TX_QUAD)
                supported_protocols |= (SPI_PROTO_1_4_4 | SPI_PROTO_4_4_4);
}
if (spi->mode & SPI_RX_DUAL) {
        supported_protocols |= SPI_PROTO_1_1_2;

        if (spi->mode & SPI_TX_DUAL)
                supported_protocols |= (SPI_PROTO_1_2_2 | SPI_PROTO_2_2_2);
}

ret = spi_nor_scan(nor, flash_name, supported_protocols);

Does it sound better to you so we don't use the flags defined in <linux/spi/spi.h> in the spi-nor framework ?

[...]

Quoted 81 lines

drivers/mtd/spi-nor/spi-nor.c line 23

Quoted 9 lines
drivers/mtd/spi-nor/spi-nor.c +1 -0
index 8818d4325d20..1908038c8f2e 100644
@@ -20,6 +20,7 @@
 #include <linux/mtd/cfi.h>
 #include <linux/mtd/mtd.h>
 #include <linux/of_platform.h>
+#include <linux/spi/spi.h>

Same comment as above - don't use spi on spi-nor.

Quoted 69 lines
drivers/mtd/spi-nor/spi-nor.c +40 -2
 #include <linux/spi/flash.h>
 #include <linux/mtd/spi-nor.h>
 
@@ -61,6 +62,11 @@ struct flash_info {
 
 #define JEDEC_MFR(info)        ((info)->id[0])
 
+struct read_id_proto {
+       enum spi_protocol       proto;  /* SPI protocol to read the JEDEC ID */
+       u16                     mode;   /* SPI controller required caps */
+};
+
 static const struct flash_info *spi_nor_match_id(const char *name);
 
 /*
@@ -701,11 +707,15 @@ static const struct flash_info spi_nor_ids[] = {
        { },
 };
 
-static const struct flash_info *spi_nor_read_id(struct spi_nor *nor)
+static const struct flash_info *spi_nor_read_id(struct spi_nor *nor, u16 mode)
 {
-       int                     tmp;
+       int                     i, tmp;
        u8                      id[SPI_NOR_MAX_ID_LEN];
        const struct flash_info *info;
+       static const struct read_id_proto proto[] = {
+               { SPI_PROTO_4_4_4, SPI_RX_QUAD | SPI_TX_QUAD },
+               { SPI_PROTO_2_2_2, SPI_RX_DUAL | SPI_TX_DUAL }
+       };
 
        tmp = nor->read_reg(nor, SPINOR_OP_RDID, id, SPI_NOR_MAX_ID_LEN);
        if (tmp < 0) {
@@ -713,6 +723,35 @@ static const struct flash_info *spi_nor_read_id(struct spi_nor *nor)
                return ERR_PTR(tmp);
        }
 
+       /* Special case for Micron/Macronix qspi nor. */
+       for (i = 0; i < ARRAY_SIZE(proto); ++i) {
+               if (!((id[0] == 0xff && id[1] == 0xff && id[2] == 0xff) ||
+                     (id[0] == 0x00 && id[1] == 0x00 && id[2] == 0x00)))
+                       break;
+
+               /* Check whether the SPI controller supports this protocol. */
+               if ((mode & proto[i].mode) != proto[i].mode)
+                       continue;
+
+               nor->erase_proto = proto[i].proto;
+               nor->read_proto = proto[i].proto;
+               nor->write_proto = proto[i].proto;
+               nor->reg_proto = proto[i].proto;
+
+               /*
+                * Multiple I/O Read ID only returns the Manufacturer ID
+                * (1 byte) and the Device ID (2 bytes). So we reset the
+                * remaining bytes.
+                */
+               memset(id, 0, sizeof(id));
+               tmp = nor->read_reg(nor, SPINOR_OP_MIO_RDID, id, 3);
+               if (tmp < 0) {
+                       dev_dbg(nor->dev,
+                               " error %d reading JEDEC ID (MULTI IO)\n", tmp);
+                       return ERR_PTR(tmp);
+               }
+       }
+
        for (tmp = 0; tmp < ARRAY_SIZE(spi_nor_ids) - 1; tmp++) {
                info = &spi_nor_ids[tmp];
                if (info->id_len) {

[...]

Quoted 4 lines

thanks!

Best Regards,

Cyrille