Skip to content

Commit bb27626

Browse files
computersforpeaceBoris Brezillon
authored andcommitted
mtd: spi-nor: only apply reset hacks to broken hardware
Commit 59b356f ("mtd: m25p80: restore the status of SPI flash when exiting") is the latest from a long history of attempts to add reboot handling to handle stateful addressing modes on SPI flash. Some prior mostly-related discussions: http://lists.infradead.org/pipermail/linux-mtd/2013-March/046343.html [PATCH 1/3] mtd: m25p80: utilize dedicated 4-byte addressing commands http://lists.infradead.org/pipermail/barebox/2014-September/020682.html [RFC] MTD m25p80 3-byte addressing and boot problem http://lists.infradead.org/pipermail/linux-mtd/2015-February/057683.html [PATCH 2/2] m25p80: if supported put chip to deep power down if not used Previously, attempts to add reboot-time software reset handling were rejected, but the latest attempt was not. Quick summary of the problem: Some systems (e.g., boot ROM or bootloader) assume that they can read initial boot code from their SPI flash using 3-byte addressing. If the flash is left in 4-byte mode after reset, these systems won't boot. The above patch provided a shutdown/remove hook to attempt to reset the addressing mode before we reboot. Notably, this patch misses out on huge classes of unexpected reboots (e.g., crashes, watchdog resets). Unfortunately, it is essentially impossible to solve this problem 100%: if your system doesn't know how to reset the SPI flash to power-on defaults at initialization time, no amount of software can really rescue you -- there will always be a chance of some unexpected reset that leaves your flash in an addressing mode that your boot sequence didn't expect. While it is not directly harmful to perform hacks like the aforementioned commit on all 4-byte addressing flash, a properly-designed system should not need the hack -- and in fact, providing this hack may mask the fact that a given system is indeed broken. So this patch attempts to apply this unsound hack more narrowly, providing a strong suggestion to developers and system designers that this is truly a hack. With luck, system designers can catch their errors early on in their development cycle, rather than applying this hack long term. But apparently enough systems are out in the wild that we still have to provide this hack. Document a new device tree property to denote systems that do not have a proper hardware (or software) reset mechanism, and apply the hack (with a loud warning) only in this case. Signed-off-by: Brian Norris <computersforpeace@gmail.com> Reviewed-by: Guenter Roeck <linux@roeck-us.net> Signed-off-by: Boris Brezillon <boris.brezillon@bootlin.com>
1 parent 3938c0d commit bb27626

3 files changed

Lines changed: 26 additions & 2 deletions

File tree

‎Documentation/devicetree/bindings/mtd/jedec,spi-nor.txt‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,15 @@ Optional properties:
6969
all chips and support for it can not be detected at runtime.
7070
Refer to your chips' datasheet to check if this is supported
7171
by your chip.
72+
- broken-flash-reset : Some flash devices utilize stateful addressing modes
73+
(e.g., for 32-bit addressing) which need to be managed
74+
carefully by a system. Because these sorts of flash don't
75+
have a standardized software reset command, and because some
76+
systems don't toggle the flash RESET# pin upon system reset
77+
(if the pin even exists at all), there are systems which
78+
cannot reboot properly if the flash is left in the "wrong"
79+
state. This boolean flag can be used on such systems, to
80+
denote the absence of a reliable reset mechanism.
7281

7382
Example:
7483

‎drivers/mtd/spi-nor/spi-nor.c‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2757,8 +2757,18 @@ static int spi_nor_init(struct spi_nor *nor)
27572757

27582758
if ((nor->addr_width == 4) &&
27592759
(JEDEC_MFR(nor->info) != SNOR_MFR_SPANSION) &&
2760-
!(nor->info->flags & SPI_NOR_4B_OPCODES))
2760+
!(nor->info->flags & SPI_NOR_4B_OPCODES)) {
2761+
/*
2762+
* If the RESET# pin isn't hooked up properly, or the system
2763+
* otherwise doesn't perform a reset command in the boot
2764+
* sequence, it's impossible to 100% protect against unexpected
2765+
* reboots (e.g., crashes). Warn the user (or hopefully, system
2766+
* designer) that this is bad.
2767+
*/
2768+
WARN_ONCE(nor->flags & SNOR_F_BROKEN_RESET,
2769+
"enabling reset hack; may not recover from unexpected reboots\n");
27612770
set_4byte(nor, nor->info, 1);
2771+
}
27622772

27632773
return 0;
27642774
}
@@ -2781,7 +2791,8 @@ void spi_nor_restore(struct spi_nor *nor)
27812791
/* restore the addressing mode */
27822792
if ((nor->addr_width == 4) &&
27832793
(JEDEC_MFR(nor->info) != SNOR_MFR_SPANSION) &&
2784-
!(nor->info->flags & SPI_NOR_4B_OPCODES))
2794+
!(nor->info->flags & SPI_NOR_4B_OPCODES) &&
2795+
(nor->flags & SNOR_F_BROKEN_RESET))
27852796
set_4byte(nor, nor->info, 0);
27862797
}
27872798
EXPORT_SYMBOL_GPL(spi_nor_restore);
@@ -2911,6 +2922,9 @@ int spi_nor_scan(struct spi_nor *nor, const char *name,
29112922
params.hwcaps.mask |= SNOR_HWCAPS_READ_FAST;
29122923
}
29132924

2925+
if (of_property_read_bool(np, "broken-flash-reset"))
2926+
nor->flags |= SNOR_F_BROKEN_RESET;
2927+
29142928
/* Some devices cannot do fast-read, no matter what DT tells us */
29152929
if (info->flags & SPI_NOR_NO_FR)
29162930
params.hwcaps.mask &= ~SNOR_HWCAPS_READ_FAST;

‎include/linux/mtd/spi-nor.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ enum spi_nor_option_flags {
235235
SNOR_F_S3AN_ADDR_DEFAULT = BIT(3),
236236
SNOR_F_READY_XSR_RDY = BIT(4),
237237
SNOR_F_USE_CLSR = BIT(5),
238+
SNOR_F_BROKEN_RESET = BIT(6),
238239
};
239240

240241
/**

0 commit comments

Comments
 (0)