Add prepare/finish_access() hooks for chip drivers

Some of the arrangements we perform in prepare_flash_access()
are actually specific to the flash chip. Allow to clean that
up by adding respective hooks into `struct flashchip`.

Change-Id: Iff79ba3d190dba04ecf58c5c53faa428bf592bdf
Signed-off-by: Nico Huber <nico.h@gmx.de>
Reviewed-on: https://review.sourcearcade.org/c/flashprog/+/72516
diff --git a/flashprog.c b/flashprog.c
index 8babb86..6a4ce12 100644
--- a/flashprog.c
+++ b/flashprog.c
@@ -1626,6 +1626,9 @@
 		return 1;
 	}
 
+	if (flash->chip->prepare_access && flash->chip->prepare_access(flash, PREPARE_FULL))
+		return 1;
+
 	if (map_flash(flash) != 0)
 		return 1;
 
@@ -1670,6 +1673,8 @@
 {
 	deregister_chip_restore(flash);
 	unmap_flash(flash);
+	if (flash->chip->finish_access)
+		flash->chip->finish_access(flash);
 }
 
 /**
diff --git a/include/flash.h b/include/flash.h
index 31d9a1c..7afdf32 100644
--- a/include/flash.h
+++ b/include/flash.h
@@ -210,6 +210,10 @@
 
 struct wp_bits;
 
+enum preparation_steps {
+	PREPARE_FULL,
+};
+
 struct flashchip {
 	const char *vendor;
 	const char *name;
@@ -324,6 +328,9 @@
 	/* Function that takes a set of WP config bits (e.g. BP, SEC, TB, etc) */
 	/* and determines what protection range they select. */
 	void (*decode_range)(size_t *start, size_t *len, const struct wp_bits *, size_t chip_len);
+
+	int (*prepare_access)(struct flashctx *, enum preparation_steps);
+	void (*finish_access)(struct flashctx *);
 };
 
 typedef int (*chip_restore_fn_cb_t)(struct flashctx *flash, uint8_t status);