Skip to contentSkip to conversation

mtd: spi-nor: generic flash driver

PATCH Applied Michael Walle 2 participants 13 messages Aug 10, 2022 – Nov 21, 2022

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

v2 cover mtd: spi-nor: generic flash driver

Add a generic flash driver, which is used when we don't find a matching flash in our database. All the basic features of a flash can be discovered by SFDP and most (if not all) newer flashes support it.

changes since v1:
- fix doc prototype mistake reported by the kernel test robot
- preset page_size to 256 to support flashes which just have SFDP data corresponding to JESD216A
- add new patch
mtd: spi-nor: fix select_uniform_erase to skip 0 erase size
- add function doc to explain wanted_size=0 in spi_nor_select_uniform_erase()

Michael Walle (7):
mtd: spi-nor: hide jedec_id sysfs attribute if not present
mtd: spi-nor: sysfs: hide manufacturer if it is not set
mtd: spi-nor: remember full JEDEC flash ID
mtd: spi-nor: move function declaration out of sfdp.h
mtd: spi-nor: fix select_uniform_erase to skip 0 erase size
mtd: spi-nor: add generic flash driver
mtd: spi-nor: sysfs: print JEDEC ID for generic flash driver

 .../ABI/testing/sysfs-bus-spi-devices-spi-nor |  6 ++++
 drivers/mtd/spi-nor/core.c                    | 35 +++++++++++++++++--
 drivers/mtd/spi-nor/core.h                    |  3 ++
 drivers/mtd/spi-nor/debugfs.c                 |  2 +-
 drivers/mtd/spi-nor/sfdp.c                    | 27 ++++++++++++++
 drivers/mtd/spi-nor/sfdp.h                    |  2 --
 drivers/mtd/spi-nor/sysfs.c                   | 20 ++++++++++-
 include/linux/mtd/spi-nor.h                   |  3 ++
 8 files changed, 92 insertions(+), 6 deletions(-)
Signature · 1 line
2.30.2
#1 lore raw

PATCH v2 1/7 mtd: spi-nor: hide jedec_id sysfs attribute if not present

Some non-jedec compliant flashes (like the Everspin flashes) don't have an ID at all. Hide the attribute in this case.

Fixes 36ac02286265 ("mtd: spi-nor: add initial sysfs support")
---
 .../ABI/testing/sysfs-bus-spi-devices-spi-nor      |  3 +++
 drivers/mtd/spi-nor/sysfs.c                        | 14 ++++++++++++++
 2 files changed, 17 insertions(+)
Documentation/ABI/testing/sysfs-bus-spi-devices-spi-nor +3 -0
index d76cd3946434..e9ef69aef20b 100644
@@ -5,6 +5,9 @@ Contact:	[email protected]
 Description:	(RO) The JEDEC ID of the SPI NOR flash as reported by the
 		flash device.
 
+		The attribute is not present if the flash doesn't support
+		the "Read JEDEC ID" command (9Fh). This is the case for
+		non-JEDEC compliant flashes.
 
 What:		/sys/bus/spi/devices/.../spi-nor/manufacturer
 Date:		April 2021
drivers/mtd/spi-nor/sysfs.c +14 -0
index 9aec9d8a98ad..4c3b351aef24 100644
@@ -67,6 +67,19 @@ static struct bin_attribute *spi_nor_sysfs_bin_entries[] = {
 	NULL
 };
 
