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/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;