Index: flash.h
===================================================================
--- flash.h	(revision 3265)
+++ flash.h	(working copy)
@@ -427,4 +427,8 @@
 /* w49f002u.c */
 int write_49f002(struct flashchip *flash, uint8_t *buf);
 
+/* stm50flw0x0x.c */
+int probe_stm50flw0x0x(struct flashchip *flash);
+int erase_stm50flw0x0x(struct flashchip *flash);
+int write_stm50flw0x0x(struct flashchip *flash, uint8_t *buf);
 #endif				/* !__FLASH_H__ */
Index: jedec.c
===================================================================
--- jedec.c	(revision 3265)
+++ jedec.c	(working copy)
@@ -22,6 +22,7 @@
  */
 
 #include <stdio.h>
+#include <string.h>
 #include <stdint.h>
 #include "flash.h"
 
@@ -123,13 +124,23 @@
 	*(volatile uint8_t *)(bios + 0x5555) = 0xF0;
 	myusec_delay(40);
 
-	printf_debug("%s: id1 0x%x, id2 0x%x\n", __FUNCTION__, largeid1, largeid2);
-	if (largeid1 == flash->manufacture_id && largeid2 == flash->model_id)
-		return 1;
+	printf_debug("%s: id1 0x%x, id2 0x%x\n", __FUNCTION__, largeid1,
+		     largeid2);
 
-	return 0;
+	if (largeid1 != flash->manufacture_id || largeid2 != flash->model_id)
+		return 0;
+
+	map_flash_registers(flash);
+
+	return 1;
+
 }
 
+/* TODO:
+ * claus.gindhart@kontron.com
+ * This is used by sst49lf040 only
+ * it would be more clean to move it there
+ */
 int erase_sector_jedec(volatile uint8_t *bios, unsigned int page)
 {
 	/*  Issue the Sector Erase command   */
@@ -153,6 +164,13 @@
 	return 0;
 }
 
+/* TODO:
+ * claus.gindhart@kontron.com
+ * This is used by pm49fl004.c and sst_fwhub.c only
+ * it would be more clean to move it there
+ * Besides that,
+ * on my board, equipped with the ST M50FLW80A this code does not work
+ */
 int erase_block_jedec(volatile uint8_t *bios, unsigned int block)
 {
 	/*  Issue the Sector Erase command   */
@@ -176,6 +194,10 @@
 	return 0;
 }
 
+/* TODO:
+ * claus.gindhart@kontron.com
+ * On my board, equipped with the ST M50FLW80A this code does not work
+ */
 int erase_chip_jedec(struct flashchip *flash)
 {
 	volatile uint8_t *bios = flash->virtual_memory;
@@ -200,6 +222,105 @@
 	return 0;
 }
 
