spi: Pass master instead of flash to .send_command

In the SPI-master API, `.send_command` should only forward commands to
the SPI bus. All details about the commands and the SPI slave should be
handled in the chip driver. Hence, replace the `flashctx` pointer with
one to the `spi_master` to enforce proper separation.

Change-Id: I50934a1294217794b7e23cc98ade7e4279c059a1
Signed-off-by: Nico Huber <nico.h@gmx.de>
Reviewed-on: https://review.sourcearcade.org/c/flashprog/+/74897
Reviewed-by: Arthur Heymans <arthur@aheymans.xyz>
diff --git a/amd_spi100.c b/amd_spi100.c
index c37ed07..e011ed5 100644
--- a/amd_spi100.c
+++ b/amd_spi100.c
@@ -101,11 +101,11 @@
 	return 0;
 }
 
-static int spi100_send_command(const struct flashctx *const flash,
+static int spi100_send_command(const struct spi_master *const mst,
 			       const unsigned int writecnt, const unsigned int readcnt,
 			       const unsigned char *const writearr, unsigned char *const readarr)
 {
-	const struct spi100 *const spi100 = flash->mst.spi->data;
+	const struct spi100 *const spi100 = mst->data;
 
 	int ret = spi100_check_readwritecnt(writecnt, readcnt);
 	if (ret)
diff --git a/bitbang_spi.c b/bitbang_spi.c
index 33ea050..eb6988f 100644
--- a/bitbang_spi.c
+++ b/bitbang_spi.c
@@ -89,11 +89,11 @@
 	}
 }
 
-static int bitbang_spi_send_command(const struct flashctx *flash,
+static int bitbang_spi_send_command(const struct spi_master *,
 				    unsigned int writecnt, unsigned int readcnt,
 				    const unsigned char *writearr,
 				    unsigned char *readarr);
-static int bitbang_spi_send_multicommand(const struct flashctx *, struct spi_command *);
+static int bitbang_spi_send_multicommand(const struct spi_master *, struct spi_command *);
 static int bitbang_spi_shutdown(void *data);
 
 static const struct spi_master spi_master_bitbang = {
@@ -245,13 +245,13 @@
 	}
 }
 
