1 From 56bd0e13d8bc3b4486251b10ac9d2ba7434c21ee Mon Sep 17 00:00:00 2001
2 From: Brian Norris <computersforpeace@gmail.com>
3 Date: Tue, 10 Nov 2015 12:15:27 -0800
4 Subject: [PATCH 081/113] mtd: spi-nor: provide default erase_sector
7 Some spi-nor drivers perform sector erase by duplicating their
8 write_reg() command. Let's not require that the driver fill this out,
9 and provide a default instead.
11 Tested on m25p80.c and Medatek's MT8173 SPI NOR flash driver.
13 Signed-off-by: Brian Norris <computersforpeace@gmail.com>
15 drivers/mtd/spi-nor/spi-nor.c | 37 +++++++++++++++++++++++++++++++++----
16 include/linux/mtd/spi-nor.h | 3 ++-
17 2 files changed, 35 insertions(+), 5 deletions(-)
19 --- a/drivers/mtd/spi-nor/spi-nor.c
20 +++ b/drivers/mtd/spi-nor/spi-nor.c
22 #define CHIP_ERASE_2MB_READY_WAIT_JIFFIES (40UL * HZ)
24 #define SPI_NOR_MAX_ID_LEN 6
25 +#define SPI_NOR_MAX_ADDR_WIDTH 4
29 @@ -313,6 +314,29 @@ static void spi_nor_unlock_and_unprep(st
33 + * Initiate the erasure of a single sector
35 +static int spi_nor_erase_sector(struct spi_nor *nor, u32 addr)
37 + u8 buf[SPI_NOR_MAX_ADDR_WIDTH];
41 + return nor->erase(nor, addr);
44 + * Default implementation, if driver doesn't have a specialized HW
47 + for (i = nor->addr_width - 1; i >= 0; i--) {
48 + buf[i] = addr & 0xff;
52 + return nor->write_reg(nor, nor->erase_opcode, buf, nor->addr_width);
56 * Erase an address range on the nor chip. The address range may extend
57 * one or more erase sectors. Return an error is there is a problem erasing.
59 @@ -371,10 +395,9 @@ static int spi_nor_erase(struct mtd_info
63 - if (nor->erase(nor, addr)) {
65 + ret = spi_nor_erase_sector(nor, addr);
70 addr += mtd->erasesize;
71 len -= mtd->erasesize;
72 @@ -1098,7 +1121,7 @@ static int set_quad_mode(struct spi_nor
73 static int spi_nor_check(struct spi_nor *nor)
75 if (!nor->dev || !nor->read || !nor->write ||
76 - !nor->read_reg || !nor->write_reg || !nor->erase) {
77 + !nor->read_reg || !nor->write_reg) {
78 pr_err("spi-nor: please fill all the necessary fields!\n");
81 @@ -1299,6 +1322,12 @@ int spi_nor_scan(struct spi_nor *nor, co
85 + if (nor->addr_width > SPI_NOR_MAX_ADDR_WIDTH) {
86 + dev_err(dev, "address width is too large: %u\n",
91 nor->read_dummy = spi_nor_read_dummy_cycles(nor);
93 dev_info(dev, "%s (%lld Kbytes)\n", info->name,
94 --- a/include/linux/mtd/spi-nor.h
95 +++ b/include/linux/mtd/spi-nor.h
96 @@ -142,7 +142,8 @@ enum spi_nor_option_flags {
97 * @read: [DRIVER-SPECIFIC] read data from the SPI NOR
98 * @write: [DRIVER-SPECIFIC] write data to the SPI NOR
99 * @erase: [DRIVER-SPECIFIC] erase a sector of the SPI NOR
100 - * at the offset @offs
101 + * at the offset @offs; if not provided by the driver,
102 + * spi-nor will send the erase opcode via write_reg()
103 * @flash_lock: [FLASH-SPECIFIC] lock a region of the SPI NOR
104 * @flash_unlock: [FLASH-SPECIFIC] unlock a region of the SPI NOR
105 * @flash_is_locked: [FLASH-SPECIFIC] check if a region of the SPI NOR is