Commit c67cbb83 authored by Brian Norris's avatar Brian Norris

mtd: spi-nor: provide default erase_sector implementation

Some spi-nor drivers perform sector erase by duplicating their
write_reg() command. Let's not require that the driver fill this out,
and provide a default instead.

Tested on m25p80.c and Medatek's MT8173 SPI NOR flash driver.
Signed-off-by: default avatarBrian Norris <computersforpeace@gmail.com>
parent 1e6460ab
...@@ -38,6 +38,7 @@ ...@@ -38,6 +38,7 @@
#define CHIP_ERASE_2MB_READY_WAIT_JIFFIES (40UL * HZ) #define CHIP_ERASE_2MB_READY_WAIT_JIFFIES (40UL * HZ)
#define SPI_NOR_MAX_ID_LEN 6 #define SPI_NOR_MAX_ID_LEN 6
#define SPI_NOR_MAX_ADDR_WIDTH 4
struct flash_info { struct flash_info {
char *name; char *name;
...@@ -312,6 +313,29 @@ static void spi_nor_unlock_and_unprep(struct spi_nor *nor, enum spi_nor_ops ops) ...@@ -312,6 +313,29 @@ static void spi_nor_unlock_and_unprep(struct spi_nor *nor, enum spi_nor_ops ops)
mutex_unlock(&nor->lock); mutex_unlock(&nor->lock);
} }
/*
* Initiate the erasure of a single sector
*/
static int spi_nor_erase_sector(struct spi_nor *nor, u32 addr)
{
u8 buf[SPI_NOR_MAX_ADDR_WIDTH];
int i;
if (nor->erase)
return nor->erase(nor, addr);
/*
* Default implementation, if driver doesn't have a specialized HW
* control
*/
for (i = nor->addr_width - 1; i >= 0; i--) {
buf[i] = addr & 0xff;
addr >>= 8;
}
return nor->write_reg(nor, nor->erase_opcode, buf, nor->addr_width);
}
/* /*
* Erase an address range on the nor chip. The address range may extend * Erase an address range on the nor chip. The address range may extend
* one or more erase sectors. Return an error is there is a problem erasing. * one or more erase sectors. Return an error is there is a problem erasing.
...@@ -371,10 +395,9 @@ static int spi_nor_erase(struct mtd_info *mtd, struct erase_info *instr) ...@@ -371,10 +395,9 @@ static int spi_nor_erase(struct mtd_info *mtd, struct erase_info *instr)
while (len) { while (len) {
write_enable(nor); write_enable(nor);
if (nor->erase(nor, addr)) { ret = spi_nor_erase_sector(nor, addr);
ret = -EIO; if (ret)
goto erase_err; goto erase_err;
}
addr += mtd->erasesize; addr += mtd->erasesize;
len -= mtd->erasesize; len -= mtd->erasesize;
...@@ -1138,7 +1161,7 @@ static int set_quad_mode(struct spi_nor *nor, const struct flash_info *info) ...@@ -1138,7 +1161,7 @@ static int set_quad_mode(struct spi_nor *nor, const struct flash_info *info)
static int spi_nor_check(struct spi_nor *nor) static int spi_nor_check(struct spi_nor *nor)
{ {
if (!nor->dev || !nor->read || !nor->write || if (!nor->dev || !nor->read || !nor->write ||
!nor->read_reg || !nor->write_reg || !nor->erase) { !nor->read_reg || !nor->write_reg) {
pr_err("spi-nor: please fill all the necessary fields!\n"); pr_err("spi-nor: please fill all the necessary fields!\n");
return -EINVAL; return -EINVAL;
} }
...@@ -1340,6 +1363,12 @@ int spi_nor_scan(struct spi_nor *nor, const char *name, enum read_mode mode) ...@@ -1340,6 +1363,12 @@ int spi_nor_scan(struct spi_nor *nor, const char *name, enum read_mode mode)
nor->addr_width = 3; nor->addr_width = 3;
} }
if (nor->addr_width > SPI_NOR_MAX_ADDR_WIDTH) {
dev_err(dev, "address width is too large: %u\n",
nor->addr_width);
return -EINVAL;
}
nor->read_dummy = spi_nor_read_dummy_cycles(nor); nor->read_dummy = spi_nor_read_dummy_cycles(nor);
dev_info(dev, "%s (%lld Kbytes)\n", info->name, dev_info(dev, "%s (%lld Kbytes)\n", info->name,
......
...@@ -143,7 +143,8 @@ struct mtd_info; ...@@ -143,7 +143,8 @@ struct mtd_info;
* @read: [DRIVER-SPECIFIC] read data from the SPI NOR * @read: [DRIVER-SPECIFIC] read data from the SPI NOR
* @write: [DRIVER-SPECIFIC] write data to the SPI NOR * @write: [DRIVER-SPECIFIC] write data to the SPI NOR
* @erase: [DRIVER-SPECIFIC] erase a sector of the SPI NOR * @erase: [DRIVER-SPECIFIC] erase a sector of the SPI NOR
* at the offset @offs * at the offset @offs; if not provided by the driver,
* spi-nor will send the erase opcode via write_reg()
* @flash_lock: [FLASH-SPECIFIC] lock a region of the SPI NOR * @flash_lock: [FLASH-SPECIFIC] lock a region of the SPI NOR
* @flash_unlock: [FLASH-SPECIFIC] unlock a region of the SPI NOR * @flash_unlock: [FLASH-SPECIFIC] unlock a region of the SPI NOR
* @flash_is_locked: [FLASH-SPECIFIC] check if a region of the SPI NOR is * @flash_is_locked: [FLASH-SPECIFIC] check if a region of the SPI NOR is
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment