All of lore.kernel.org
 help / color / mirror / Atom feed
From: Brian Norris <computersforpeace@gmail.com>
To: linux-mtd@lists.infradead.org
Cc: Marek Vasut <marex@denx.de>, Graham Moore <grmoore@altera.com>,
	Brian Norris <computersforpeace@gmail.com>,
	Huang Shijie <shijie8@gmail.com>,
	zajec5@gmail.com
Subject: [PATCH v2 1/10] mtd: spi-nor: eliminate duplicate spi_nor_wait_till_{, fsr}_ready() code
Date: Wed, 10 Sep 2014 00:26:16 -0700	[thread overview]
Message-ID: <1410333976-19252-1-git-send-email-computersforpeace@gmail.com> (raw)
In-Reply-To: <1407374222-8448-2-git-send-email-computersforpeace@gmail.com>

These functions were near-carbon-copies due to a small per-flash quirk.
Let's add a new spi_nor::flags bitfield to support these types of
quirks.

Signed-off-by: Brian Norris <computersforpeace@gmail.com>
Reviewed-by: Marek Vasut <marex@denx.de>
Cc: Graham Moore <grmoore@altera.com>
Cc: Huang Shijie <shijie8@gmail.com>
Signed-off-by: Brian Norris <computersforpeace@gmail.com>
---
v2: replace 'sr' with 'fsr' (typo)

I think this was the only patch (of the first 7) that required changes due to
review. I may rework patch 8 eventually, but it was not meant for inclusion in
its current form.

 drivers/mtd/spi-nor/spi-nor.c | 66 ++++++++++++++++++++++---------------------
 include/linux/mtd/spi-nor.h   |  6 ++++
 2 files changed, 40 insertions(+), 32 deletions(-)

diff --git a/drivers/mtd/spi-nor/spi-nor.c b/drivers/mtd/spi-nor/spi-nor.c
index 03e0ab8b2086..ba324e293645 100644
--- a/drivers/mtd/spi-nor/spi-nor.c
+++ b/drivers/mtd/spi-nor/spi-nor.c
@@ -163,48 +163,51 @@ static inline int set_4byte(struct spi_nor *nor, u32 jedec_id, int enable)
 		return nor->write_reg(nor, SPINOR_OP_BRWR, nor->cmd_buf, 1, 0);
 	}
 }
-
-static int spi_nor_wait_till_ready(struct spi_nor *nor)
+static inline int spi_nor_sr_ready(struct spi_nor *nor)
 {
-	unsigned long deadline;
-	int sr;
-
-	deadline = jiffies + MAX_READY_WAIT_JIFFIES;
-
-	do {
-		cond_resched();
+	int sr = read_sr(nor);
+	if (sr < 0)
+		return sr;
+	else
+		return !(sr & SR_WIP);
+}
 
-		sr = read_sr(nor);
-		if (sr < 0)
-			break;
-		else if (!(sr & SR_WIP))
-			return 0;
-	} while (!time_after_eq(jiffies, deadline));
+static inline int spi_nor_fsr_ready(struct spi_nor *nor)
+{
+	int fsr = read_fsr(nor);
+	if (fsr < 0)
+		return fsr;
+	else
+		return fsr & FSR_READY;
+}
 
-	return -ETIMEDOUT;
+static int spi_nor_ready(struct spi_nor *nor)
+{
+	int sr, fsr;
+	sr = spi_nor_sr_ready(nor);
+	if (sr < 0)
+		return sr;
+	fsr = nor->flags & SNOR_F_USE_FSR ? spi_nor_fsr_ready(nor) : 1;
+	if (fsr < 0)
+		return fsr;
+	return sr && fsr;
 }
 
