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
Change the function signature of wait_82802ab to eliminate single-use variables.
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.
With these changes it becomes easy to remove the wrappers in the next step and use the inner write functions directly to allow partial write.
The 80 column limit is ignored in a few places to help with readability while the code is in flux.
Signed-off-by: Carl-Daniel Hailfinger c-d.hailfinger.devel.2006@gmx.net
Index: flashrom-partial_write_inner_function_signature/jedec.c =================================================================== --- flashrom-partial_write_inner_function_signature/jedec.c (Revision 1207) +++ flashrom-partial_write_inner_function_signature/jedec.c (Arbeitskopie) @@ -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) +static 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; Index: flashrom-partial_write_inner_function_signature/sst49lfxxxc.c =================================================================== --- flashrom-partial_write_inner_function_signature/sst49lfxxxc.c (Revision 1207) +++ flashrom-partial_write_inner_function_signature/sst49lfxxxc.c (Arbeitskopie) @@ -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); Index: flashrom-partial_write_inner_function_signature/sharplhf00l04.c =================================================================== --- flashrom-partial_write_inner_function_signature/sharplhf00l04.c (Revision 1207) +++ flashrom-partial_write_inner_function_signature/sharplhf00l04.c (Arbeitskopie) @@ -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; Index: flashrom-partial_write_inner_function_signature/Makefile =================================================================== --- flashrom-partial_write_inner_function_signature/Makefile (Revision 1207) +++ flashrom-partial_write_inner_function_signature/Makefile (Arbeitskopie) @@ -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
Index: flashrom-partial_write_inner_function_signature/82802ab.c =================================================================== --- flashrom-partial_write_inner_function_signature/82802ab.c (Revision 1207) +++ flashrom-partial_write_inner_function_signature/82802ab.c (Arbeitskopie) @@ -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,25 @@ return 0; }
-void write_page_82802ab(chipaddr bios, uint8_t *src, - chipaddr dst, int page_size) +void 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); } }
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; Index: flashrom-partial_write_inner_function_signature/sst28sf040.c =================================================================== --- flashrom-partial_write_inner_function_signature/sst28sf040.c (Revision 1207) +++ flashrom-partial_write_inner_function_signature/sst28sf040.c (Arbeitskopie) @@ -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) +static 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; } Index: flashrom-partial_write_inner_function_signature/stm50flw0x0x.c =================================================================== --- flashrom-partial_write_inner_function_signature/stm50flw0x0x.c (Revision 1207) +++ flashrom-partial_write_inner_function_signature/stm50flw0x0x.c (Arbeitskopie) @@ -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"); Index: flashrom-partial_write_inner_function_signature/m29f400bt.c =================================================================== --- flashrom-partial_write_inner_function_signature/m29f400bt.c (Revision 1207) +++ flashrom-partial_write_inner_function_signature/m29f400bt.c (Arbeitskopie) @@ -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) +void 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); @@ -138,20 +139,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(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);
- 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); - return 0; } Index: flashrom-partial_write_inner_function_signature/chipdrivers.h =================================================================== --- flashrom-partial_write_inner_function_signature/chipdrivers.h (Revision 1207) +++ flashrom-partial_write_inner_function_signature/chipdrivers.h (Arbeitskopie) @@ -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); +void 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,7 @@ 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);
/* m29f400bt.c */ int probe_m29f400bt(struct flashchip *flash); @@ -94,8 +94,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); +void write_page_m29f400bt(struct flashchip *flash, uint8_t *src, int start, int len);
/* pm49fl00x.c */ int unlock_49fl00x(struct flashchip *flash);