85f6cc6da9b3dcdfc9cc808ce2e573a97207974c
[openwrt/openwrt.git] / target / linux / layerscape / patches-4.4 / 1081-mtd-spi-nor-provide-default-erase_sector-implementat.patch
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
5 implementation
6
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.
10
11 Tested on m25p80.c and Medatek's MT8173 SPI NOR flash driver.
12
13 Signed-off-by: Brian Norris <computersforpeace@gmail.com>
14 ---
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(-)
18
19 --- a/drivers/mtd/spi-nor/spi-nor.c
20 +++ b/drivers/mtd/spi-nor/spi-nor.c
21 @@ -38,6 +38,7 @@
22 #define CHIP_ERASE_2MB_READY_WAIT_JIFFIES (40UL * HZ)
23
24 #define SPI_NOR_MAX_ID_LEN 6
25 +#define SPI_NOR_MAX_ADDR_WIDTH 4
26
27 struct flash_info {
28 char *name;
29 @@ -313,6 +314,29 @@ static void spi_nor_unlock_and_unprep(st
30 }
31
32 /*
33 + * Initiate the erasure of a single sector
34 + */
35 +static int spi_nor_erase_sector(struct spi_nor *nor, u32 addr)
36 +{
37 + u8 buf[SPI_NOR_MAX_ADDR_WIDTH];
38 + int i;
39 +
40 + if (nor->erase)
41 + return nor->erase(nor, addr);
42 +
43 + /*
44 + * Default implementation, if driver doesn't have a specialized HW
45 + * control
46 + */
47 + for (i = nor->addr_width - 1; i >= 0; i--) {
48 + buf[i] = addr & 0xff;
49 + addr >>= 8;
50 + }
51 +
52 + return nor->write_reg(nor, nor->erase_opcode, buf, nor->addr_width);
53 +}
54 +
55 +/*
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.
58 */
59 @@ -371,10 +395,9 @@ static int spi_nor_erase(struct mtd_info
60 while (len) {
61 write_enable(nor);
62
63 - if (nor->erase(nor, addr)) {
64 - ret = -EIO;
65 + ret = spi_nor_erase_sector(nor, addr);
66 + if (ret)
67 goto erase_err;
68 - }
69
70 addr += mtd->erasesize;
71 len -= mtd->erasesize;
72 @@ -1099,7 +1122,7 @@ static int set_quad_mode(struct spi_nor
73 static int spi_nor_check(struct spi_nor *nor)
74 {
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");
79 return -EINVAL;
80 }
81 @@ -1300,6 +1323,12 @@ int spi_nor_scan(struct spi_nor *nor, co
82 nor->addr_width = 3;
83 }
84
85 + if (nor->addr_width > SPI_NOR_MAX_ADDR_WIDTH) {
86 + dev_err(dev, "address width is too large: %u\n",
87 + nor->addr_width);
88 + return -EINVAL;
89 + }
90 +
91 nor->read_dummy = spi_nor_read_dummy_cycles(nor);
92
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