spi25: Extract 4BA preparations into new `spi25_prepare.c`
We will have more preparation steps for fast-read operations and
QPI in the future. Better start a new file, as `spi25.c` already
is rather long.
Change-Id: I253b270ce6796fb09e6d74903bd65a6fbc06c7d6
Signed-off-by: Nico Huber <nico.h@gmx.de>
Reviewed-on: https://review.sourcearcade.org/c/flashprog/+/162
Reviewed-by: Arthur Heymans <arthur@aheymans.xyz>
diff --git a/Makefile b/Makefile
index 6d993e7..c3bf071 100644
--- a/Makefile
+++ b/Makefile
@@ -386,7 +386,8 @@
CHIP_OBJS = memory_bus.o jedec.o stm50.o w39.o w29ee011.o \
sst28sf040.o 82802ab.o \
- sst49lfxxxc.o sst_fwhub.o edi.o flashchips.o spi.o spi25.o spi25_statusreg.o \
+ sst49lfxxxc.o sst_fwhub.o edi.o flashchips.o \
+ spi.o spi25.o spi25_prepare.o spi25_statusreg.o \
spi95.o opaque.o sfdp.o en29lv640b.o at45db.o \
writeprotect.o writeprotect_ranges.o
diff --git a/include/chipdrivers.h b/include/chipdrivers.h
index 1ac5e53..272b65c 100644
--- a/include/chipdrivers.h
+++ b/include/chipdrivers.h
@@ -28,6 +28,7 @@
int spi_chip_read(struct flashctx *flash, uint8_t *buf, unsigned int start, int unsigned len);
/* spi25.c */
+int spi_simple_write_cmd(struct flashctx *flash, uint8_t op, unsigned int poll_delay);
int probe_spi_rdid(struct flashctx *flash);
int probe_spi_rdid4(struct flashctx *flash);
int probe_spi_rems(struct flashctx *flash);
@@ -57,9 +58,9 @@
int spi_chip_write_1(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len);
int spi_nbyte_read(struct flashctx *flash, uint8_t *dst, unsigned int addr, unsigned int len);
int spi_write_chunked(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len, unsigned int chunksize);
-int spi_enter_4ba(struct flashctx *flash);
-int spi_exit_4ba(struct flashctx *flash);
int spi_set_extended_address(struct flashctx *, uint8_t addr_high);
+
+/* spi25_prepare.c */
int spi_prepare_4ba(struct flashctx *, enum preparation_steps);
diff --git a/meson.build b/meson.build
index efe10cd..408b87a 100644
--- a/meson.build
+++ b/meson.build
@@ -69,6 +69,7 @@
'programmer_table.c',
'sfdp.c',
'spi25.c',
+ 'spi25_prepare.c',
'spi25_statusreg.c',
'spi95.c',
'spi.c',
diff --git a/spi25.c b/spi25.c
index 82fe4a8..30ccc30 100644
--- a/spi25.c
+++ b/spi25.c
@@ -308,7 +308,7 @@
* @param poll_delay interval in us for polling WIP, don't poll if zero
* @return 0 on success, non-zero otherwise
*/
-static int spi_simple_write_cmd(struct flashctx *const flash, const uint8_t op, const unsigned int poll_delay)
+int spi_simple_write_cmd(struct flashctx *const flash, const uint8_t op, const unsigned int poll_delay)
{
struct spi_command cmds[] = {
{
@@ -797,64 +797,3 @@
msg_cerr("%s failed to disable AAI mode.\n", __func__);
return SPI_GENERIC_ERROR;
}
-
-static int spi_enter_exit_4ba(struct flashctx *const flash, const bool enter)
-{
- const unsigned char cmd = enter ? JEDEC_ENTER_4_BYTE_ADDR_MODE : JEDEC_EXIT_4_BYTE_ADDR_MODE;
- int ret = 1;
-
- if (flash->chip->feature_bits & FEATURE_4BA_ENTER)
- ret = spi_send_command(flash, sizeof(cmd), 0, &cmd, NULL);
- else if (flash->chip->feature_bits & FEATURE_4BA_ENTER_WREN)
- ret = spi_simple_write_cmd(flash, cmd, 0);
- else if (flash->chip->feature_bits & FEATURE_4BA_ENTER_EAR7)
- ret = spi_set_extended_address(flash, enter ? 0x80 : 0x00);
-
- if (!ret)
- flash->in_4ba_mode = enter;
- return ret;
-}
-
-int spi_enter_4ba(struct flashctx *const flash)
-{
- return spi_enter_exit_4ba(flash, true);
-}
-
-int spi_exit_4ba(struct flashctx *flash)
-{
- return spi_enter_exit_4ba(flash, false);
-}
-
-int spi_prepare_4ba(struct flashctx *const flash, const enum preparation_steps prep)
-{
- if (prep != PREPARE_FULL)
- return 0;
-
- flash->address_high_byte = -1;
- flash->in_4ba_mode = false;
-
- /* Be careful about 4BA chips and broken masters */
- if (flash->chip->total_size > 16 * 1024 && spi_master_no_4ba_modes(flash)) {
- /* If we can't use native instructions, bail out */
- if ((flash->chip->feature_bits & FEATURE_4BA_NATIVE) != FEATURE_4BA_NATIVE
- || !spi_master_4ba(flash)) {
- msg_cerr("Programmer doesn't support this chip. Aborting.\n");
- return 1;
- }
- }
-
- /* Enable/disable 4-byte addressing mode if flash chip supports it */
- if (flash->chip->feature_bits & (FEATURE_4BA_ENTER | FEATURE_4BA_ENTER_WREN | FEATURE_4BA_ENTER_EAR7)) {
- int ret;
- if (spi_master_4ba(flash))
- ret = spi_enter_4ba(flash);
- else
- ret = spi_exit_4ba(flash);
- if (ret) {
- msg_cerr("Failed to set correct 4BA mode! Aborting.\n");
- return 1;
- }
- }
-
- return 0;
-}
diff --git a/spi25_prepare.c b/spi25_prepare.c
new file mode 100644
index 0000000..7fb1a10
--- /dev/null
+++ b/spi25_prepare.c
@@ -0,0 +1,81 @@
+/*
+ * This file is part of the flashprog project.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ */
+
+#include <stdbool.h>
+#include "flash.h"
+#include "chipdrivers.h"
+#include "programmer.h"
+#include "spi_command.h"
+#include "spi.h"
+
+static int spi_enter_exit_4ba(struct flashctx *const flash, const bool enter)
+{
+ const unsigned char cmd = enter ? JEDEC_ENTER_4_BYTE_ADDR_MODE : JEDEC_EXIT_4_BYTE_ADDR_MODE;
+ int ret = 1;
+
+ if (flash->chip->feature_bits & FEATURE_4BA_ENTER)
+ ret = spi_send_command(flash, sizeof(cmd), 0, &cmd, NULL);
+ else if (flash->chip->feature_bits & FEATURE_4BA_ENTER_WREN)
+ ret = spi_simple_write_cmd(flash, cmd, 0);
+ else if (flash->chip->feature_bits & FEATURE_4BA_ENTER_EAR7)
+ ret = spi_set_extended_address(flash, enter ? 0x80 : 0x00);
+
+ if (!ret)
+ flash->in_4ba_mode = enter;
+ return ret;
+}
+
+static int spi_enter_4ba(struct flashctx *const flash)
+{
+ return spi_enter_exit_4ba(flash, true);
+}
+
+static int spi_exit_4ba(struct flashctx *flash)
+{
+ return spi_enter_exit_4ba(flash, false);
+}
+
+int spi_prepare_4ba(struct flashctx *const flash, const enum preparation_steps prep)
+{
+ if (prep != PREPARE_FULL)
+ return 0;
+
+ flash->address_high_byte = -1;
+ flash->in_4ba_mode = false;
+
+ /* Be careful about 4BA chips and broken masters */
+ if (flash->chip->total_size > 16 * 1024 && spi_master_no_4ba_modes(flash)) {
+ /* If we can't use native instructions, bail out */
+ if ((flash->chip->feature_bits & FEATURE_4BA_NATIVE) != FEATURE_4BA_NATIVE
+ || !spi_master_4ba(flash)) {
+ msg_cerr("Programmer doesn't support this chip. Aborting.\n");
+ return 1;
+ }
+ }
+
+ /* Enable/disable 4-byte addressing mode if flash chip supports it */
+ if (flash->chip->feature_bits & (FEATURE_4BA_ENTER | FEATURE_4BA_ENTER_WREN | FEATURE_4BA_ENTER_EAR7)) {
+ int ret;
+ if (spi_master_4ba(flash))
+ ret = spi_enter_4ba(flash);
+ else
+ ret = spi_exit_4ba(flash);
+ if (ret) {
+ msg_cerr("Failed to set correct 4BA mode! Aborting.\n");
+ return 1;
+ }
+ }
+
+ return 0;
+}