memory_bus: Move (un)map_flash_region into par master

Now that the map/unmap_flash functions are only called from memory-
mapped chip drivers, we can safely move the hooks into the parallel
masters.

This also allows us to move the code away from the globals in
`flashprog.c` into a new `memory_bus.c`.

Change-Id: Ic476cf4d96200232900537b997e1d07bb4e8b809
Signed-off-by: Nico Huber <nico.h@gmx.de>
Reviewed-on: https://review.sourcearcade.org/c/flashprog/+/72522
Reviewed-by: Riku Viitanen <riku.viitanen@protonmail.com>
diff --git a/flashprog.c b/flashprog.c
index e1049f3..a9bba8f 100644
--- a/flashprog.c
+++ b/flashprog.c
@@ -207,27 +207,6 @@
 	return ret;
 }
 
-void *programmer_map_flash_region(const char *descr, uintptr_t phys_addr, size_t len)
-{
-	void *ret;
-	if (programmer->map_flash_region)
-		ret = programmer->map_flash_region(descr, phys_addr, len);
-	else
-		ret = fallback_map(descr, phys_addr, len);
-	msg_gspew("%s: mapping %s from 0x%0*" PRIxPTR " to 0x%0*" PRIxPTR "\n",
-		  __func__, descr, PRIxPTR_WIDTH, phys_addr, PRIxPTR_WIDTH, (uintptr_t) ret);
-	return ret;
-}
-
-void programmer_unmap_flash_region(void *virt_addr, size_t len)
-{
-	if (programmer->unmap_flash_region)
-		programmer->unmap_flash_region(virt_addr, len);
-	else
-		fallback_unmap(virt_addr, len);
-	msg_gspew("%s: unmapped 0x%0*" PRIxPTR "\n", __func__, PRIxPTR_WIDTH, (uintptr_t)virt_addr);
-}
-
 void programmer_delay(unsigned int usecs)
 {
 	if (usecs > 0) {
@@ -546,61 +525,6 @@
 	return first_len;
 }
 
-void finish_memory_access(struct flashctx *flash)
-{
-	if (flash->virtual_registers != (chipaddr)ERROR_PTR) {
-		programmer_unmap_flash_region((void *)flash->virtual_registers, flash->chip->total_size * 1024);
-		flash->physical_registers = 0;
-		flash->virtual_registers = (chipaddr)ERROR_PTR;
-	}
-
-	if (flash->virtual_memory != (chipaddr)ERROR_PTR) {
-		programmer_unmap_flash_region((void *)flash->virtual_memory, flash->chip->total_size * 1024);
-		flash->physical_memory = 0;
-		flash->virtual_memory = (chipaddr)ERROR_PTR;
-	}
-}
-
-int prepare_memory_access(struct flashctx *flash, enum preparation_steps prep)
-{
-	/* Init pointers to the fail-safe state to distinguish them later from legit values. */
-	flash->virtual_memory = (chipaddr)ERROR_PTR;
-	flash->virtual_registers = (chipaddr)ERROR_PTR;
-
-	/* FIXME: This avoids mapping (and unmapping) of flash chip definitions with size 0.
-	 * These are used for various probing-related hacks that would not map successfully anyway and should be
-	 * removed ASAP. */
-	if (flash->chip->total_size == 0)
-		return 0;
-
-	const chipsize_t size = flash->chip->total_size * 1024;
-	uintptr_t base = flashbase ? flashbase : (0xffffffff - size + 1);
-	void *addr = programmer_map_flash_region(flash->chip->name, base, size);
-	if (addr == ERROR_PTR) {
-		msg_perr("Could not map flash chip %s at 0x%0*" PRIxPTR ".\n",
-			 flash->chip->name, PRIxPTR_WIDTH, base);
-		return 1;
-	}
-	flash->physical_memory = base;
-	flash->virtual_memory = (chipaddr)addr;
-
-	/* FIXME: Special function registers normally live 4 MByte below flash space, but it might be somewhere
-	 * completely different on some chips and programmers, or not mappable at all.
-	 * Ignore these problems for now and always report success. */
-	if (flash->chip->feature_bits & FEATURE_REGISTERMAP) {
-		base = 0xffffffff - size - 0x400000 + 1;
-		addr = programmer_map_flash_region("flash chip registers", base, size);
-		if (addr == ERROR_PTR) {
-			msg_pdbg2("Could not map flash chip registers %s at 0x%0*" PRIxPTR ".\n",
-				 flash->chip->name, PRIxPTR_WIDTH, base);
-			return 0;
-		}
-		flash->physical_registers = base;
-		flash->virtual_registers = (chipaddr)addr;
-	}
-	return 0;
-}
-
 /*
  * Return a string corresponding to the bustype parameter.
  * Memory is obtained with malloc() and must be freed with free() by the caller.
@@ -743,7 +667,7 @@
 		  flash->chip->vendor, flash->chip->name, flash->chip->total_size, tmp);
 	free(tmp);
 #if CONFIG_INTERNAL == 1
-	if (flash->physical_memory != 0 && programmer->map_flash_region == physmap)
+	if (flash->physical_memory != 0 && mst->par.map_flash == physmap)
 		msg_cinfo("mapped at physical address 0x%0*" PRIxPTR ".\n",
 			  PRIxPTR_WIDTH, flash->physical_memory);
 	else