Flashrom only checks for very few chips if the erase worked

And even when it checks if the erase worked, the result of that check is
often ignored.

Convert all erase functions and actually check return codes
almost everywhere.
Check inside all erase_* routines if erase worked, not outside.
erase_sector_jedec and erase_block_jedec have changed prototypes to
enable erase checking.

Uwe successfully tested LPC on an CK804 box and SPI on some SB600 box.

Corresponding to flashrom svn r595.

Signed-off-by: Carl-Daniel Hailfinger <c-d.hailfinger.devel.2006@gmx.net>
Signed-off-by: Urja Rannikko <urjaman@gmail.com>
Acked-by: Uwe Hermann <uwe@hermann-uwe.de>
diff --git a/jedec.c b/jedec.c
index 26e9caa..711a56d 100644
--- a/jedec.c
+++ b/jedec.c
@@ -175,8 +175,10 @@
 	return 0;
 }
 
-int erase_sector_jedec(chipaddr bios, unsigned int page)
+int erase_sector_jedec(struct flashchip *flash, unsigned int page, int pagesize)
 {
+	chipaddr bios = flash->virtual_memory;
+
 	/*  Issue the Sector Erase command   */
 	chip_writeb(0xAA, bios + 0x5555);
 	programmer_delay(10);
@@ -195,11 +197,17 @@
 	/* wait for Toggle bit ready         */
 	toggle_ready_jedec(bios);
 
+	if (check_erased_range(flash, page, pagesize)) {
+		fprintf(stderr,"ERASE FAILED!\n");
+		return -1;
+	}
 	return 0;
 }
 
-int erase_block_jedec(chipaddr bios, unsigned int block)
+int erase_block_jedec(struct flashchip *flash, unsigned int block, int blocksize)
 {
+	chipaddr bios = flash->virtual_memory;
+
 	/*  Issue the Sector Erase command   */
 	chip_writeb(0xAA, bios + 0x5555);
 	programmer_delay(10);
@@ -218,11 +226,16 @@
 	/* wait for Toggle bit ready         */
 	toggle_ready_jedec(bios);
 
+	if (check_erased_range(flash, block, blocksize)) {
+		fprintf(stderr,"ERASE FAILED!\n");
+		return -1;
+	}
 	return 0;
 }
 
 int erase_chip_jedec(struct flashchip *flash)
 {
+	int total_size = flash->total_size * 1024;
 	chipaddr bios = flash->virtual_memory;
 
 	/*  Issue the JEDEC Chip Erase command   */
@@ -242,6 +255,10 @@
 
 	toggle_ready_jedec(bios);
 
+	if (check_erased_range(flash, 0, total_size)) {
+		fprintf(stderr,"ERASE FAILED!\n");
+		return -1;
+	}
 	return 0;
 }
 
@@ -342,15 +359,11 @@
 	int page_size = flash->page_size;
 	chipaddr bios = flash->virtual_memory;
 
-	erase_chip_jedec(flash);
-	// dumb check if erase was successful.
-	for (i = 0; i < total_size; i++) {
-		if (chip_readb(bios + i) != 0xff) {
-			printf("ERASE FAILED @%d, val %02x!\n", i, chip_readb(bios + i));
-			return -1;
-		}
+	if (erase_chip_jedec(flash)) {
+		fprintf(stderr,"ERASE FAILED!\n");
+		return -1;
 	}
-
+	
 	printf("Programming page: ");
 	for (i = 0; i < total_size / page_size; i++) {
 		printf("%04d at address: 0x%08x", i, i * page_size);