[PATCH v2 2/3] mtd: spi-nor: core: reuse spi_nor_clear_sr and spi_nor_read_sr functions

yaliang.wang at windriver.com yaliang.wang at windriver.com
Fri Apr 16 08:31:05 BST 2021


From: Yaliang Wang <Yaliang.Wang at windriver.com>

spi_nor_clear_fsr() and spi_nor_read_fsr() two functions are almost the
same with the functions spi_nor_clear_sr() and spi_nor_read_sr(), with
little change, the last two functions' codes can be reused, and can be
easily expanded to other manufacturers or chips.

Also, rename spi_nor_clear_sr() as spi_nor_clear_status()
spi_nor_read_sr() as spi_nor_read_status(), because they no longer just
deal with a specific register now, they deal with one type of registers.

Signed-off-by: Yaliang Wang <Yaliang.Wang at windriver.com>
---
New in v2:
  - Also reuse spi_nor_read_sr() codes.
Changes in v2:
  - Rename spi_nor_read_sr() to spi_nor_read_status()
  - Rename spi_nor_clear_sr() to spi_nor_clear_status()

 drivers/mtd/spi-nor/atmel.c |   4 +-
 drivers/mtd/spi-nor/core.c  | 119 ++++++++----------------------------
 drivers/mtd/spi-nor/core.h  |   2 +-
 3 files changed, 28 insertions(+), 97 deletions(-)

diff --git a/drivers/mtd/spi-nor/atmel.c b/drivers/mtd/spi-nor/atmel.c
index 1fea5cab492c..769299d06f78 100644
--- a/drivers/mtd/spi-nor/atmel.c
+++ b/drivers/mtd/spi-nor/atmel.c
@@ -76,7 +76,7 @@ static int atmel_set_global_protection(struct spi_nor *nor, loff_t ofs,
 	if (ofs || len != nor->params->size)
 		return -EINVAL;
 
-	ret = spi_nor_read_sr(nor, nor->bouncebuf);
+	ret = spi_nor_read_status(nor, SPINOR_OP_RDSR, nor->bouncebuf);
 	if (ret)
 		return ret;
 
@@ -133,7 +133,7 @@ static int atmel_is_global_protected(struct spi_nor *nor, loff_t ofs, uint64_t l
 	if (ofs >= nor->params->size || (ofs + len) > nor->params->size)
 		return -EINVAL;
 
-	ret = spi_nor_read_sr(nor, nor->bouncebuf);
+	ret = spi_nor_read_status(nor, SPINOR_OP_RDSR, nor->bouncebuf);
 	if (ret)
 		return ret;
 
diff --git a/drivers/mtd/spi-nor/core.c b/drivers/mtd/spi-nor/core.c
index 5de72322ae32..0e7fcbc4136f 100644
--- a/drivers/mtd/spi-nor/core.c
+++ b/drivers/mtd/spi-nor/core.c
@@ -370,23 +370,24 @@ int spi_nor_write_disable(struct spi_nor *nor)
 }
 
 /**
- * spi_nor_read_sr() - Read the Status Register.
+ * spi_nor_read_status() - Read the chip status.
  * @nor:	pointer to 'struct spi_nor'.
- * @sr:		pointer to a DMA-able buffer where the value of the
+ * @opcode:	the SPI command op code to get the chip status
+ * @st:		pointer to a DMA-able buffer where the value of the
  *              Status Register will be written. Should be at least 2 bytes.
  *
  * Return: 0 on success, -errno otherwise.
  */
-int spi_nor_read_sr(struct spi_nor *nor, u8 *sr)
+int spi_nor_read_status(struct spi_nor *nor, u8 opcode, u8 *st)
 {
 	int ret;
 
 	if (nor->spimem) {
 		struct spi_mem_op op =
-			SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_RDSR, 0),
+			SPI_MEM_OP(SPI_MEM_OP_CMD(opcode, 0),
 				   SPI_MEM_OP_NO_ADDR,
 				   SPI_MEM_OP_NO_DUMMY,
-				   SPI_MEM_OP_DATA_IN(1, sr, 0));
+				   SPI_MEM_OP_DATA_IN(1, st, 0));
 
 		if (nor->reg_proto == SNOR_PROTO_8_8_8_DTR) {
 			op.addr.nbytes = nor->params->rdsr_addr_nbytes;
@@ -402,56 +403,12 @@ int spi_nor_read_sr(struct spi_nor *nor, u8 *sr)
 
 		ret = spi_mem_exec_op(nor->spimem, &op);
 	} else {
-		ret = spi_nor_controller_ops_read_reg(nor, SPINOR_OP_RDSR, sr,
+		ret = spi_nor_controller_ops_read_reg(nor, opcode, st,
 						      1);
 	}
 
 	if (ret)
-		dev_dbg(nor->dev, "error %d reading SR\n", ret);
-
-	return ret;
-}
-
-/**
- * spi_nor_read_fsr() - Read the Flag Status Register.
- * @nor:	pointer to 'struct spi_nor'
- * @fsr:	pointer to a DMA-able buffer where the value of the
- *              Flag Status Register will be written. Should be at least 2
- *              bytes.
- *
- * Return: 0 on success, -errno otherwise.
- */
-static int spi_nor_read_fsr(struct spi_nor *nor, u8 *fsr)
-{
-	int ret;
-
-	if (nor->spimem) {
-		struct spi_mem_op op =
-			SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_RDFSR, 0),
-				   SPI_MEM_OP_NO_ADDR,
-				   SPI_MEM_OP_NO_DUMMY,
-				   SPI_MEM_OP_DATA_IN(1, fsr, 0));
-
-		if (nor->reg_proto == SNOR_PROTO_8_8_8_DTR) {
-			op.addr.nbytes = nor->params->rdsr_addr_nbytes;
-			op.dummy.nbytes = nor->params->rdsr_dummy;
-			/*
-			 * We don't want to read only one byte in DTR mode. So,
-			 * read 2 and then discard the second byte.
-			 */
-			op.data.nbytes = 2;
-		}
-
-		spi_nor_spimem_setup_op(nor, &op, nor->reg_proto);
-
-		ret = spi_mem_exec_op(nor->spimem, &op);
-	} else {
-		ret = spi_nor_controller_ops_read_reg(nor, SPINOR_OP_RDFSR, fsr,
-						      1);
-	}
-
-	if (ret)
-		dev_dbg(nor->dev, "error %d reading FSR\n", ret);
+		dev_dbg(nor->dev, "error %d reading status\n", ret);
 
 	return ret;
 }
