spi: Use bus probing for ST95 RDID

As the SPI instruction used for probing conflicts with AT45DB chips,
let it only run at priority `1', when no flash chip was detected.

Change-Id: I61db0d6fa7be81d120bc84213c358498019dc52d
Signed-off-by: Nico Huber <nico.h@gmx.de>
Reviewed-on: https://review.sourcearcade.org/c/flashprog/+/420
diff --git a/flashchips.c b/flashchips.c
index 1722bc3..eb8e2fe 100644
--- a/flashchips.c
+++ b/flashchips.c
@@ -21278,7 +21278,7 @@
 		.feature_bits	= FEATURE_WRSR_WREN | FEATURE_NO_ERASE | FEATURE_ERASED_ZERO,
 		.tested		= TEST_OK_PREW,
 		.spi_cmd_set	= SPI95,
-		.probe		= probe_spi_st95,
+		.probe		= probe_buses,
 		.probe_timing	= TIMING_ZERO,
 		.block_erasers	=
 		{
diff --git a/include/chipdrivers/spi.h b/include/chipdrivers/spi.h
index 1646561..b664a19 100644
--- a/include/chipdrivers/spi.h
+++ b/include/chipdrivers/spi.h
@@ -119,7 +119,7 @@
 int spi_erase_at45cs_sector(struct flashprog_flashctx *, unsigned int addr, unsigned int blocklen);
 
 /* spi95.c */
-int probe_spi_st95(struct flashprog_flashctx *);
+struct found_id *probe_spi_st95(const struct bus_probe *, const struct master_common *);
 int spi_block_erase_emulation(struct flashprog_flashctx *, unsigned int addr, unsigned int blocklen);
 
 /* writeprotect_ranges.c */
diff --git a/spi.c b/spi.c
index da66a68..d674a6d 100644
--- a/spi.c
+++ b/spi.c
@@ -170,6 +170,8 @@
 	{ 0, ID_SPI_RES3,	probe_spi_res,		NULL },
 	{ 0, ID_SPI_RES2,	probe_spi_res,		NULL },
 	{ 0, ID_SPI_RES1,	probe_spi_res,		NULL },
+	{ 1, ID_SPI_ST95,	probe_spi_st95,		(void *)(uintptr_t)3 },
+	{ 1, ID_SPI_ST95,	probe_spi_st95,		(void *)(uintptr_t)2 },
 };
 
 static bool spi_probe_match(const struct flashchip *chip, const struct id_info_ext *found)
diff --git a/spi95.c b/spi95.c
index b7a7b21..1ffa00f 100644
--- a/spi95.c
+++ b/spi95.c
@@ -20,41 +20,42 @@
 #include <string.h>
 #include <stdlib.h>
 
-#include "flash.h"
-#include "flashchips.h"
 #include "chipdrivers/spi.h"
-#include "spi_command.h"
+#include "programmer.h"
 #include "spi.h"
 
 /* For ST95XXX chips which have RDID */
-int probe_spi_st95(struct flashctx *flash)
+struct found_id *probe_spi_st95(const struct bus_probe *probe, const struct master_common *mst)
 {
 	/*
 	 * ST_M95_RDID_OUTSIZE depends on size of the flash and
 	 * not all ST_M95XXX have RDID.
 	 */
 	static const unsigned char cmd[ST_M95_RDID_OUTSIZE_MAX] = { ST_M95_RDID };
+	const struct spi_master *const spi = (const struct spi_master *)mst;
+	const size_t address_len = (uintptr_t)probe->arg;
 	unsigned char readarr[ST_M95_RDID_INSIZE];
-	uint32_t id1, id2;
-	int ret;
 
-	uint32_t rdid_outsize = ST_M95_RDID_2BA_OUTSIZE; // 16 bit address
-	if (flash->chip->total_size * KiB > 64 * KiB)
-		rdid_outsize = ST_M95_RDID_3BA_OUTSIZE; // 24 bit address
+	if (spi->command(spi, 1 + address_len, sizeof(readarr), cmd, readarr))
+		return NULL;
+	if (flashprog_no_data(readarr, sizeof(readarr)))
+		return NULL;
 
-	ret = spi_send_command(flash, rdid_outsize, sizeof(readarr), cmd, readarr);
-	if (ret)
-		return 0;
+	struct found_id *const found = calloc(1, sizeof(*found));
+	if (!found) {
+		msg_cerr("Out of memory!\n");
+		return NULL;
+	}
 
-	id1 = readarr[0]; // manufacture id
-	id2 = (readarr[1] << 8) | readarr[2]; // SPI family code + model id
+	struct id_info *const id = &found->info.id;
 
-	msg_cdbg("%s: id1 0x%02x, id2 0x%02x\n", __func__, id1, id2);
+	id->manufacture	= readarr[0];
+	id->model	= (readarr[1] << 8) | readarr[2];
+	id->type	= ID_SPI_ST95;
 
-	if (id1 == flash->chip->id.manufacture && id2 == flash->chip->id.model)
-		return 1;
+	msg_cdbg("%s: id1 0x%02x, id2 0x%04x\n", __func__, id->id1, id->id2);
 
-	return 0;
+	return found;
 }
 
 /* ST95XXX chips don't have erase operation and erase is made as part of write command */