Skip to content

Commit

Permalink
mtd: spi-nor: add generic flash driver
Browse files Browse the repository at this point in the history
Our SFDP is 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.

It is still recommended to add the flash to the in-kernel database.
First, we get a proper partname and secondly, for all features not
described by the SFDP like OTP we need the entry anyway.

Signed-off-by: Michael Walle <michael@walle.cc>
  • Loading branch information
mwalle authored and intel-lab-lkp committed May 13, 2022
1 parent 71c1a6e commit d38c0ac
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
13 changes: 13 additions & 0 deletions drivers/mtd/spi-nor/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -1632,6 +1632,11 @@ static const struct spi_nor_manufacturer *manufacturers[] = {
&spi_nor_xmc,
};

static const struct flash_info spi_nor_generic_flash = {
.name = "spi-nor-generic",
.parse_sfdp = true,
};

static const struct flash_info *spi_nor_match_id(struct spi_nor *nor,
const u8 *id)
{
Expand Down Expand Up @@ -1670,6 +1675,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);
Expand Down
1 change: 1 addition & 0 deletions drivers/mtd/spi-nor/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,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)
Expand Down
27 changes: 27 additions & 0 deletions drivers/mtd/spi-nor/sfdp.c
Original file line number Diff line number Diff line change
Expand Up @@ -1247,6 +1247,33 @@ static void spi_nor_post_sfdp_fixups(struct spi_nor *nor)
nor->info->fixups->post_sfdp(nor);
}

/**
* spi_nor_check_sfdp_header() - check for a valid SFDP header
* @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'
Expand Down

0 comments on commit d38c0ac

Please sign in to comment.