Unify chip write functions

The currently used write functions (wrappers) all use helpers which
perform the actual write (inner functions).

The signature of the write wrappers is: int write_chip(struct flashchip
*flash, uint8_t * buf);

The signature of the inner write functions varied a lot. This patch
changes them to: int write_part(struct flashchip *flash, uint8_t *src,
int start, int len);

Did you know that flashrom has only 8 inner write functions for all
flash chips? write_page_write_jedec_common write_sector_jedec_common
write_sector_28sf040 spi_chip_write_256_new spi_chip_write_1_new
spi_aai_write_new write_page_82802ab write_page_m29f400bt

Export all inner write functions.

Change the function signature of wait_82802ab to eliminate single-use
variables.

Remove an error message in write_page_m29f400bt which was printed for
every byte written regardless of success.

Add sharplhf00l04.c to the list of flash chip drivers in the Makefile.
While the functions in there are unused, I suspect we will need them
later, and by hooking the file up we ensure that compilation won't
break.

Corresponding to flashrom svn r1208.

Signed-off-by: Carl-Daniel Hailfinger <c-d.hailfinger.devel.2006@gmx.net>
Acked-by: Uwe Hermann <uwe@hermann-uwe.de>
diff --git a/82802ab.c b/82802ab.c
index 3fec376..ac42bb9 100644
--- a/82802ab.c
+++ b/82802ab.c
@@ -88,9 +88,10 @@
 	return 1;
 }
 
