diff mbox series

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

Message ID 20220810220654.1297699-7-michael@walle.cc
State Accepted
Delegated to: Ambarus Tudor
Headers show
Series mtd: spi-nor: generic flash driver | expand

Commit Message

Michael Walle Aug. 10, 2022, 10:06 p.m. UTC
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.

Signed-off-by: Michael Walle <michael@walle.cc>
Reviewed-by: Takahiro Kuwano <Takahiro.Kuwano@infineon.com>
---
 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(-)

Comments

Tudor Ambarus Nov. 1, 2022, 8:46 a.m. UTC | #1
On 8/11/22 01:06, Michael Walle wrote:
> --- a/drivers/mtd/spi-nor/core.c
> +++ b/drivers/mtd/spi-nor/core.c
> @@ -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.
Tudor Ambarus Nov. 7, 2022, 2:42 p.m. UTC | #2
On 11/1/22 10:46, Tudor.Ambarus@microchip.com wrote:
> EXTERNAL EMAIL: Do not click links or open attachments unless you know the content is safe
> 
> On 8/11/22 01:06, Michael Walle wrote:
>> --- a/drivers/mtd/spi-nor/core.c
>> +++ b/drivers/mtd/spi-nor/core.c
>> @@ -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.
> 

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
Michael Walle Nov. 7, 2022, 6:22 p.m. UTC | #3
Am 2022-11-01 09:46, schrieb Tudor.Ambarus@microchip.com:
> On 8/11/22 01:06, Michael Walle wrote:
>> --- a/drivers/mtd/spi-nor/core.c
>> +++ b/drivers/mtd/spi-nor/core.c
>> @@ -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.

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
Tudor Ambarus Nov. 21, 2022, 1:35 p.m. UTC | #4
On 8/11/22 01:06, Michael Walle wrote:
> 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.
> 
> Signed-off-by: Michael Walle <michael@walle.cc>
> Reviewed-by: Takahiro Kuwano <Takahiro.Kuwano@infineon.com>
Tested-by: Tudor Ambarus <tudor.ambarus@microchip.com>

> ---
>  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(-)
>
diff mbox series

Patch

diff --git a/drivers/mtd/spi-nor/core.c b/drivers/mtd/spi-nor/core.c
index 42f9bb63919c..f10fca5730b8 100644
--- a/drivers/mtd/spi-nor/core.c
+++ b/drivers/mtd/spi-nor/core.c
@@ -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.
diff --git a/drivers/mtd/spi-nor/core.h b/drivers/mtd/spi-nor/core.h
index 68aaccaa12d8..ef0a73dbf348 100644
--- a/drivers/mtd/spi-nor/core.h
+++ b/drivers/mtd/spi-nor/core.h
@@ -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)
diff --git a/drivers/mtd/spi-nor/sfdp.c b/drivers/mtd/spi-nor/sfdp.c
index 2257f1b4c2e2..0a77f50a2691 100644
--- a/drivers/mtd/spi-nor/sfdp.c
+++ b/drivers/mtd/spi-nor/sfdp.c
@@ -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'