spi: Use bus probing for RDID

We put 3 and 4 byte RDID into a single function. Only if we can't
read 4 bytes, we try again with 3. There are no conflicts because
the only RDID4 manufacturer ID contains the 0x7f prefix, hence it
can't match any 3-byte ID.

Change-Id: I5d35bc30255aae66da35d58431628512e50b39f0
Signed-off-by: Nico Huber <nico.h@gmx.de>
Reviewed-on: https://review.sourcearcade.org/c/flashprog/+/74900
diff --git a/spi.c b/spi.c
index f6ec250..fee3aee 100644
--- a/spi.c
+++ b/spi.c
@@ -23,6 +23,7 @@
 #include "flash.h"
 #include "flashchips.h"
 #include "chipdrivers/spi.h"
+#include "chipdrivers/probing.h"
 #include "programmer.h"
 #include "spi_command.h"
 #include "spi.h"
@@ -161,6 +162,28 @@
 	return true;
 }
 
+static const struct bus_probe spi_probes[] = {
+       /* type		function		function argument */
+	{ ID_SPI_RDID,	probe_spi_rdid,		NULL },
+};
+
+static bool spi_probe_match(const struct flashchip *chip, const struct id_info_ext *found)
+{
+	if (memcmp(&chip->id, &found->id, sizeof(found->id)) == 0)
+		return true;
+
+	/* Test if this is a pure vendor match. */
+	if (found->id.manufacture == chip->id.manufacture && GENERIC_DEVICE_ID == chip->id.model)
+		return true;
+
+	/* Test if there is any vendor ID. */
+	if (GENERIC_MANUF_ID == chip->id.manufacture &&
+	    found->id.manufacture != 0xff && found->id.manufacture != 0x00)
+		return true;
+
+	return false;
+}
+
 int register_spi_master(const struct spi_master *mst, size_t max_rom_decode, void *data)
 {
 	struct registered_master rmst = { 0 };
@@ -196,6 +219,9 @@
 	else
 		rmst.max_rom_decode = MAX_ROM_DECODE_UNLIMITED;
 	rmst.buses_supported = BUS_SPI;
+	rmst.probing.probe_count = ARRAY_SIZE(spi_probes);
+	rmst.probing.probes = spi_probes;
+	rmst.probing.match = spi_probe_match;
 	rmst.spi = *mst;
 	if (data)
 		rmst.spi.data = data;