Skip to contentSkip to conversation

mtd: spi-nor: read while write support

PATCH Superseded Miquel Raynal 2 participants 18 messages Dec 15, 2022 – Mar 24, 2023

Subsystems (2)

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Miquel Raynal

author maintainer
#0 lore raw

v3 cover mtd: spi-nor: read while write support

Hello folks,

Here is the follow-up of the RFC trying to bring a little bit of parallelism to support SPI-NOR Read While Write feature on parts supporting it and featuring several banks.

I have received some hardware to make it work, so since the RFC, the series has been updated to fix my mistakes, but the overall idea is the same.

There is nothing Macronix specific in the implementation, the operations and opcodes are exactly the same as before. The only difference being:
we may consider the chip usable when it is in the busy state during a write or an erase. Any chip with an internal split allowing to perform parallel operations might possibly leverage the benefits of this implementation.

The first patches are just refactoring and preparation work, there is almost no functional change, it's just a way to prepare the introduction of the new locking mechanism and hopefully provide the cleanest and simplest diff possible for this new feature. The actual change is all contained in "mtd: spi-nor: Enhance locking to support reads while writes". The logic is described in the commit log and copy/pasted here for clarity:

"
On devices featuring several banks, the Read While Write (RWW) feature is here to improve the overall performance when performing parallel reads and writes at different locations (different banks). The following constraints have to be taken into account:
1#: A single operation can be performed in a given bank.
2#: Only a single program or erase operation can happen on the entire
chip (common hardware limitation to limit costs)
3#: Reads must remain serialized even though reads on different banks
might occur at the same time.
4#: The I/O bus is unique and thus is the most constrained resource, all
spi-nor operations requiring access to the spi bus (through the spi controller) must be serialized until the bus exchanges are over. So we must ensure a single operation can be "sent" at a time.
5#: Any other operation that would not be either a read or a write or an
erase is considered requiring access to the full chip and cannot be parallelized, we then need to ensure the full chip is in the idle state when this occurs.

All these constraints can easily be managed with a proper locking model:
1#: Is enforced by a bitfield of the in-use banks, so that only a single
operation can happen in a specific bank at any time.
2#: Is handled by the ongoing_pe boolean which is set before any write
or erase, and is released only at the very end of the operation. This way, no other destructive operation on the chip can start during this time frame.
3#: An ongoing_rd boolean allows to track the ongoing reads, so that
only one can be performed at a time.
4#: An ongoing_io boolean is introduced in order to capture and
serialize bus accessed. This is the one being released "sooner" than before, because we only need to protect the chip against other SPI accesses during the I/O phase, which for the destructive operations is the beginning of the operation (when we send the command cycles and possibly the data), while the second part of the operation (the erase delay or the programmation delay) is when we can do something else in another bank.
5#: Is handled by the three booleans presented above, if any of them is
set, the chip is not yet ready for the operation and must wait.

All these internal variables are protected by the existing lock, so that changes in this structure are atomic. The serialization is handled with a wait queue."

Here is now a benchmark with a Macronix MX25UW51245G with 4 banks and RWW support:

     // Testing the two accesses in the same bank
     $ flash_speed -b0 -k0 -c10 -d /dev/mtd0
     [...]
     testing read while write latency
     read while write took 51ms, read ended after 51ms

     // Testing the two accesses within different banks
     $ flash_speed -b0 -k4096 -c10 -d /dev/mtd0
     [...]
     testing read while write latency
     read while write took 51ms, read ended after 20ms

Parallel accesses have been validated with io_paral. A slight increase of the time spent on this test has however been noticed. With my configuration, over a limited number of blocks, the overall operation took 22 min without any RWW changes up to 27 min with these changes, maybe due to the number of additional scheduling situations involved).

Here is a branch with the mtd-utils patch bringing support for this additional "-k" parameter in flash_speed (for the second block to use during RWW testing), used to get the above results:
https://github.com/miquelraynal/mtd-utils/compare/master...rww

Cheers,
Miquèl

Changes in v3:
* Fix the bank offsets calculations by providing the same values when locking and when unlocking (might be changed by the functions themselves without use noticing).
* I completely changed the way the locking works because there was a new
constraint: reads cannot be interrupted and status reads cannot happen during a read. Hence, as the multi-locks design was starting to be too messy, I changed the implementation to use a bunch of variables to track the read while write state, protected by the main spi-nor lock. If the internal state does not allow the operation, a sleep starts in a queue, until the threads are woken up after a state update. I know it is very verbose, I am open to suggestions.

Miquel Raynal (9):
mtd: spi-nor: Create macros to define chip IDs and geometries
mtd: spi-nor: Introduce the concept of bank
mtd: spi-nor: Add a macro to define more banks
mtd: spi-nor: Reorder the preparation vs locking steps
mtd: spi-nor: Separate preparation and locking
mtd: spi-nor: Prepare the introduction of a new locking mechanism
mtd: spi-nor: Add a RWW flag
mtd: spi-nor: Enhance locking to support reads while writes
mtd: spi-nor: macronix: Add support for mx25uw51245g with RWW

 drivers/mtd/spi-nor/core.c     | 396 +++++++++++++++++++++++++++++++--
 drivers/mtd/spi-nor/core.h     |  61 ++---
 drivers/mtd/spi-nor/macronix.c |   3 +
 include/linux/mtd/spi-nor.h    |  13 ++
 4 files changed, 424 insertions(+), 49 deletions(-)
Signature · 6 lines
2.34.1


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
author maintainer
#1 lore raw

PATCH v3 1/9 mtd: spi-nor: Create macros to define chip IDs and geometries

The INFO() macro defines an ID array and a couple of geometry properties. Right now all its lines are duplicated twice because of the INFO6() macro (for extended IDs) and soon as well we will need to add a geometry parameter to include the number of banks.

In order to limit the code duplication, let's create a number of intermediate macros which will facilitate defining high-level INFOX() macros.

There is not functional change.

---
 drivers/mtd/spi-nor/core.h | 43 ++++++++++++++++++--------------------
 1 file changed, 20 insertions(+), 23 deletions(-)
drivers/mtd/spi-nor/core.h +20 -23
index 85b0cf254e97..dc74c7be3e28 100644
@@ -527,33 +527,30 @@ struct flash_info {
 	const struct spi_nor_fixups *fixups;
 };
 
+#define SPI_NOR_ID_2ITEMS(_id) ((_id) >> 8) & 0xff, (_id) & 0xff
+#define SPI_NOR_ID_3ITEMS(_id) ((_id) >> 16) & 0xff, SPI_NOR_ID_2ITEMS(_id)
+
+#define SPI_NOR_ID(_jedec_id, _ext_id)					\
+	.id = { SPI_NOR_ID_3ITEMS(_jedec_id), SPI_NOR_ID_2ITEMS(_ext_id) }, \
+	.id_len = !(_jedec_id) ? 0 : (3 + ((_ext_id) ? 2 : 0))
+
+#define SPI_NOR_ID6(_jedec_id, _ext_id)					\
+	.id = { SPI_NOR_ID_3ITEMS(_jedec_id), SPI_NOR_ID_3ITEMS(_ext_id) }, \
+	.id_len = 6
+
+#define SPI_NOR_GEOMETRY(_sector_size, _n_sectors)			\
+	.sector_size = (_sector_size),					\
+	.n_sectors = (_n_sectors),					\
+	.page_size = 256
+
 /* Used when the "_ext_id" is two bytes at most */
 #define INFO(_jedec_id, _ext_id, _sector_size, _n_sectors)		\
-		.id = {							\
-			((_jedec_id) >> 16) & 0xff,			\
-			((_jedec_id) >> 8) & 0xff,			\
-			(_jedec_id) & 0xff,				\
-			((_ext_id) >> 8) & 0xff,			\
-			(_ext_id) & 0xff,				\
-			},						\
-		.id_len = (!(_jedec_id) ? 0 : (3 + ((_ext_id) ? 2 : 0))),	\
-		.sector_size = (_sector_size),				\
-		.n_sectors = (_n_sectors),				\
-		.page_size = 256,					\
+	SPI_NOR_ID((_jedec_id), (_ext_id)),				\
+	SPI_NOR_GEOMETRY((_sector_size), (_n_sectors)),
 
 #define INFO6(_jedec_id, _ext_id, _sector_size, _n_sectors)		\
-		.id = {							\
-			((_jedec_id) >> 16) & 0xff,			\
-			((_jedec_id) >> 8) & 0xff,			\
-			(_jedec_id) & 0xff,				\
-			((_ext_id) >> 16) & 0xff,			\
-			((_ext_id) >> 8) & 0xff,			\
-			(_ext_id) & 0xff,				\
-			},						\
-		.id_len = 6,						\
-		.sector_size = (_sector_size),				\
-		.n_sectors = (_n_sectors),				\
-		.page_size = 256,					\
+	SPI_NOR_ID6((_jedec_id), (_ext_id)),				\
+	SPI_NOR_GEOMETRY((_sector_size), (_n_sectors)),
 
 #define CAT25_INFO(_sector_size, _n_sectors, _page_size, _addr_nbytes)	\
 		.sector_size = (_sector_size),				\
Signature · 6 lines
2.34.1


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
#2 lore raw

Re: [PATCH v3 1/9] mtd: spi-nor: Create macros to define chip IDs and geometries

Quoted 10 lines · Miquel Raynal

The INFO() macro defines an ID array and a couple of geometry properties. Right now all its lines are duplicated twice because of the INFO6() macro (for extended IDs) and soon as well we will need to add a geometry parameter to include the number of banks.

In order to limit the code duplication, let's create a number of intermediate macros which will facilitate defining high-level INFOX() macros.

There is not functional change.

s/not/no and applied to spi-nor/next.

Thanks!

Quoted 6 lines
---
  drivers/mtd/spi-nor/core.h | 43 ++++++++++++++++++--------------------
  1 file changed, 20 insertions(+), 23 deletions(-)

______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

author maintainer
#3 lore raw

PATCH v3 2/9 mtd: spi-nor: Introduce the concept of bank

In reply to Miquel Raynal

