1 From ea92cbb50a78404e29de2cc3999a240615ffb1c8 Mon Sep 17 00:00:00 2001
2 From: Chuanhong Guo <gch981213@gmail.com>
3 Date: Mon, 6 Apr 2020 17:58:48 +0800
4 Subject: [PATCH] mtd: spi-nor: rework broken-flash-reset support
6 Instead of resetting flash to 3B address on remove hook, this
7 implementation only enters 4B mode when needed, which prevents
8 more unexpected reboot stuck. This implementation makes it only
9 break when a kernel panic happens during flash operation on 16M+
11 *OpenWrt only*: silent broken-flash-reset warning. We are not dealing
12 with vendors and it's unpleasant for users to se that unnecessary
13 and long WARN_ON print.
15 Signed-off-by: Chuanhong Guo <gch981213@gmail.com>
17 drivers/mtd/spi-nor/spi-nor.c | 52 +++++++++++++++++++++++++++++++++--
18 1 file changed, 49 insertions(+), 3 deletions(-)
20 --- a/drivers/mtd/spi-nor/spi-nor.c
21 +++ b/drivers/mtd/spi-nor/spi-nor.c
22 @@ -616,6 +616,22 @@ static void spi_nor_set_4byte_opcodes(st
26 +static int spi_nor_check_set_addr_width(struct spi_nor *nor, loff_t addr)
30 + if ((nor->flags & (SNOR_F_4B_OPCODES | SNOR_F_BROKEN_RESET)) !=
31 + SNOR_F_BROKEN_RESET)
34 + addr_width = addr & 0xff000000 ? 4 : 3;
35 + if (nor->addr_width == addr_width)
38 + nor->addr_width = addr_width;
39 + return nor->params.set_4byte(nor, addr_width == 4);
42 static int macronix_set_4byte(struct spi_nor *nor, bool enable)
45 @@ -1261,6 +1277,10 @@ static int spi_nor_erase(struct mtd_info
49 + ret = spi_nor_check_set_addr_width(nor, instr->addr + instr->len);
53 /* whole-chip erase? */
54 if (len == mtd->size && !(nor->flags & SNOR_F_NO_OP_CHIP_ERASE)) {
55 unsigned long timeout;
56 @@ -1317,6 +1337,7 @@ static int spi_nor_erase(struct mtd_info
60 + spi_nor_check_set_addr_width(nor, 0);
61 spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_ERASE);
64 @@ -1623,7 +1644,9 @@ static int spi_nor_lock(struct mtd_info
68 + spi_nor_check_set_addr_width(nor, ofs + len);
69 ret = nor->params.locking_ops->lock(nor, ofs, len);
70 + spi_nor_check_set_addr_width(nor, 0);
72 spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_UNLOCK);
74 @@ -1638,7 +1661,9 @@ static int spi_nor_unlock(struct mtd_inf
78 + spi_nor_check_set_addr_width(nor, ofs + len);
79 ret = nor->params.locking_ops->unlock(nor, ofs, len);
80 + spi_nor_check_set_addr_width(nor, 0);
82 spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_LOCK);
84 @@ -1653,7 +1678,9 @@ static int spi_nor_is_locked(struct mtd_
88 + spi_nor_check_set_addr_width(nor, ofs + len);
89 ret = nor->params.locking_ops->is_locked(nor, ofs, len);
90 + spi_nor_check_set_addr_width(nor, 0);
92 spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_LOCK);
94 @@ -2559,6 +2586,10 @@ static int spi_nor_read(struct mtd_info
98 + ret = spi_nor_check_set_addr_width(nor, from + len);
105 @@ -2582,6 +2613,7 @@ static int spi_nor_read(struct mtd_info
109 + spi_nor_check_set_addr_width(nor, 0);
110 spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_READ);
113 @@ -2599,6 +2631,10 @@ static int sst_write(struct mtd_info *mt
117 + ret = spi_nor_check_set_addr_width(nor, to + len);
123 nor->sst_write_second = false;
124 @@ -2661,6 +2697,7 @@ static int sst_write(struct mtd_info *mt
128 + spi_nor_check_set_addr_width(nor, 0);
129 spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_WRITE);
132 @@ -2683,6 +2720,10 @@ static int spi_nor_write(struct mtd_info
136 + ret = spi_nor_check_set_addr_width(nor, to + len);
140 for (i = 0; i < len; ) {
142 loff_t addr = to + i;
143 @@ -2722,6 +2763,7 @@ static int spi_nor_write(struct mtd_info
147 + spi_nor_check_set_addr_width(nor, 0);
148 spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_WRITE);
151 @@ -4726,9 +4768,13 @@ static int spi_nor_init(struct spi_nor *
152 * reboots (e.g., crashes). Warn the user (or hopefully, system
153 * designer) that this is bad.
155 - WARN_ONCE(nor->flags & SNOR_F_BROKEN_RESET,
156 - "enabling reset hack; may not recover from unexpected reboots\n");
157 - nor->params.set_4byte(nor, true);
158 + if (nor->flags & SNOR_F_BROKEN_RESET) {
160 + "enabling reset hack; may not recover from unexpected reboots\n");
161 + nor->addr_width = 3;
163 + nor->params.set_4byte(nor, true);