There are two locking strategies used by this umbrella family, one uniform and one that matches the sector layout of the chip. Refactor the functions involved and rename the overly complicated file to just stm50.c and the functions accordingly.
This fixes unlocking for some of the non-uniform chips, gets rid of the abuse of page_size
Signed-off-by: Stefan Tauner stefan.tauner@student.tuwien.ac.at --- 82802ab.c | 1 + Makefile | 2 +- chipdrivers.h | 7 ++-- flashchips.c | 36 +++++++++--------- stm50.c | 102 ++++++++++++++++++++++++++++++++++++++++++++++++++ stm50flw0x0x.c | 115 --------------------------------------------------------- 6 files changed, 126 insertions(+), 137 deletions(-) create mode 100644 stm50.c delete mode 100644 stm50flw0x0x.c
diff --git a/82802ab.c b/82802ab.c index 608995d..2a43813 100644 --- a/82802ab.c +++ b/82802ab.c @@ -89,6 +89,7 @@ int probe_82802ab(struct flashctx *flash) return 1; }
+/* FIXME: needs timeout */ uint8_t wait_82802ab(struct flashctx *flash) { uint8_t status; diff --git a/Makefile b/Makefile index 3b24bc4..9065ee7 100644 --- a/Makefile +++ b/Makefile @@ -314,7 +314,7 @@ endif ############################################################################### # Flash chip drivers and bus support infrastructure.
-CHIP_OBJS = jedec.o stm50flw0x0x.o w39.o w29ee011.o \ +CHIP_OBJS = jedec.o stm50.o w39.o w29ee011.o \ sst28sf040.o m29f400bt.o 82802ab.o pm49fl00x.o \ sst49lfxxxc.o sst_fwhub.o flashchips.o spi.o spi25.o spi25_statusreg.o \ opaque.o sfdp.o en29lv640b.o diff --git a/chipdrivers.h b/chipdrivers.h index 091d14c..f04cfcb 100644 --- a/chipdrivers.h +++ b/chipdrivers.h @@ -179,9 +179,10 @@ int printlock_at49f(struct flashctx *flash); /* w29ee011.c */ int probe_w29ee011(struct flashctx *flash);
-/* stm50flw0x0x.c */ -int erase_sector_stm50flw0x0x(struct flashctx *flash, unsigned int block, unsigned int blocksize); -int unlock_stm50flw0x0x(struct flashctx *flash); +/* stm50.c */ +int erase_sector_stm50(struct flashctx *flash, unsigned int block, unsigned int blocksize); +int unlock_stm50_uniform(struct flashctx *flash); +int unlock_stm50_nonuniform(struct flashctx *flash);
/* en29lv640b.c */ int probe_en29lv640b(struct flashctx *flash); diff --git a/flashchips.c b/flashchips.c index 9b331c7..886e707 100644 --- a/flashchips.c +++ b/flashchips.c @@ -10752,7 +10752,7 @@ const struct flashchip flashchips[] = { .manufacture_id = ST_ID, .model_id = ST_M50FLW040A, .total_size = 512, - .page_size = 64 * 1024, + .page_size = 0, .feature_bits = FEATURE_REGISTERMAP, .tested = TEST_UNTESTED, .probe = probe_82802ab, @@ -10772,8 +10772,8 @@ const struct flashchip flashchips[] = { .block_erase = erase_block_82802ab, } }, - .unlock = unlock_stm50flw0x0x, .write = write_82802ab, + .unlock = unlock_stm50_uniform, .read = read_memmapped, .voltage = {3000, 3600}, /* Also has 12V fast program & erase */ }, @@ -10785,7 +10785,7 @@ const struct flashchip flashchips[] = { .manufacture_id = ST_ID, .model_id = ST_M50FLW040B, .total_size = 512, - .page_size = 64 * 1024, + .page_size = 0, .feature_bits = FEATURE_REGISTERMAP, .tested = TEST_UNTESTED, .probe = probe_82802ab, @@ -10805,8 +10805,8 @@ const struct flashchip flashchips[] = { .block_erase = erase_block_82802ab, } }, - .unlock = unlock_stm50flw0x0x, .write = write_82802ab, + .unlock = unlock_stm50_uniform, .read = read_memmapped, .voltage = {3000, 3600}, /* Also has 12V fast program & erase */ }, @@ -10818,7 +10818,7 @@ const struct flashchip flashchips[] = { .manufacture_id = ST_ID, .model_id = ST_M50FLW080A, .total_size = 1024, - .page_size = 64 * 1024, + .page_size = 0, .feature_bits = FEATURE_REGISTERMAP, .tested = TEST_OK_PRE, .probe = probe_82802ab, @@ -10838,7 +10838,7 @@ const struct flashchip flashchips[] = { .block_erase = erase_block_82802ab, } }, - .unlock = unlock_stm50flw0x0x, + .unlock = unlock_stm50_nonuniform, .write = write_82802ab, .read = read_memmapped, .voltage = {3000, 3600}, /* Also has 12V fast program & erase */ @@ -10851,7 +10851,7 @@ const struct flashchip flashchips[] = { .manufacture_id = ST_ID, .model_id = ST_M50FLW080B, .total_size = 1024, - .page_size = 64 * 1024, + .page_size = 0, .feature_bits = FEATURE_REGISTERMAP, .tested = TEST_UNTESTED, .probe = probe_82802ab, @@ -10871,7 +10871,7 @@ const struct flashchip flashchips[] = { .block_erase = erase_block_82802ab, } }, - .unlock = unlock_stm50flw0x0x, + .unlock = unlock_stm50_nonuniform, .write = write_82802ab, .read = read_memmapped, .voltage = {3000, 3600}, /* Also has 12V fast program & erase */ @@ -10884,7 +10884,7 @@ const struct flashchip flashchips[] = { .manufacture_id = ST_ID, .model_id = ST_M50FW002, .total_size = 256, - .page_size = 64 * 1024, + .page_size = 0, .feature_bits = FEATURE_REGISTERMAP, .tested = TEST_UNTESTED, .probe = probe_82802ab, @@ -10901,7 +10901,7 @@ const struct flashchip flashchips[] = { .block_erase = erase_block_82802ab, } }, - .unlock = unlock_stm50flw0x0x, + .unlock = unlock_stm50_nonuniform, .write = write_82802ab, .read = read_memmapped, .voltage = {3000, 3600}, /* Also has 12V fast program & erase */ @@ -10914,7 +10914,7 @@ const struct flashchip flashchips[] = { .manufacture_id = ST_ID, .model_id = ST_M50FW016, .total_size = 2048, - .page_size = 64 * 1024, + .page_size = 0, .feature_bits = FEATURE_REGISTERMAP, .tested = TEST_UNTESTED, .probe = probe_82802ab, @@ -10926,7 +10926,7 @@ const struct flashchip flashchips[] = { .block_erase = erase_block_82802ab, } }, - .unlock = unlock_stm50flw0x0x, + .unlock = unlock_stm50_uniform, .write = write_82802ab, .read = read_memmapped, .voltage = {3000, 3600}, /* Also has 12V fast program & erase */ @@ -10939,7 +10939,7 @@ const struct flashchip flashchips[] = { .manufacture_id = ST_ID, .model_id = ST_M50FW040, .total_size = 512, - .page_size = 64 * 1024, + .page_size = 0, .feature_bits = FEATURE_REGISTERMAP, .tested = TEST_OK_PR, .probe = probe_82802ab, @@ -10951,7 +10951,7 @@ const struct flashchip flashchips[] = { .block_erase = erase_block_82802ab, } }, - .unlock = unlock_stm50flw0x0x, + .unlock = unlock_stm50_uniform, .write = write_82802ab, .read = read_memmapped, .voltage = {3000, 3600}, /* Also has 12V fast program & erase */ @@ -10964,7 +10964,7 @@ const struct flashchip flashchips[] = { .manufacture_id = ST_ID, .model_id = ST_M50FW080, .total_size = 1024, - .page_size = 64 * 1024, + .page_size = 0, .feature_bits = FEATURE_REGISTERMAP, .tested = TEST_OK_PREW, .probe = probe_82802ab, @@ -10976,7 +10976,7 @@ const struct flashchip flashchips[] = { .block_erase = erase_block_82802ab, } }, - .unlock = unlock_stm50flw0x0x, + .unlock = unlock_stm50_uniform, .write = write_82802ab, .read = read_memmapped, .voltage = {3000, 3600}, /* Also has 12V fast program & erase */ @@ -10989,7 +10989,7 @@ const struct flashchip flashchips[] = { .manufacture_id = ST_ID, .model_id = ST_M50LPW116, .total_size = 2048, - .page_size = 64 * 1024, + .page_size = 0, .feature_bits = FEATURE_REGISTERMAP, .tested = TEST_UNTESTED, .probe = probe_82802ab, @@ -11007,7 +11007,7 @@ const struct flashchip flashchips[] = { .block_erase = erase_block_82802ab, } }, - .unlock = unlock_stm50flw0x0x, + .unlock = unlock_stm50_nonuniform, .write = write_82802ab, .read = read_memmapped, .voltage = {3000, 3600}, /* Also has 12V fast program & erase */ diff --git a/stm50.c b/stm50.c new file mode 100644 index 0000000..f6c30a9 --- /dev/null +++ b/stm50.c @@ -0,0 +1,102 @@ +/* + * This file is part of the flashrom project. + * + * Copyright (C) 2008 Claus Gindhart claus.gindhart@kontron.com + * Copyright (C) 2009 Sean Nelson audiohacked@gmail.com + * Copyright (C) 2013 Stefan Tauner + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * 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 + */ + +/* + * All ST M50 chips are locked on startup. Most of them have a uniform 64 kB block layout, but some have + * a non-uniform block/sector segmentation which has to be handled with more care. + */ + +#include "flash.h" +#include "flashchips.h" +#include "chipdrivers.h" + +static int stm50_unlock_address(struct flashctx *flash, int offset) +{ + chipaddr wrprotect = flash->virtual_registers + 2; + static const uint8_t unlock_sector = 0x00; + msg_cdbg("unlocking at 0x%x\n", offset); + chip_writeb(flash, unlock_sector, wrprotect + offset); + if (chip_readb(flash, wrprotect + offset) != unlock_sector) { + msg_cerr("Cannot unlock address 0x%x\n", offset); + return -1; + } + return 0; +} + +/* Chips known to use a non-uniform block and sector layout (for locking as well as (optionally) for erasing): + * Name Size Address range of lock registers + * M50FLW080A 1MB FFB00002 - FFBFF002 + * M50FLW080B 1MB FFB00002 - FFBFF002 + * M50FW002 256k FFBC0002 - FFBFC002 + * M50LPW116 2MB FFA00002 - FFBFC002 + */ +int unlock_stm50_nonuniform(struct flashctx *flash) +{ + int i; + struct eraseblock *eraseblocks = flash->chip->block_erasers[0].eraseblocks; + unsigned int done = 0; + for (i = 0; i < NUM_ERASEREGIONS && eraseblocks[i].count != 0; i++) { + unsigned int block_size = eraseblocks[i].size; + unsigned int block_count = eraseblocks[i].count; + + int j; + for (j = 0; j < block_count; j++) { + if (stm50_unlock_address(flash, done)) { + msg_cerr("UNLOCK FAILED!\n"); + return -1; + } + done += block_count * block_size; + } + } + return 0; +} + +/* Unlocking for uniform 64 kB blocks starting at offset 2 of the feature registers. */ +int unlock_stm50_uniform(struct flashctx *flash) +{ + int i; + for (i = 0; i < flash->chip->total_size * 1024; i+= 64 * 1024) { + if (stm50_unlock_address(flash, i)) { + msg_cerr("UNLOCK FAILED!\n"); + return -1; + } + } + return 0; +} + +/* This function is unused. */ +int erase_sector_stm50(struct flashctx *flash, unsigned int sector, unsigned int sectorsize) +{ + chipaddr bios = flash->virtual_memory + sector; + + // clear status register + chip_writeb(flash, 0x50, bios); + // now start it + chip_writeb(flash, 0x32, bios); + chip_writeb(flash, 0xd0, bios); + programmer_delay(10); + + wait_82802ab(flash); + + /* FIXME: Check the status register for errors. */ + return 0; +} diff --git a/stm50flw0x0x.c b/stm50flw0x0x.c deleted file mode 100644 index e6c7c05..0000000 --- a/stm50flw0x0x.c +++ /dev/null @@ -1,115 +0,0 @@ -/* - * This file is part of the flashrom project. - * - * Copyright (C) 2008 Claus Gindhart claus.gindhart@kontron.com - * Copyright (C) 2009 Sean Nelson audiohacked@gmail.com - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * 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 - */ - -/* - * This module is designed for supporting the devices - * ST M50FLW040A (not yet tested) - * ST M50FLW040B (not yet tested) - * ST M50FLW080A - * ST M50FLW080B (not yet tested) - */ - -#include "flash.h" -#include "flashchips.h" -#include "chipdrivers.h" - -/* - * claus.gindhart@kontron.com - * The ST M50FLW080B and STM50FLW080B chips have to be unlocked, - * before you can erase them or write to them. - */ -static int unlock_block_stm50flw0x0x(struct flashctx *flash, int offset) -{ - chipaddr wrprotect = flash->virtual_registers + 2; - static const uint8_t unlock_sector = 0x00; - int j; - - /* - * These chips have to be unlocked before you can erase them or write - * to them. The size of the locking sectors depends on the type - * of chip. - * - * Sometimes, the BIOS does this for you; so you probably - * don't need to worry about that. - */ - - /* Check, if it's is a top/bottom-block with 4k-sectors. */ - /* TODO: What about the other types? */ - if ((offset == 0) || - (offset == (flash->chip->model_id == ST_M50FLW080A ? 0xE0000 : 0x10000)) - || (offset == 0xF0000)) { - - // unlock each 4k-sector - for (j = 0; j < 0x10000; j += 0x1000) { - msg_cdbg("unlocking at 0x%x\n", offset + j); - chip_writeb(flash, unlock_sector, - wrprotect + offset + j); - if (chip_readb(flash, wrprotect + offset + j) != - unlock_sector) { - msg_cerr("Cannot unlock sector @ 0x%x\n", - offset + j); - return -1; - } - } - } else { - msg_cdbg("unlocking at 0x%x\n", offset); - chip_writeb(flash, unlock_sector, wrprotect + offset); - if (chip_readb(flash, wrprotect + offset) != unlock_sector) { - msg_cerr("Cannot unlock sector @ 0x%x\n", offset); - return -1; - } - } - - return 0; -} - -int unlock_stm50flw0x0x(struct flashctx *flash) -{ - int i; - - for (i = 0; i < flash->chip->total_size * 1024; i+= flash->chip->page_size) { - if(unlock_block_stm50flw0x0x(flash, i)) { - msg_cerr("UNLOCK FAILED!\n"); - return -1; - } - } - - return 0; -} - -/* This function is unused. */ -int erase_sector_stm50flw0x0x(struct flashctx *flash, unsigned int sector, - unsigned int sectorsize) -{ - chipaddr bios = flash->virtual_memory + sector; - - // clear status register - chip_writeb(flash, 0x50, bios); - // now start it - chip_writeb(flash, 0x32, bios); - chip_writeb(flash, 0xd0, bios); - programmer_delay(10); - - wait_82802ab(flash); - - /* FIXME: Check the status register for errors. */ - return 0; -}