SPI-NOR chips are made of pages, which gathered in small groups make (erase) sectors. Sectors, gathered together, make banks inside the chip. So far there was only one bank per device supported, but we are about to introduce support for new chips featuring several banks (up to 4 so far) where different operations may happen in parallel.

Let's allow describing these additional bank parameters.

---
 drivers/mtd/spi-nor/core.c |  3 ++-
 drivers/mtd/spi-nor/core.h | 16 +++++++++++-----
 2 files changed, 13 insertions(+), 6 deletions(-)
drivers/mtd/spi-nor/core.c +2 -1
index f2c64006f8d7..38a57aac6754 100644
@@ -2539,7 +2539,8 @@ static void spi_nor_init_default_params(struct spi_nor *nor)
 
 	/* Set SPI NOR sizes. */
 	params->writesize = 1;
-	params->size = (u64)info->sector_size * info->n_sectors;
+	params->bank_size = (u64)info->sector_size * info->n_sectors;
+	params->size = params->bank_size * info->n_banks;
 	params->page_size = info->page_size;
 
 	if (!(info->flags & SPI_NOR_NO_FR)) {
drivers/mtd/spi-nor/core.h +11 -5
index dc74c7be3e28..8a067d56c995 100644
@@ -336,7 +336,8 @@ struct spi_nor_otp {
  * by the spi_nor_fixups hooks, or dynamically when parsing the JESD216
  * Serial Flash Discoverable Parameters (SFDP) tables.
  *
- * @size:		the flash memory density in bytes.
+ * @bank_size:		the flash memory bank density in bytes.
+ * @size:		the total flash memory density in bytes.
  * @writesize		Minimal writable flash unit size. Defaults to 1. Set to
  *			ECC unit size for ECC-ed flashes.
  * @page_size:		the page size of the SPI NOR flash memory.
@@ -374,6 +375,7 @@ struct spi_nor_otp {
  * @locking_ops:	SPI NOR locking methods.
  */
 struct spi_nor_flash_parameter {
+	u64				bank_size;
 	u64				size;
 	u32				writesize;
 	u32				page_size;
@@ -434,7 +436,8 @@ struct spi_nor_fixups {
  * @id_len:         the number of bytes of ID.
  * @sector_size:    the size listed here is what works with SPINOR_OP_SE, which
  *                  isn't necessarily called a "sector" by the vendor.
- * @n_sectors:      the number of sectors.
+ * @n_sectors:      the number of sectors per bank.
+ * @n_banks:        the number of banks.
  * @page_size:      the flash's page size.
  * @addr_nbytes:    number of address bytes to send.
  *
@@ -493,6 +496,7 @@ struct flash_info {
 	u8 id_len;
 	unsigned sector_size;
 	u16 n_sectors;
+	u16 n_banks;
 	u16 page_size;
 	u8 addr_nbytes;
 
@@ -538,23 +542,25 @@ struct flash_info {
 	.id = { SPI_NOR_ID_3ITEMS(_jedec_id), SPI_NOR_ID_3ITEMS(_ext_id) }, \
 	.id_len = 6
 
-#define SPI_NOR_GEOMETRY(_sector_size, _n_sectors)			\
+#define SPI_NOR_GEOMETRY(_sector_size, _n_sectors, _n_banks)		\
 	.sector_size = (_sector_size),					\
 	.n_sectors = (_n_sectors),					\
+	.n_banks = (_n_banks),						\
 	.page_size = 256
 
 /* Used when the "_ext_id" is two bytes at most */
 #define INFO(_jedec_id, _ext_id, _sector_size, _n_sectors)		\
 	SPI_NOR_ID((_jedec_id), (_ext_id)),				\
-	SPI_NOR_GEOMETRY((_sector_size), (_n_sectors)),
+	SPI_NOR_GEOMETRY((_sector_size), (_n_sectors), 1),
 
 #define INFO6(_jedec_id, _ext_id, _sector_size, _n_sectors)		\
 	SPI_NOR_ID6((_jedec_id), (_ext_id)),				\
-	SPI_NOR_GEOMETRY((_sector_size), (_n_sectors)),
+	SPI_NOR_GEOMETRY((_sector_size), (_n_sectors), 1),
 
 #define CAT25_INFO(_sector_size, _n_sectors, _page_size, _addr_nbytes)	\
 		.sector_size = (_sector_size),				\
 		.n_sectors = (_n_sectors),				\
+		.n_banks = 1,						\
 		.page_size = (_page_size),				\
 		.addr_nbytes = (_addr_nbytes),				\
 		.flags = SPI_NOR_NO_ERASE | SPI_NOR_NO_FR,		\
Signature · 6 lines
2.34.1


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
#4 lore raw

Re: [PATCH v3 2/9] mtd: spi-nor: Introduce the concept of bank

Hi, Miquel,

Quoted 1 line · Miquel Raynal

SPI-NOR chips are made of pages, which gathered in small groups make

nit: s/SPI-NOR/ SPI NOR

Quoted 19 lines

(erase) sectors. Sectors, gathered together, make banks inside the chip. So far there was only one bank per device supported, but we are about to introduce support for new chips featuring several banks (up to 4 so far) where different operations may happen in parallel.

Let's allow describing these additional bank parameters.

---
  drivers/mtd/spi-nor/core.c |  3 ++-
  drivers/mtd/spi-nor/core.h | 16 +++++++++++-----
  2 files changed, 13 insertions(+), 6 deletions(-)
drivers/mtd/spi-nor/core.c +0 -0
index f2c64006f8d7..38a57aac6754 100644

drivers/mtd/spi-nor/core.c static void spi_nor_init_default_params(struct spi_nor *nor) · line 2543

Quoted 6 lines
drivers/mtd/spi-nor/core.c +2 -1
@@ -2539,7 +2539,8 @@ static void spi_nor_init_default_params(struct spi_nor *nor)
 
  	/* Set SPI NOR sizes. */
  	params->writesize = 1;
-	params->size = (u64)info->sector_size * info->n_sectors;
+	params->bank_size = (u64)info->sector_size * info->n_sectors;
+	params->size = params->bank_size * info->n_banks;

Is the datasheet for these chips public? I see JESD216 says nothing about flash banks.

I'm wondering whether we should keep the n_sectors as the total number of sectors per flash or not. Does this flash type support Software Block Protection? How do they count the sectors on Block Protection, per flash
or per bank?

Quoted 36 lines
drivers/mtd/spi-nor/core.c +0 -0
  	params->page_size = info->page_size;
 
  	if (!(info->flags & SPI_NOR_NO_FR)) {
drivers/mtd/spi-nor/core.h +5 -2
index dc74c7be3e28..8a067d56c995 100644
@@ -336,7 +336,8 @@ struct spi_nor_otp {
   * by the spi_nor_fixups hooks, or dynamically when parsing the JESD216
   * Serial Flash Discoverable Parameters (SFDP) tables.
   *
- * @size:		the flash memory density in bytes.
+ * @bank_size:		the flash memory bank density in bytes.
+ * @size:		the total flash memory density in bytes.
   * @writesize		Minimal writable flash unit size. Defaults to 1. Set to
   *			ECC unit size for ECC-ed flashes.
   * @page_size:		the page size of the SPI NOR flash memory.
@@ -374,6 +375,7 @@ struct spi_nor_otp {
   * @locking_ops:	SPI NOR locking methods.
   */
  struct spi_nor_flash_parameter {
+	u64				bank_size;
  	u64				size;
  	u32				writesize;
  	u32				page_size;
@@ -434,7 +436,8 @@ struct spi_nor_fixups {
   * @id_len:         the number of bytes of ID.
   * @sector_size:    the size listed here is what works with SPINOR_OP_SE, which
   *                  isn't necessarily called a "sector" by the vendor.
- * @n_sectors:      the number of sectors.
+ * @n_sectors:      the number of sectors per bank.
+ * @n_banks:        the number of banks.
   * @page_size:      the flash's page size.
   * @addr_nbytes:    number of address bytes to send.
   *

drivers/mtd/spi-nor/core.h struct flash_info { · line 500

Quoted 5 lines
drivers/mtd/spi-nor/core.h +1 -0
@@ -493,6 +496,7 @@ struct flash_info {
  	u8 id_len;
  	unsigned sector_size;
  	u16 n_sectors;
+	u16 n_banks;
  	u16 page_size;

We can try u8 nbanks for now. And we would define it here, to avoid struct padding.

Quoted 21 lines
drivers/mtd/spi-nor/core.h +3 -2
  	u8 addr_nbytes;
 
@@ -538,23 +542,25 @@ struct flash_info {
  	.id = { SPI_NOR_ID_3ITEMS(_jedec_id), SPI_NOR_ID_3ITEMS(_ext_id) }, \
  	.id_len = 6
 
-#define SPI_NOR_GEOMETRY(_sector_size, _n_sectors)			\
+#define SPI_NOR_GEOMETRY(_sector_size, _n_sectors, _n_banks)		\
  	.sector_size = (_sector_size),					\
  	.n_sectors = (_n_sectors),					\
+	.n_banks = (_n_banks),						\
  	.page_size = 256
 
  /* Used when the "_ext_id" is two bytes at most */
  #define INFO(_jedec_id, _ext_id, _sector_size, _n_sectors)		\
  	SPI_NOR_ID((_jedec_id), (_ext_id)),				\
-	SPI_NOR_GEOMETRY((_sector_size), (_n_sectors)),
+	SPI_NOR_GEOMETRY((_sector_size), (_n_sectors), 1),
 
  #define INFO6(_jedec_id, _ext_id, _sector_size, _n_sectors)		\
  	SPI_NOR_ID6((_jedec_id), (_ext_id)),				\

drivers/mtd/spi-nor/core.h struct flash_info { · line 566

Quoted 10 lines
drivers/mtd/spi-nor/core.h +2 -1
-	SPI_NOR_GEOMETRY((_sector_size), (_n_sectors)),
+	SPI_NOR_GEOMETRY((_sector_size), (_n_sectors), 1),
 
  #define CAT25_INFO(_sector_size, _n_sectors, _page_size, _addr_nbytes)	\
  		.sector_size = (_sector_size),				\
  		.n_sectors = (_n_sectors),				\
+		.n_banks = 1,						\
  		.page_size = (_page_size),				\
  		.addr_nbytes = (_addr_nbytes),				\
  		.flags = SPI_NOR_NO_ERASE | SPI_NOR_NO_FR,		\

you need to update S3AN_INFO as well.

Cheers,
ta

______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

author maintainer
#5 lore raw

Re: [PATCH v3 2/9] mtd: spi-nor: Introduce the concept of bank

Hi Tudor,

Jaime, a few questions for you below.

[email protected] wrote on Thu, 19 Jan 2023 16:34:28 +0000:

Quoted 6 lines

Hi, Miquel,

Quoted 1 line · Miquel Raynal

SPI-NOR chips are made of pages, which gathered in small groups make

nit: s/SPI-NOR/ SPI NOR

Noted.

Quoted 15 lines
Quoted 15 lines

(erase) sectors. Sectors, gathered together, make banks inside the chip. So far there was only one bank per device supported, but we are about to introduce support for new chips featuring several banks (up to 4 so far) where different operations may happen in parallel.

Let's allow describing these additional bank parameters.

---
  drivers/mtd/spi-nor/core.c |  3 ++-
  drivers/mtd/spi-nor/core.h | 16 +++++++++++-----
  2 files changed, 13 insertions(+), 6 deletions(-)
Quoted 13 lines

drivers/mtd/spi-nor/core.c static void spi_nor_init_default_params(struct spi_nor *nor) · line 2542

Quoted 10 lines
drivers/mtd/spi-nor/core.c +2 -1
index f2c64006f8d7..38a57aac6754 100644
@@ -2539,7 +2539,8 @@ static void spi_nor_init_default_params(struct spi_nor *nor)  
  >   	/* Set SPI NOR sizes. */  
  	params->writesize = 1;
-	params->size = (u64)info->sector_size * info->n_sectors;
+	params->bank_size = (u64)info->sector_size * info->n_sectors;
+	params->size = params->bank_size * info->n_banks;  

Is the datasheet for these chips public? I see JESD216 says nothing about flash banks.

Jaime, do you have a public datasheet for the MX25UW51245G ?

Quoted 1 line

I'm wondering whether we should keep the n_sectors as the total number of sectors per flash or not.

This is indeed a good point and I did think about it when I wrote the initial support. For me it looks like the banks are only relevant for the RWW purpose (for now) so I decided I would keep the changes minimal and not mess with the existing variables further. So I just added a "bank" member, and if you want the number of sectors per bank, you can divide nsectors by the number of banks. Another approach might be, as you ask, to count the number of sectors based on the number of sectors per bank and the number of banks. We can move to this approach later I believe, if ever useful.

Quoted 3 lines

Does this flash type support Software Block
Protection? How do they count the sectors on Block Protection, per flash or per bank?

Yes it supports block protection, AFAICS it is a per-sector configuration, which does not care about banks at all. It looks like you can protect 2^n sectors (called "blocks" in the datasheet) from the start or from the end of the device.

Jaime, do you confirm?

Quoted 35 lines
Quoted 35 lines
drivers/mtd/spi-nor/core.c +0 -0
  	params->page_size = info->page_size;  
  >   	if (!(info->flags & SPI_NOR_NO_FR)) {  
drivers/mtd/spi-nor/core.h +5 -2
index dc74c7be3e28..8a067d56c995 100644
@@ -336,7 +336,8 @@ struct spi_nor_otp {
   * by the spi_nor_fixups hooks, or dynamically when parsing the JESD216
   * Serial Flash Discoverable Parameters (SFDP) tables.
   *
- * @size:		the flash memory density in bytes.
+ * @bank_size:		the flash memory bank density in bytes.
+ * @size:		the total flash memory density in bytes.
   * @writesize		Minimal writable flash unit size. Defaults to 1. Set to
   *			ECC unit size for ECC-ed flashes.
   * @page_size:		the page size of the SPI NOR flash memory.
@@ -374,6 +375,7 @@ struct spi_nor_otp {
   * @locking_ops:	SPI NOR locking methods.
   */
  struct spi_nor_flash_parameter {
+	u64				bank_size;
  	u64				size;
  	u32				writesize;
  	u32				page_size;
@@ -434,7 +436,8 @@ struct spi_nor_fixups {
   * @id_len:         the number of bytes of ID.
   * @sector_size:    the size listed here is what works with SPINOR_OP_SE, which
   *                  isn't necessarily called a "sector" by the vendor.
- * @n_sectors:      the number of sectors.
+ * @n_sectors:      the number of sectors per bank.
+ * @n_banks:        the number of banks.
   * @page_size:      the flash's page size.
   * @addr_nbytes:    number of address bytes to send.
   *
@@ -493,6 +496,7 @@ struct flash_info {
Quoted 8 lines

drivers/mtd/spi-nor/core.h struct flash_info { · line 500

Quoted 5 lines
drivers/mtd/spi-nor/core.h +1 -0
  	u8 id_len;
  	unsigned sector_size;
  	u16 n_sectors;
+	u16 n_banks;
  	u16 page_size;  

We can try u8 nbanks for now. And we would define it here, to avoid struct padding.

Ok.

Quoted 20 lines
Quoted 20 lines
drivers/mtd/spi-nor/core.h +0 -0
  	u8 addr_nbytes;  
  > @@ -538,23 +542,25 @@ struct flash_info {  
  	.id = { SPI_NOR_ID_3ITEMS(_jedec_id), SPI_NOR_ID_3ITEMS(_ext_id) }, \
  	.id_len = 6  
  > -#define SPI_NOR_GEOMETRY(_sector_size, _n_sectors)			\  
drivers/mtd/spi-nor/core.h +4 -2
+#define SPI_NOR_GEOMETRY(_sector_size, _n_sectors, _n_banks)		\
  	.sector_size = (_sector_size),					\
  	.n_sectors = (_n_sectors),					\
+	.n_banks = (_n_banks),						\
  	.page_size = 256  
  >   /* Used when the "_ext_id" is two bytes at most */  
  #define INFO(_jedec_id, _ext_id, _sector_size, _n_sectors)		\
  	SPI_NOR_ID((_jedec_id), (_ext_id)),				\
-	SPI_NOR_GEOMETRY((_sector_size), (_n_sectors)),
+	SPI_NOR_GEOMETRY((_sector_size), (_n_sectors), 1),  
  >   #define INFO6(_jedec_id, _ext_id, _sector_size, _n_sectors)		\  
  	SPI_NOR_ID6((_jedec_id), (_ext_id)),				\
-	SPI_NOR_GEOMETRY((_sector_size), (_n_sectors)),
+	SPI_NOR_GEOMETRY((_sector_size), (_n_sectors), 1),  
  >   #define CAT25_INFO(_sector_size, _n_sectors, _page_size, _addr_nbytes)	\  
Quoted 8 lines

drivers/mtd/spi-nor/core.h struct flash_info { · line 566

Quoted 6 lines
  		.sector_size = (_sector_size),				\
  		.n_sectors = (_n_sectors),				\
drivers/mtd/spi-nor/core.h +1 -0
+		.n_banks = 1,						\
  		.page_size = (_page_size),				\
  		.addr_nbytes = (_addr_nbytes),				\
  		.flags = SPI_NOR_NO_ERASE | SPI_NOR_NO_FR,		\  

you need to update S3AN_INFO as well.

Completely missed that one, thanks!

Cheers,
Miquèl

______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

#6 lore raw

Re: [PATCH v3 2/9] mtd: spi-nor: Introduce the concept of bank

Quoted 58 lines · Miquel Raynal

Hi Tudor,

Jaime, a few questions for you below.

[email protected] wrote on Thu, 19 Jan 2023 16:34:28 +0000:

Quoted 6 lines

Hi, Miquel,

Quoted 1 line · Miquel Raynal

SPI-NOR chips are made of pages, which gathered in small groups make

nit: s/SPI-NOR/ SPI NOR

Noted.

Quoted 15 lines
Quoted 15 lines

(erase) sectors. Sectors, gathered together, make banks inside the chip. So far there was only one bank per device supported, but we are about to introduce support for new chips featuring several banks (up to 4 so far) where different operations may happen in parallel.

Let's allow describing these additional bank parameters.

---
  drivers/mtd/spi-nor/core.c |  3 ++-
  drivers/mtd/spi-nor/core.h | 16 +++++++++++-----
  2 files changed, 13 insertions(+), 6 deletions(-)
Quoted 13 lines

drivers/mtd/spi-nor/core.c static void spi_nor_init_default_params(struct spi_nor *nor) · line 2542

Quoted 10 lines
drivers/mtd/spi-nor/core.c +2 -1
index f2c64006f8d7..38a57aac6754 100644
@@ -2539,7 +2539,8 @@ static void spi_nor_init_default_params(struct spi_nor *nor)  
  >   	/* Set SPI NOR sizes. */  
  	params->writesize = 1;
-	params->size = (u64)info->sector_size * info->n_sectors;
+	params->bank_size = (u64)info->sector_size * info->n_sectors;
+	params->size = params->bank_size * info->n_banks;  

Is the datasheet for these chips public? I see JESD216 says nothing about flash banks.

Jaime, do you have a public datasheet for the MX25UW51245G ?

Quoted 1 line

I'm wondering whether we should keep the n_sectors as the total number of sectors per flash or not.

This is indeed a good point and I did think about it when I wrote the initial support. For me it looks like the banks are only relevant for the RWW purpose (for now) so I decided I would keep the changes minimal and not mess with the existing variables further. So I just added a "bank" member, and if you want the number of sectors per bank, you can divide nsectors by the number of banks. Another approach might be, as you ask, to count the number of sectors based on the number of sectors per bank and the number of banks. We can move to this approach later I believe, if ever useful.

Quoted 8 lines
Quoted 3 lines

Does this flash type support Software Block
Protection? How do they count the sectors on Block Protection, per flash or per bank?

Yes it supports block protection, AFAICS it is a per-sector configuration, which does not care about banks at all. It looks like you can protect 2^n sectors (called "blocks" in the datasheet) from the start or from the end of the device.

then you shouldn't change the n_sectors meaning, and keep it per flash, and not per bank. Otherwise you break the block protection support for these flashes. If you really need the number of sectors per bank, you can divide n_sectors (per flash) by nbanks, can't you? So bank_size should look like (n_sectors / nbanks) * sector_size.

Quoted 78 lines

Jaime, do you confirm?

Quoted 35 lines
Quoted 35 lines
drivers/mtd/spi-nor/core.c +0 -0
  	params->page_size = info->page_size;  
  >   	if (!(info->flags & SPI_NOR_NO_FR)) {  
drivers/mtd/spi-nor/core.h +5 -2
index dc74c7be3e28..8a067d56c995 100644
@@ -336,7 +336,8 @@ struct spi_nor_otp {
   * by the spi_nor_fixups hooks, or dynamically when parsing the JESD216
   * Serial Flash Discoverable Parameters (SFDP) tables.
   *
- * @size:		the flash memory density in bytes.
+ * @bank_size:		the flash memory bank density in bytes.
+ * @size:		the total flash memory density in bytes.
   * @writesize		Minimal writable flash unit size. Defaults to 1. Set to
   *			ECC unit size for ECC-ed flashes.
   * @page_size:		the page size of the SPI NOR flash memory.
@@ -374,6 +375,7 @@ struct spi_nor_otp {
   * @locking_ops:	SPI NOR locking methods.
   */
  struct spi_nor_flash_parameter {
+	u64				bank_size;
  	u64				size;
  	u32				writesize;
  	u32				page_size;
@@ -434,7 +436,8 @@ struct spi_nor_fixups {
   * @id_len:         the number of bytes of ID.
   * @sector_size:    the size listed here is what works with SPINOR_OP_SE, which
   *                  isn't necessarily called a "sector" by the vendor.
- * @n_sectors:      the number of sectors.
+ * @n_sectors:      the number of sectors per bank.
+ * @n_banks:        the number of banks.
   * @page_size:      the flash's page size.
   * @addr_nbytes:    number of address bytes to send.
   *
@@ -493,6 +496,7 @@ struct flash_info {
Quoted 8 lines

drivers/mtd/spi-nor/core.h struct flash_info { · line 500

Quoted 5 lines
drivers/mtd/spi-nor/core.h +1 -0
  	u8 id_len;
  	unsigned sector_size;
  	u16 n_sectors;
+	u16 n_banks;
  	u16 page_size;  

We can try u8 nbanks for now. And we would define it here, to avoid struct padding.

Ok.

Quoted 20 lines
Quoted 20 lines
drivers/mtd/spi-nor/core.h +0 -0
  	u8 addr_nbytes;  
  > @@ -538,23 +542,25 @@ struct flash_info {  
  	.id = { SPI_NOR_ID_3ITEMS(_jedec_id), SPI_NOR_ID_3ITEMS(_ext_id) }, \
  	.id_len = 6  
  > -#define SPI_NOR_GEOMETRY(_sector_size, _n_sectors)			\  
drivers/mtd/spi-nor/core.h +4 -2
+#define SPI_NOR_GEOMETRY(_sector_size, _n_sectors, _n_banks)		\
  	.sector_size = (_sector_size),					\
  	.n_sectors = (_n_sectors),					\
+	.n_banks = (_n_banks),						\
  	.page_size = 256  
  >   /* Used when the "_ext_id" is two bytes at most */  
  #define INFO(_jedec_id, _ext_id, _sector_size, _n_sectors)		\
  	SPI_NOR_ID((_jedec_id), (_ext_id)),				\
-	SPI_NOR_GEOMETRY((_sector_size), (_n_sectors)),
+	SPI_NOR_GEOMETRY((_sector_size), (_n_sectors), 1),  
  >   #define INFO6(_jedec_id, _ext_id, _sector_size, _n_sectors)		\  
  	SPI_NOR_ID6((_jedec_id), (_ext_id)),				\
-	SPI_NOR_GEOMETRY((_sector_size), (_n_sectors)),
+	SPI_NOR_GEOMETRY((_sector_size), (_n_sectors), 1),  
  >   #define CAT25_INFO(_sector_size, _n_sectors, _page_size, _addr_nbytes)	\  
Quoted 8 lines

drivers/mtd/spi-nor/core.h struct flash_info { · line 566

Quoted 6 lines
  		.sector_size = (_sector_size),				\
  		.n_sectors = (_n_sectors),				\
drivers/mtd/spi-nor/core.h +1 -0
+		.n_banks = 1,						\
  		.page_size = (_page_size),				\
  		.addr_nbytes = (_addr_nbytes),				\
  		.flags = SPI_NOR_NO_ERASE | SPI_NOR_NO_FR,		\  

you need to update S3AN_INFO as well.

Quoted 4 lines

Completely missed that one, thanks!

Cheers,
Miquèl

______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

author maintainer
#7 lore raw

Re: [PATCH v3 2/9] mtd: spi-nor: Introduce the concept of bank

Hi Tudor,

[email protected] wrote on Fri, 17 Mar 2023 03:33:02 +0000:

Quoted 63 lines · Miquel Raynal
Quoted 62 lines

Hi Tudor,

Jaime, a few questions for you below.

[email protected] wrote on Thu, 19 Jan 2023 16:34:28 +0000:

Quoted 6 lines

Hi, Miquel,

Quoted 1 line · Miquel Raynal

SPI-NOR chips are made of pages, which gathered in small groups make

nit: s/SPI-NOR/ SPI NOR

Noted.

Quoted 15 lines
Quoted 15 lines

(erase) sectors. Sectors, gathered together, make banks inside the chip. So far there was only one bank per device supported, but we are about to introduce support for new chips featuring several banks (up to 4 so far) where different operations may happen in parallel.

Let's allow describing these additional bank parameters.

---
  drivers/mtd/spi-nor/core.c |  3 ++-
  drivers/mtd/spi-nor/core.h | 16 +++++++++++-----
  2 files changed, 13 insertions(+), 6 deletions(-)
Quoted 13 lines

drivers/mtd/spi-nor/core.c static void spi_nor_init_default_params(struct spi_nor *nor) · line 2542

Quoted 10 lines
drivers/mtd/spi-nor/core.c +2 -1
index f2c64006f8d7..38a57aac6754 100644
@@ -2539,7 +2539,8 @@ static void spi_nor_init_default_params(struct spi_nor *nor)    
  >   	/* Set SPI NOR sizes. */    
  	params->writesize = 1;
-	params->size = (u64)info->sector_size * info->n_sectors;
+	params->bank_size = (u64)info->sector_size * info->n_sectors;
+	params->size = params->bank_size * info->n_banks;    

Is the datasheet for these chips public? I see JESD216 says nothing about flash banks.

Jaime, do you have a public datasheet for the MX25UW51245G ?

Quoted 1 line

I'm wondering whether we should keep the n_sectors as the total number of sectors per flash or not.

This is indeed a good point and I did think about it when I wrote the initial support. For me it looks like the banks are only relevant for the RWW purpose (for now) so I decided I would keep the changes minimal and not mess with the existing variables further. So I just added a "bank" member, and if you want the number of sectors per bank, you can divide nsectors by the number of banks. Another approach might be, as you ask, to count the number of sectors based on the number of sectors per bank and the number of banks. We can move to this approach later I believe, if ever useful.

Quoted 3 lines

Does this flash type support Software Block
Protection? How do they count the sectors on Block Protection, per flash or per bank?

Quoted 8 lines
Quoted 4 lines

Yes it supports block protection, AFAICS it is a per-sector configuration, which does not care about banks at all. It looks like you can protect 2^n sectors (called "blocks" in the datasheet) from the start or from the end of the device.

then you shouldn't change the n_sectors meaning, and keep it per flash, and not per bank. Otherwise you break the block protection support for these flashes.

That's totally right, good point. I'll update. Actually that was my initial intention and the commit log reflected this idea, instead of what I actually implemented in a second time.

Quoted 86 lines

If you really need the number of sectors per bank, you can divide n_sectors (per flash) by nbanks, can't you? So bank_size should look like (n_sectors / nbanks) * sector_size.

Quoted 82 lines

Jaime, do you confirm?

Quoted 35 lines
Quoted 35 lines
drivers/mtd/spi-nor/core.c +0 -0
  	params->page_size = info->page_size;    
  >   	if (!(info->flags & SPI_NOR_NO_FR)) {    
drivers/mtd/spi-nor/core.h +5 -2
index dc74c7be3e28..8a067d56c995 100644
@@ -336,7 +336,8 @@ struct spi_nor_otp {
   * by the spi_nor_fixups hooks, or dynamically when parsing the JESD216
   * Serial Flash Discoverable Parameters (SFDP) tables.
   *
- * @size:		the flash memory density in bytes.
+ * @bank_size:		the flash memory bank density in bytes.
+ * @size:		the total flash memory density in bytes.
   * @writesize		Minimal writable flash unit size. Defaults to 1. Set to
   *			ECC unit size for ECC-ed flashes.
   * @page_size:		the page size of the SPI NOR flash memory.
@@ -374,6 +375,7 @@ struct spi_nor_otp {
   * @locking_ops:	SPI NOR locking methods.
   */
  struct spi_nor_flash_parameter {
+	u64				bank_size;
  	u64				size;
  	u32				writesize;
  	u32				page_size;
@@ -434,7 +436,8 @@ struct spi_nor_fixups {
   * @id_len:         the number of bytes of ID.
   * @sector_size:    the size listed here is what works with SPINOR_OP_SE, which
   *                  isn't necessarily called a "sector" by the vendor.
- * @n_sectors:      the number of sectors.
+ * @n_sectors:      the number of sectors per bank.
+ * @n_banks:        the number of banks.
   * @page_size:      the flash's page size.
   * @addr_nbytes:    number of address bytes to send.
   *
@@ -493,6 +496,7 @@ struct flash_info {
Quoted 8 lines

drivers/mtd/spi-nor/core.h struct flash_info { · line 500

Quoted 5 lines
drivers/mtd/spi-nor/core.h +1 -0
  	u8 id_len;
  	unsigned sector_size;
  	u16 n_sectors;
+	u16 n_banks;
  	u16 page_size;    

We can try u8 nbanks for now. And we would define it here, to avoid struct padding.

Ok.

Quoted 20 lines
Quoted 20 lines
drivers/mtd/spi-nor/core.h +0 -0
  	u8 addr_nbytes;    
  > @@ -538,23 +542,25 @@ struct flash_info {    
  	.id = { SPI_NOR_ID_3ITEMS(_jedec_id), SPI_NOR_ID_3ITEMS(_ext_id) }, \
  	.id_len = 6    
  > -#define SPI_NOR_GEOMETRY(_sector_size, _n_sectors)			\    
drivers/mtd/spi-nor/core.h +4 -2
+#define SPI_NOR_GEOMETRY(_sector_size, _n_sectors, _n_banks)		\
  	.sector_size = (_sector_size),					\
  	.n_sectors = (_n_sectors),					\
+	.n_banks = (_n_banks),						\
  	.page_size = 256    
  >   /* Used when the "_ext_id" is two bytes at most */    
  #define INFO(_jedec_id, _ext_id, _sector_size, _n_sectors)		\
  	SPI_NOR_ID((_jedec_id), (_ext_id)),				\
-	SPI_NOR_GEOMETRY((_sector_size), (_n_sectors)),
+	SPI_NOR_GEOMETRY((_sector_size), (_n_sectors), 1),    
  >   #define INFO6(_jedec_id, _ext_id, _sector_size, _n_sectors)		\    
  	SPI_NOR_ID6((_jedec_id), (_ext_id)),				\
-	SPI_NOR_GEOMETRY((_sector_size), (_n_sectors)),
+	SPI_NOR_GEOMETRY((_sector_size), (_n_sectors), 1),    
  >   #define CAT25_INFO(_sector_size, _n_sectors, _page_size, _addr_nbytes)	\    
Quoted 8 lines

drivers/mtd/spi-nor/core.h struct flash_info { · line 566

Quoted 6 lines
  		.sector_size = (_sector_size),				\
  		.n_sectors = (_n_sectors),				\
drivers/mtd/spi-nor/core.h +1 -0
+		.n_banks = 1,						\
  		.page_size = (_page_size),				\
  		.addr_nbytes = (_addr_nbytes),				\
  		.flags = SPI_NOR_NO_ERASE | SPI_NOR_NO_FR,		\    

you need to update S3AN_INFO as well.

Completely missed that one, thanks!

Cheers,
Miquèl

Thanks,
Miquèl

______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

author maintainer
#8 lore raw

PATCH v3 3/9 mtd: spi-nor: Add a macro to define more banks

In reply to Miquel Raynal

Most of the chips on the market only feature a single bank. However, new chips may support more than a single bank, with the possibility to parallelize some operations. Let's introduce an INFOB() macro which also takes a n_bank parameter.

---
 drivers/mtd/spi-nor/core.h | 4 ++++
 1 file changed, 4 insertions(+)
drivers/mtd/spi-nor/core.h +4 -0
index 8a067d56c995..044d49d749e0 100644
@@ -553,6 +553,10 @@ struct flash_info {
 	SPI_NOR_ID((_jedec_id), (_ext_id)),				\
 	SPI_NOR_GEOMETRY((_sector_size), (_n_sectors), 1),
 
+#define INFOB(_jedec_id, _ext_id, _sector_size, _n_sectors, _n_banks)	\
+	SPI_NOR_ID((_jedec_id), (_ext_id)),				\
+	SPI_NOR_GEOMETRY((_sector_size), (_n_sectors), (_n_banks)),
+
 #define INFO6(_jedec_id, _ext_id, _sector_size, _n_sectors)		\
 	SPI_NOR_ID6((_jedec_id), (_ext_id)),				\
 	SPI_NOR_GEOMETRY((_sector_size), (_n_sectors), 1),
Signature · 6 lines
2.34.1


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
author maintainer
#9 lore raw

PATCH v3 4/9 mtd: spi-nor: Reorder the preparation vs locking steps

In reply to Miquel Raynal

The ->prepare()/->unprepare() hooks are now legacy, and there are only two controllers left supporting them. In both cases, the implementation acquires a mutex, which is somehow redundant with the spi-nor main lock that we acquire as well in the spi_nor_[un]lock_and_[un]prep() helpers.

While the mutex taken in the core is necessary, the helper can be reorganized to first do the preparation, then acquire the core lock. This is necessary in order to be able to improve the locking mechanism in the core and should have no side effect.

---
 drivers/mtd/spi-nor/core.c | 15 ++++++---------
 1 file changed, 6 insertions(+), 9 deletions(-)
drivers/mtd/spi-nor/core.c +6 -9
index 38a57aac6754..de77ca55f74d 100644
@@ -1074,23 +1074,20 @@ int spi_nor_lock_and_prep(struct spi_nor *nor)
 {
 	int ret = 0;
 
-	mutex_lock(&nor->lock);
-
-	if (nor->controller_ops &&  nor->controller_ops->prepare) {
+	if (nor->controller_ops && nor->controller_ops->prepare)
 		ret = nor->controller_ops->prepare(nor);
-		if (ret) {
-			mutex_unlock(&nor->lock);
-			return ret;
-		}
-	}
+
+	mutex_lock(&nor->lock);
+
 	return ret;
 }
 
 void spi_nor_unlock_and_unprep(struct spi_nor *nor)
 {
+	mutex_unlock(&nor->lock);
+
 	if (nor->controller_ops && nor->controller_ops->unprepare)
 		nor->controller_ops->unprepare(nor);
-	mutex_unlock(&nor->lock);
 }
 
 static u32 spi_nor_convert_addr(struct spi_nor *nor, loff_t addr)
Signature · 6 lines
2.34.1


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
#10 lore raw

Re: [PATCH v3 4/9] mtd: spi-nor: Reorder the preparation vs locking steps

Hi, Miquel,

Quoted 2 lines · Miquel Raynal

The ->prepare()/->unprepare() hooks are now legacy, and there are only two controllers left supporting them. In both cases, the implementation

now there's only one, hisi-sfc.c.

Quoted 1 line

acquires a mutex, which is somehow redundant with the spi-nor main lock

I see a HIFMC_MAX_CHIP_NUM with value 2, the controller seems to be able to operate 2 flashes in parallel and that's why the internal mutex.

Quoted 7 lines

that we acquire as well in the spi_nor_[un]lock_and_[un]prep() helpers.

While the mutex taken in the core is necessary, the helper can be reorganized to first do the preparation, then acquire the core lock. This is necessary in order to be able to improve the locking mechanism in the core and should have no side effect.

the change seems fine for hisi, and since we're no longer adding drivers under spi-nor/controllers we should be good.

drivers/mtd/spi-nor/core.c int spi_nor_lock_and_prep(struct spi_nor *nor)

Quoted 10 lines
---
  drivers/mtd/spi-nor/core.c | 15 ++++++---------
  1 file changed, 6 insertions(+), 9 deletions(-)
drivers/mtd/spi-nor/core.c +0 -0
index 38a57aac6754..de77ca55f74d 100644
@@ -1074,23 +1074,20 @@ int spi_nor_lock_and_prep(struct spi_nor *nor)

you'll have to rename it to spi_nor_prep_and_lock to reflect the order of ops.

Quoted 9 lines
drivers/mtd/spi-nor/core.c +1 -4
  {
  	int ret = 0;
 
-	mutex_lock(&nor->lock);
-
-	if (nor->controller_ops &&  nor->controller_ops->prepare) {
+	if (nor->controller_ops && nor->controller_ops->prepare)
  		ret = nor->controller_ops->prepare(nor);
-		if (ret) {

drivers/mtd/spi-nor/core.c int spi_nor_lock_and_prep(struct spi_nor *nor) · line 1084

Quoted 10 lines
drivers/mtd/spi-nor/core.c +3 -4
-			mutex_unlock(&nor->lock);
-			return ret;
-		}
-	}
+
+	mutex_lock(&nor->lock);
+
  	return ret;
  }
 

Cheers,
ta

______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

author maintainer
#11 lore raw

Re: [PATCH v3 4/9] mtd: spi-nor: Reorder the preparation vs locking steps

Hi Tudor,

[email protected] wrote on Tue, 31 Jan 2023 05:11:33 +0000:

Quoted 7 lines

Hi, Miquel,

Quoted 2 lines · Miquel Raynal

The ->prepare()/->unprepare() hooks are now legacy, and there are only two controllers left supporting them. In both cases, the implementation

now there's only one, hisi-sfc.c.

Indeed.

Quoted 22 lines
Quoted 2 lines

acquires a mutex, which is somehow redundant with the spi-nor main lock

I see a HIFMC_MAX_CHIP_NUM with value 2, the controller seems to be able to operate 2 flashes in parallel and that's why the internal mutex.

Quoted 7 lines

that we acquire as well in the spi_nor_[un]lock_and_[un]prep() helpers.

While the mutex taken in the core is necessary, the helper can be reorganized to first do the preparation, then acquire the core lock. This is necessary in order to be able to improve the locking mechanism in the core and should have no side effect.

the change seems fine for hisi, and since we're no longer adding drivers under spi-nor/controllers we should be good.

Quoted 5 lines
---
  drivers/mtd/spi-nor/core.c | 15 ++++++---------
  1 file changed, 6 insertions(+), 9 deletions(-)
Quoted 8 lines

drivers/mtd/spi-nor/core.c int spi_nor_lock_and_prep(struct spi_nor *nor)

Quoted 5 lines
drivers/mtd/spi-nor/core.c +0 -0
index 38a57aac6754..de77ca55f74d 100644
@@ -1074,23 +1074,20 @@ int spi_nor_lock_and_prep(struct spi_nor *nor)  

you'll have to rename it to spi_nor_prep_and_lock to reflect the order of ops.

Sure.

Quoted 14 lines
Quoted 14 lines
drivers/mtd/spi-nor/core.c +2 -7
  {
  	int ret = 0;  
  > -	mutex_lock(&nor->lock);  
-
-	if (nor->controller_ops &&  nor->controller_ops->prepare) {
+	if (nor->controller_ops && nor->controller_ops->prepare)
  		ret = nor->controller_ops->prepare(nor);
-		if (ret) {
-			mutex_unlock(&nor->lock);
-			return ret;
-		}
-	}
+
Quoted 8 lines

drivers/mtd/spi-nor/core.c int spi_nor_lock_and_prep(struct spi_nor *nor) · line 1084

Quoted 5 lines
drivers/mtd/spi-nor/core.c +2 -0
+	mutex_lock(&nor->lock);
+
  	return ret;
  }
 

Cheers,
ta

Thanks,
Miquèl

______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

author maintainer
#12 lore raw

PATCH v3 5/9 mtd: spi-nor: Separate preparation and locking

In reply to Miquel Raynal

While this operation will remain a single function call in the end, let's extract the logic of the [un]prepare calls within their own static helper. We will soon add new flavors of the *_[un]lock_and_[un]prepare() helper, having the preparation logic outside will save us from duplicating code over and over again.

There is no functional change.

---
 drivers/mtd/spi-nor/core.c | 24 ++++++++++++++++++++----
 1 file changed, 20 insertions(+), 4 deletions(-)
drivers/mtd/spi-nor/core.c +20 -4
index de77ca55f74d..f87e57d97692 100644
@@ -1070,24 +1070,40 @@ static void spi_nor_set_4byte_opcodes(struct spi_nor *nor)
 	}
 }
 
-int spi_nor_lock_and_prep(struct spi_nor *nor)
+static int spi_nor_prep(struct spi_nor *nor)
 {
 	int ret = 0;
 
 	if (nor->controller_ops && nor->controller_ops->prepare)
 		ret = nor->controller_ops->prepare(nor);
 
+	return ret;
+}
+
+static void spi_nor_unprep(struct spi_nor *nor)
+{
+	if (nor->controller_ops && nor->controller_ops->unprepare)
+		nor->controller_ops->unprepare(nor);
+}
+
+int spi_nor_lock_and_prep(struct spi_nor *nor)
+{
+	int ret;
+
+	ret = spi_nor_prep(nor);
+	if (ret)
+		return ret;
+
 	mutex_lock(&nor->lock);
 
-	return ret;
+	return 0;
 }
 
 void spi_nor_unlock_and_unprep(struct spi_nor *nor)
 {
 	mutex_unlock(&nor->lock);
 
-	if (nor->controller_ops && nor->controller_ops->unprepare)
-		nor->controller_ops->unprepare(nor);
+	spi_nor_unprep(nor);
 }
 
 static u32 spi_nor_convert_addr(struct spi_nor *nor, loff_t addr)
Signature · 6 lines
2.34.1


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
author maintainer
#13 lore raw

PATCH v3 6/9 mtd: spi-nor: Prepare the introduction of a new locking mechanism

In reply to Miquel Raynal

This commit alone just introduces two new "lock and prepare" pairs of helpers which do the exact same thing as before. They will soon be improved in a followup commit which actually brings the logic, but I figured out it was more readable to do it this way.

One new pair is suffixed _pe which stands for "program and erase" and hence is being called by spi_nor_write() and spi_nor_erase().

The other pair is suffixed _rd which stands for "read" and hence is being called by spi_nor_read().

One note however, these extra helpers will need to know the operation range, so they come with two new parameters to define it. Otherwise there is no functional change.

---
 drivers/mtd/spi-nor/core.c | 59 ++++++++++++++++++++++++++++++++++----
 1 file changed, 53 insertions(+), 6 deletions(-)
drivers/mtd/spi-nor/core.c +53 -6
index f87e57d97692..6c08114e4275 100644
@@ -1086,6 +1086,7 @@ static void spi_nor_unprep(struct spi_nor *nor)
 		nor->controller_ops->unprepare(nor);
 }
 
+/* Generic helpers for internal locking and serialization */
 int spi_nor_lock_and_prep(struct spi_nor *nor)
 {
 	int ret;
@@ -1106,6 +1107,48 @@ void spi_nor_unlock_and_unprep(struct spi_nor *nor)
 	spi_nor_unprep(nor);
 }
 
+/* Internal locking helpers for program and erase operations */
+static int spi_nor_lock_and_prep_pe(struct spi_nor *nor, loff_t start, size_t len)
+{
+	int ret;
+
+	ret = spi_nor_prep(nor);
+	if (ret)
+		return ret;
+
+	mutex_lock(&nor->lock);
+
+	return 0;
+}
+
+static void spi_nor_unlock_and_unprep_pe(struct spi_nor *nor, loff_t start, size_t len)
+{
+	mutex_unlock(&nor->lock);
+
+	spi_nor_unprep(nor);
+}
+
+/* Internal locking helpers for read operations */
+static int spi_nor_lock_and_prep_rd(struct spi_nor *nor, loff_t start, size_t len)
+{
+	int ret;
+
+	ret = spi_nor_prep(nor);
+	if (ret)
+		return ret;
+
+	mutex_lock(&nor->lock);
+
+	return 0;
+}
+
+static void spi_nor_unlock_and_unprep_rd(struct spi_nor *nor, loff_t start, size_t len)
+{
+	mutex_unlock(&nor->lock);
+
+	spi_nor_unprep(nor);
+}
+
 static u32 spi_nor_convert_addr(struct spi_nor *nor, loff_t addr)
 {
 	if (!nor->params->convert_addr)
@@ -1457,7 +1500,7 @@ static int spi_nor_erase(struct mtd_info *mtd, struct erase_info *instr)
 	addr = instr->addr;
 	len = instr->len;
 
-	ret = spi_nor_lock_and_prep(nor);
+	ret = spi_nor_lock_and_prep_pe(nor, instr->addr, instr->len);
 	if (ret)
 		return ret;
 
@@ -1520,7 +1563,7 @@ static int spi_nor_erase(struct mtd_info *mtd, struct erase_info *instr)
 	ret = spi_nor_write_disable(nor);
 
 erase_err:
-	spi_nor_unlock_and_unprep(nor);
+	spi_nor_unlock_and_unprep_pe(nor, instr->addr, instr->len);
 
 	return ret;
 }
@@ -1690,11 +1733,13 @@ static int spi_nor_read(struct mtd_info *mtd, loff_t from, size_t len,
 			size_t *retlen, u_char *buf)
 {
 	struct spi_nor *nor = mtd_to_spi_nor(mtd);
+	loff_t from_lock = from;
+	size_t len_lock = len;
 	ssize_t ret;
 
 	dev_dbg(nor->dev, "from 0x%08x, len %zd\n", (u32)from, len);
 
-	ret = spi_nor_lock_and_prep(nor);
+	ret = spi_nor_lock_and_prep_rd(nor, from_lock, len_lock);
 	if (ret)
 		return ret;
 
@@ -1721,7 +1766,8 @@ static int spi_nor_read(struct mtd_info *mtd, loff_t from, size_t len,
 	ret = 0;
 
 read_err:
-	spi_nor_unlock_and_unprep(nor);
+	spi_nor_unlock_and_unprep_rd(nor, from_lock, len_lock);
+
 	return ret;
 }
 
@@ -1740,7 +1786,7 @@ static int spi_nor_write(struct mtd_info *mtd, loff_t to, size_t len,
 
 	dev_dbg(nor->dev, "to 0x%08x, len %zd\n", (u32)to, len);
 
-	ret = spi_nor_lock_and_prep(nor);
+	ret = spi_nor_lock_and_prep_pe(nor, to, len);
 	if (ret)
 		return ret;
 
@@ -1782,7 +1828,8 @@ static int spi_nor_write(struct mtd_info *mtd, loff_t to, size_t len,
 	}
 
 write_err:
-	spi_nor_unlock_and_unprep(nor);
+	spi_nor_unlock_and_unprep_pe(nor, to, len);
+
 	return ret;
 }
 
Signature · 6 lines
2.34.1


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
#14 lore raw

Re: [PATCH v3 6/9] mtd: spi-nor: Prepare the introduction of a new locking mechanism

Quoted 5 lines · Miquel Raynal

This commit alone just introduces two new "lock and prepare" pairs of helpers which do the exact same thing as before. They will soon be improved in a followup commit which actually brings the logic, but I figured out it was more readable to do it this way.

I'd like to not introduce new support for SPI NOR controller drivers (those under drivers/mtd/spi-nor/controllers/) so that we motivate the owners to move their drivers to SPI. So we may have to rewrite the helpers a bit, but I won't stumble on this right now, let me get the general idea of read while write support first.

Cheers,
ta

______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

author maintainer
#15 lore raw

PATCH v3 7/9 mtd: spi-nor: Add a RWW flag

In reply to Miquel Raynal

Introduce a new (no SFDP) flag for the feature that we are about to
support: Read While Write. This means, if the chip has several banks and supports RWW, once a page of data to write has been transferred into the chip's internal SRAM, another read operation happening on a different bank can be performed during the tPROG delay.

Adding this new flag involves enlarging the no_sfdp_flags variable to 16 bits.

---
 drivers/mtd/spi-nor/core.h | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/mtd/spi-nor/core.h +3 -1
index 044d49d749e0..299b60788597 100644
@@ -472,6 +472,7 @@ struct spi_nor_fixups {
  *   SPI_NOR_OCTAL_READ:      flash supports Octal Read.
  *   SPI_NOR_OCTAL_DTR_READ:  flash supports octal DTR Read.
  *   SPI_NOR_OCTAL_DTR_PP:    flash supports Octal DTR Page Program.
+ *   SPI_NOR_RWW:             flash supports reads while write.
  *
  * @fixup_flags:    flags that indicate support that can be discovered via SFDP
  *                  ideally, but can not be discovered for this particular flash
@@ -512,7 +513,7 @@ struct flash_info {
 #define NO_CHIP_ERASE			BIT(7)
 #define SPI_NOR_NO_FR			BIT(8)
 
-	u8 no_sfdp_flags;
+	u16 no_sfdp_flags;
 #define SPI_NOR_SKIP_SFDP		BIT(0)
 #define SECT_4K				BIT(1)
 #define SPI_NOR_DUAL_READ		BIT(3)
@@ -520,6 +521,7 @@ struct flash_info {
 #define SPI_NOR_OCTAL_READ		BIT(5)
 #define SPI_NOR_OCTAL_DTR_READ		BIT(6)
 #define SPI_NOR_OCTAL_DTR_PP		BIT(7)
+#define SPI_NOR_RWW			BIT(8)
 
 	u8 fixup_flags;
 #define SPI_NOR_4B_OPCODES		BIT(0)
Signature · 6 lines
2.34.1


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
author maintainer
#16 lore raw

PATCH v3 8/9 mtd: spi-nor: Enhance locking to support reads while writes

In reply to Miquel Raynal

On devices featuring several banks, the Read While Write (RWW) feature is here to improve the overall performance when performing parallel reads and writes at different locations (different banks). The following constraints have to be taken into account:
1#: A single operation can be performed in a given bank.
2#: Only a single program or erase operation can happen on the entire
chip (common hardware limitation to limit costs)
3#: Reads must remain serialized even though reads on different banks
might occur at the same time.
4#: The I/O bus is unique and thus is the most constrained resource, all
spi-nor operations requiring access to the spi bus (through the spi controller) must be serialized until the bus exchanges are over. So we must ensure a single operation can be "sent" at a time.
5#: Any other operation that would not be either a read or a write or an
erase is considered requiring access to the full chip and cannot be parallelized, we then need to ensure the full chip is in the idle state when this occurs.

All these constraints can easily be managed with a proper locking model:
1#: Is enforced by a bitfield of the in-use banks, so that only a single
operation can happen in a specific bank at any time.
2#: Is handled by the ongoing_pe boolean which is set before any write
or erase, and is released only at the very end of the operation. This way, no other destructive operation on the chip can start during this time frame.
3#: An ongoing_rd boolean allows to track the ongoing reads, so that
only one can be performed at a time.
4#: An ongoing_io boolean is introduced in order to capture and serialize
bus accessed. This is the one being released "sooner" than before, because we only need to protect the chip against other SPI accesses during the I/O phase, which for the destructive operations is the beginning of the operation (when we send the command cycles and possibly the data), while the second part of the operation (the erase delay or the programmation delay) is when we can do something else in another bank.
5#: Is handled by the three booleans presented above, if any of them is
set, the chip is not yet ready for the operation and must wait.

All these internal variables are protected by the existing lock, so that changes in this structure are atomic. The serialization is handled with a wait queue.

---
 drivers/mtd/spi-nor/core.c  | 319 ++++++++++++++++++++++++++++++++++--
 include/linux/mtd/spi-nor.h |  13 ++
 2 files changed, 317 insertions(+), 15 deletions(-)
drivers/mtd/spi-nor/core.c +304 -15
index 6c08114e4275..5d9eb40dfb3f 100644
@@ -588,6 +588,66 @@ int spi_nor_sr_ready(struct spi_nor *nor)
 	return !(nor->bouncebuf[0] & SR_WIP);
 }
 
+/**
+ * spi_nor_parallel_locking() - Checks if the RWW locking scheme shall be used
+ * @nor:	pointer to 'struct spi_nor'.
+ *
+ * Return: true if parallel locking is enabled, false otherwise.
+ */
+static bool spi_nor_parallel_locking(struct spi_nor *nor)
+{
+	if (nor->controller_ops &&
+	    (nor->controller_ops->prepare || nor->controller_ops->unprepare))
+		return false;
+
+	return nor->info->n_banks > 1 && nor->info->no_sfdp_flags & SPI_NOR_RWW;
+}
+
+/* Locking helpers for status read operations */
+static int spi_nor_rww_start_rdst(struct spi_nor *nor)
+{
+	int ret = -EAGAIN;
+
+	mutex_lock(&nor->lock);
+
+	if (nor->rww.ongoing_io || nor->rww.ongoing_rd)
+		goto busy;
+
+	nor->rww.ongoing_io = true;
+	nor->rww.ongoing_rd = true;
+	ret = 0;
+
+busy:
+	mutex_unlock(&nor->lock);
+	return ret;
+}
+
+static void spi_nor_rww_end_rdst(struct spi_nor *nor)
+{
+	mutex_lock(&nor->lock);
+
+	nor->rww.ongoing_io = false;
+	nor->rww.ongoing_rd = false;
+
+	mutex_unlock(&nor->lock);
+}
+
+static int spi_nor_lock_rdst(struct spi_nor *nor)
+{
+	if (spi_nor_parallel_locking(nor))
+		return spi_nor_rww_start_rdst(nor);
+
+	return 0;
+}
+
+static void spi_nor_unlock_rdst(struct spi_nor *nor)
+{
+	if (spi_nor_parallel_locking(nor)) {
+		spi_nor_rww_end_rdst(nor);
+		wake_up(&nor->rww.wait);
+	}
+}
+
 /**
  * spi_nor_ready() - Query the flash to see if it is ready for new commands.
  * @nor:	pointer to 'struct spi_nor'.
@@ -596,11 +656,21 @@ int spi_nor_sr_ready(struct spi_nor *nor)
  */
 static int spi_nor_ready(struct spi_nor *nor)
 {
+	int ret;
+
+	ret = spi_nor_lock_rdst(nor);
+	if (ret)
+		return 0;
+
 	/* Flashes might override the standard routine. */
 	if (nor->params->ready)
-		return nor->params->ready(nor);
+		ret = nor->params->ready(nor);
+	else
+		ret = spi_nor_sr_ready(nor);
 
-	return spi_nor_sr_ready(nor);
+	spi_nor_unlock_rdst(nor);
+
+	return ret;
 }
 
 /**
@@ -1086,7 +1156,81 @@ static void spi_nor_unprep(struct spi_nor *nor)
 		nor->controller_ops->unprepare(nor);
 }
 
+static void spi_nor_offset_to_banks(struct spi_nor *nor, loff_t start, size_t len,
+				    unsigned int *first, unsigned int *last)
+{
+	*first = DIV_ROUND_DOWN_ULL(start, nor->params->bank_size);
+	*last = DIV_ROUND_DOWN_ULL(start + len - 1, nor->params->bank_size);
+}
+
 /* Generic helpers for internal locking and serialization */
+static bool spi_nor_rww_start_io(struct spi_nor *nor)
+{
+	bool start = false;
+
+	mutex_lock(&nor->lock);
+
+	if (nor->rww.ongoing_io)
+		goto busy;
+
+	nor->rww.ongoing_io = true;
+	start = true;
+
+busy:
+	mutex_unlock(&nor->lock);
+	return start;
+}
+
+static void spi_nor_rww_end_io(struct spi_nor *nor)
+{
+	mutex_lock(&nor->lock);
+	nor->rww.ongoing_io = false;
+	mutex_unlock(&nor->lock);
+}
+
+static int spi_nor_lock_device(struct spi_nor *nor)
+{
+	if (!spi_nor_parallel_locking(nor))
+		return 0;
+
+	return wait_event_killable(nor->rww.wait, spi_nor_rww_start_io(nor));
+}
+
+static void spi_nor_unlock_device(struct spi_nor *nor)
+{
+	if (spi_nor_parallel_locking(nor))
+		spi_nor_rww_end_io(nor);
+}
+
+/* Generic helpers for internal locking and serialization */
+static bool spi_nor_rww_start_exclusive(struct spi_nor *nor)
+{
+	bool start = false;
+
+	mutex_lock(&nor->lock);
+
+	if (nor->rww.ongoing_io || nor->rww.ongoing_rd || nor->rww.ongoing_pe)
+		goto busy;
+
+	nor->rww.ongoing_io = true;
+	nor->rww.ongoing_rd = true;
+	nor->rww.ongoing_pe = true;
+	start = true;
+
+busy:
+	mutex_unlock(&nor->lock);
+	return start;
+}
+
+static void spi_nor_rww_end_exclusive(struct spi_nor *nor)
+{
+	mutex_lock(&nor->lock);
+	nor->rww.ongoing_io = false;
+	nor->rww.ongoing_rd = false;
+	nor->rww.ongoing_pe = false;
+	mutex_unlock(&nor->lock);
+}
+
 int spi_nor_lock_and_prep(struct spi_nor *nor)
 {
 	int ret;
@@ -1095,19 +1239,71 @@ int spi_nor_lock_and_prep(struct spi_nor *nor)
 	if (ret)
 		return ret;
 
-	mutex_lock(&nor->lock);
+	if (!spi_nor_parallel_locking(nor))
+		mutex_lock(&nor->lock);
+	else
+		ret = wait_event_killable(nor->rww.wait,
+					  spi_nor_rww_start_exclusive(nor));
 
-	return 0;
+	return ret;
 }
 
 void spi_nor_unlock_and_unprep(struct spi_nor *nor)
 {
-	mutex_unlock(&nor->lock);
+	if (!spi_nor_parallel_locking(nor)) {
+		mutex_unlock(&nor->lock);
+	} else {
+		spi_nor_rww_end_exclusive(nor);
+		wake_up(&nor->rww.wait);
+	}
 
 	spi_nor_unprep(nor);
 }
 
 /* Internal locking helpers for program and erase operations */
+static bool spi_nor_rww_start_pe(struct spi_nor *nor, loff_t start, size_t len)
+{
+	unsigned int first, last;
+	bool started = false;
+	int bank;
+
+	mutex_lock(&nor->lock);
+
+	if (nor->rww.ongoing_io || nor->rww.ongoing_rd || nor->rww.ongoing_pe)
+		goto busy;
+
+	spi_nor_offset_to_banks(nor, start, len, &first, &last);
+	for (bank = first; bank <= last; bank++)
+		if (nor->rww.used_banks & BIT(bank))
+			goto busy;
+
+	for (bank = first; bank <= last; bank++)
+		nor->rww.used_banks |= BIT(bank);
+
+	nor->rww.ongoing_pe = true;
+	started = true;
+
+busy:
+	mutex_unlock(&nor->lock);
+	return started;
+}
+
+static void spi_nor_rww_end_pe(struct spi_nor *nor, loff_t start, size_t len)
+{
+	unsigned int first, last;
+	int bank;
+
+	mutex_lock(&nor->lock);
+
+	spi_nor_offset_to_banks(nor, start, len, &first, &last);
+	for (bank = first; bank <= last; bank++)
+		nor->rww.used_banks &= ~BIT(bank);
+
+	nor->rww.ongoing_pe = false;
+
+	mutex_unlock(&nor->lock);
+}
+
 static int spi_nor_lock_and_prep_pe(struct spi_nor *nor, loff_t start, size_t len)
 {
 	int ret;
@@ -1116,19 +1312,73 @@ static int spi_nor_lock_and_prep_pe(struct spi_nor *nor, loff_t start, size_t le
 	if (ret)
 		return ret;
 
-	mutex_lock(&nor->lock);
+	if (!spi_nor_parallel_locking(nor))
+		mutex_lock(&nor->lock);
+	else
+		ret = wait_event_killable(nor->rww.wait,
+					  spi_nor_rww_start_pe(nor, start, len));
 
-	return 0;
+	return ret;
 }
 
 static void spi_nor_unlock_and_unprep_pe(struct spi_nor *nor, loff_t start, size_t len)
 {
-	mutex_unlock(&nor->lock);
+	if (!spi_nor_parallel_locking(nor)) {
+		mutex_unlock(&nor->lock);
+	} else {
+		spi_nor_rww_end_pe(nor, start, len);
+		wake_up(&nor->rww.wait);
+	}
 
 	spi_nor_unprep(nor);
 }
 
 /* Internal locking helpers for read operations */
+static bool spi_nor_rww_start_rd(struct spi_nor *nor, loff_t start, size_t len)
+{
+	unsigned int first, last;
+	bool started = false;
+	int bank;
+
+	mutex_lock(&nor->lock);
+
+	if (nor->rww.ongoing_io || nor->rww.ongoing_rd)
+		goto busy;
+
+	spi_nor_offset_to_banks(nor, start, len, &first, &last);
+	for (bank = first; bank <= last; bank++)
+		if (nor->rww.used_banks & BIT(bank))
+			goto busy;
+
+	for (bank = first; bank <= last; bank++)
+		nor->rww.used_banks |= BIT(bank);
+
+	nor->rww.ongoing_io = true;
+	nor->rww.ongoing_rd = true;
+	started = true;
+
+busy:
+	mutex_unlock(&nor->lock);
+	return started;
+}
+
+static void spi_nor_rww_end_rd(struct spi_nor *nor, loff_t start, size_t len)
+{
+	unsigned int first, last;
+	int bank;
+
+	mutex_lock(&nor->lock);
+
+	spi_nor_offset_to_banks(nor, start, len, &first, &last);
+	for (bank = first; bank <= last; bank++)
+		nor->rww.used_banks &= ~BIT(bank);
+
+	nor->rww.ongoing_io = false;
+	nor->rww.ongoing_rd = false;
+
+	mutex_unlock(&nor->lock);
+}
+
 static int spi_nor_lock_and_prep_rd(struct spi_nor *nor, loff_t start, size_t len)
 {
 	int ret;
@@ -1137,14 +1387,23 @@ static int spi_nor_lock_and_prep_rd(struct spi_nor *nor, loff_t start, size_t le
 	if (ret)
 		return ret;
 
-	mutex_lock(&nor->lock);
+	if (!spi_nor_parallel_locking(nor))
+		mutex_lock(&nor->lock);
+	else
+		ret = wait_event_killable(nor->rww.wait,
+					  spi_nor_rww_start_rd(nor, start, len));
 
-	return 0;
+	return ret;
 }
 
 static void spi_nor_unlock_and_unprep_rd(struct spi_nor *nor, loff_t start, size_t len)
 {
-	mutex_unlock(&nor->lock);
+	if (!spi_nor_parallel_locking(nor)) {
+		mutex_unlock(&nor->lock);
+	} else {
+		spi_nor_rww_end_rd(nor, start, len);
+		wake_up(&nor->rww.wait);
+	}
 
 	spi_nor_unprep(nor);
 }
@@ -1451,11 +1710,18 @@ static int spi_nor_erase_multi_sectors(struct spi_nor *nor, u64 addr, u32 len)
 			dev_vdbg(nor->dev, "erase_cmd->size = 0x%08x, erase_cmd->opcode = 0x%02x, erase_cmd->count = %u\n",
 				 cmd->size, cmd->opcode, cmd->count);
 
-			ret = spi_nor_write_enable(nor);
+			ret = spi_nor_lock_device(nor);
 			if (ret)
 				goto destroy_erase_cmd_list;
 
+			ret = spi_nor_write_enable(nor);
+			if (ret) {
+				spi_nor_unlock_device(nor);
+				goto destroy_erase_cmd_list;
+			}
+
 			ret = spi_nor_erase_sector(nor, addr);
+			spi_nor_unlock_device(nor);
 			if (ret)
 				goto destroy_erase_cmd_list;
 
@@ -1508,11 +1774,18 @@ static int spi_nor_erase(struct mtd_info *mtd, struct erase_info *instr)
 	if (len == mtd->size && !(nor->flags & SNOR_F_NO_OP_CHIP_ERASE)) {
 		unsigned long timeout;
 
-		ret = spi_nor_write_enable(nor);
+		ret = spi_nor_lock_device(nor);
 		if (ret)
 			goto erase_err;
 
+		ret = spi_nor_write_enable(nor);
+		if (ret) {
+			spi_nor_unlock_device(nor);
+			goto erase_err;
+		}
+
 		ret = spi_nor_erase_chip(nor);
+		spi_nor_unlock_device(nor);
 		if (ret)
 			goto erase_err;
 
@@ -1537,11 +1810,18 @@ static int spi_nor_erase(struct mtd_info *mtd, struct erase_info *instr)
 	/* "sector"-at-a-time erase */
 	} else if (spi_nor_has_uniform_erase(nor)) {
 		while (len) {
-			ret = spi_nor_write_enable(nor);
+			ret = spi_nor_lock_device(nor);
 			if (ret)
 				goto erase_err;
 
+			ret = spi_nor_write_enable(nor);
+			if (ret) {
+				spi_nor_unlock_device(nor);
+				goto erase_err;
+			}
+
 			ret = spi_nor_erase_sector(nor, addr);
+			spi_nor_unlock_device(nor);
 			if (ret)
 				goto erase_err;
 
@@ -1811,11 +2091,18 @@ static int spi_nor_write(struct mtd_info *mtd, loff_t to, size_t len,
 
 		addr = spi_nor_convert_addr(nor, addr);
 
-		ret = spi_nor_write_enable(nor);
+		ret = spi_nor_lock_device(nor);
 		if (ret)
 			goto write_err;
 
+		ret = spi_nor_write_enable(nor);
+		if (ret) {
+			spi_nor_unlock_device(nor);
+			goto write_err;
+		}
+
 		ret = spi_nor_write_data(nor, addr, page_remain, buf + i);
+		spi_nor_unlock_device(nor);
 		if (ret < 0)
 			goto write_err;
 		written = ret;
@@ -3033,6 +3320,8 @@ int spi_nor_scan(struct spi_nor *nor, const char *name,
 	nor->info = info;
 
 	mutex_init(&nor->lock);
+	if (spi_nor_parallel_locking(nor))
+		init_waitqueue_head(&nor->rww.wait);
 
 	/* Init flash parameters based on flash_info struct and SFDP */
 	ret = spi_nor_init_params(nor);
include/linux/mtd/spi-nor.h +13 -0
index 42218a1164f6..51e247d854c4 100644
@@ -344,6 +344,12 @@ struct spi_nor_flash_parameter;
  * struct spi_nor - Structure for defining the SPI NOR layer
  * @mtd:		an mtd_info structure
  * @lock:		the lock for the read/write/erase/lock/unlock operations
+ * @rww:		Read-While-Write (RWW) sync lock
+ * @rww.wait:		wait queue for the RWW sync
+ * @rww.ongoing_io:	the bus is busy
+ * @rww.ongoing_rd:	a read is ongoing on the chip
+ * @rww.ongoing_pe:	a program/erase is ongoing on the chip
+ * @rww.used_banks:	bitmap of the banks in use
  * @dev:		pointer to an SPI device or an SPI NOR controller device
  * @spimem:		pointer to the SPI memory device
  * @bouncebuf:		bounce buffer used when the buffer passed by the MTD
@@ -375,6 +381,13 @@ struct spi_nor_flash_parameter;
 struct spi_nor {
 	struct mtd_info		mtd;
 	struct mutex		lock;
+	struct {
+		wait_queue_head_t wait;
+		bool		ongoing_io;
+		bool		ongoing_rd;
+		bool		ongoing_pe;
+		unsigned int	used_banks;
+	} rww;
 	struct device		*dev;
 	struct spi_mem		*spimem;
 	u8			*bouncebuf;
Signature · 6 lines
2.34.1


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
author maintainer
#17 lore raw

PATCH v3 9/9 mtd: spi-nor: macronix: Add support for mx25uw51245g with RWW

In reply to Miquel Raynal

Describe this new part and provide the RWW flag for it.

---
 drivers/mtd/spi-nor/macronix.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/mtd/spi-nor/macronix.c +3 -0
index d81a4cb2812b..e9b82afcd6c4 100644
@@ -82,6 +82,9 @@ static const struct flash_info macronix_nor_parts[] = {
 	{ "mx25u51245g", INFO(0xc2253a, 0, 64 * 1024, 1024)
 		NO_SFDP_FLAGS(SECT_4K | SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ)
 		FIXUP_FLAGS(SPI_NOR_4B_OPCODES) },
+	{ "mx25uw51245g", INFOB(0xc2813a, 0, 16 * 1024, 1024, 4)
+		NO_SFDP_FLAGS(SECT_4K | SPI_NOR_RWW)
+		FIXUP_FLAGS(SPI_NOR_4B_OPCODES) },
 	{ "mx25v8035f",  INFO(0xc22314, 0, 64 * 1024,  16)
 		NO_SFDP_FLAGS(SECT_4K | SPI_NOR_DUAL_READ |
 			      SPI_NOR_QUAD_READ) },
Signature · 6 lines
2.34.1


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/