-static int spi_nor_wait_till_fsr_ready(struct spi_nor *nor)
+static int spi_nor_wait_till_ready(struct spi_nor *nor)
 {
 	unsigned long deadline;
-	int sr;
-	int fsr;
+	int ret;
 
 	deadline = jiffies + MAX_READY_WAIT_JIFFIES;
 
 	do {
 		cond_resched();
 
-		sr = read_sr(nor);
-		if (sr < 0) {
-			break;
-		} else if (!(sr & SR_WIP)) {
-			fsr = read_fsr(nor);
-			if (fsr < 0)
-				break;
-			if (fsr & FSR_READY)
-				return 0;
-		}
+		ret = spi_nor_ready(nor);
+		if (ret < 0)
+			return ret;
+		if (ret)
+			return 0;
 	} while (!time_after_eq(jiffies, deadline));
 
 	return -ETIMEDOUT;
@@ -1009,9 +1012,8 @@ int spi_nor_scan(struct spi_nor *nor, const struct spi_device_id *id,
 	else
 		mtd->_write = spi_nor_write;
 
-	if ((info->flags & USE_FSR) &&
-	    nor->wait_till_ready == spi_nor_wait_till_ready)
-		nor->wait_till_ready = spi_nor_wait_till_fsr_ready;
+	if (info->flags & USE_FSR)
+		nor->flags |= SNOR_F_USE_FSR;
 
 	/* prefer "small sector" erase if possible */
 	if (info->flags & SECT_4K) {
diff --git a/include/linux/mtd/spi-nor.h b/include/linux/mtd/spi-nor.h
index 9e6294f32ba8..603ac0306663 100644
--- a/include/linux/mtd/spi-nor.h
+++ b/include/linux/mtd/spi-nor.h
@@ -116,6 +116,10 @@ enum spi_nor_ops {
 	SPI_NOR_OPS_UNLOCK,
 };
 
+enum spi_nor_option_flags {
+	SNOR_F_USE_FSR		= BIT(0),
+};
+
 /**
  * struct spi_nor - Structure for defining a the SPI NOR layer
  * @mtd:		point to a mtd_info structure
@@ -129,6 +133,7 @@ enum spi_nor_ops {
  * @program_opcode:	the program opcode
  * @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
@@ -160,6 +165,7 @@ struct spi_nor {
 	u8			program_opcode;
 	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];
 
-- 
1.9.1

  parent reply	other threads:[~2014-09-10  7:27 UTC|newest]

Thread overview: 42+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-08-07  1:16 [PATCH 0/8] mtd: spi-nor: refactor wait-till-ready Brian Norris
2014-08-07  1:16 ` [PATCH 1/8] mtd: spi-nor: eliminate duplicate spi_nor_wait_till_{, fsr}_ready() code Brian Norris
2014-08-07 14:23   ` Marek Vasut
2014-08-09  6:25   ` Huang Shijie
2014-08-11 17:59     ` Brian Norris
2014-09-10  7:26   ` Brian Norris [this message]
2014-08-07  1:16 ` [PATCH 2/8] mtd: spi-nor: handle timeout errors in spi_nor_write() Brian Norris
2014-08-07 14:23   ` Marek Vasut
2014-08-09  7:37   ` Huang Shijie
2014-08-07  1:16 ` [PATCH 3/8] mtd: spi-nor: move "wait-till-ready" checks into erase/write functions Brian Norris
2014-08-07 14:24   ` Marek Vasut
2014-08-09  8:42   ` Huang Shijie
2014-08-11 18:23     ` Brian Norris
2014-08-12  1:37       ` Huang Shijie
2014-08-07  1:16 ` [PATCH 4/8] mtd: m25p80: drop wait-till-ready checks Brian Norris
2014-08-07 14:24   ` Marek Vasut
2014-08-07  1:16 ` [PATCH 5/8] mtd: fsl-quadspi: " Brian Norris
2014-08-07 14:24   ` Marek Vasut
2014-08-07  1:17 ` [PATCH 6/8] mtd: spi-nor: drop replaceable wait-till-ready function pointer Brian Norris
2014-08-07 14:25   ` Marek Vasut
2014-08-09  9:53   ` Huang Shijie
2014-08-11 18:43     ` Brian Norris
2014-08-12  1:16       ` Huang Shijie
2014-09-10  7:02         ` Brian Norris
2014-08-12  5:13   ` Rafał Miłecki
2014-08-12  5:14     ` Rafał Miłecki
2014-08-07  1:17 ` [PATCH 7/8] mtd: spi-nor: factor out write_enable() for erase commands Brian Norris
2014-08-07 14:25   ` Marek Vasut
2014-08-09 10:52   ` Huang Shijie
2014-08-11 18:48     ` Brian Norris
2014-08-12  0:59       ` Huang Shijie
2014-09-10  7:05         ` Brian Norris
2014-09-10 15:20           ` Huang Shijie
2014-09-10  7:47             ` Brian Norris
2014-09-10 16:12               ` Huang Shijie
2014-09-10 23:25                 ` Brian Norris
2014-11-05 10:29   ` [PATCH v2] " Brian Norris
2014-11-06  3:39     ` Huang Shijie
2014-12-01  8:19     ` Brian Norris
2014-08-07  1:17 ` [RFC 8/8] debug: mtd: spi-nor: add BUG_ON() prints to check for !ready Brian Norris
2014-08-07 14:26   ` Marek Vasut
2014-11-05 10:10 ` [PATCH 0/8] mtd: spi-nor: refactor wait-till-ready Brian Norris

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1410333976-19252-1-git-send-email-computersforpeace@gmail.com \
    --to=computersforpeace@gmail.com \
    --cc=grmoore@altera.com \
    --cc=linux-mtd@lists.infradead.org \
    --cc=marex@denx.de \
    --cc=shijie8@gmail.com \
    --cc=zajec5@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.