memory_bus: Add infrastructure for per-bus probing

All the probing functions for the traditional memory-mapped chips,
parallel, LPC and FWH, are parameterized by additional chip proper-
ties like the chip size and feature bits like FEATURE_ADDR_SHIFTED.
Hence, we match against an extended `id_info' with chip size and
feature bits. For a match, all the feature bits assumed during
probing, need to be set for a given chip as well.

Change-Id: Id5c3d8933cd6aeaf87a090b6f0798d2a5746ee17
Signed-off-by: Nico Huber <nico.h@gmx.de>
Reviewed-on: https://review.sourcearcade.org/c/flashprog/+/440
diff --git a/include/chipdrivers/memory_bus.h b/include/chipdrivers/memory_bus.h
index 2a5e080..75501da 100644
--- a/include/chipdrivers/memory_bus.h
+++ b/include/chipdrivers/memory_bus.h
@@ -90,6 +90,18 @@
 int write_en29lv640b(struct flashprog_flashctx *, const uint8_t *buf, unsigned int start, unsigned int len);
 
 /* memory_bus.c */
+struct memory_chip_info {
+	chipsize_t chip_size;
+	feature_bits_t chip_features;
+};
+
+struct memory_found_id {
+	struct found_id generic;
+	struct memory_chip_info memory_info;
+};
+
+struct memory_found_id *alloc_memory_found_id(void);
+
 struct par_master;
 void *programmer_map_flash_data(const struct par_master *, chipsize_t, const char *descr);
 void programmer_unmap_flash_region(const struct par_master *, void *, chipsize_t);
diff --git a/include/flash.h b/include/flash.h
index b89df03..0f14037 100644
--- a/include/flash.h
+++ b/include/flash.h
@@ -108,6 +108,8 @@
 
 #define MAX_CHIP_RESTORE_FUNCTIONS 4
 
+typedef uint32_t feature_bits_t;
+
 /* Feature bits used for non-SPI only */
 #define FEATURE_LONG_RESET	(1 << 0)
 #define FEATURE_SHORT_RESET	(1 << 1)
@@ -255,7 +257,7 @@
 	unsigned int total_size;
 	/* Chip page size in bytes */
 	unsigned int page_size;
-	int feature_bits;
+	feature_bits_t feature_bits;
 
 	/* Indicate how well flashprog supports different operations of this flash chip. */
 	struct tested {
diff --git a/parallel.c b/parallel.c
index 1ef7305..b113670 100644
--- a/parallel.c
+++ b/parallel.c
@@ -19,8 +19,13 @@
  * GNU General Public License for more details.
  */
 
+#include <stdlib.h>
+#include <string.h>
+
 #include "flash.h"
 #include "programmer.h"
+#include "chipdrivers/probing.h"
+#include "chipdrivers/memory_bus.h"
 
 void chip_writeb(const struct flashctx *flash, uint8_t val, chipaddr addr)
 {
@@ -63,6 +68,27 @@
 	flash->mst.par->chip_readn(flash->mst.par, buf, addr, len);
 }
 
+struct memory_found_id *alloc_memory_found_id(void)
+{
+	struct memory_found_id *const found = calloc(1, sizeof(*found));
+	if (found)
+		found->generic.info.ext = &found->memory_info;
+	return found;
+}
+
+static const struct bus_probe memory_probes[] = {
+    /* prio. type		function		function argument */
+};
+
+static bool memory_probe_match(const struct flashprog_chip *chip, const struct id_info_ext *found)
+{
+	const struct memory_chip_info *const probe_info = found->ext;
+
+	return	(memcmp(&found->id, &chip->id, sizeof(chip->id)) == 0) &&
+		(probe_info->chip_size == chip->total_size * KiB) &&
+		(probe_info->chip_features == (probe_info->chip_features & chip->feature_bits));
+}
+
 int register_par_master(const struct par_master *mst, const enum chipbustype buses,
 			const uintptr_t rom_base, const size_t max_rom_decode, void *data)
 {
@@ -85,6 +111,9 @@
 	}
 
 	rmst.buses_supported = buses;
+	rmst.probing.probe_count = ARRAY_SIZE(memory_probes);
+	rmst.probing.probes = memory_probes;
+	rmst.probing.match = memory_probe_match;
 	rmst.par = *mst;
 
 	rmst.par.rom_base = rom_base;