+static void wait_jedec(volatile uint8_t *bios)
+{
+	uint8_t id1;
+	// id2;
+
+	*bios = 0x70;
+	if ((*bios & 0x80) == 0) {	// it's busy
+		while ((*bios & 0x80) == 0) ;
+	}
+	// put another command to get out of status register mode
+
+	*bios = 0x90;
+	myusec_delay(10);
+
+	id1 = *(volatile uint8_t *)bios;
+
+	// this is needed to jam it out of "read id" mode
+	*(volatile uint8_t *)(bios + 0x5555) = 0xAA;
+	*(volatile uint8_t *)(bios + 0x2AAA) = 0x55;
+	*(volatile uint8_t *)(bios + 0x5555) = 0xF0;
+
+}
+
+/*
+ * claus.gindhart@kontron.com
+ * The ST M50FLW080B and STM50FLW080B chips have to be unlocked,
+ * before you can erase them or write to them
+ *
+ * Blocks
+*/
+int unlock_erase_jedec_block(struct flashchip *flash, int offset)
+{
+	volatile uint8_t *bios = flash->virtual_memory + offset;
+	volatile uint8_t *flash_addr = flash->virtual_registers + 2;
+	const uint8_t unlock_sector = 0x00;
+	int j;
+
+	switch (flash->model_id)
+	{
+		case ST_M50FLW080A:
+		case ST_M50FLW080B:
+			/* 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 propably
+			 * dont need to worry about that
+			 */
+
+			/* check, if it's is a top/bottom-block with 4k-sectors */
+			if ((offset == 0) ||
+				(offset == (flash->model_id == ST_M50FLW080A ? 0xE0000 : 0x10000))
+				|| (offset == 0xF0000)) {
+
+				// unlock each 4k-sector
+				for (j = 0; j < 0x10000; j += 0x1000) {
+					printf_debug("unlocking at 0x%x\n", offset + j);
+					*(flash_addr + offset + j) = unlock_sector;
+					if (*(flash_addr + offset + j) != unlock_sector)
+						printf("Cannot unlock sector @ 0x%x\n",offset + j);
+				}
+			} else {
+				printf_debug("unlocking at 0x%x\n", offset);
+				*(flash_addr + offset) = unlock_sector;
+				if (*(flash_addr + offset) != unlock_sector)
+					printf("Cannot unlock sector @ 0x%x\n", offset);
+			}
+		break;
+
+		default:
+			/* All chips, which do not need unlocking, or
+			 * there unlocking is not yet implemented, go here
+			 */
+		break;
+	}
+
+	// clear status register
+	*bios = 0x50;
+	printf_debug("Erase at %p\n", bios);
+	// now start it
+	*(volatile uint8_t *)(bios) = 0x20;
+	*(volatile uint8_t *)(bios) = 0xd0;
+	myusec_delay(10);
+
+	wait_jedec(flash->virtual_memory);
+
+	for (j = 0; j < flash->page_size; j++) {
+		if (*(bios + j) != 0xFF) {
+			printf("Erase failed at 0x%x\n", offset + j);
+			return -1;
+		}
+	}
+
+	printf("DONE BLOCK 0x%x\n", offset);
+
+	return 0;
+}
+
 int write_page_write_jedec(volatile uint8_t *bios, uint8_t *src,
 			   volatile uint8_t *dst, int page_size)
 {
@@ -207,19 +328,31 @@
 	volatile uint8_t *d = dst;
 	uint8_t *s = src;
 
+/* TODO
+ * claus.gindhart@kontron.com
+ * For my opinion, this retry is not necessary
+ * It only came in, because the wait_jedec() below was missing
+ * and this caused flashing problems.
+ * With my fixed code, i never had a retry
+ */
 retry:
+
+#if 0
+	/* claus.gindhart@kontron.com
+	 * commented this out, because its not necessary in my opinion
+	 */
+
 	/* Issue JEDEC Data Unprotect comand */
 	*(volatile uint8_t *)(bios + 0x5555) = 0xAA;
 	*(volatile uint8_t *)(bios + 0x2AAA) = 0x55;
 	*(volatile uint8_t *)(bios + 0x5555) = 0xA0;
+#endif
 
 	/* transfer data from source to destination */
 	for (i = start_index; i < page_size; i++) {
-		/* If the data is 0xFF, don't program it */
-		if (*src != 0xFF)
-			*dst = *src;
-		dst++;
-		src++;
+		*dst = 0x40;
+		*dst++ = *src++;
+		wait_jedec(bios);
 	}
 
 	toggle_ready_jedec(dst - 1);
@@ -238,6 +371,7 @@
 
 	if (!ok && tried++ < MAX_REFLASH_TRIES) {
 		start_index = i;
+		printf("Warning,retry %d required\n",tried);
 		goto retry;
 	}
 	if (!ok) {
@@ -257,7 +391,7 @@
 		return -1;
 	}
 
-retry:
+      retry:
 	/* Issue JEDEC Byte Program command */
 	*(volatile uint8_t *)(bios + 0x5555) = 0xAA;
 	*(volatile uint8_t *)(bios + 0x2AAA) = 0x55;
@@ -290,28 +424,35 @@
 	return 0;
 }
 
