Add -E option for chip erase, remove duplicated code

Corresponding to flashrom svn r30 and coreboot v2 svn r1815.
diff --git a/flash_rom.c b/flash_rom.c
index bf338cc..35a9f50 100644
--- a/flash_rom.c
+++ b/flash_rom.c
@@ -194,7 +194,8 @@
 	FILE *image;
 	struct flashchip *flash;
 	int opt;
-	int read_it = 0, write_it = 0, verify_it = 0, verbose = 0;
+	int read_it = 0, write_it = 0, erase_it = 0, verify_it = 0,
+		verbose = 0;
 	char *filename = NULL;
 
 
@@ -211,7 +212,7 @@
 
 	setbuf(stdout, NULL);
 
-	while ((opt = getopt(argc, argv, "rwvVc:s:e:")) != EOF) {
+	while ((opt = getopt(argc, argv, "rwvVEc:s:e:")) != EOF) {
 		switch (opt) {
 		case 'r':
 			read_it = 1;
@@ -228,6 +229,9 @@
 		case 'V':
 			verbose = 1;
 			break;
+		case 'E':
+			erase_it = 1;
+			break;
 		case 's':
 			tempstr = strdup(optarg);
 			sscanf(tempstr,"%x",&exclude_start_position);
@@ -266,14 +270,18 @@
 	}
 
 	printf("Part is %s\n", flash->name);
-	if (!filename) {
+	if (!filename && !erase_it) {
 		printf("OK, only ENABLING flash write, but NOT FLASHING\n");
 		return 0;
 	}
 	size = flash->total_size * 1024;
 	buf = (char *) calloc(size, sizeof(char));
 
-	if (read_it) {
+	if (erase_it) {
+		printf("Erasing flash chip\n");
+		flash->erase(flash);
+		exit(0);		
+	} else if (read_it) {
 		if ((image = fopen(filename, "w")) == NULL) {
 			perror(filename);
 			exit(1);
diff --git a/jedec.c b/jedec.c
index 8cab944..353b17a 100644
--- a/jedec.c
+++ b/jedec.c
@@ -62,26 +62,19 @@
 
 int erase_sector_jedec(volatile unsigned char *bios, unsigned int page)
 {
-	volatile unsigned char *Temp;
-
 	/*  Issue the Sector Erase command   */
-	Temp = bios + 0x5555;	/* set up address to be BASE:5555h      */
-	*Temp = 0xAA;		/* write data 0xAA to the address       */
+	*(volatile char *) (bios + 0x5555) = 0xAA;
 	myusec_delay(10);
-	Temp = bios + 0x2AAA;	/* set up address to be BASE:2AAAh      */
-	*Temp = 0x55;		/* write data 0x55 to the address       */
+	*(volatile char *) (bios + 0x2AAA) = 0x55;
 	myusec_delay(10);
-	Temp = bios + 0x5555;	/* set up address to be BASE:5555h      */
-	*Temp = 0x80;		/* write data 0x80 to the address       */
+	*(volatile char *) (bios + 0x5555) = 0x80;
 	myusec_delay(10);
-	Temp = bios + 0x5555;	/* set up address to be BASE:5555h      */
-	*Temp = 0xAA;		/* write data 0xAA to the address       */
+
+	*(volatile char *) (bios + 0x5555) = 0xAA;
 	myusec_delay(10);
-	Temp = bios + 0x2AAA;	/* set up address to be BASE:2AAAh      */
-	*Temp = 0x55;		/* write data 0x55 to the address       */
+	*(volatile char *) (bios + 0x2AAA) = 0x55;
 	myusec_delay(10);
-	Temp = bios + page;	/* set up address to be the current sector */
-	*Temp = 0x30;		/* write data 0x30 to the address       */
+	*(volatile char *) (bios + page)   = 0x30;
 	myusec_delay(10);
 
 	/* wait for Toggle bit ready         */
@@ -92,26 +85,19 @@
 
 int erase_block_jedec(volatile unsigned char *bios, unsigned int block)
 {
-	volatile unsigned char *Temp;
-
 	/*  Issue the Sector Erase command   */
-	Temp = bios + 0x5555;	/* set up address to be BASE:5555h      */
-	*Temp = 0xAA;		/* write data 0xAA to the address       */
+	*(volatile char *) (bios + 0x5555) = 0xAA;
 	myusec_delay(10);
-	Temp = bios + 0x2AAA;	/* set up address to be BASE:2AAAh      */
-	*Temp = 0x55;		/* write data 0x55 to the address       */
+	*(volatile char *) (bios + 0x2AAA) = 0x55;
 	myusec_delay(10);
-	Temp = bios + 0x5555;	/* set up address to be BASE:5555h      */
-	*Temp = 0x80;		/* write data 0x80 to the address       */
+	*(volatile char *) (bios + 0x5555) = 0x80;
 	myusec_delay(10);
-	Temp = bios + 0x5555;	/* set up address to be BASE:5555h      */
-	*Temp = 0xAA;		/* write data 0xAA to the address       */
+
+	*(volatile char *) (bios + 0x5555) = 0xAA;
 	myusec_delay(10);
-	Temp = bios + 0x2AAA;	/* set up address to be BASE:2AAAh      */
-	*Temp = 0x55;		/* write data 0x55 to the address       */
+	*(volatile char *) (bios + 0x2AAA) = 0x55;
 	myusec_delay(10);
-	Temp = bios + block;	/* set up address to be the current sector */
-	*Temp = 0x50;		/* write data 0x30 to the address       */
+	*(volatile char *) (bios + block)  = 0x50;
 	myusec_delay(10);
 
 	/* wait for Toggle bit ready         */
@@ -123,26 +109,20 @@
 int erase_chip_jedec(struct flashchip *flash)
 {
 	volatile unsigned char *bios = flash->virt_addr;
-	volatile unsigned char *Temp;
 
 	/*  Issue the JEDEC Chip Erase command   */
-	Temp = bios + 0x5555;	/* set up address to be BASE:5555h      */
-	*Temp = 0xAA;		/* write data 0xAA to the address       */
+	*(volatile char *) (bios + 0x5555) = 0xAA;
 	myusec_delay(10);
-	Temp = bios + 0x2AAA;	/* set up address to be BASE:2AAAh      */
-	*Temp = 0x55;		/* write data 0x55 to the address       */
+	*(volatile char *) (bios + 0x2AAA) = 0x55;
 	myusec_delay(10);
-	Temp = bios + 0x5555;	/* set up address to be BASE:5555h      */
-	*Temp = 0x80;		/* write data 0x80 to the address       */
+	*(volatile char *) (bios + 0x5555) = 0x80;
 	myusec_delay(10);
-	Temp = bios + 0x5555;	/* set up address to be BASE:5555h      */
-	*Temp = 0xAA;		/* write data 0xAA to the address       */
+
+	*(volatile char *) (bios + 0x5555) = 0xAA;
 	myusec_delay(10);
-	Temp = bios + 0x2AAA;	/* set up address to be BASE:2AAAh      */
-	*Temp = 0x55;		/* write data 0x55 to the address       */
+	*(volatile char *) (bios + 0x2AAA) = 0x55;
 	myusec_delay(10);
-	Temp = bios + 0x5555;	/* set up address to be BASEy:5555h     */
-	*Temp = 0x10;		/* write data 0x10 to the address       */
+	*(volatile char *) (bios + 0x5555) = 0x10;
 	myusec_delay(10);
 
 	toggle_ready_jedec(bios);
diff --git a/pm49fl004.c b/pm49fl004.c
index a3f42fa..afcd55a 100644
--- a/pm49fl004.c
+++ b/pm49fl004.c
@@ -27,43 +27,13 @@
 #include "jedec.h"
 #include "pm49fl004.h"
 
-static __inline__ int erase_block_49fl004(volatile unsigned char *bios,
-					  unsigned long address)
-{
-	volatile unsigned char *Temp;
-
-	Temp = bios + 0x5555;	/* set up address to be C000:5555h      */
-	*Temp = 0xAA;		/* write data 0xAA to the address       */
-	myusec_delay(10);
-	Temp = bios + 0x2AAA;	/* set up address to be C000:2AAAh      */
-	*Temp = 0x55;		/* write data 0x55 to the address       */
-	myusec_delay(10);
-	Temp = bios + 0x5555;	/* set up address to be C000:5555h      */
-	*Temp = 0x80;		/* write data 0x80 to the address       */
-	myusec_delay(10);
-	Temp = bios + 0x5555;	/* set up address to be C000:5555h      */
-	*Temp = 0xAA;		/* write data 0xAA to the address       */
-	myusec_delay(10);
-	Temp = bios + 0x2AAA;	/* set up address to be C000:2AAAh      */
-	*Temp = 0x55;		/* write data 0x55 to the address       */
-	myusec_delay(10);
-	Temp = bios + address;	/* set up address to be C000:5555h      */
-	*Temp = 0x50;		/* write data 0x50 to the address       */
-
-	/* wait for Toggle bit ready         */
-	toggle_ready_jedec(bios);
-
-	return (0);
-}
-
-
 extern int exclude_start_page, exclude_end_page;
 
 int write_49fl004(struct flashchip *flash, unsigned char *buf)
 {
 	int i;
 	int total_size = flash->total_size * 1024, page_size =
-	    flash->page_size;
+		flash->page_size;
 	volatile char *bios = flash->virt_addr;
 	
 	printf("Programming Page: ");
@@ -72,7 +42,7 @@
 			continue;
 		
 		/* erase the page before programming */
-		erase_block_49fl004(bios, i * page_size);
+		erase_block_jedec(bios, i * page_size);
 
 		/* write to the sector */
 		printf("%04d at address: 0x%08x", i, i * page_size);
diff --git a/sst39sf020.c b/sst39sf020.c
index 401247a..97e87a1 100644
--- a/sst39sf020.c
+++ b/sst39sf020.c
@@ -51,22 +51,18 @@
 {
 	int i;
 	int total_size = flash->total_size * 1024, page_size =
-	    flash->page_size;
+		flash->page_size;
 	volatile char *bios = flash->virt_addr;
 
 	erase_chip_jedec(flash);
 
 	printf("Programming Page: ");
 	for (i = 0; i < total_size / page_size; i++) {
-		/* erase the page before programming */
-		//erase_sector_39sf020(bios, i * page_size);
-
 		/* write to the sector */
 		printf("%04d at address: 0x%08x", i, i * page_size);
 		write_sector_jedec(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");
 		fflush(stdout);
 	}
 	printf("\n");
diff --git a/sst_fwhub.c b/sst_fwhub.c
index d9dd467..c064ed6 100644
--- a/sst_fwhub.c
+++ b/sst_fwhub.c
@@ -47,38 +47,14 @@
 }
 
 /* probe_jedec works fine for probing */
-
 int probe_sst_fwhub(struct flashchip *flash)
 {
-	volatile unsigned char *bios = flash->virt_addr;
-	unsigned char id1, id2;
+	volatile unsigned char *bios;
 	size_t size = flash->total_size * 1024;
-	/* Issue JEDEC Product ID Entry command */
-	*(volatile char *) (bios + 0x5555) = 0xAA;
-	myusec_delay(10);
-	*(volatile char *) (bios + 0x2AAA) = 0x55;
-	myusec_delay(10);
-	*(volatile char *) (bios + 0x5555) = 0x90;
-	myusec_delay(10);
 
-	/* Read product ID */
-	id1 = *(volatile unsigned char *) bios;
-	id2 = *(volatile unsigned char *) (bios + 0x01);
-
-	/* Issue JEDEC Product ID Exit command */
-	*(volatile char *) (bios + 0x5555) = 0xAA;
-	myusec_delay(10);
-	*(volatile char *) (bios + 0x2AAA) = 0x55;
-	myusec_delay(10);
-	*(volatile char *) (bios + 0x5555) = 0xF0;
-	myusec_delay(10);
-
-	printf("%s: id1 0x%x, id2 0x%x\n", __FUNCTION__, id1, id2);
-	if (id1 != flash->manufacture_id || id2 != flash->model_id)
+	if (probe_jedec(flash) == 0)
 		return 0;
 
-	myusec_delay(10);
-
 	bios = mmap(0, size, PROT_WRITE | PROT_READ, MAP_SHARED,
 		    flash->fd_mem, (off_t) (0xFFFFFFFF - 0x400000 - size + 1));
 	if (bios == MAP_FAILED) {
@@ -91,23 +67,15 @@
 	return 1;
 }
 
-unsigned char wait_sst_fwhub(volatile unsigned char *bios)
-{
-	toggle_ready_jedec(bios);
-	return 0;
-}
-
 int erase_sst_fwhub_block(struct flashchip *flash, int offset)
 {
 	volatile unsigned char *wrprotect = flash->virt_addr_2 + offset + 2;
-	unsigned char status;
 
-	//printf("Erase at %p\n", bios);
 	// clear write protect
 	*(wrprotect) = 0;
 
 	erase_block_jedec(flash->virt_addr, offset);
-	status = wait_sst_fwhub(flash->virt_addr);
+	toggle_ready_jedec(flash->virt_addr);
 
 	return (0);
 }
@@ -122,19 +90,6 @@
 	return (0);
 }
 
-void write_page_sst_fwhub(volatile char *bios, char *src,
-			  volatile char *dst, int page_size)
-{
-	int i;
-
-	for (i = 0; i < page_size; i++) {
-		/* transfer data from source to destination */
-		write_byte_program_jedec(bios, src, dst);
-		src++;
-		dst++;
-	}
-}
-
 int write_sst_fwhub(struct flashchip *flash, unsigned char *buf)
 {
 	int i;
@@ -150,8 +105,8 @@
 	printf("Programming Page: ");
 	for (i = 0; i < total_size / page_size; i++) {
 		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);
+		write_sector_jedec(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("\n");
diff --git a/w49f002u.c b/w49f002u.c
index c6200d0..5325076 100644
--- a/w49f002u.c
+++ b/w49f002u.c
@@ -35,27 +35,20 @@
 int write_49f002(struct flashchip *flash, unsigned char *buf)
 {
 	int i;
-	int total_size = flash->total_size * 1024;
+	int total_size = flash->total_size * 1024, page_size =
+		flash->page_size;
 	volatile char *bios = flash->virt_addr;
-	volatile char *dst = bios;
 
 	erase_chip_jedec(flash);
 
 	printf("Programming Page: ");
 	for (i = 0; i < total_size; i++) {
 		/* write to the sector */
-		if ((i & 0xfff) == 0)
-			printf("address: 0x%08lx", (unsigned long) i);
-		*(bios + 0x5555) = 0xAA;
-		*(bios + 0x2AAA) = 0x55;
-		*(bios + 0x5555) = 0xA0;
-		*dst++ = *buf++;
-
-		/* wait for Toggle bit ready */
-		toggle_ready_jedec(dst);
-
-		if ((i & 0xfff) == 0)
-			printf("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b");
+		printf("%04d at address: 0x%08x ", i, i * page_size);
+		write_sector_jedec(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\b");
+		fflush(stdout);
 	}
 	printf("\n");