drivers/spi/spi_flash: separate out protection ops

Put the write protection into its own object. This allows
for easier future reuse of objects in future consolidation
patches. It's also possible to eliminate the code implmementing
these in the future if the platform doesn't require it. For now
leave current behavior as-is.

The names of the callbacks were shortened as they are now in
the spi_flash_protection_ops object which is a new field in the
spi_flash object.

Change-Id: I2fec4e4430709fcf3e08a55dd36583211c035c08
Signed-off-by: Aaron Durbin <adurbin@chromium.org>
Reviewed-on: https://review.coreboot.org/c/coreboot/+/38376
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Reviewed-by: Furquan Shaikh <furquan@google.com>
This commit is contained in:
Aaron Durbin 2020-01-11 14:03:27 -07:00
parent cb01aa586f
commit f584f19efc
3 changed files with 18 additions and 9 deletions

View File

@ -520,13 +520,13 @@ int spi_flash_is_write_protected(const struct spi_flash *flash,
if (!region_is_subregion(&flash_region, region))
return -1;
if (!flash->ops->get_write_protection) {
if (!flash->prot_ops) {
printk(BIOS_WARNING, "SPI: Write-protection gathering not "
"implemented for this vendor.\n");
return -1;
}
return flash->ops->get_write_protection(flash, region);
return flash->prot_ops->get_write(flash, region);
}
int spi_flash_set_write_protected(const struct spi_flash *flash,
@ -545,14 +545,13 @@ int spi_flash_set_write_protected(const struct spi_flash *flash,
if (!region_is_subregion(&flash_region, region))
return -1;
if (!flash->ops->set_write_protection) {
if (!flash->prot_ops) {
printk(BIOS_WARNING, "SPI: Setting write-protection is not "
"implemented for this vendor.\n");
return -1;
}
ret = flash->ops->set_write_protection(flash, region, non_volatile,
mode);
ret = flash->prot_ops->set_write(flash, region, non_volatile, mode);
if (ret == 0 && mode != SPI_WRITE_PROTECTION_PRESERVE) {
printk(BIOS_INFO, "SPI: SREG lock-down was set to ");

View File

@ -615,8 +615,11 @@ static const struct spi_flash_ops spi_flash_ops = {
.write = spi_flash_cmd_write_page_program,
.erase = spi_flash_cmd_erase,
.status = spi_flash_cmd_status,
.get_write_protection = winbond_get_write_protection,
.set_write_protection = winbond_set_write_protection,
};
static const struct spi_flash_protection_ops spi_flash_protection_ops = {
.get_write = winbond_get_write_protection,
.set_write = winbond_set_write_protection,
};
int spi_flash_probe_winbond(const struct spi_slave *spi, u8 *idcode,
@ -655,6 +658,7 @@ int spi_flash_probe_winbond(const struct spi_slave *spi, u8 *idcode,
flash->flags.dual_spi = params->dual_spi;
flash->ops = &spi_flash_ops;
flash->prot_ops = &spi_flash_protection_ops;
flash->driver_private = params;
return 0;

View File

@ -59,6 +59,10 @@ struct spi_flash_ops {
const void *buf);
int (*erase)(const struct spi_flash *flash, u32 offset, size_t len);
int (*status)(const struct spi_flash *flash, u8 *reg);
};
/* Current code assumes all callbacks are supplied in this object. */
struct spi_flash_protection_ops {
/*
* Returns 1 if the whole region is software write protected.
* Hardware write protection mechanism aren't accounted.
@ -66,7 +70,7 @@ struct spi_flash_ops {
* register for example, 0 should be returned.
* Returns 0 on success.
*/
int (*get_write_protection)(const struct spi_flash *flash,
int (*get_write)(const struct spi_flash *flash,
const struct region *region);
/*
* Enable the status register write protection, if supported on the
@ -80,7 +84,7 @@ struct spi_flash_ops {
* @return 0 on success
*/
int
(*set_write_protection)(const struct spi_flash *flash,
(*set_write)(const struct spi_flash *flash,
const struct region *region,
const bool non_volatile,
const enum spi_flash_status_reg_lockdown mode);
@ -107,6 +111,8 @@ struct spi_flash {
u8 pp_cmd; /* Page program command. */
u8 wren_cmd; /* Write Enable command. */
const struct spi_flash_ops *ops;
/* If !NULL all protection callbacks exist. */
const struct spi_flash_protection_ops *prot_ops;
const void *driver_private;
};