-int write_jedec(struct flashchip *flash, uint8_t *buf)
+int write_jedec(struct flashchip *flash, uint8_t * buf)
 {
 	int i;
 	int total_size = flash->total_size * 1024;
 	int page_size = flash->page_size;
 	volatile uint8_t *bios = flash->virtual_memory;
 
-	erase_chip_jedec(flash);
-	// dumb check if erase was successful.
-	for (i = 0; i < total_size; i++) {
-		if (bios[i] != (uint8_t) 0xff) {
-			printf("ERASE FAILED @%d, val %02x!\n", i, bios[i]);
-			return -1;
+	printf("Programming page: ");
+	for (i = 0; i < total_size / page_size; i++) {
+		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("%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
+		 */
+		if (!memcmp((void *)(buf + i * page_size),
+			    (void *)(bios + i * page_size), page_size)) {
+			printf("SKIPPED\n");
+			continue;
 		}
-	}
 
-	printf("Programming page: ");
-	for (i = 0; i < total_size / page_size; i++) {
-		printf("%04d at address: 0x%08x", i, i * page_size);
+		unlock_erase_jedec_block(flash, i * page_size);
 		write_page_write_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");
 	protect_jedec(bios);
Index: flashchips.c
===================================================================
--- flashchips.c	(revision 3265)
+++ flashchips.c	(working copy)
@@ -102,10 +102,10 @@
 	{"ST",		"M29F400BT",		ST_ID,		ST_M29F400BT,		512,	64 * 1024,	probe_m29f400bt,	erase_m29f400bt,		write_coreboot_m29f400bt},
 	{"ST",		"M29W010B",		ST_ID,		ST_M29W010B,		128,	16 * 1024,	probe_jedec,		erase_chip_jedec,		write_jedec},
 	{"ST",		"M29W040B",		ST_ID,		ST_M29W040B,		512,	64 * 1024,	probe_jedec,		erase_chip_jedec,		write_jedec},
-	{"ST",		"M50FLW040A",		ST_ID,		ST_M50FLW040A,		512,	64 * 1024,	probe_jedec,		erase_chip_jedec,		write_jedec},
-	{"ST",		"M50FLW040B",		ST_ID,		ST_M50FLW040B,		512,	64 * 1024,	probe_jedec,		erase_chip_jedec,		write_jedec},
-	{"ST",		"M50FLW080A",		ST_ID,		ST_M50FLW080A,		1024,	64 * 1024,	probe_jedec,		erase_chip_jedec,		write_jedec},
-	{"ST",		"M50FLW080B",		ST_ID,		ST_M50FLW080B,		1024,	64 * 1024,	probe_jedec,		erase_chip_jedec,		write_jedec},
+	{"ST",		"M50FLW040A",		ST_ID,		ST_M50FLW040A,		512,	64 * 1024,	probe_stm50flw0x0x,		erase_stm50flw0x0x,		write_stm50flw0x0x},
+	{"ST",		"M50FLW040B",		ST_ID,		ST_M50FLW040B,		512,	64 * 1024,	probe_stm50flw0x0x,		erase_stm50flw0x0x,		write_stm50flw0x0x},
+	{"ST",		"M50FLW080A",		ST_ID,		ST_M50FLW080A,		1024,	64 * 1024,	probe_stm50flw0x0x,		erase_stm50flw0x0x,		write_stm50flw0x0x},
+	{"ST",		"M50FLW080B",		ST_ID,		ST_M50FLW080B,		1024,	64 * 1024,	probe_stm50flw0x0x,		erase_stm50flw0x0x,		write_stm50flw0x0x},
 	{"ST",		"M50FW016",		ST_ID,		ST_M50FW016,		2048,	64 * 1024,	probe_82802ab,		erase_82802ab,			write_82802ab},
 	{"ST",		"M50FW040",		ST_ID,		ST_M50FW040,		512,	64 * 1024,	probe_82802ab,		erase_82802ab,			write_82802ab},
 	{"ST",		"M50FW080",		ST_ID,		ST_M50FW080,		1024,	64 * 1024,	probe_82802ab,		erase_82802ab,			write_82802ab},
Index: Makefile
===================================================================
--- Makefile	(revision 3265)
+++ Makefile	(working copy)
@@ -20,11 +20,11 @@
 STRIP_ARGS = -s
 endif
 
-OBJS = chipset_enable.o board_enable.o udelay.o jedec.o sst28sf040.o \
-	am29f040b.o mx29f002.o sst39sf020.o m29f400bt.o w49f002u.o \
-	82802ab.o msys_doc.o pm49fl004.o sst49lf040.o sst49lfxxxc.o \
-	sst_fwhub.o layout.o cbtable.o flashchips.o flashrom.o \
-	w39v080fa.o sharplhf00l04.o w29ee011.o spi.o
+OBJS = chipset_enable.o board_enable.o udelay.o jedec.o stm50flw0x0x.c \
+	sst28sf040.o am29f040b.o mx29f002.o sst39sf020.o m29f400bt.o \
+	w49f002u.o 82802ab.o msys_doc.o pm49fl004.o sst49lf040.o \
+	sst49lfxxxc.o sst_fwhub.o layout.o cbtable.o flashchips.o \
+	flashrom.o w39v080fa.o sharplhf00l04.o w29ee011.o spi.o
 
 all: pciutils dep $(PROGRAM)
 
Index: stm50flw0x0x.c
===================================================================
--- stm50flw0x0x.c	(revision 0)
+++ stm50flw0x0x.c	(revision 0)
@@ -0,0 +1,293 @@
+/*
+ * This file is part of the flashrom project.
+ *
+ * Copyright (C) 2008 Claus Gindhart <claus.gindhart@kontron.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 <stdio.h>
+#include <string.h>
+#include <stdint.h>
+#include "flash.h"
+
+void protect_stm50flw0x0x(volatile uint8_t *bios)
+{
+	*(volatile uint8_t *)(bios + 0x5555) = 0xAA;
+	*(volatile uint8_t *)(bios + 0x2AAA) = 0x55;
+	*(volatile uint8_t *)(bios + 0x5555) = 0xA0;
+
+	usleep(200);
+}
+
+int probe_stm50flw0x0x(struct flashchip *flash)
+{
+	volatile uint8_t *bios = flash->virtual_memory;
+	uint8_t id1, id2;
+	uint32_t largeid1, largeid2;
+
+	/* Issue JEDEC Product ID Entry command */
+	*(volatile uint8_t *)(bios + 0x5555) = 0xAA;
+	myusec_delay(10);
+	*(volatile uint8_t *)(bios + 0x2AAA) = 0x55;
+	myusec_delay(10);
+	*(volatile uint8_t *)(bios + 0x5555) = 0x90;
+	myusec_delay(40);
+
+	/* Read product ID */
+	id1 = *(volatile uint8_t *)bios;
+	id2 = *(volatile uint8_t *)(bios + 0x01);
+	largeid1 = id1;
+	largeid2 = id2;
+
+	/* Check if it is a continuation ID, this should be a while loop. */
+	if (id1 == 0x7F) {
+		largeid1 <<= 8;
+		id1 = *(volatile uint8_t *)(bios + 0x100);
+		largeid1 |= id1;
+	}
+	if (id2 == 0x7F) {
+		largeid2 <<= 8;
+		id2 = *(volatile uint8_t *)(bios + 0x101);
+		largeid2 |= id2;
+	}
+
+	/* Issue JEDEC Product ID Exit command */
+	*(volatile uint8_t *)(bios + 0x5555) = 0xAA;
+	myusec_delay(10);
+	*(volatile uint8_t *)(bios + 0x2AAA) = 0x55;
+	myusec_delay(10);
+	*(volatile uint8_t *)(bios + 0x5555) = 0xF0;
+	myusec_delay(40);
+
+	printf_debug("%s: id1 0x%x, id2 0x%x\n", __FUNCTION__, largeid1,
+		     largeid2);
+
+	if (largeid1 != flash->manufacture_id || largeid2 != flash->model_id)
+		return 0;
+
+	map_flash_registers(flash);
+
+	return 1;
+}
+
+
+static void wait_stm50flw0x0x(volatile uint8_t *bios)
+{
+	uint8_t id1;
+	// id2;
+
+	*bios = 0x70;
+	if ((*bios & 0x80) == 0) {	// it's busy
+		while ((*bios & 0x80) == 0) ;
+	}
+	// put another command to get out of status register mode
+
+	*bios = 0x90;
+	myusec_delay(10);
+
+	id1 = *(volatile uint8_t *)bios;
+
+	// this is needed to jam it out of "read id" mode
+	*(volatile uint8_t *)(bios + 0x5555) = 0xAA;
+	*(volatile uint8_t *)(bios + 0x2AAA) = 0x55;
+	*(volatile uint8_t *)(bios + 0x5555) = 0xF0;
+
+}
+
+/*
+ * claus.gindhart@kontron.com
+ * The ST M50FLW080B and STM50FLW080B chips have to be unlocked,
+ * before you can erase them or write to them
+*/
+int unlock_block_stm50flw0x0x(struct flashchip *flash, int offset)
+{
+	volatile uint8_t *flash_addr = flash->virtual_registers + 2;
+	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 propably
+	* dont 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->model_id == ST_M50FLW080A ? 0xE0000 : 0x10000))
+		|| (offset == 0xF0000)) {
+
+		// unlock each 4k-sector
+		for (j = 0; j < 0x10000; j += 0x1000) {
+			printf_debug("unlocking at 0x%x\n", offset + j);
+			*(flash_addr + offset + j) = unlock_sector;
+			if (*(flash_addr + offset + j) != unlock_sector)
+			{
+				printf("Cannot unlock sector @ 0x%x\n",offset + j);
+				return -1;
+			}
+		}
+	} else {
+		printf_debug("unlocking at 0x%x\n", offset);
+		*(flash_addr + offset) = unlock_sector;
+		if (*(flash_addr + offset) != unlock_sector)
+		{
+			printf("Cannot unlock sector @ 0x%x\n", offset);
+			return -1;
+		}
+	}
+
+	return 0;
+}
+
+int erase_block_stm50flw0x0x(struct flashchip *flash, int offset)
+{
+	volatile uint8_t *bios = flash->virtual_memory + offset;
+	int j;
+
+	// clear status register
+	*bios = 0x50;
+	printf_debug("Erase at %p\n", bios);
+	// now start it
+	*(volatile uint8_t *)(bios) = 0x20;
+	*(volatile uint8_t *)(bios) = 0xd0;
+	myusec_delay(10);
+
+	wait_stm50flw0x0x(flash->virtual_memory);
+
+	for (j = 0; j < flash->page_size; j++) {
+		if (*(bios + j) != 0xFF) {
+			printf("Erase failed at 0x%x\n", offset + j);
+			return -1;
+		}
+	}
+
+	printf("DONE BLOCK 0x%x\n", offset);
+
+	return 0;
+}
+
+int write_page_stm50flw0x0x(volatile uint8_t *bios, uint8_t *src,
+			   volatile uint8_t *dst, int page_size)
+{
+	int i, rc=0;
+	volatile uint8_t *d = dst;
+	uint8_t *s = src;
+
+	/* transfer data from source to destination */
+	for (i = 0; i < page_size; i++) {
+		*dst = 0x40;
+		*dst++ = *src++;
+		wait_stm50flw0x0x(bios);
+	}
+
+/* claus.gindhart@kontron.com
+ * TODO
+ * I think, that verification is not required, but
+ * i leave it in anyway
+ */
+	dst = d;
+	src = s;
+	for (i = 0; i < page_size; i++) {
+		if (*dst != *src) {
+			rc = -1;
+			break;
+		}
+		dst++;
+		src++;
+	}
+
+	if (rc) {
+		fprintf(stderr, " page %d failed!\n",
+			(unsigned int)(d - bios) / page_size);
+	}
+
+	return rc;
+}
+
+/* I simply erase block by block
+ * I Chip This is not the fastest way, but it works
+ */
+int erase_stm50flw0x0x(struct flashchip *flash)
+{
+	int i,rc=0;
+	int total_size = flash->total_size * 1024;
+	int page_size = flash->page_size;
+	volatile uint8_t *bios = flash->virtual_memory;
+
+	printf("Erasing page:\n");
+	for (i = 0;(i < total_size / page_size) && (rc==0); i++) {
+		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("%04d at address: 0x%08x ", i, i * page_size);
+		rc = unlock_block_stm50flw0x0x(flash, i * page_size);
+		if (!rc) rc = erase_block_stm50flw0x0x(flash, i * page_size);
+	}
+	printf("\n");
+	protect_stm50flw0x0x(bios);
+
+	return rc;
+}
+
+int write_stm50flw0x0x(struct flashchip *flash, uint8_t * buf)
+{
+	int i,rc=0;
+	int total_size = flash->total_size * 1024;
+	int page_size = flash->page_size;
+	volatile uint8_t *bios = flash->virtual_memory;
+
+	printf("Programming page: \n");
+	for (i = 0;(i < total_size / page_size) && (rc==0); i++) {
+		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("%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
+		 */
+		if (!memcmp((void *)(buf + i * page_size),
+			    (void *)(bios + i * page_size), page_size)) {
+			printf("SKIPPED\n");
+			continue;
+		}
+
+		rc = unlock_block_stm50flw0x0x(flash, i * page_size);
+		if (!rc) rc = erase_block_stm50flw0x0x(flash, i * page_size);
+		if (!rc) write_page_stm50flw0x0x(bios, buf + i * page_size,
+				       bios + i * page_size, page_size);
+	}
+	printf("\n");
+	protect_stm50flw0x0x(bios);
+
+	return rc;
+}

Property changes on: stm50flw0x0x.c
___________________________________________________________________
Name: svn:executable
   + *

