Add retry to write_byte_program_jedec(), 99% success rate

Corresponding to flashrom svn r29 and coreboot v2 svn r1814.
diff --git a/flash_rom.c b/flash_rom.c
index c01f531..bf338cc 100644
--- a/flash_rom.c
+++ b/flash_rom.c
@@ -200,19 +200,6 @@
 
         unsigned int exclude_start_position=0, exclude_end_position=0; // [x,y)
 	char *tempstr=NULL;
-#if 0
-
-#if 1
-	/* Keep fallback image */
-	exclude_start_position = 0x60000;
-	exclude_end_position = 0x80000;
-#else 
-	/* Keep DMI etc. */
-        exclude_start_position = 0x60000;
-        exclude_end_position = 0x70000;
-#endif
-
-#endif
 
 	if (argc > 1) {
 		/* Yes, print them. */
diff --git a/jedec.c b/jedec.c
index 0344b69..8cab944 100644
--- a/jedec.c
+++ b/jedec.c
@@ -176,11 +176,14 @@
 int write_byte_program_jedec(volatile unsigned char *bios, unsigned char *src,
 			     volatile unsigned char *dst)
 {
+	int tried = 0;
+
 	/* If the data is 0xFF, don't program it */
 	if (*src == 0xFF) {
 		return 0;
 	}
 
+retry:
 	/* Issue JEDEC Byte Program command */
 	*(volatile unsigned char *) (bios + 0x5555) = 0xAA;
 	*(volatile unsigned char *) (bios + 0x2AAA) = 0x55;
@@ -188,9 +191,12 @@
 
 	/* transfer data from source to destination */
 	*dst = *src;
-
 	toggle_ready_jedec(bios);
 
+	if (*dst != *src && tried++ < 0x10) {
+ 		goto retry;
+ 	}
+
 	return 0;
 }
 
diff --git a/sst_fwhub.c b/sst_fwhub.c
index 71f5d33..d9dd467 100644
--- a/sst_fwhub.c
+++ b/sst_fwhub.c
@@ -79,10 +79,8 @@
 
 	myusec_delay(10);
 
-	// we need to mmap the write-protect space. 
-	printf("mapping control register at %x\n", 0 - 0x400000 -size);
 	bios = mmap(0, size, PROT_WRITE | PROT_READ, MAP_SHARED,
-		    flash->fd_mem, (off_t) (0 - 0x400000 - size));
+		    flash->fd_mem, (off_t) (0xFFFFFFFF - 0x400000 - size + 1));
 	if (bios == MAP_FAILED) {
 		// it's this part but we can't map it ...
 		perror("Error MMAP /dev/mem");
@@ -101,23 +99,16 @@
 
 int erase_sst_fwhub_block(struct flashchip *flash, int offset)
 {
-	volatile unsigned char *bios = flash->virt_addr + offset;
-	volatile unsigned char *wrprotect =
-	    flash->virt_addr_2 + offset + 2;
+	volatile unsigned char *wrprotect = flash->virt_addr_2 + offset + 2;
 	unsigned char status;
 
-	// clear status register
-	printf("Erase at %p\n", bios);
+	//printf("Erase at %p\n", bios);
 	// clear write protect
-	printf("write protect is at %p\n", (wrprotect));
-	printf("write protect is 0x%x\n", *(wrprotect));
 	*(wrprotect) = 0;
-	printf("write protect is 0x%x\n", *(wrprotect));
 
 	erase_block_jedec(flash->virt_addr, offset);
 	status = wait_sst_fwhub(flash->virt_addr);
-	//print_sst_fwhub_status(status);
-	printf("DONE BLOCK 0x%x\n", offset);
+
 	return (0);
 }
 
@@ -126,11 +117,8 @@
 	int i;
 	unsigned int total_size = flash->total_size * 1024;
 
-	printf("total_size is %d; flash->page_size is %d\n",
-	       total_size, flash->page_size);
 	for (i = 0; i < total_size; i += flash->page_size)
 		erase_sst_fwhub_block(flash, i);
-	printf("DONE ERASE\n");
 	return (0);
 }
 
@@ -145,7 +133,6 @@
 		src++;
 		dst++;
 	}
-
 }
 
 int write_sst_fwhub(struct flashchip *flash, unsigned char *buf)
@@ -165,10 +152,8 @@
 		printf("%04d at address: 0x%08x", i, i * page_size);
 		write_page_sst_fwhub(bios, buf + i * page_size,
 				     bios + i * page_size, page_size);
-		printf
-		    ("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b");
+		printf("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b");
 	}
 	printf("\n");
-	protect_sst_fwhub(bios);
 	return (0);
 }
diff --git a/sst_fwhub.h b/sst_fwhub.h
index 3569130..eb2b364 100644
--- a/sst_fwhub.h
+++ b/sst_fwhub.h
@@ -5,44 +5,4 @@
 extern int erase_sst_fwhub(struct flashchip *flash);
 extern int write_sst_fwhub(struct flashchip *flash, unsigned char *buf);
 
-extern __inline__ void toggle_ready_sst_fwhub(volatile char *dst)
-{
-	unsigned int i = 0;
-	char tmp1, tmp2;
-
-	tmp1 = *dst & 0x40;
-
-	while (i++ < 0xFFFFFF) {
-		tmp2 = *dst & 0x40;
-		if (tmp1 == tmp2) {
-			break;
-		}
-		tmp1 = tmp2;
-	}
-}
-
-extern __inline__ void data_polling_sst_fwhub(volatile char *dst, char data)
-{
-	unsigned int i = 0;
-	char tmp;
-
-	data &= 0x80;
-
-	while (i++ < 0xFFFFFF) {
-		tmp = *dst & 0x80;
-		if (tmp == data) {
-			break;
-		}
-	}
-}
-
-extern __inline__ void protect_sst_fwhub(volatile char *bios)
-{
-	*(volatile char *) (bios + 0x5555) = 0xAA;
-	*(volatile char *) (bios + 0x2AAA) = 0x55;
-	*(volatile char *) (bios + 0x5555) = 0xA0;
-
-	usleep(200);
-}
-
 #endif				/* !__SST_FWHUB_H__ */