>From cbf64c01c778afc5b580dee0fb992e71af65f587 Mon Sep 17 00:00:00 2001
From: Sean Nelson <audiohacked@gmail.com>
Date: Mon, 15 Mar 2010 15:45:47 -0700
Subject: [PATCH] convert _sst_fwhub functions to jedec
 kill unused sst_fwhub.c functions
 make unlock_* check if unlock was successful and only return 0 when fully successful

---
 chipdrivers.h |    5 +--
 flashchips.c  |   32 +++++++++++-------
 sst_fwhub.c   |  103 +++++----------------------------------------------------
 3 files changed, 30 insertions(+), 110 deletions(-)

diff --git a/chipdrivers.h b/chipdrivers.h
index 029c82a..ca82c2d 100644
--- a/chipdrivers.h
+++ b/chipdrivers.h
@@ -101,31 +101,28 @@ int erase_chip_28sf040(struct flashchip *flash, unsigned int addr, unsigned int
 int erase_sector_28sf040(struct flashchip *flash, unsigned int address, unsigned int sector_size);
 int write_28sf040(struct flashchip *flash, uint8_t *buf);
 
 /* sst49lfxxxc.c */
 int probe_49lfxxxc(struct flashchip *flash);
 int erase_49lfxxxc(struct flashchip *flash);
 int erase_sector_49lfxxxc(struct flashchip *flash, unsigned int address, unsigned int sector_size);
 int erase_block_49lfxxxc(struct flashchip *flash, unsigned int address, unsigned int sector_size);
 int erase_chip_49lfxxxc(struct flashchip *flash, unsigned int addr, unsigned int blocksize);
 int write_49lfxxxc(struct flashchip *flash, uint8_t *buf);
 int unlock_49lfxxxc(struct flashchip *flash);
 
 /* sst_fwhub.c */
-int erase_sst_fwhub(struct flashchip *flash);
-int erase_sst_fwhub_block(struct flashchip *flash, unsigned int offset, unsigned int page_size);
-int erase_sst_fwhub_sector(struct flashchip *flash, unsigned int offset, unsigned int page_size);
-int write_sst_fwhub(struct flashchip *flash, uint8_t *buf);
 int printlock_sst_fwhub(struct flashchip *flash);
+int unlock_sst_fwhub(struct flashchip *flash);
 
 /* w39v040c.c */
 int printlock_w39v040c(struct flashchip *flash);
 
 /* w39V080fa.c */
 int unlock_winbond_fwhub(struct flashchip *flash);
 
 /* w29ee011.c */
 int probe_w29ee011(struct flashchip *flash);
 
 /* stm50flw0x0x.c */
 int probe_stm50flw0x0x(struct flashchip *flash);
 int erase_stm50flw0x0x(struct flashchip *flash);
diff --git a/flashchips.c b/flashchips.c
index 727d69d..bc9b295 100644
--- a/flashchips.c
+++ b/flashchips.c
@@ -4074,100 +4074,106 @@ struct flashchip flashchips[] = {
 		.bustype	= CHIP_BUSTYPE_FWH, /* A/A Mux */
 		.manufacture_id	= SST_ID,
 		.model_id	= SST_49LF002A,
 		.total_size	= 256,
 		.page_size	= 16 * 1024,
 		.feature_bits	= FEATURE_REGISTERMAP | FEATURE_EITHER_RESET,
 		.tested		= TEST_OK_PRW,
 		.probe		= probe_jedec,
 		.probe_timing	= 1,		/* 150 ns */
 		.block_erasers	=
 		{
 			{
 				.eraseblocks = { {4 * 1024, 64} },
-				.block_erase = erase_sst_fwhub_sector,
+				.block_erase = erase_sector_jedec,
 			}, {
 				.eraseblocks = { {16 * 1024, 16} },
-				.block_erase = erase_sst_fwhub_block,
+				.block_erase = erase_block_jedec,
 			}, {
 				.eraseblocks = { {256 * 1024, 1} },
 				.block_erase = NULL, /* AA 55 80 AA 55 10, only in A/A mux mode */
 			}
 		},
 		.printlock	= printlock_sst_fwhub,
-		.write		= write_sst_fwhub,
+		.printlock	= printlock_sst_fwhub,
+		.unlock		= unlock_sst_fwhub,
+		.write		= write_jedec_1,
 		.read		= read_memmapped,
 	},
 
 	{
 		.vendor		= "SST",
 		.name		= "SST49LF003A/B",
 		.bustype	= CHIP_BUSTYPE_FWH, /* A/A Mux */
 		.manufacture_id	= SST_ID,
 		.model_id	= SST_49LF003A,
 		.total_size	= 384,
 		.page_size	= 64 * 1024,
 		.feature_bits	= FEATURE_REGISTERMAP | FEATURE_EITHER_RESET,
 		.tested		= TEST_OK_PR,
 		.probe		= probe_jedec,
 		.probe_timing	= 1,		/* 150 ns */
 		.block_erasers	=
 		{
 			{
 				.eraseblocks = { {4 * 1024, 96} },
-				.block_erase = erase_sst_fwhub_sector,
+				.block_erase = erase_sector_jedec,
 			}, {
 				.eraseblocks = { {64 * 1024, 6} },
-				.block_erase = erase_sst_fwhub_block,
+				.block_erase = erase_block_jedec,
 			}, {
 				.eraseblocks = { {384 * 1024, 1} },
 				.block_erase = NULL, /* AA 55 80 AA 55 10, only in A/A mux mode */
 			}
 		},
 		.printlock	= printlock_sst_fwhub,
-		.write		= write_sst_fwhub,
+		.printlock	= printlock_sst_fwhub,
+		.unlock		= unlock_sst_fwhub,
+		.write		= write_jedec_1,
 		.read		= read_memmapped,
 	},
 
 	{
 		/* Contrary to the data sheet, TBL# on the SST49LF004B affects the top 128kB (instead of 64kB)
 		 * and is only honored for 64k block erase, but not 4k sector erase.
 		 */
 		.vendor		= "SST",
 		.name		= "SST49LF004A/B",
 		.bustype	= CHIP_BUSTYPE_FWH, /* A/A Mux */
 		.manufacture_id	= SST_ID,
 		.model_id	= SST_49LF004A,
 		.total_size	= 512,
 		.page_size	= 64 * 1024,
 		.feature_bits	= FEATURE_REGISTERMAP | FEATURE_EITHER_RESET,
 		.tested		= TEST_OK_PREW,
 		.probe		= probe_jedec,
 		.probe_timing	= 1,		/* 150 ns */
 		.block_erasers	=
 		{
 			{
 				.eraseblocks = { {4 * 1024, 128} },
-				.block_erase = erase_sector_jedec, /* missing unlock */
+				.block_erase = erase_sector_jedec,
 			}, {
 				.eraseblocks = { {64 * 1024, 8} },
-				.block_erase = erase_sst_fwhub_block, /* same as erase_block_jedec, but with unlock */
+				.block_erase = erase_block_jedec,
 			}, {
 				.eraseblocks = { {512 * 1024, 1} },
 				.block_erase = NULL, /* AA 55 80 AA 55 10, only in A/A mux mode */
 			},
 		},
 		.printlock	= printlock_sst_fwhub,
-		.write		= write_sst_fwhub,
+		.printlock	= printlock_sst_fwhub,
+		.unlock		= unlock_sst_fwhub,
+		.write		= write_jedec_1,
 		.read		= read_memmapped,
 	},
 
 	{
 		.vendor		= "SST",
 		.name		= "SST49LF004C",
 		.bustype	= CHIP_BUSTYPE_FWH,
 		.manufacture_id	= SST_ID,
 		.model_id	= SST_49LF004C,
 		.total_size	= 512,
 		.page_size	= 4 * 1024,
 		.feature_bits	= FEATURE_REGISTERMAP,
 		.tested		= TEST_UNTESTED,
@@ -4198,37 +4204,39 @@ struct flashchip flashchips[] = {
 		.bustype	= CHIP_BUSTYPE_FWH, /* A/A Mux */
 		.manufacture_id	= SST_ID,
 		.model_id	= SST_49LF008A,
 		.total_size	= 1024,
 		.page_size	= 64 * 1024,
 		.feature_bits	= FEATURE_REGISTERMAP | FEATURE_EITHER_RESET,
 		.tested		= TEST_OK_PRW,
 		.probe		= probe_jedec,
 		.probe_timing	= 1,		/* 150 ns */
 		.block_erasers	=
 		{
 			{
 				.eraseblocks = { {4 * 1024, 256} },
-				.block_erase = erase_sst_fwhub_sector,
+				.block_erase = erase_sector_jedec,
 			}, {
 				.eraseblocks = { {64 * 1024, 16} },
-				.block_erase = erase_sst_fwhub_block,
+				.block_erase = erase_block_jedec,
 			}, {
 				.eraseblocks = { {1024 * 1024, 1} },
 				.block_erase = NULL, /* AA 55 80 AA 55 10, only in A/A mux mode */
 			}
 		},
 		.printlock	= printlock_sst_fwhub,
-		.write		= write_sst_fwhub,
+		.printlock	= printlock_sst_fwhub,
+		.unlock		= unlock_sst_fwhub,
+		.write		= write_jedec_1,
 		.read		= read_memmapped,
 	},
 
 	{
 		.vendor		= "SST",
 		.name		= "SST49LF008C",
 		.bustype	= CHIP_BUSTYPE_FWH,
 		.manufacture_id	= SST_ID,
 		.model_id	= SST_49LF008C,
 		.total_size	= 1024,
 		.page_size	= 4 * 1024,
 		.feature_bits	= FEATURE_REGISTERMAP,
 		.tested		= TEST_UNTESTED,
diff --git a/sst_fwhub.c b/sst_fwhub.c
index a325278..328d39a 100644
--- a/sst_fwhub.c
+++ b/sst_fwhub.c
@@ -17,38 +17,26 @@
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
  */
 
 /* Adapted from the Intel FW hub stuff for 82802ax parts. */
 
 #include <stdlib.h>
 #include <string.h>
 #include "flash.h"
 #include "chipdrivers.h"
 
-// I need that Berkeley bit-map printer
-void print_sst_fwhub_status(uint8_t status)
-{
-	printf("%s", status & 0x80 ? "Ready:" : "Busy:");
-	printf("%s", status & 0x40 ? "BE SUSPEND:" : "BE RUN/FINISH:");
-	printf("%s", status & 0x20 ? "BE ERROR:" : "BE OK:");
-	printf("%s", status & 0x10 ? "PROG ERR:" : "PROG OK:");
-	printf("%s", status & 0x8 ? "VP ERR:" : "VPP OK:");
-	printf("%s", status & 0x4 ? "PROG SUSPEND:" : "PROG RUN/FINISH:");
-	printf("%s", status & 0x2 ? "WP|TBL#|WP#,ABORT:" : "UNLOCK:");
-}
-
 int check_sst_fwhub_block_lock(struct flashchip *flash, int offset)
 {
 	chipaddr registers = flash->virtual_registers;
 	uint8_t blockstatus;
 
 	blockstatus = chip_readb(registers + offset + 2);
 	printf_debug("Lock status for 0x%06x (size 0x%06x) is %02x, ",
 		     offset, flash->page_size, blockstatus);
 	switch (blockstatus & 0x3) {
 	case 0x0:
 		printf_debug("full access\n");
 		break;
 	case 0x1:
@@ -83,101 +71,28 @@ int clear_sst_fwhub_block_lock(struct flashchip *flash, int offset)
 	return blockstatus;
 }
 
 int printlock_sst_fwhub(struct flashchip *flash)
 {
 	int i;
 
 	for (i = 0; i < flash->total_size * 1024; i += flash->page_size)
 		check_sst_fwhub_block_lock(flash, i);
 
 	return 0;
 }
 
-int erase_sst_fwhub_block(struct flashchip *flash, unsigned int offset, unsigned int page_size)
-{
-	uint8_t blockstatus = clear_sst_fwhub_block_lock(flash, offset);
-
-	if (blockstatus) {
-		printf("Block lock clearing failed, not erasing block "
-			"at 0x%06x\n", offset);
-		return 1;
-	}
-
-	if (erase_block_jedec(flash, offset, page_size)) {
-		fprintf(stderr, "ERASE FAILED!\n");
-		return -1;
-	}
-	toggle_ready_jedec(flash->virtual_memory);
-
-	return 0;
-}
-
-int erase_sst_fwhub_sector(struct flashchip *flash, unsigned int offset, unsigned int page_size)
-{
-	uint8_t blockstatus = clear_sst_fwhub_block_lock(flash, offset);
-
-	if (blockstatus) {
-		printf("Sector lock clearing failed, not erasing sector "
-			"at 0x%06x\n", offset);
-		return 1;
-	}
-
-	if (erase_sector_jedec(flash, offset, page_size)) {
-		fprintf(stderr, "ERASE FAILED!\n");
-		return -1;
-	}
-	toggle_ready_jedec(flash->virtual_memory);
-
-	return 0;
-}
-
-int erase_sst_fwhub(struct flashchip *flash)
+int unlock_sst_fwhub(struct flashchip *flash)
 {
-	int i;
-	unsigned int total_size = flash->total_size * 1024;
+	int i, ret=0;
 
-	for (i = 0; i < total_size; i += flash->page_size) {
-		if (erase_sst_fwhub_block(flash, i, flash->page_size)) {
-			fprintf(stderr, "ERASE FAILED!\n");
-			return -1;
+	for (i = 0; i < flash->total_size * 1024; i += flash->page_size)
+	{
+		if (clear_sst_fwhub_block_lock(flash, i))
+		{
+			msg_cdbg("Warning: Unlock Failed for block 0x%06x\n", i);
+			ret++;
 		}
 	}
-
-	return 0;
+	return ret;
 }
 
-int write_sst_fwhub(struct flashchip *flash, uint8_t *buf)
-{
-	int i, rc;
-	int total_size = flash->total_size * 1024;
-	int page_size = flash->page_size;
-	chipaddr bios = flash->virtual_memory;
-	uint8_t *readbuf = malloc(page_size);
-	
-	printf("Programming page: ");
-	for (i = 0; i < total_size / page_size; i++) {
-		printf("%04d at address: 0x%08x", i, i * page_size);
-
-		/* Auto Skip Blocks, which already contain the desired data:
-		 * Faster, because we only write, what has changed
-		 * More secure, because blocks, which are excluded
-		 * (with the exclude or layout feature)
-		 * are not erased and rewritten; data is retained also
-		 * in sudden power off situations
-		 */
-		flash->read(flash, readbuf, i * page_size, page_size);
-		if (memcmp((void *)(buf + i * page_size),
-			   (void *)(readbuf), page_size)) {
-			rc = erase_sst_fwhub_block(flash, i * page_size,
-						   page_size);
-			if (rc)
-				return 1;
-			write_sector_jedec_common(flash, buf + i * page_size,
-					   bios + i * page_size, page_size, 0xffff);
-		}
-		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");
-
-	return 0;
-}
-- 
1.6.6

