1 From 773bbe10449731c9525457873e0c2342e5cf883b Mon Sep 17 00:00:00 2001
2 From: Michael Walle <michael@walle.cc>
3 Date: Thu, 11 Aug 2022 00:06:53 +0200
4 Subject: [PATCH] mtd: spi-nor: add generic flash driver
6 Our SFDP parsing is everything we need to support all basic operations
7 of a flash device. If the flash isn't found in our in-kernel flash
8 database, gracefully fall back to a driver described solely by its SFDP
11 Signed-off-by: Michael Walle <michael@walle.cc>
12 Signed-off-by: Tudor Ambarus <tudor.ambarus@microchip.com>
13 Tested-by: Tudor Ambarus <tudor.ambarus@microchip.com>
14 Reviewed-by: Takahiro Kuwano <Takahiro.Kuwano@infineon.com>
15 Link: https://lore.kernel.org/r/20220810220654.1297699-7-michael@walle.cc
17 drivers/mtd/spi-nor/core.c | 26 ++++++++++++++++++++++++--
18 drivers/mtd/spi-nor/core.h | 1 +
19 drivers/mtd/spi-nor/sfdp.c | 27 +++++++++++++++++++++++++++
20 3 files changed, 52 insertions(+), 2 deletions(-)
22 --- a/drivers/mtd/spi-nor/core.c
23 +++ b/drivers/mtd/spi-nor/core.c
24 @@ -1636,6 +1636,16 @@ static const struct spi_nor_manufacturer
28 +static const struct flash_info spi_nor_generic_flash = {
29 + .name = "spi-nor-generic",
31 + * JESD216 rev A doesn't specify the page size, therefore we need a
38 static const struct flash_info *spi_nor_match_id(struct spi_nor *nor,
41 @@ -1669,6 +1679,14 @@ static const struct flash_info *spi_nor_
44 info = spi_nor_match_id(nor, id);
46 + /* Fallback to a generic flash described only by its SFDP data. */
48 + ret = spi_nor_check_sfdp_signature(nor);
50 + info = &spi_nor_generic_flash;
54 dev_err(nor->dev, "unrecognized JEDEC id bytes: %*ph\n",
55 SPI_NOR_MAX_ID_LEN, id);
56 @@ -2105,8 +2123,12 @@ static int spi_nor_select_pp(struct spi_
57 * spi_nor_select_uniform_erase() - select optimum uniform erase type
58 * @map: the erase map of the SPI NOR
59 * @wanted_size: the erase type size to search for. Contains the value of
60 - * info->sector_size or of the "small sector" size in case
61 - * CONFIG_MTD_SPI_NOR_USE_4K_SECTORS is defined.
62 + * info->sector_size, the "small sector" size in case
63 + * CONFIG_MTD_SPI_NOR_USE_4K_SECTORS is defined or 0 if
64 + * there is no information about the sector size. The
65 + * latter is the case if the flash parameters are parsed
66 + * solely by SFDP, then the largest supported erase type
69 * Once the optimum uniform sector erase command is found, disable all the
71 --- a/drivers/mtd/spi-nor/core.h
72 +++ b/drivers/mtd/spi-nor/core.h
73 @@ -708,6 +708,8 @@ int spi_nor_controller_ops_read_reg(stru
74 int spi_nor_controller_ops_write_reg(struct spi_nor *nor, u8 opcode,
75 const u8 *buf, size_t len);
77 +int spi_nor_check_sfdp_signature(struct spi_nor *nor);
79 static inline struct spi_nor *mtd_to_spi_nor(struct mtd_info *mtd)
81 return container_of(mtd, struct spi_nor, mtd);
82 --- a/drivers/mtd/spi-nor/sfdp.c
83 +++ b/drivers/mtd/spi-nor/sfdp.c
84 @@ -1250,6 +1250,33 @@ static void spi_nor_post_sfdp_fixups(str
88 + * spi_nor_check_sfdp_signature() - check for a valid SFDP signature
89 + * @nor: pointer to a 'struct spi_nor'
91 + * Used to detect if the flash supports the RDSFDP command as well as the
92 + * presence of a valid SFDP table.
94 + * Return: 0 on success, -errno otherwise.
96 +int spi_nor_check_sfdp_signature(struct spi_nor *nor)
101 + /* Get the SFDP header. */
102 + err = spi_nor_read_sfdp_dma_unsafe(nor, 0, sizeof(signature),
107 + /* Check the SFDP signature. */
108 + if (le32_to_cpu(signature) != SFDP_SIGNATURE)
115 * spi_nor_parse_sfdp() - parse the Serial Flash Discoverable Parameters.
116 * @nor: pointer to a 'struct spi_nor'