@@ -650,16 +607,17 @@ static int spi_nor_xsr_ready(struct spi_nor *nor)
 }
 
 /**
- * spi_nor_clear_sr() - Clear the Status Register.
+ * spi_nor_clear_status() - Clear the chip's abnormal status.
  * @nor:	pointer to 'struct spi_nor'.
+ * @opcode:	the SPI command op code to clear the obnormal status.
  */
-static void spi_nor_clear_sr(struct spi_nor *nor)
+static void spi_nor_clear_status(struct spi_nor *nor, u8 opcode)
 {
 	int ret;
 
 	if (nor->spimem) {
 		struct spi_mem_op op =
-			SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_CLSR, 0),
+			SPI_MEM_OP(SPI_MEM_OP_CMD(opcode, 0),
 				   SPI_MEM_OP_NO_ADDR,
 				   SPI_MEM_OP_NO_DUMMY,
 				   SPI_MEM_OP_NO_DATA);
@@ -668,12 +626,12 @@ static void spi_nor_clear_sr(struct spi_nor *nor)
 
 		ret = spi_mem_exec_op(nor->spimem, &op);
 	} else {
-		ret = spi_nor_controller_ops_write_reg(nor, SPINOR_OP_CLSR,
+		ret = spi_nor_controller_ops_write_reg(nor, opcode,
 						       NULL, 0);
 	}
 
 	if (ret)
-		dev_dbg(nor->dev, "error %d clearing SR\n", ret);
+		dev_dbg(nor->dev, "error %d clearing status\n", ret);
 }
 
 /**
@@ -685,7 +643,7 @@ static void spi_nor_clear_sr(struct spi_nor *nor)
  */
 static int spi_nor_sr_ready(struct spi_nor *nor)
 {
-	int ret = spi_nor_read_sr(nor, nor->bouncebuf);
+	int ret = spi_nor_read_status(nor, SPINOR_OP_RDSR, nor->bouncebuf);
 
 	if (ret)
 		return ret;
@@ -697,7 +655,7 @@ static int spi_nor_sr_ready(struct spi_nor *nor)
 		else
 			dev_err(nor->dev, "Programming Error occurred\n");
 
-		spi_nor_clear_sr(nor);
+		spi_nor_clear_status(nor, SPINOR_OP_CLSR);
 
 		/*
 		 * WEL bit remains set to one when an erase or page program
@@ -715,33 +673,6 @@ static int spi_nor_sr_ready(struct spi_nor *nor)
 	return !(nor->bouncebuf[0] & SR_WIP);
 }
 
-/**
- * spi_nor_clear_fsr() - Clear the Flag Status Register.
- * @nor:	pointer to 'struct spi_nor'.
- */
-static void spi_nor_clear_fsr(struct spi_nor *nor)
-{
-	int ret;
-
-	if (nor->spimem) {
-		struct spi_mem_op op =
-			SPI_MEM_OP(SPI_MEM_OP_CMD(SPINOR_OP_CLFSR, 0),
-				   SPI_MEM_OP_NO_ADDR,
-				   SPI_MEM_OP_NO_DUMMY,
-				   SPI_MEM_OP_NO_DATA);
-
-		spi_nor_spimem_setup_op(nor, &op, nor->reg_proto);
-
-		ret = spi_mem_exec_op(nor->spimem, &op);
-	} else {
-		ret = spi_nor_controller_ops_write_reg(nor, SPINOR_OP_CLFSR,
-						       NULL, 0);
-	}
-
-	if (ret)
-		dev_dbg(nor->dev, "error %d clearing FSR\n", ret);
-}
-
 /**
  * spi_nor_fsr_ready() - Query the Flag Status Register to see if the flash is
  * ready for new commands.
@@ -751,7 +682,7 @@ static void spi_nor_clear_fsr(struct spi_nor *nor)
  */
 static int spi_nor_fsr_ready(struct spi_nor *nor)
 {
-	int ret = spi_nor_read_fsr(nor, nor->bouncebuf);
+	int ret = spi_nor_read_status(nor, SPINOR_OP_RDFSR, nor->bouncebuf);
 
 	if (ret)
 		return ret;
@@ -766,7 +697,7 @@ static int spi_nor_fsr_ready(struct spi_nor *nor)
 			dev_err(nor->dev,
 			"Attempted to modify a protected sector.\n");
 
-		spi_nor_clear_fsr(nor);
+		spi_nor_clear_status(nor, SPINOR_OP_CLFSR);
 
 		/*
 		 * WEL bit remains set to one when an erase or page program
@@ -948,7 +879,7 @@ static int spi_nor_write_sr1_and_check(struct spi_nor *nor, u8 sr1)
 	if (ret)
 		return ret;
 
-	ret = spi_nor_read_sr(nor, nor->bouncebuf);
+	ret = spi_nor_read_status(nor, SPINOR_OP_RDSR, nor->bouncebuf);
 	if (ret)
 		return ret;
 
@@ -1042,7 +973,7 @@ static int spi_nor_write_16bit_cr_and_check(struct spi_nor *nor, u8 cr)
 	u8 sr_written;
 
 	/* Keep the current value of the Status Register 1. */
-	ret = spi_nor_read_sr(nor, sr_cr);
+	ret = spi_nor_read_status(nor, SPINOR_OP_RDSR, sr_cr);
 	if (ret)
 		return ret;
 
@@ -1054,7 +985,7 @@ static int spi_nor_write_16bit_cr_and_check(struct spi_nor *nor, u8 cr)
 
 	sr_written = sr_cr[0];
 
-	ret = spi_nor_read_sr(nor, sr_cr);
+	ret = spi_nor_read_status(nor, SPINOR_OP_RDSR, sr_cr);
 	if (ret)
 		return ret;
 
@@ -1878,7 +1809,7 @@ static int spi_nor_sr_lock(struct spi_nor *nor, loff_t ofs, uint64_t len)
 	bool can_be_top = true, can_be_bottom = nor->flags & SNOR_F_HAS_SR_TB;
 	bool use_top;
 
-	ret = spi_nor_read_sr(nor, nor->bouncebuf);
+	ret = spi_nor_read_status(nor, SPINOR_OP_RDSR, nor->bouncebuf);
 	if (ret)
 		return ret;
 
@@ -1963,7 +1894,7 @@ static int spi_nor_sr_unlock(struct spi_nor *nor, loff_t ofs, uint64_t len)
 	bool can_be_top = true, can_be_bottom = nor->flags & SNOR_F_HAS_SR_TB;
 	bool use_top;
 
-	ret = spi_nor_read_sr(nor, nor->bouncebuf);
+	ret = spi_nor_read_status(nor, SPINOR_OP_RDSR, nor->bouncebuf);
 	if (ret)
 		return ret;
 
@@ -2040,7 +1971,7 @@ static int spi_nor_sr_is_locked(struct spi_nor *nor, loff_t ofs, uint64_t len)
 {
 	int ret;
 
-	ret = spi_nor_read_sr(nor, nor->bouncebuf);
+	ret = spi_nor_read_status(nor, SPINOR_OP_RDSR, nor->bouncebuf);
 	if (ret)
 		return ret;
 
@@ -2111,7 +2042,7 @@ int spi_nor_sr1_bit6_quad_enable(struct spi_nor *nor)
 {
 	int ret;
 
-	ret = spi_nor_read_sr(nor, nor->bouncebuf);
+	ret = spi_nor_read_status(nor, SPINOR_OP_RDSR, nor->bouncebuf);
 	if (ret)
 		return ret;
 
diff --git a/drivers/mtd/spi-nor/core.h b/drivers/mtd/spi-nor/core.h
index cf265044b543..02c12e04811d 100644
--- a/drivers/mtd/spi-nor/core.h
+++ b/drivers/mtd/spi-nor/core.h
@@ -442,7 +442,7 @@ void spi_nor_unlock_and_unprep(struct spi_nor *nor);
 int spi_nor_sr1_bit6_quad_enable(struct spi_nor *nor);
 int spi_nor_sr2_bit1_quad_enable(struct spi_nor *nor);
 int spi_nor_sr2_bit7_quad_enable(struct spi_nor *nor);
-int spi_nor_read_sr(struct spi_nor *nor, u8 *sr);
+int spi_nor_read_status(struct spi_nor *nor, u8 opcode, u8 *st);
 int spi_nor_read_cr(struct spi_nor *nor, u8 *cr);
 int spi_nor_write_sr(struct spi_nor *nor, const u8 *sr, size_t len);
 int spi_nor_write_sr_and_check(struct spi_nor *nor, u8 sr1);
-- 
2.25.1




More information about the linux-mtd mailing list