-uint8_t wait_82802ab(chipaddr bios)
+uint8_t wait_82802ab(struct flashchip *flash)
 {
 	uint8_t status;
+	chipaddr bios = flash->virtual_memory;
 
 	chip_writeb(0x70, bios);
 	if ((chip_readb(bios) & 0x80) == 0) {	// it's busy
@@ -132,7 +133,7 @@
 	programmer_delay(10);
 
 	// now let's see what the register is
-	status = wait_82802ab(bios);
+	status = wait_82802ab(flash);
 	print_status_82802ab(status);
 
 	if (check_erased_range(flash, page, pagesize)) {
@@ -143,26 +144,28 @@
 	return 0;
 }
 
-void write_page_82802ab(chipaddr bios, uint8_t *src,
-			chipaddr dst, int page_size)
+int write_page_82802ab(struct flashchip *flash, uint8_t *src, int start, int len)
 {
 	int i;
+	chipaddr dst = flash->virtual_memory + start;
 
-	for (i = 0; i < page_size; i++) {
+	for (i = 0; i < len; i++) {
 		/* transfer data from source to destination */
 		chip_writeb(0x40, dst);
 		chip_writeb(*src++, dst++);
-		wait_82802ab(bios);
+		wait_82802ab(flash);
 	}
+
+	/* FIXME: Ignore errors for now. */
+	return 0;
 }
 
 int write_82802ab(struct flashchip *flash, uint8_t *buf)
 {
 	int i;
-	chipaddr bios = flash->virtual_memory;
 
 	for (i = 0; i < flash->total_size; i++) {
-                write_page_82802ab(bios, buf + i * 1024, bios + i * 1024, 1024);
+                write_page_82802ab(flash, buf + i * 1024, i * 1024, 1024);
 	}
 
 	return 0;
diff --git a/Makefile b/Makefile
index c525e39..3215992 100644
--- a/Makefile
+++ b/Makefile
@@ -84,7 +84,7 @@
 
 CHIP_OBJS = jedec.o stm50flw0x0x.o w39v040c.o w39v080fa.o w29ee011.o \
 	sst28sf040.o m29f400bt.o 82802ab.o pm49fl00x.o \
-	sst49lfxxxc.o sst_fwhub.o flashchips.o spi.o spi25.o
+	sst49lfxxxc.o sst_fwhub.o flashchips.o spi.o spi25.o sharplhf00l04.o
 
 LIB_OBJS = layout.o
 
diff --git a/chipdrivers.h b/chipdrivers.h
index 60220e6..f19e991 100644
--- a/chipdrivers.h
+++ b/chipdrivers.h
@@ -65,12 +65,12 @@
 int spi_aai_write(struct flashchip *flash, uint8_t *buf);
 
 /* 82802ab.c */
-uint8_t wait_82802ab(chipaddr bios);
+uint8_t wait_82802ab(struct flashchip *flash);
 int probe_82802ab(struct flashchip *flash);
 int erase_block_82802ab(struct flashchip *flash, unsigned int page, unsigned int pagesize);
 int write_82802ab(struct flashchip *flash, uint8_t *buf);
 void print_status_82802ab(uint8_t status);
-void write_page_82802ab(chipaddr bios, uint8_t *src, chipaddr dst, int page_size);
+int write_page_82802ab(struct flashchip *flash, uint8_t *src, int start, int len);
 int unlock_82802ab(struct flashchip *flash);
 int unlock_28f004s5(struct flashchip *flash);
 
@@ -86,7 +86,8 @@
 int erase_sector_jedec(struct flashchip *flash, unsigned int page, unsigned int pagesize);
 int erase_block_jedec(struct flashchip *flash, unsigned int page, unsigned int blocksize);
 int erase_chip_block_jedec(struct flashchip *flash, unsigned int page, unsigned int blocksize);
-int write_sector_jedec_common(struct flashchip *flash, uint8_t *src, chipaddr dst, unsigned int page_size, unsigned int mask);
+int write_sector_jedec_common(struct flashchip *flash, uint8_t *src, int start, int len, unsigned int mask);
+int write_page_write_jedec_common(struct flashchip *flash, uint8_t *src, int start, int page_size, unsigned int mask);
 
 /* m29f400bt.c */
 int probe_m29f400bt(struct flashchip *flash);
@@ -94,8 +95,7 @@
 int block_erase_chip_m29f400bt(struct flashchip *flash, unsigned int start, unsigned int len);
 int write_m29f400bt(struct flashchip *flash, uint8_t *buf);
 void protect_m29f400bt(chipaddr bios);
-void write_page_m29f400bt(chipaddr bios, uint8_t *src,
-			  chipaddr dst, int page_size);
+int write_page_m29f400bt(struct flashchip *flash, uint8_t *src, int start, int len);
 
 /* pm49fl00x.c */
 int unlock_49fl00x(struct flashchip *flash);
@@ -105,6 +105,7 @@
 int erase_chip_28sf040(struct flashchip *flash, unsigned int addr, unsigned int blocklen);
 int erase_sector_28sf040(struct flashchip *flash, unsigned int address, unsigned int sector_size);
 int write_28sf040(struct flashchip *flash, uint8_t *buf);
+int write_sector_28sf040(struct flashchip *flash, uint8_t *src, int start, int len);
 
 /* sst49lfxxxc.c */
 int erase_sector_49lfxxxc(struct flashchip *flash, unsigned int address, unsigned int sector_size);
diff --git a/jedec.c b/jedec.c
index fea17b6..7e956e2 100644
--- a/jedec.c
+++ b/jedec.c
@@ -317,14 +317,14 @@
 	return failed;
 }
 
-int write_sector_jedec_common(struct flashchip *flash, uint8_t *src,
-		       chipaddr dst, unsigned int page_size, unsigned int mask)
+int write_sector_jedec_common(struct flashchip *flash, uint8_t *src, int start, int len, unsigned int mask)
 {
 	int i, failed = 0;
+	chipaddr dst = flash->virtual_memory + start;
 	chipaddr olddst;
 
 	olddst = dst;
-	for (i = 0; i < page_size; i++) {
+	for (i = 0; i < len; i++) {
 		if (write_byte_program_jedec_common(flash, src, dst, mask))
 			failed = 1;
 		dst++, src++;
@@ -335,8 +335,7 @@
 	return failed;
 }
 
-static int write_page_write_jedec_common(struct flashchip *flash, uint8_t *src,
-			   int start, int page_size, unsigned int mask)
+int write_page_write_jedec_common(struct flashchip *flash, uint8_t *src, int start, int page_size, unsigned int mask)
 {
 	int i, tried = 0, failed;
 	uint8_t *s = src;
@@ -403,8 +402,7 @@
 	mask = getaddrmask(flash);
 
 	for (i = 0; i < total_size / page_size; i++) {
-		if (write_page_write_jedec_common(flash, buf + i * page_size,
-					   i * page_size, page_size, mask))
+		if (write_page_write_jedec_common(flash, buf + i * page_size, i * page_size, page_size, mask))
 			failed = 1;
 	}
 
@@ -414,14 +412,12 @@
 int write_jedec_1(struct flashchip *flash, uint8_t * buf)
 {
 	int i;
-	chipaddr bios = flash->virtual_memory;
-	chipaddr dst = bios;
 	int mask;
 
 	mask = getaddrmask(flash);
 
 	for (i = 0; i < flash->total_size; i++) {
-                write_sector_jedec_common(flash, buf + i * 1024, dst + i * 1024, 1024, mask);
+		write_sector_jedec_common(flash, buf + i * 1024, i * 1024, 1024, mask);
 	}
 
 	return 0;
diff --git a/m29f400bt.c b/m29f400bt.c
index 3c00306..98e0762 100644
--- a/m29f400bt.c
+++ b/m29f400bt.c
@@ -27,12 +27,13 @@
    0x555 instead of 0x2AA. Do *not* blindly replace with standard JEDEC
    functions. */
 
-void write_page_m29f400bt(chipaddr bios, uint8_t *src,
-			  chipaddr dst, int page_size)
+int write_page_m29f400bt(struct flashchip *flash, uint8_t *src, int start, int len)
 {
 	int i;
+	chipaddr bios = flash->virtual_memory;
+	chipaddr dst = flash->virtual_memory + start;
 
-	for (i = 0; i < page_size; i++) {
+	for (i = 0; i < len; i++) {
 		chip_writeb(0xAA, bios + 0xAAA);
 		chip_writeb(0x55, bios + 0x555);
 		chip_writeb(0xA0, bios + 0xAAA);
@@ -40,11 +41,17 @@
 		/* transfer data from source to destination */
 		chip_writeb(*src, dst);
 		toggle_ready_jedec(dst);
+#if 0
+		/* We only want to print something in the error case. */
 		msg_cerr("Value in the flash at address 0x%lx = %#x, want %#x\n",
 		     (dst - bios), chip_readb(dst), *src);
+#endif
 		dst++;
 		src++;
 	}
+
+	/* FIXME: Ignore errors for now. */
+	return 0;
 }
 
 int probe_m29f400bt(struct flashchip *flash)
@@ -138,20 +145,15 @@
 	int i;
 	int total_size = flash->total_size * 1024;
 	int page_size = flash->page_size;
-	chipaddr bios = flash->virtual_memory;
 
 	for (i = 0; i < (total_size / page_size) - 1; i++) {
-		write_page_m29f400bt(bios, buf + i * page_size,
-				     bios + i * page_size, page_size);
+		write_page_m29f400bt(flash, buf + i * page_size, i * page_size, page_size);
 	}
 
-	write_page_m29f400bt(bios, buf + 0x70000, bios + 0x70000, 32 * 1024);
-
-	write_page_m29f400bt(bios, buf + 0x78000, bios + 0x78000, 8 * 1024);
-
-	write_page_m29f400bt(bios, buf + 0x7a000, bios + 0x7a000, 8 * 1024);
-
-	write_page_m29f400bt(bios, buf + 0x7c000, bios + 0x7c000, 16 * 1024);
+	write_page_m29f400bt(flash, buf + 0x70000, 0x70000, 32 * 1024);
+	write_page_m29f400bt(flash, buf + 0x78000, 0x78000, 8 * 1024);
+	write_page_m29f400bt(flash, buf + 0x7a000, 0x7a000, 8 * 1024);
+	write_page_m29f400bt(flash, buf + 0x7c000, 0x7c000, 16 * 1024);
 
 	return 0;
 }
diff --git a/sharplhf00l04.c b/sharplhf00l04.c
index d291573..bb3e359 100644
--- a/sharplhf00l04.c
+++ b/sharplhf00l04.c
@@ -23,6 +23,7 @@
 
 /* FIXME: The datasheet is unclear whether we should use toggle_ready_jedec
  * or wait_82802ab.
+ * FIXME: This file is unused.
  */
 
 int erase_lhf00l04_block(struct flashchip *flash, unsigned int blockaddr, unsigned int blocklen)
@@ -33,7 +34,7 @@
 
 	// clear status register
 	chip_writeb(0x50, bios);
-	status = wait_82802ab(flash->virtual_memory);
+	status = wait_82802ab(flash);
 	print_status_82802ab(status);
 	// clear write protect
 	msg_cspew("write protect is at 0x%lx\n", (wrprotect));
@@ -46,7 +47,7 @@
 	chip_writeb(0xd0, bios);
 	programmer_delay(10);
 	// now let's see what the register is
-	status = wait_82802ab(flash->virtual_memory);
+	status = wait_82802ab(flash);
 	print_status_82802ab(status);
 
 	if (check_erased_range(flash, blockaddr, blocklen)) {
@@ -61,11 +62,9 @@
 	int i;
 	int total_size = flash->total_size * 1024;
 	int page_size = flash->page_size;
-	chipaddr bios = flash->virtual_memory;
 
 	for (i = 0; i < total_size / page_size; i++) {
-		write_page_82802ab(bios, buf + i * page_size,
-				    bios + i * page_size, page_size);
+		write_page_82802ab(flash, buf + i * page_size, i * page_size, page_size);
 	}
 
 	return 0;
diff --git a/sst28sf040.c b/sst28sf040.c
index d81f831..2de876b 100644
--- a/sst28sf040.c
+++ b/sst28sf040.c
@@ -30,8 +30,10 @@
 #define RESET			0xFF
 #define READ_ID			0x90
 
-static void protect_28sf040(chipaddr bios)
+static void protect_28sf040(struct flashchip *flash)
 {
+	chipaddr bios = flash->virtual_memory;
+
 	chip_readb(bios + 0x1823);
 	chip_readb(bios + 0x1820);
 	chip_readb(bios + 0x1822);
@@ -41,8 +43,10 @@
 	chip_readb(bios + 0x040A);
 }
 
-static void unprotect_28sf040(chipaddr bios)
+static void unprotect_28sf040(struct flashchip *flash)
 {
+	chipaddr bios = flash->virtual_memory;
+
 	chip_readb(bios + 0x1823);
 	chip_readb(bios + 0x1820);
 	chip_readb(bios + 0x1822);
@@ -59,7 +63,7 @@
 	chip_writeb(AUTO_PG_ERASE1, bios);
 	chip_writeb(AUTO_PG_ERASE2, bios + address);
 
-	/* wait for Toggle bit ready         */
+	/* wait for Toggle bit ready */
 	toggle_ready_jedec(bios);
 
 	if (check_erased_range(flash, address, sector_size)) {
@@ -69,12 +73,13 @@
 	return 0;
 }
 
-static int write_sector_28sf040(chipaddr bios, uint8_t *src, chipaddr dst,
-				unsigned int page_size)
+int write_sector_28sf040(struct flashchip *flash, uint8_t *src, int start, int len)
 {
 	int i;
+	chipaddr bios = flash->virtual_memory;
+	chipaddr dst = flash->virtual_memory + start;
 
-	for (i = 0; i < page_size; i++) {
+	for (i = 0; i < len; i++) {
 		/* transfer data from source to destination */
 		if (*src == 0xFF) {
 			dst++, src++;
@@ -96,10 +101,10 @@
 {
 	chipaddr bios = flash->virtual_memory;
 
-	unprotect_28sf040(bios);
+	unprotect_28sf040(flash);
 	chip_writeb(CHIP_ERASE, bios);
 	chip_writeb(CHIP_ERASE, bios);
-	protect_28sf040(bios);
+	protect_28sf040(flash);
 
 	programmer_delay(10);
 	toggle_ready_jedec(bios);
@@ -116,17 +121,14 @@
 	int i;
 	int total_size = flash->total_size * 1024;
 	int page_size = flash->page_size;
-	chipaddr bios = flash->virtual_memory;
 
-	unprotect_28sf040(bios);
+	unprotect_28sf040(flash);
 
 	for (i = 0; i < total_size / page_size; i++) {
-		/* write to the sector */
-		write_sector_28sf040(bios, buf + i * page_size,
-				     bios + i * page_size, page_size);
+		write_sector_28sf040(flash, buf + i * page_size, i * page_size, page_size);
 	}
 
-	protect_28sf040(bios);
+	protect_28sf040(flash);
 
 	return 0;
 }
diff --git a/sst49lfxxxc.c b/sst49lfxxxc.c
index a6571ef..ec7d3da 100644
--- a/sst49lfxxxc.c
+++ b/sst49lfxxxc.c
@@ -67,7 +67,7 @@
 	chip_writeb(0x30, bios);
 	chip_writeb(0xD0, bios + address);
 
-	status = wait_82802ab(bios);
+	status = wait_82802ab(flash);
 
 	if (check_erased_range(flash, address, sector_size)) {
 		msg_cerr("ERASE FAILED!\n");
@@ -85,9 +85,7 @@
 
 	write_lockbits_49lfxxxc(flash, 0);
 	for (i = 0; i < total_size / page_size; i++) {
-		/* write to the sector */
-		write_page_82802ab(bios, buf + i * page_size,
-				      bios + i * page_size, page_size);
+		write_page_82802ab(flash, buf + i * page_size, i * page_size, page_size);
 	}
 
 	chip_writeb(0xFF, bios);
diff --git a/stm50flw0x0x.c b/stm50flw0x0x.c
index bc3a5e8..80f518e 100644
--- a/stm50flw0x0x.c
+++ b/stm50flw0x0x.c
@@ -105,7 +105,7 @@
 	chip_writeb(0xd0, bios);
 	programmer_delay(10);
 
-	wait_82802ab(flash->virtual_memory);
+	wait_82802ab(flash);
 
 	if (check_erased_range(flash, sector, sectorsize)) {
 		msg_cerr("ERASE FAILED!\n");