-static int bitbang_spi_send_command(const struct flashctx *flash,
+static int bitbang_spi_send_command(const struct spi_master *mst,
 				    unsigned int writecnt, unsigned int readcnt,
 				    const unsigned char *writearr,
 				    unsigned char *readarr)
 {
 	unsigned int i;
-	const struct bitbang_spi_master_data *data = flash->mst.spi->data;
+	const struct bitbang_spi_master_data *data = mst->data;
 	const struct bitbang_spi_master *master = data->mst;
 
 	/* FIXME: Run bitbang_spi_request_bus here or in programmer init?
@@ -275,9 +275,9 @@
 	return 0;
 }
 
-static int bitbang_spi_send_multicommand(const struct flashctx *flash, struct spi_command *cmds)
+static int bitbang_spi_send_multicommand(const struct spi_master *mst, struct spi_command *cmds)
 {
-	const struct bitbang_spi_master_data *const bbs = flash->mst.spi->data;
+	const struct bitbang_spi_master_data *const bbs = mst->data;
 	int ret = 0;
 
 	bitbang_spi_request_bus(bbs->mst, bbs->spi_data);
diff --git a/buspirate_spi.c b/buspirate_spi.c
index d12c100..9d2d3f5 100644
--- a/buspirate_spi.c
+++ b/buspirate_spi.c
@@ -131,9 +131,9 @@
 	return ret;
 }
 
-static int buspirate_spi_send_command_v1(const struct flashctx *flash, unsigned int writecnt, unsigned int readcnt,
+static int buspirate_spi_send_command_v1(const struct spi_master *, unsigned int writecnt, unsigned int readcnt,
 					 const unsigned char *writearr, unsigned char *readarr);
-static int buspirate_spi_send_command_v2(const struct flashctx *flash, unsigned int writecnt, unsigned int readcnt,
+static int buspirate_spi_send_command_v2(const struct spi_master *, unsigned int writecnt, unsigned int readcnt,
 					 const unsigned char *writearr, unsigned char *readarr);
 static int buspirate_spi_shutdown(void *data);
 
@@ -614,10 +614,11 @@
 	return ret;
 }
 
-static int buspirate_spi_send_command_v1(const struct flashctx *flash, unsigned int writecnt, unsigned int readcnt,
+static int buspirate_spi_send_command_v1(const struct spi_master *mst,
+					 unsigned int writecnt, unsigned int readcnt,
 					 const unsigned char *writearr, unsigned char *readarr)
 {
-	struct bp_spi_data *bp_data = flash->mst.spi->data;
+	struct bp_spi_data *bp_data = mst->data;
 	unsigned int i = 0;
 	int ret = 0;
 
@@ -670,10 +671,11 @@
 	return ret;
 }
 
-static int buspirate_spi_send_command_v2(const struct flashctx *flash, unsigned int writecnt, unsigned int readcnt,
+static int buspirate_spi_send_command_v2(const struct spi_master *mst,
+					 unsigned int writecnt, unsigned int readcnt,
 					 const unsigned char *writearr, unsigned char *readarr)
 {
-	struct bp_spi_data *bp_data = flash->mst.spi->data;
+	struct bp_spi_data *bp_data = mst->data;
 	int i = 0, ret = 0;
 
 	if (writecnt > 4096 || readcnt > 4096 || (readcnt + writecnt) > 4096)
diff --git a/ch341a_spi.c b/ch341a_spi.c
index 00c2cc7..b031caa 100644
--- a/ch341a_spi.c
+++ b/ch341a_spi.c
@@ -335,7 +335,9 @@
 	stored_delay_us += usecs;
 }
 
-static int ch341a_spi_spi_send_command(const struct flashctx *flash, unsigned int writecnt, unsigned int readcnt, const unsigned char *writearr, unsigned char *readarr)
+static int ch341a_spi_spi_send_command(const struct spi_master *mst,
+				       unsigned int writecnt, unsigned int readcnt,
+				       const unsigned char *writearr, unsigned char *readarr)
 {
 	if (handle == NULL)
 		return -1;
diff --git a/ch347_spi.c b/ch347_spi.c
index 7772fe0..cd334da 100644
--- a/ch347_spi.c
+++ b/ch347_spi.c
@@ -176,10 +176,10 @@
 	return 0;
 }
 
-static int ch347_spi_send_command(const struct flashctx *flash, unsigned int writecnt,
+static int ch347_spi_send_command(const struct spi_master *mst, unsigned int writecnt,
 		unsigned int readcnt, const unsigned char *writearr, unsigned char *readarr)
 {
-	struct ch347_spi_data *ch347_data = flash->mst.spi->data;
+	struct ch347_spi_data *ch347_data = mst->data;
 	int ret = 0;
 
 	ch347_cs_control(ch347_data, CH347_CS_ASSERT | CH347_CS_CHANGE, CH347_CS_IGNORE);
diff --git a/dediprog.c b/dediprog.c
index af9e449..c114f7b 100644
--- a/dediprog.c
+++ b/dediprog.c
@@ -841,21 +841,21 @@
 	return dediprog_spi_write_chunked(flash, buf, start, len, WRITE_MODE_2B_AAI);
 }
 
-static int dediprog_spi_send_command(const struct flashctx *flash,
+static int dediprog_spi_send_command(const struct spi_master *mst,
 				     unsigned int writecnt,
 				     unsigned int readcnt,
 				     const unsigned char *writearr,
 				     unsigned char *readarr)
 {
-	struct dediprog_data *const dp_data = flash->mst.spi->data;
+	struct dediprog_data *dp_data = mst->data;
 	int ret;
 
 	msg_pspew("%s, writecnt=%i, readcnt=%i\n", __func__, writecnt, readcnt);
-	if (writecnt > flash->mst.spi->max_data_write + 5) {
+	if (writecnt > mst->max_data_write + 5) {
 		msg_perr("Invalid writecnt=%i, aborting.\n", writecnt);
 		return 1;
 	}
-	if (readcnt > flash->mst.spi->max_data_read) {
+	if (readcnt > mst->max_data_read) {
 		msg_perr("Invalid readcnt=%i, aborting.\n", readcnt);
 		return 1;
 	}
diff --git a/digilent_spi.c b/digilent_spi.c
index ee4d497..9ca0329 100644
--- a/digilent_spi.c
+++ b/digilent_spi.c
@@ -257,7 +257,7 @@
 	return 0;
 }
 
-static int digilent_spi_send_command(const struct flashctx *flash, unsigned int writecnt, unsigned int readcnt,
+static int digilent_spi_send_command(const struct spi_master *mst, unsigned int writecnt, unsigned int readcnt,
 				     const unsigned char *writearr, unsigned char *readarr)
 {
 	int ret;
diff --git a/dirtyjtag_spi.c b/dirtyjtag_spi.c
index e648cfa..c3c768e 100644
--- a/dirtyjtag_spi.c
+++ b/dirtyjtag_spi.c
@@ -170,11 +170,11 @@
 	return dirtyjtag_send(context, tms_reset_buffer, sizeof(tms_reset_buffer));
 }
 
-static int dirtyjtag_djtag1_spi_send_command(const struct flashctx *flash,
+static int dirtyjtag_djtag1_spi_send_command(const struct spi_master *mst,
 					     unsigned int writecnt, unsigned int readcnt,
 					     const unsigned char *writearr, unsigned char *readarr)
 {
-	struct dirtyjtag_spi_data *context = flash->mst.spi->data;
+	struct dirtyjtag_spi_data *context = mst->data;
 	const size_t max_xfer_size = 30; // max transfer size in DJTAG1
 	size_t len = writecnt + readcnt;
 	size_t num_xfer = (len + max_xfer_size - 1 ) / max_xfer_size; // ceil(len/max_xfer_size)
@@ -220,11 +220,11 @@
 	return -1;
 }
 
-static int dirtyjtag_djtag2_spi_send_command(const struct flashctx *flash,
+static int dirtyjtag_djtag2_spi_send_command(const struct spi_master *mst,
 					     unsigned int writecnt, unsigned int readcnt,
 					     const unsigned char *writearr, unsigned char *readarr)
 {
-	struct dirtyjtag_spi_data *const context = flash->mst.spi->data;
+	struct dirtyjtag_spi_data *const context = mst->data;
 	const size_t max_xfer_size = 62; /* max transfer size in DJTAG2 */
 	uint8_t transfer_buffer[2 + max_xfer_size]; /* 1B command + 1B len + payload */
 	size_t i;
diff --git a/dummyflasher.c b/dummyflasher.c
index 948dc0b..a1e15b0 100644
--- a/dummyflasher.c
+++ b/dummyflasher.c
@@ -100,7 +100,7 @@
 
 
 
-static int dummy_spi_send_command(const struct flashctx *flash, unsigned int writecnt, unsigned int readcnt,
+static int dummy_spi_send_command(const struct spi_master *, unsigned int writecnt, unsigned int readcnt,
 				  const unsigned char *writearr, unsigned char *readarr);
 static int dummy_spi_write_256(struct flashctx *flash, const uint8_t *buf,
 			       unsigned int start, unsigned int len);
@@ -1143,13 +1143,12 @@
 	return 0;
 }
 
-static int dummy_spi_send_command(const struct flashctx *flash, unsigned int writecnt,
-				  unsigned int readcnt,
-				  const unsigned char *writearr,
-				  unsigned char *readarr)
+static int dummy_spi_send_command(const struct spi_master *mst,
+				  unsigned int writecnt, unsigned int readcnt,
+				  const unsigned char *writearr, unsigned char *readarr)
 {
 	unsigned int i;
-	struct emu_data *emu_data = flash->mst.spi->data;
+	struct emu_data *emu_data = mst->data;
 	if (!emu_data) {
 		msg_perr("No data in flash context!\n");
 		return 1;
diff --git a/ft2232_spi.c b/ft2232_spi.c
index a82153d..6c6d49b 100644
--- a/ft2232_spi.c
+++ b/ft2232_spi.c
@@ -214,9 +214,9 @@
 }
 
 /* Returns 0 upon success, a negative number upon errors. */
-static int ft2232_spi_send_multicommand(const struct flashctx *flash, struct spi_command *cmds)
+static int ft2232_spi_send_multicommand(const struct spi_master *mst, struct spi_command *cmds)
 {
-	struct ft2232_data *spi_data = flash->mst.spi->data;
+	struct ft2232_data *spi_data = mst->data;
 	struct ftdi_context *ftdic = &spi_data->ftdi_context;
 	static unsigned char buf[FTDI_HW_BUFFER_SIZE];
 	size_t i = 0;
diff --git a/ft4222_spi.c b/ft4222_spi.c
index 46fa698..9709154 100644
--- a/ft4222_spi.c
+++ b/ft4222_spi.c
@@ -484,11 +484,11 @@
 }
 
 static int ft4222_spi_send_command(
-		const struct flashctx *const flash,
+		const struct spi_master *const mst,
 		const unsigned int writecnt, const unsigned int readcnt,
 		const unsigned char *const writearr, unsigned char *const readarr)
 {
-	struct ft4222 *const ft4222 = flash->mst.spi->data;
+	struct ft4222 *const ft4222 = mst->data;
 	int ret, poll_ret;
 
 	ret = ft4222_spi_set_io_lines(ft4222, 1);
@@ -606,14 +606,14 @@
 	return ret ? ret : poll_ret;
 }
 
-static int ft4222_spi_send_multicommand(const struct flashctx *flash, struct spi_command *cmds)
+static int ft4222_spi_send_multicommand(const struct spi_master *mst, struct spi_command *cmds)
 {
-	struct ft4222 *const ft4222 = flash->mst.spi->data;
+	struct ft4222 *const ft4222 = mst->data;
 
 	for (; !spi_is_empty(cmds); ++cmds) {
 		int ret;
 		if (cmds->io_mode == SINGLE_IO_1_1_1) {
-			ret = ft4222_spi_send_command(flash, spi_write_len(cmds),
+			ret = ft4222_spi_send_command(mst, spi_write_len(cmds),
 					spi_read_len(cmds), cmds->writearr, cmds->readarr);
 		} else {
 			ret = ft4222_spi_send_multi_io(ft4222, cmds);
diff --git a/ichspi.c b/ichspi.c
index 0a32778..36e1dce 100644
--- a/ichspi.c
+++ b/ichspi.c
@@ -294,7 +294,7 @@
 static int find_preop(OPCODES *op, uint8_t preop);
 static int generate_opcodes(OPCODES * op);
 static int program_opcodes(OPCODES *op, int enable_undo);
-static int run_opcode(const struct flashctx *flash, OPCODE op, uint32_t offset,
+static int run_opcode(const struct spi_master *, OPCODE op, uint32_t offset,
 		      uint8_t datalength, uint8_t * data);
 
 /* for pairing opcodes with their required preop */
@@ -1013,11 +1013,11 @@
 	return 0;
 }
 
-static int run_opcode(const struct flashctx *flash, OPCODE op, uint32_t offset,
+static int run_opcode(const struct spi_master *spi, OPCODE op, uint32_t offset,
 		      uint8_t datalength, uint8_t * data)
 {
 	/* max_data_read == max_data_write for all Intel/VIA SPI masters */
-	uint8_t maxlength = flash->mst.spi->max_data_read;
+	uint8_t maxlength = spi->max_data_read;
 
 	if (ich_generation == CHIPSET_ICH_UNKNOWN) {
 		msg_perr("%s: unsupported chipset\n", __func__);
@@ -1037,10 +1037,9 @@
 		return ich9_run_opcode(op, offset, datalength, data);
 }
 
-static int ich_spi_send_command(const struct flashctx *flash, unsigned int writecnt,
-				unsigned int readcnt,
-				const unsigned char *writearr,
-				unsigned char *readarr)
+static int ich_spi_send_command(const struct spi_master *spi,
+				unsigned int writecnt, unsigned int readcnt,
+				const unsigned char *writearr, unsigned char *readarr)
 {
 	int result;
 	int opcode_index = -1;
@@ -1140,7 +1139,7 @@
 		}
 	}
 
-	result = run_opcode(flash, *opcode, addr, count, data);
+	result = run_opcode(spi, *opcode, addr, count, data);
 	if (result) {
 		msg_pdbg("Running OPCODE 0x%02x failed ", opcode->opcode);
 		if ((opcode->spi_type == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS) ||
@@ -1437,8 +1436,7 @@
 	return 0;
 }
 
-static int ich_spi_send_multicommand(const struct flashctx *flash,
-				     struct spi_command *cmds)
+static int ich_spi_send_multicommand(const struct spi_master *spi, struct spi_command *cmds)
 {
 	int ret = 0;
 	int i;
@@ -1489,7 +1487,7 @@
 			 * preoppos matched, this is a normal opcode.
 			 */
 		}
-		ret = ich_spi_send_command(flash, spi_write_len(cmds), spi_read_len(cmds),
+		ret = ich_spi_send_command(spi, spi_write_len(cmds), spi_read_len(cmds),
 					   cmds->writearr, cmds->readarr);
 		/* Reset the type of all opcodes to non-atomic. */
 		for (i = 0; i < 8; i++)
diff --git a/include/programmer.h b/include/programmer.h
index df5f51f..5cef491 100644
--- a/include/programmer.h
+++ b/include/programmer.h
@@ -311,9 +311,9 @@
 	uint32_t features;
 	unsigned int max_data_read; // (Ideally,) maximum data read size in one go (excluding opcode+address).
 	unsigned int max_data_write; // (Ideally,) maximum data write size in one go (excluding opcode+address).
-	int (*command)(const struct flashctx *flash, unsigned int writecnt, unsigned int readcnt,
+	int (*command)(const struct spi_master *, unsigned int writecnt, unsigned int readcnt,
 		   const unsigned char *writearr, unsigned char *readarr);
-	int (*multicommand)(const struct flashctx *flash, struct spi_command *cmds);
+	int (*multicommand)(const struct spi_master *, struct spi_command *cmds);
 
 	/* Optimized functions for this master */
 	int (*read)(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len);
@@ -324,9 +324,9 @@
 	void *data;
 };
 
-int default_spi_send_command(const struct flashctx *flash, unsigned int writecnt, unsigned int readcnt,
+int default_spi_send_command(const struct spi_master *, unsigned int writecnt, unsigned int readcnt,
 			     const unsigned char *writearr, unsigned char *readarr);
-int default_spi_send_multicommand(const struct flashctx *flash, struct spi_command *cmds);
+int default_spi_send_multicommand(const struct spi_master *, struct spi_command *cmds);
 int default_spi_read(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len);
 int default_spi_write_256(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len);
 int default_spi_write_aai(struct flashctx *flash, const uint8_t *buf, unsigned int start, unsigned int len);
diff --git a/it87spi.c b/it87spi.c
index 70b1a8f..127270a 100644
--- a/it87spi.c
+++ b/it87spi.c
@@ -100,7 +100,7 @@
 	return;
 }
 
-static int it8716f_spi_send_command(const struct flashctx *flash,
+static int it8716f_spi_send_command(const struct spi_master *,
 				    unsigned int writecnt, unsigned int readcnt,
 				    const unsigned char *writearr,
 				    unsigned char *readarr);
@@ -274,7 +274,7 @@
  * commands with the address in inverse wire order. That's why the register
  * ordering in case 4 and 5 may seem strange.
  */
-static int it8716f_spi_send_command(const struct flashctx *flash,
+static int it8716f_spi_send_command(const struct spi_master *mst,
 				    unsigned int writecnt, unsigned int readcnt,
 				    const unsigned char *writearr,
 				    unsigned char *readarr)
diff --git a/jlink_spi.c b/jlink_spi.c
index 7534cbb..5ba40d5 100644
--- a/jlink_spi.c
+++ b/jlink_spi.c
@@ -104,12 +104,13 @@
 	return true;
 }
 
-static int jlink_spi_send_command(const struct flashctx *flash, unsigned int writecnt, unsigned int readcnt,
+static int jlink_spi_send_command(
+		const struct spi_master *mst, unsigned int writecnt, unsigned int readcnt,
 		const unsigned char *writearr, unsigned char *readarr)
 {
 	uint32_t length;
 	uint8_t *buffer;
-	struct jlink_spi_data *jlink_data = flash->mst.spi->data;
+	struct jlink_spi_data *jlink_data = mst->data;
 
 	length = writecnt + readcnt;
 
diff --git a/linux_spi.c b/linux_spi.c
index bf8d7f2..dda3808 100644
--- a/linux_spi.c
+++ b/linux_spi.c
@@ -50,10 +50,9 @@
 };
 
 static int linux_spi_shutdown(void *data);
-static int linux_spi_send_command(const struct flashctx *flash, unsigned int writecnt,
-				  unsigned int readcnt,
-				  const unsigned char *txbuf,
-				  unsigned char *rxbuf);
+static int linux_spi_send_command(
+		const struct spi_master *, unsigned int writecnt, unsigned int readcnt,
+		const unsigned char *txbuf, unsigned char *rxbuf);
 
 static const struct spi_master spi_master_linux = {
 	.features	= SPI_MASTER_4BA,
@@ -203,12 +202,11 @@
 	return 0;
 }
 
-static int linux_spi_send_command(const struct flashctx *flash, unsigned int writecnt,
-				  unsigned int readcnt,
-				  const unsigned char *txbuf,
-				  unsigned char *rxbuf)
+static int linux_spi_send_command(
+		const struct spi_master *mst, unsigned int writecnt, unsigned int readcnt,
+		const unsigned char *txbuf, unsigned char *rxbuf)
 {
-	struct linux_spi_data *spi_data = flash->mst.spi->data;
+	struct linux_spi_data *spi_data = mst->data;
 	int iocontrol_code;
 	struct spi_ioc_transfer msg[2] = {
 		{
diff --git a/mstarddc_spi.c b/mstarddc_spi.c
index b9ee23f..c55d743 100644
--- a/mstarddc_spi.c
+++ b/mstarddc_spi.c
@@ -149,11 +149,9 @@
 }
 
 /* Returns 0 upon success, a negative number upon errors. */
-static int mstarddc_spi_send_command(const struct flashctx *flash,
-				     unsigned int writecnt,
-				     unsigned int readcnt,
-				     const unsigned char *writearr,
-				     unsigned char *readarr)
+static int mstarddc_spi_send_command(
+		const struct spi_master *mst, unsigned int writecnt, unsigned int readcnt,
+		const unsigned char *writearr, unsigned char *readarr)
 {
 	int ret = 0;
 	uint8_t *cmd = malloc((writecnt + 1) * sizeof(uint8_t));
diff --git a/ni845x_spi.c b/ni845x_spi.c
index 177c799..21d39a8 100644
--- a/ni845x_spi.c
+++ b/ni845x_spi.c
@@ -567,7 +567,7 @@
 	return 0;
 }
 
-static int ni845x_spi_transmit(const struct flashctx *flash,
+static int ni845x_spi_transmit(const struct spi_master *mst,
 			       unsigned int write_cnt,
 			       unsigned int read_cnt,
 			       const unsigned char *write_arr,
diff --git a/pickit2_spi.c b/pickit2_spi.c
index 9a0691e..43126cc 100644
--- a/pickit2_spi.c
+++ b/pickit2_spi.c
@@ -198,11 +198,11 @@
 	return 0;
 }
 
-static int pickit2_spi_send_command(const struct flashctx *flash, unsigned int writecnt, unsigned int readcnt,
-				     const unsigned char *writearr, unsigned char *readarr)
+static int pickit2_spi_send_command(const struct spi_master *mst, unsigned int writecnt, unsigned int readcnt,
+				    const unsigned char *writearr, unsigned char *readarr)
 {
 	const unsigned int total_packetsize = writecnt + readcnt + 20;
-	struct pickit2_spi_data *pickit2_data = flash->mst.spi->data;
+	struct pickit2_spi_data *pickit2_data = mst->data;
 
 	/* Maximum number of bytes per transaction (including command overhead) is 64. Lets play it safe
 	 * and always assume the worst case scenario of 20 bytes command overhead.
diff --git a/sb600spi.c b/sb600spi.c
index 37da989..0a8bc31 100644
--- a/sb600spi.c
+++ b/sb600spi.c
@@ -56,9 +56,9 @@
 #define FIFO_SIZE_OLD		8
 #define FIFO_SIZE_YANGTZE	71
 
-static int sb600_spi_send_command(const struct flashctx *flash, unsigned int writecnt, unsigned int readcnt,
+static int sb600_spi_send_command(const struct spi_master *, unsigned int writecnt, unsigned int readcnt,
 				  const unsigned char *writearr, unsigned char *readarr);
-static int spi100_spi_send_command(const struct flashctx *flash, unsigned int writecnt, unsigned int readcnt,
+static int spi100_spi_send_command(const struct spi_master *, unsigned int writecnt, unsigned int readcnt,
 				  const unsigned char *writearr, unsigned char *readarr);
 
 static struct spi_master spi_master_sb600 = {
@@ -191,16 +191,16 @@
 }
 
 /* Check the number of bytes to be transmitted and extract opcode. */
-static int check_readwritecnt(const struct flashctx *flash, unsigned int writecnt, unsigned int readcnt)
+static int check_readwritecnt(const struct spi_master *spi, unsigned int writecnt, unsigned int readcnt)
 {
-	unsigned int maxwritecnt = flash->mst.spi->max_data_write + 3;
+	unsigned int maxwritecnt = spi->max_data_write + 3;
 	if (writecnt > maxwritecnt) {
 		msg_pinfo("%s: SPI controller can not send %d bytes, it is limited to %d bytes\n",
 			  __func__, writecnt, maxwritecnt);
 		return SPI_INVALID_LENGTH;
 	}
 
-	unsigned int maxreadcnt = flash->mst.spi->max_data_read;
+	unsigned int maxreadcnt = spi->max_data_read;
 	if (readcnt > maxreadcnt) {
 		msg_pinfo("%s: SPI controller can not receive %d bytes, it is limited to %d bytes\n",
 			  __func__, readcnt, maxreadcnt);
@@ -218,10 +218,8 @@
 	msg_pspew("done\n");
 }
 
-static int sb600_spi_send_command(const struct flashctx *flash, unsigned int writecnt,
-				  unsigned int readcnt,
-				  const unsigned char *writearr,
-				  unsigned char *readarr)
+static int sb600_spi_send_command(const struct spi_master *spi, unsigned int writecnt, unsigned int readcnt,
+				  const unsigned char *writearr, unsigned char *readarr)
 {
 	/* First byte is cmd which can not be sent through the FIFO. */
 	unsigned char cmd = *writearr++;
@@ -229,7 +227,7 @@
 	msg_pspew("%s, cmd=0x%02x, writecnt=%d, readcnt=%d\n", __func__, cmd, writecnt, readcnt);
 	mmio_writeb(cmd, sb600_spibar + 0);
 
-	int ret = check_readwritecnt(flash, writecnt, readcnt);
+	int ret = check_readwritecnt(spi, writecnt, readcnt);
 	if (ret != 0)
 		return ret;
 
@@ -304,10 +302,8 @@
 	return 0;
 }
 
-static int spi100_spi_send_command(const struct flashctx *flash, unsigned int writecnt,
-				  unsigned int readcnt,
-				  const unsigned char *writearr,
-				  unsigned char *readarr)
+static int spi100_spi_send_command(const struct spi_master *spi, unsigned int writecnt, unsigned int readcnt,
+				   const unsigned char *writearr, unsigned char *readarr)
 {
 	/* First byte is cmd which can not be sent through the buffer. */
 	unsigned char cmd = *writearr++;
@@ -315,7 +311,7 @@
 	msg_pspew("%s, cmd=0x%02x, writecnt=%d, readcnt=%d\n", __func__, cmd, writecnt, readcnt);
 	mmio_writeb(cmd, sb600_spibar + 0);
 
-	int ret = check_readwritecnt(flash, writecnt, readcnt);
+	int ret = check_readwritecnt(spi, writecnt, readcnt);
 	if (ret != 0)
 		return ret;
 
diff --git a/serprog.c b/serprog.c
index 6cf87f0..257562a 100644
--- a/serprog.c
+++ b/serprog.c
@@ -356,7 +356,7 @@
 	return 0;
 }
 
-static int serprog_spi_send_command(const struct flashctx *flash,
+static int serprog_spi_send_command(const struct spi_master *,
 				    unsigned int writecnt, unsigned int readcnt,
 				    const unsigned char *writearr,
 				    unsigned char *readarr);
@@ -989,7 +989,7 @@
 	sp_prev_was_write = 0;
 }
 
-static int serprog_spi_send_command(const struct flashctx *flash,
+static int serprog_spi_send_command(const struct spi_master *mst,
 				    unsigned int writecnt, unsigned int readcnt,
 				    const unsigned char *writearr,
 				    unsigned char *readarr)
diff --git a/spi.c b/spi.c
index 42a6a96..9f8fb89 100644
--- a/spi.c
+++ b/spi.c
@@ -28,7 +28,7 @@
 #include "spi.h"
 
 static int spi_send_wrapped_command(
-		const struct flashctx *flash, enum io_mode io_mode,
+		const struct spi_master *mst, enum io_mode io_mode,
 		unsigned int writecnt, unsigned int readcnt,
 		const unsigned char *writearr, unsigned char *readarr)
 {
@@ -44,7 +44,7 @@
 		NULL_SPI_CMD
 	};
 
-	return spi_send_multicommand(flash, cmd);
+	return mst->multicommand(mst, cmd);
 }
 
 int spi_send_command(const struct flashctx *flash, unsigned int writecnt,
@@ -52,34 +52,31 @@
 		     unsigned char *readarr)
 {
 	if (spi_current_io_mode(flash) != SINGLE_IO_1_1_1)
-		return spi_send_wrapped_command(flash, spi_current_io_mode(flash),
+		return spi_send_wrapped_command(flash->mst.spi, spi_current_io_mode(flash),
 						writecnt, readcnt, writearr, readarr);
 
-	return flash->mst.spi->command(flash, writecnt, readcnt, writearr,
-				       readarr);
+	return flash->mst.spi->command(flash->mst.spi, writecnt, readcnt, writearr, readarr);
 }
 
 int spi_send_multicommand(const struct flashctx *flash, struct spi_command *cmds)
 {
-	return flash->mst.spi->multicommand(flash, cmds);
+	return flash->mst.spi->multicommand(flash->mst.spi, cmds);
 }
 
-int default_spi_send_command(const struct flashctx *flash, unsigned int writecnt,
-			     unsigned int readcnt,
-			     const unsigned char *writearr,
-			     unsigned char *readarr)
+int default_spi_send_command(const struct spi_master *mst,
+			     unsigned int writecnt, unsigned int readcnt,
+			     const unsigned char *writearr, unsigned char *readarr)
 {
-	return spi_send_wrapped_command(flash, SINGLE_IO_1_1_1, writecnt, readcnt, writearr, readarr);
+	return spi_send_wrapped_command(mst, SINGLE_IO_1_1_1, writecnt, readcnt, writearr, readarr);
 }
 
-int default_spi_send_multicommand(const struct flashctx *flash,
-				  struct spi_command *cmds)
+int default_spi_send_multicommand(const struct spi_master *mst, struct spi_command *cmds)
 {
 	int result = 0;
 	for (; !spi_is_empty(cmds) && !result; cmds++) {
 		if (cmds->io_mode != SINGLE_IO_1_1_1)
 			return SPI_FLASHPROG_BUG;
-		result = spi_send_command(flash,
+		result = mst->command(mst,
 				spi_write_len(cmds), spi_read_len(cmds),
 				cmds->writearr, cmds->readarr);
 	}
diff --git a/stlinkv3_spi.c b/stlinkv3_spi.c
index b1c7457..50910ae 100644
--- a/stlinkv3_spi.c
+++ b/stlinkv3_spi.c
@@ -317,7 +317,7 @@
 	return 0;
 }
 
-static int stlinkv3_spi_transmit(const struct flashctx *flash,
+static int stlinkv3_spi_transmit(const struct spi_master *mst,
 				 unsigned int write_cnt,
 				 unsigned int read_cnt,
 				 const unsigned char *write_arr,
diff --git a/usbblaster_spi.c b/usbblaster_spi.c
index 5464c11..a003d5e 100644
--- a/usbblaster_spi.c
+++ b/usbblaster_spi.c
@@ -174,7 +174,8 @@
 }
 
 /* Returns 0 upon success, a negative number upon errors. */
-static int usbblaster_spi_send_command(const struct flashctx *flash, unsigned int writecnt, unsigned int readcnt,
+static int usbblaster_spi_send_command(const struct spi_master *mst,
+				       unsigned int writecnt, unsigned int readcnt,
 				       const unsigned char *writearr, unsigned char *readarr)
 {
 	uint8_t cmd;
diff --git a/wbsio_spi.c b/wbsio_spi.c
index 2b36d16..a37116b 100644
--- a/wbsio_spi.c
+++ b/wbsio_spi.c
@@ -59,10 +59,9 @@
 	return flashport;
 }
 
-static int wbsio_spi_send_command(const struct flashctx *flash, unsigned int writecnt,
-				  unsigned int readcnt,
-				  const unsigned char *writearr,
-				  unsigned char *readarr);
+static int wbsio_spi_send_command(const struct spi_master *,
+				  unsigned int writecnt, unsigned int readcnt,
+				  const unsigned char *writearr, unsigned char *readarr);
 static int wbsio_spi_read(struct flashctx *flash, uint8_t *buf,
 			  unsigned int start, unsigned int len);
 
@@ -117,10 +116,9 @@
  * Would one more byte of RAM in the chip (to get all 24 bits) really make
  * such a big difference?
  */
-static int wbsio_spi_send_command(const struct flashctx *flash, unsigned int writecnt,
-				  unsigned int readcnt,
-				  const unsigned char *writearr,
-				  unsigned char *readarr)
+static int wbsio_spi_send_command(const struct spi_master *mst,
+				  unsigned int writecnt, unsigned int readcnt,
+				  const unsigned char *writearr, unsigned char *readarr)
 {
 	unsigned int i;
 	uint8_t mode = 0;