generic: 6.1: backport support for generic spi-nor from SFDP data
[openwrt/openwrt.git] / target / linux / generic / backport-6.1 / 410-v6.2-mtd-spi-nor-add-generic-flash-driver.patch
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
5
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
9 tables.
10
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
16 ---
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(-)
21
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
25 &spi_nor_xmc,
26 };
27
28 +static const struct flash_info spi_nor_generic_flash = {
29 + .name = "spi-nor-generic",
30 + /*
31 + * JESD216 rev A doesn't specify the page size, therefore we need a
32 + * sane default.
33 + */
34 + .page_size = 256,
35 + .parse_sfdp = true,
36 +};
37 +
38 static const struct flash_info *spi_nor_match_id(struct spi_nor *nor,
39 const u8 *id)
40 {
41 @@ -1669,6 +1679,14 @@ static const struct flash_info *spi_nor_
42 }
43
44 info = spi_nor_match_id(nor, id);
45 +
46 + /* Fallback to a generic flash described only by its SFDP data. */
47 + if (!info) {
48 + ret = spi_nor_check_sfdp_signature(nor);
49 + if (!ret)
50 + info = &spi_nor_generic_flash;
51 + }
52 +
53 if (!info) {
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
67 + * is selected.
68 *
69 * Once the optimum uniform sector erase command is found, disable all the
70 * other.
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);
76
77 +int spi_nor_check_sfdp_signature(struct spi_nor *nor);
78 +
79 static inline struct spi_nor *mtd_to_spi_nor(struct mtd_info *mtd)
80 {
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
85 }
86
87 /**
88 + * spi_nor_check_sfdp_signature() - check for a valid SFDP signature
89 + * @nor: pointer to a 'struct spi_nor'
90 + *
91 + * Used to detect if the flash supports the RDSFDP command as well as the
92 + * presence of a valid SFDP table.
93 + *
94 + * Return: 0 on success, -errno otherwise.
95 + */
96 +int spi_nor_check_sfdp_signature(struct spi_nor *nor)
97 +{
98 + u32 signature;
99 + int err;
100 +
101 + /* Get the SFDP header. */
102 + err = spi_nor_read_sfdp_dma_unsafe(nor, 0, sizeof(signature),
103 + &signature);
104 + if (err < 0)
105 + return err;
106 +
107 + /* Check the SFDP signature. */
108 + if (le32_to_cpu(signature) != SFDP_SIGNATURE)
109 + return -EINVAL;
110 +
111 + return 0;
112 +}
113 +
114 +/**
115 * spi_nor_parse_sfdp() - parse the Serial Flash Discoverable Parameters.
116 * @nor: pointer to a 'struct spi_nor'
117 *