+static umode_t spi_nor_sysfs_is_visible(struct kobject *kobj,
+					struct attribute *attr, int n)
+{
+	struct spi_device *spi = to_spi_device(kobj_to_dev(kobj));
+	struct spi_mem *spimem = spi_get_drvdata(spi);
+	struct spi_nor *nor = spi_mem_get_drvdata(spimem);
+
+	if (attr == &dev_attr_jedec_id.attr && !nor->info->id_len)
+		return 0;
+
+	return 0444;
+}
+
 static umode_t spi_nor_sysfs_is_bin_visible(struct kobject *kobj,
 					    struct bin_attribute *attr, int n)
 {
@@ -82,6 +95,7 @@ static umode_t spi_nor_sysfs_is_bin_visible(struct kobject *kobj,
 
 static const struct attribute_group spi_nor_sysfs_group = {
 	.name		= "spi-nor",
+	.is_visible	= spi_nor_sysfs_is_visible,
 	.is_bin_visible	= spi_nor_sysfs_is_bin_visible,
 	.attrs		= spi_nor_sysfs_entries,
 	.bin_attrs	= spi_nor_sysfs_bin_entries,
Signature · 1 line
2.30.2
#2 lore raw

PATCH v2 2/7 mtd: spi-nor: sysfs: hide manufacturer if it is not set

In reply to Michael Walle

The manufacturer may be optional when pure SFDP flashes are supported. Hide the sysfs property if no manufacturer is set.

---
 Documentation/ABI/testing/sysfs-bus-spi-devices-spi-nor | 3 +++
 drivers/mtd/spi-nor/sysfs.c                             | 2 ++
 2 files changed, 5 insertions(+)
Documentation/ABI/testing/sysfs-bus-spi-devices-spi-nor +3 -0
index e9ef69aef20b..c800621eff95 100644
@@ -15,6 +15,9 @@ KernelVersion:	5.14
 Contact:	[email protected]
 Description:	(RO) Manufacturer of the SPI NOR flash.
 
+		The attribute is not present if the flash device isn't
+		known to the kernel and is only probed by its SFDP
+		tables.
 
 What:		/sys/bus/spi/devices/.../spi-nor/partname
 Date:		April 2021
drivers/mtd/spi-nor/sysfs.c +2 -0
index 4c3b351aef24..20563c1926f4 100644
@@ -74,6 +74,8 @@ static umode_t spi_nor_sysfs_is_visible(struct kobject *kobj,
 	struct spi_mem *spimem = spi_get_drvdata(spi);
 	struct spi_nor *nor = spi_mem_get_drvdata(spimem);
 
+	if (attr == &dev_attr_manufacturer.attr && !nor->manufacturer)
+		return 0;
 	if (attr == &dev_attr_jedec_id.attr && !nor->info->id_len)
 		return 0;
 
Signature · 1 line
2.30.2
#3 lore raw

PATCH v2 3/7 mtd: spi-nor: remember full JEDEC flash ID

In reply to Michael Walle

At the moment, we print the JEDEC ID that is stored in our database. The generic flash support won't have such an entry in our database. To find out the JEDEC ID later we will have to cache it. There is also another
advantage: If the flash is found in the database, the ID could be truncated because the ID of the entry is used which can be shorter. Some flashes still holds valuable information in the bytes after the JEDEC ID and come in handy during debugging of when coping with INFO6() entries. These are not accessible for now.

Save a copy of the ID bytes after reading and display it via debugfs.

---
 drivers/mtd/spi-nor/core.c    | 5 +++++
 drivers/mtd/spi-nor/debugfs.c | 2 +-
 include/linux/mtd/spi-nor.h   | 3 +++
 3 files changed, 9 insertions(+), 1 deletion(-)
drivers/mtd/spi-nor/core.c +5 -0
index f2c64006f8d7..f1d01c7dacce 100644
@@ -1664,6 +1664,11 @@ static const struct flash_info *spi_nor_detect(struct spi_nor *nor)
 		return ERR_PTR(ret);
 	}
 
+	/* Cache the complete flash ID. */
+	nor->id = devm_kmemdup(nor->dev, id, SPI_NOR_MAX_ID_LEN, GFP_KERNEL);
+	if (!nor->id)
+		return ERR_PTR(-ENOMEM);
+
 	info = spi_nor_match_id(nor, id);
 	if (!info) {
 		dev_err(nor->dev, "unrecognized JEDEC id bytes: %*ph\n",
drivers/mtd/spi-nor/debugfs.c +1 -1
index df76cb5de3f9..ff895f6758ea 100644
@@ -81,7 +81,7 @@ static int spi_nor_params_show(struct seq_file *s, void *data)
 	int i;
 
 	seq_printf(s, "name\t\t%s\n", info->name);
-	seq_printf(s, "id\t\t%*ph\n", info->id_len, info->id);
+	seq_printf(s, "id\t\t%*ph\n", SPI_NOR_MAX_ID_LEN, nor->id);
 	string_get_size(params->size, 1, STRING_UNITS_2, buf, sizeof(buf));
 	seq_printf(s, "size\t\t%s\n", buf);
 	seq_printf(s, "write size\t%u\n", params->writesize);
include/linux/mtd/spi-nor.h +3 -0
index 42218a1164f6..25765556223a 100644
@@ -349,6 +349,8 @@ struct spi_nor_flash_parameter;
  * @bouncebuf:		bounce buffer used when the buffer passed by the MTD
  *                      layer is not DMA-able
  * @bouncebuf_size:	size of the bounce buffer
+ * @id:			The flash's ID bytes. Always contains
+ *			SPI_NOR_MAX_ID_LEN bytes.
  * @info:		SPI NOR part JEDEC MFR ID and other info
  * @manufacturer:	SPI NOR manufacturer
  * @addr_nbytes:	number of address bytes
@@ -379,6 +381,7 @@ struct spi_nor {
 	struct spi_mem		*spimem;
 	u8			*bouncebuf;
 	size_t			bouncebuf_size;
+	u8			*id;
 	const struct flash_info	*info;
 	const struct spi_nor_manufacturer *manufacturer;
 	u8			addr_nbytes;
Signature · 1 line
2.30.2
#4 lore raw

PATCH v2 4/7 mtd: spi-nor: move function declaration out of sfdp.h

In reply to Michael Walle

sfdp.h should only contain constants related to the JEDEC SFDP specification(s).

---
 drivers/mtd/spi-nor/core.h | 2 ++
 drivers/mtd/spi-nor/sfdp.h | 2 --
 2 files changed, 2 insertions(+), 2 deletions(-)
drivers/mtd/spi-nor/core.h +2 -0
index 85b0cf254e97..68aaccaa12d8 100644
@@ -701,6 +701,8 @@ int spi_nor_controller_ops_read_reg(struct spi_nor *nor, u8 opcode,
 int spi_nor_controller_ops_write_reg(struct spi_nor *nor, u8 opcode,
 				     const u8 *buf, size_t len);
 
+int spi_nor_parse_sfdp(struct spi_nor *nor);
+
 static inline struct spi_nor *mtd_to_spi_nor(struct mtd_info *mtd)
 {
 	return container_of(mtd, struct spi_nor, mtd);
drivers/mtd/spi-nor/sfdp.h +0 -2
index bbf80d2990ab..c1969f0a2f46 100644
@@ -107,6 +107,4 @@ struct sfdp_parameter_header {
 	u8		id_msb;
 };
 
-int spi_nor_parse_sfdp(struct spi_nor *nor);
-
 #endif /* __LINUX_MTD_SFDP_H */
Signature · 6 lines
2.30.2


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

PATCH v2 5/7 mtd: spi-nor: fix select_uniform_erase to skip 0 erase size

In reply to Michael Walle

4bait will set the erase size to 0 if there is no corresponding opcode for the 4byte erase. Fix spi_nor_select_uniform_erase to skip the 0 erase size to avoid mtd device registration failure cases.

---
 drivers/mtd/spi-nor/core.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/mtd/spi-nor/core.c +4 -0
index f1d01c7dacce..42f9bb63919c 100644
@@ -2118,6 +2118,10 @@ spi_nor_select_uniform_erase(struct spi_nor_erase_map *map,
 
 		tested_erase = &map->erase_type[i];
 
+		/* Skip masked erase types. */
+		if (!tested_erase->size)
+			continue;
+
 		/*
 		 * If the current erase size is the one, stop here:
 		 * we have found the right uniform Sector Erase command.
Signature · 1 line
2.30.2
#6 lore raw

PATCH v2 6/7 mtd: spi-nor: add generic flash driver

In reply to Michael Walle

Our SFDP parsing is everything we need to support all basic operations of a flash device. If the flash isn't found in our in-kernel flash database, gracefully fall back to a driver described solely by its SFDP tables.

---
 drivers/mtd/spi-nor/core.c | 26 ++++++++++++++++++++++++--
 drivers/mtd/spi-nor/core.h |  1 +
 drivers/mtd/spi-nor/sfdp.c | 27 +++++++++++++++++++++++++++
 3 files changed, 52 insertions(+), 2 deletions(-)
drivers/mtd/spi-nor/core.c +24 -2
index 42f9bb63919c..f10fca5730b8 100644
@@ -1632,6 +1632,16 @@ static const struct spi_nor_manufacturer *manufacturers[] = {
 	&spi_nor_xmc,
 };
 
+static const struct flash_info spi_nor_generic_flash = {
+	.name = "spi-nor-generic",
+	/*
+	 * JESD216 rev A doesn't specify the page size, therefore we need a
+	 * sane default.
+	 */
+	.page_size = 256,
+	.parse_sfdp = true,
+};
+
 static const struct flash_info *spi_nor_match_id(struct spi_nor *nor,
 						 const u8 *id)
 {
@@ -1670,6 +1680,14 @@ static const struct flash_info *spi_nor_detect(struct spi_nor *nor)
 		return ERR_PTR(-ENOMEM);
 
 	info = spi_nor_match_id(nor, id);
+
+	/* Fallback to a generic flash described only by its SFDP data. */
+	if (!info) {
+		ret = spi_nor_check_sfdp_signature(nor);
+		if (!ret)
+			info = &spi_nor_generic_flash;
+	}
+
 	if (!info) {
 		dev_err(nor->dev, "unrecognized JEDEC id bytes: %*ph\n",
 			SPI_NOR_MAX_ID_LEN, id);
@@ -2096,8 +2114,12 @@ static int spi_nor_select_pp(struct spi_nor *nor,
  * spi_nor_select_uniform_erase() - select optimum uniform erase type
  * @map:		the erase map of the SPI NOR
  * @wanted_size:	the erase type size to search for. Contains the value of
- *			info->sector_size or of the "small sector" size in case
- *			CONFIG_MTD_SPI_NOR_USE_4K_SECTORS is defined.
+ *			info->sector_size, the "small sector" size in case
+ *			CONFIG_MTD_SPI_NOR_USE_4K_SECTORS is defined or 0 if
+ *			there is no information about the sector size. The
+ *			latter is the case if the flash parameters are parsed
+ *			solely by SFDP, then the largest supported erase type
+ *			is selected.
  *
  * Once the optimum uniform sector erase command is found, disable all the
  * other.
drivers/mtd/spi-nor/core.h +1 -0
index 68aaccaa12d8..ef0a73dbf348 100644
@@ -701,6 +701,7 @@ int spi_nor_controller_ops_read_reg(struct spi_nor *nor, u8 opcode,
 int spi_nor_controller_ops_write_reg(struct spi_nor *nor, u8 opcode,
 				     const u8 *buf, size_t len);
 
+int spi_nor_check_sfdp_signature(struct spi_nor *nor);
 int spi_nor_parse_sfdp(struct spi_nor *nor);
 
 static inline struct spi_nor *mtd_to_spi_nor(struct mtd_info *mtd)
drivers/mtd/spi-nor/sfdp.c +27 -0
index 2257f1b4c2e2..0a77f50a2691 100644
@@ -1249,6 +1249,33 @@ static void spi_nor_post_sfdp_fixups(struct spi_nor *nor)
 		nor->info->fixups->post_sfdp(nor);
 }
 
+/**
+ * spi_nor_check_sfdp_signature() - check for a valid SFDP signature
+ * @nor:	pointer to a 'struct spi_nor'
+ *
+ * Used to detect if the flash supports the RDSFDP command as well as the
+ * presence of a valid SFDP table.
+ *
+ * Return: 0 on success, -errno otherwise.
+ */
+int spi_nor_check_sfdp_signature(struct spi_nor *nor)
+{
+	u32 signature;
+	int err;
+
+	/* Get the SFDP header. */
+	err = spi_nor_read_sfdp_dma_unsafe(nor, 0, sizeof(signature),
+					   &signature);
+	if (err < 0)
+		return err;
+
+	/* Check the SFDP signature. */
+	if (le32_to_cpu(signature) != SFDP_SIGNATURE)
+		return -EINVAL;
+
+	return 0;
+}
+
 /**
  * spi_nor_parse_sfdp() - parse the Serial Flash Discoverable Parameters.
  * @nor:		pointer to a 'struct spi_nor'
Signature · 1 line
2.30.2
#7 lore raw

Re: [PATCH v2 6/7] mtd: spi-nor: add generic flash driver

drivers/mtd/spi-nor/core.c static const struct spi_nor_manufacturer *manufacturers[] = { · line 1636

Quoted 8 lines · Michael Walle
drivers/mtd/spi-nor/core.c +2 -0
@@ -1632,6 +1632,16 @@ static const struct spi_nor_manufacturer *manufacturers[] = {
        &spi_nor_xmc,
 };
 
+static const struct flash_info spi_nor_generic_flash = {
+       .name = "spi-nor-generic",

How about "jedec,spi-nor" instead? The series looks good, I intend to test it and apply it soon.

Signature · 2 lines
Cheers,
ta
#8 lore raw

Re: [PATCH v2 6/7] mtd: spi-nor: add generic flash driver

Quoted 9 lines · On 11/1/22 10:46, [email protected] wrote:

EXTERNAL EMAIL: Do not click links or open attachments unless you know the content is safe

Quoted 6 lines · Michael Walle
drivers/mtd/spi-nor/core.c +0 -0
@@ -1632,6 +1632,16 @@ static const struct spi_nor_manufacturer *manufacturers[] = {
        &spi_nor_xmc,
 };
 
Quoted 6 lines

drivers/mtd/spi-nor/core.c static const struct spi_nor_manufacturer *manufacturers[] = { · line 1636

Quoted 2 lines
drivers/mtd/spi-nor/core.c +2 -0
+static const struct flash_info spi_nor_generic_flash = {
+       .name = "spi-nor-generic",

How about "jedec,spi-nor" instead? The series looks good, I intend to test it and apply it soon.

Pratyush, do you agree with the change proposed, s/spi-nor-generic/jedec,spi-nor. Michael seems busy and I'd like to progress with this.

Thanks,
ta

#9 lore raw

Re: [PATCH v2 6/7] mtd: spi-nor: add generic flash driver

In reply to [email protected]

Am 2022-11-01 09:46, schrieb [email protected]:

Quoted 8 lines · Michael Walle
Quoted 7 lines
drivers/mtd/spi-nor/core.c +0 -0
@@ -1632,6 +1632,16 @@ static const struct spi_nor_manufacturer 
 *manufacturers[] = {
        &spi_nor_xmc,
 };
 
Quoted 5 lines

drivers/mtd/spi-nor/core.c static const struct spi_nor_manufacturer *manufacturers[] = { · line 1636

Quoted 2 lines
drivers/mtd/spi-nor/core.c +2 -0
+static const struct flash_info spi_nor_generic_flash = {
+       .name = "spi-nor-generic",

How about "jedec,spi-nor" instead? The series looks good, I intend to test it and apply it soon.

I had that string before actually but decided against it because it looks like "jedec" is the vendor, which is (a) not true for any flash and (b) if it would be a vendor, it should go into the manufacturer property, which isn't supported at the moment.

That being said, I don't care too much. After all, it's just a name.

(and yes, I'm on vacation till next week ;)

-michael

Tested-by on v2 6/7
#10 lore raw

Re: [PATCH v2 6/7] mtd: spi-nor: add generic flash driver

In reply to Michael Walle
Quoted 9 lines · Michael Walle

EXTERNAL EMAIL: Do not click links or open attachments unless you know the content is safe

Our SFDP parsing is everything we need to support all basic operations of a flash device. If the flash isn't found in our in-kernel flash database, gracefully fall back to a driver described solely by its SFDP tables.

Quoted 6 lines
---
 drivers/mtd/spi-nor/core.c | 26 ++++++++++++++++++++++++--
 drivers/mtd/spi-nor/core.h |  1 +
 drivers/mtd/spi-nor/sfdp.c | 27 +++++++++++++++++++++++++++
 3 files changed, 52 insertions(+), 2 deletions(-)
#11 lore raw

PATCH v2 7/7 mtd: spi-nor: sysfs: print JEDEC ID for generic flash driver

In reply to Michael Walle

We don't have a database entry for the generic SPI-NOR flash driver and thus we don't have a JEDEC ID to print. Print the (cached) JEDEC ID instead.

---
 drivers/mtd/spi-nor/sysfs.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/mtd/spi-nor/sysfs.c +4 -2
index 20563c1926f4..c09bb832b3b9 100644
@@ -35,8 +35,10 @@ static ssize_t jedec_id_show(struct device *dev,
 	struct spi_device *spi = to_spi_device(dev);
 	struct spi_mem *spimem = spi_get_drvdata(spi);
 	struct spi_nor *nor = spi_mem_get_drvdata(spimem);
+	const u8 *id = nor->info->id_len ? nor->info->id : nor->id;
+	u8 id_len = nor->info->id_len ?: SPI_NOR_MAX_ID_LEN;
 
-	return sysfs_emit(buf, "%*phN\n", nor->info->id_len, nor->info->id);
+	return sysfs_emit(buf, "%*phN\n", id_len, id);
 }
 static DEVICE_ATTR_RO(jedec_id);
 
@@ -76,7 +78,7 @@ static umode_t spi_nor_sysfs_is_visible(struct kobject *kobj,
 
 	if (attr == &dev_attr_manufacturer.attr && !nor->manufacturer)
 		return 0;
-	if (attr == &dev_attr_jedec_id.attr && !nor->info->id_len)
+	if (attr == &dev_attr_jedec_id.attr && !nor->info->id_len && !nor->id)
 		return 0;
 
 	return 0444;
Signature · 1 line
2.30.2
#12 lore raw
In reply to Michael Walle
Quoted 13 lines · Michael Walle

Add a generic flash driver, which is used when we don't find a matching flash in our database. All the basic features of a flash can be discovered by SFDP and most (if not all) newer flashes support it.

changes since v1:
- fix doc prototype mistake reported by the kernel test robot
- preset page_size to 256 to support flashes which just have SFDP data corresponding to JESD216A
- add new patch
mtd: spi-nor: fix select_uniform_erase to skip 0 erase size
- add function doc to explain wanted_size=0 in spi_nor_select_uniform_erase()

Quoted 1 line

[...]

Applied to spi-nor/next, thanks!

[1/7] mtd: spi-nor: hide jedec_id sysfs attribute if not present

      https://git.kernel.org/mtd/c/7d388551b688
[2/7] mtd: spi-nor: sysfs: hide manufacturer if it is not set
      https://git.kernel.org/mtd/c/0d9270f2762b
[3/7] mtd: spi-nor: remember full JEDEC flash ID
      https://git.kernel.org/mtd/c/28ef7670414e
[4/7] mtd: spi-nor: move function declaration out of sfdp.h
      https://git.kernel.org/mtd/c/fa06bb26a40c
[5/7] mtd: spi-nor: fix select_uniform_erase to skip 0 erase size
      https://git.kernel.org/mtd/c/39eece67a3cf
[6/7] mtd: spi-nor: add generic flash driver
      https://git.kernel.org/mtd/c/773bbe104497
[7/7] mtd: spi-nor: sysfs: print JEDEC ID for generic flash driver
      https://git.kernel.org/mtd/c/0a92de16b61b

Best regards,

Signature · 5 lines
Tudor Ambarus <[email protected]>

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