Fixes

 - switch to volatile everywhere
 - use myusec_delay instead of usleep

Corresponding to coreboot v1 svn r492.
diff --git a/Makefile b/Makefile
index 0d9712b..603a97f 100644
--- a/Makefile
+++ b/Makefile
@@ -3,9 +3,7 @@
 
 all: ${OBJS}
 	${CC} -o flash_rom flash_rom.c ${OBJS}
-	${CC} -o flash_on flash_on.c
-	${CC} -o acpi_reset acpi_reset.c
-	${CC} -o acpi_shutdown acpi_shutdown.c
+#	${CC} -o flash_on flash_on.c
 
 clean:
-	rm -f flash_rom flash_on acpi_reset acpi_shutdown *.o *~
\ No newline at end of file
+	rm -f flash_rom flash_on *.o *~
diff --git a/am29f040b.c b/am29f040b.c
index 121297f..ceba046 100644
--- a/am29f040b.c
+++ b/am29f040b.c
@@ -27,7 +27,7 @@
 #include "flash.h"
 #include "jedec.h"
 
-static __inline__ erase_sector_29f040b (char * bios, unsigned long address)
+static __inline__ erase_sector_29f040b (volatile char * bios, unsigned long address)
 {
 	*(bios +   0x555) = 0xAA;
 	*(bios +   0x2AA) = 0x55;
@@ -42,8 +42,8 @@
 	toggle_ready_jedec(bios + address);
 }
 
-static __inline__ write_sector_29f040b(char * bios, unsigned char * src,
-				       unsigned char * dst, unsigned int page_size)
+static __inline__ write_sector_29f040b(volatile char * bios, unsigned char * src,
+				       volatile unsigned char * dst, unsigned int page_size)
 {
 	int i;
 
@@ -64,7 +64,7 @@
 
 int probe_29f040b (struct flashchip * flash)
 {
-	char * bios = flash->virt_addr;
+	volatile char * bios = flash->virt_addr;
 	unsigned char id1, id2;
 
 	*(bios + 0x555) = 0xAA;
@@ -76,8 +76,9 @@
 
 	*bios = 0xF0;
 
-	usleep(10);
+	myusec_delay(10);
     
+	printf(__FUNCTION__ "id1 %d, id2 %d\n", id1, id2);
 	if (id1 == flash->manufacture_id && id2 == flash->model_id)
 		return 1;
 
@@ -86,7 +87,7 @@
 
 int erase_29f040b (struct flashchip * flash)
 {
-	char * bios = flash->virt_addr;
+	volatile char * bios = flash->virt_addr;
 
 	*(bios + 0x555) = 0xAA;
 	*(bios + 0x2AA) = 0x55;
@@ -95,7 +96,7 @@
 	*(bios + 0x2AA) = 0x55;
 	*(bios + 0x555) = 0x10;
 
-	usleep(10);
+	myusec_delay(10);
 	toggle_ready_jedec(bios);
 }
 
@@ -103,7 +104,7 @@
 {
 	int i;
 	int total_size = flash->total_size * 1024, page_size = flash->page_size;
-	char * bios = flash->virt_addr;
+	volatile char * bios = flash->virt_addr;
 
 	printf ("Programming Page: ");
 	for (i = 0; i < total_size/page_size; i++) {
diff --git a/flash.h b/flash.h
index 3b763a7..1828176 100644
--- a/flash.h
+++ b/flash.h
@@ -6,7 +6,7 @@
 	int manufacture_id;
 	int model_id;
 
-	char * virt_addr;
+	volatile char * virt_addr;
 	int total_size;
 	int page_size;
 
diff --git a/flash_rom.c b/flash_rom.c
index 12aa273..0b7b768 100644
--- a/flash_rom.c
+++ b/flash_rom.c
@@ -109,7 +109,7 @@
 struct flashchip * probe_flash(struct flashchip * flash)
 {
     int fd_mem;
-    char * bios;
+    volatile char * bios;
     unsigned long size;
 
     if ((fd_mem = open("/dev/mem", O_RDWR)) < 0) {
@@ -118,6 +118,7 @@
     }
 
     while (flash->name != NULL) {
+	printf("Trying %s, %d KB\n", flash->name, flash->total_size);
 	size = flash->total_size * 1024;
 	bios = mmap (0, size, PROT_WRITE | PROT_READ, MAP_SHARED,
 		     fd_mem, (off_t) (0 - size));
@@ -132,7 +133,7 @@
 		    flash->name, (0 - size), bios);
 	    return flash;
 	}
-	munmap (bios, size);
+	munmap ((void *) bios, size);
 	flash++;
     }
     return NULL;
@@ -142,7 +143,7 @@
 {
     int i = 0;
     int total_size = flash->total_size *1024;
-    char * bios = flash->virt_addr;
+    volatile char * bios = flash->virt_addr;
 
     printf("Verifying address: ");
     while (i++ < total_size) {
@@ -156,6 +157,52 @@
     return 1;
 }
 
+// count to a billion. Time it. If it's < 1 sec, count to 10B, etc.
+
+unsigned long micro = 0;
+
+void 
+myusec_calibrate_delay()
+{
+	unsigned long count = 2 *  1024 * 1024;
+	volatile unsigned long i;
+	unsigned long timeusec;
+	struct timeval start, end;
+	int ok = 0;
+
+	fprintf(stderr, "Setting up microsecond timing loop\n");
+	while (! ok) {
+		fprintf(stderr, "Try %d\n", count);
+		gettimeofday(&start, 0);
+		for( i = count; i; i--)
+			;
+		gettimeofday(&end, 0);
+		timeusec = 1000000 * (end.tv_sec - start.tv_sec ) + 
+				(end.tv_usec - start.tv_usec);
+		fprintf(stderr, "timeusec is %d\n", timeusec);
+		count *= 10;
+		if (timeusec < 1000000)
+			continue;
+		ok = 1;
+	}
+
+	// compute one microsecond. That will be count / time
+	micro = count / timeusec;
+
+	fprintf(stderr, "one us is %d count\n", micro);
+
+
+}
+
+void
+myusec_delay(time)
+{
+  volatile unsigned long i;
+  for(i = 0; i < time * micro; i++)
+	;
+
+}
+
 main (int argc, char * argv[])
 {
     char * buf;
@@ -163,20 +210,20 @@
     FILE * image;
     struct flashchip * flash;
 
+    myusec_calibrate_delay();
+
     if (argc > 2){
 	printf("usage: %s [romimage]\n", argv[0]);
 	printf(" If no romimage is specified, then all that happens\n");
-	printf(" is that flash writes are enabled (useful for DoC)\n");
-	exit(1);
+	printf(" is that flash info is dumped\n");
     }
 
-    enable_flash_sis630 ();
-
     if ((flash = probe_flash (flashchips)) == NULL) {
 	printf("EEPROM not found\n");
 	exit(1);
     }
 
+    printf("Part is %s\n", flash->name);
     if (argc < 2){
 	printf("OK, only ENABLING flash write, but NOT FLASHING\n");
         exit(0);
diff --git a/jedec.c b/jedec.c
index 73a399c..594d9c2 100644
--- a/jedec.c
+++ b/jedec.c
@@ -29,24 +29,25 @@
 
 int probe_jedec (struct flashchip * flash)
 {
-	char * bios = flash->virt_addr;
+	volatile char * bios = flash->virt_addr;
 	unsigned char  id1, id2;
 
-	*(char *) (bios + 0x5555) = 0xAA;
-	*(char *) (bios + 0x2AAA) = 0x55;
-	*(char *) (bios + 0x5555) = 0x90;
+	*(volatile char *) (bios + 0x5555) = 0xAA;
+	*(volatile char *) (bios + 0x2AAA) = 0x55;
+	*(volatile char *) (bios + 0x5555) = 0x90;
 
-	usleep(10);
+	myusec_delay(10);
 
-	id1 = *(unsigned char *) bios;
-	id2 = *(unsigned char *) (bios + 0x01);
+	id1 = *(volatile unsigned char *) bios;
+	id2 = *(volatile unsigned char *) (bios + 0x01);
 
-	*(char *) (bios + 0x5555) = 0xAA;
-	*(char *) (bios + 0x2AAA) = 0x55;
-	*(char *) (bios + 0x5555) = 0xF0;
+	*(volatile char *) (bios + 0x5555) = 0xAA;
+	*(volatile char *) (bios + 0x2AAA) = 0x55;
+	*(volatile char *) (bios + 0x5555) = 0xF0;
 
-	usleep(10);
+	myusec_delay(10);
 
+	printf(__FUNCTION__ "id1 %d, id2 %d\n", id1, id2);
 	if (id1 == flash->manufacture_id && id2 == flash->model_id)
 		return 1;
 
@@ -55,17 +56,17 @@
 
 int erase_jedec (struct flashchip * flash)
 {
-	char * bios = flash->virt_addr;
+	volatile char * bios = flash->virt_addr;
 
-	*(char *) (bios + 0x5555) = 0xAA;
-	*(char *) (bios + 0x2AAA) = 0x55;
-	*(char *) (bios + 0x5555) = 0x80;
+	*(volatile char *) (bios + 0x5555) = 0xAA;
+	*(volatile char *) (bios + 0x2AAA) = 0x55;
+	*(volatile char *) (bios + 0x5555) = 0x80;
 
-	*(char *) (bios + 0x5555) = 0xAA;
-	*(char *) (bios + 0x2AAA) = 0x55;
-	*(char *) (bios + 0x5555) = 0x10;
+	*(volatile char *) (bios + 0x5555) = 0xAA;
+	*(volatile char *) (bios + 0x2AAA) = 0x55;
+	*(volatile char *) (bios + 0x5555) = 0x10;
 
-	usleep(10);
+	myusec_delay(10);
 	toggle_ready_jedec(bios);
 }
 
@@ -73,7 +74,7 @@
 {
 	int i;
 	int total_size = flash->total_size *1024, page_size = flash->page_size;
-	char * bios = flash->virt_addr;
+	volatile char * bios = flash->virt_addr;
 
 	erase_jedec (flash);
 	printf ("Programming Page: ");
diff --git a/jedec.h b/jedec.h
index de9c3d6..18c5153 100644
--- a/jedec.h
+++ b/jedec.h
@@ -2,7 +2,7 @@
 extern int erase_jedec (struct flashchip * flash);
 extern int write_jedec (struct flashchip * flash, char * buf);
 
-extern __inline__ void toggle_ready_jedec (char * dst)
+extern __inline__ void toggle_ready_jedec (volatile char * dst)
 {
 	unsigned int i = 0;
 	char tmp1, tmp2;
@@ -18,7 +18,7 @@
 	}
 }
 
-extern __inline__ void data_polling_jedec (char * dst, char data)
+extern __inline__ void data_polling_jedec (volatile char * dst, char data)
 {
 	unsigned int i = 0;
 	char tmp;
@@ -33,23 +33,23 @@
 	}
 }
 
-extern __inline__ void protect_jedec (char * bios)
+extern __inline__ void protect_jedec (volatile char * bios)
 {
-	*(char *) (bios + 0x5555) = 0xAA;
-	*(char *) (bios + 0x2AAA) = 0x55;
-	*(char *) (bios + 0x5555) = 0xA0;
+	*(volatile char *) (bios + 0x5555) = 0xAA;
+	*(volatile char *) (bios + 0x2AAA) = 0x55;
+	*(volatile char *) (bios + 0x5555) = 0xA0;
 
 	usleep(200);
 }
 
-extern __inline__ void write_page_jedec (char * bios, char * src, char * dst,
+extern __inline__ void write_page_jedec (volatile char * bios, char * src, volatile char * dst,
 					 int page_size)
 {
 	int i;
 
-	*(char *) (bios + 0x5555) = 0xAA;
-	*(char *) (bios + 0x2AAA) = 0x55;
-	*(char *) (bios + 0x5555) = 0xA0;
+	*(volatile char *) (bios + 0x5555) = 0xAA;
+	*(volatile char *) (bios + 0x2AAA) = 0x55;
+	*(volatile char *) (bios + 0x5555) = 0xA0;
 
 	for (i = 0; i < page_size; i++) {
 		/* transfer data from source to destination */
diff --git a/mx29f002.c b/mx29f002.c
index e5c2c08..7b7f1e1 100644
--- a/mx29f002.c
+++ b/mx29f002.c
@@ -30,20 +30,21 @@
 
 int probe_29f002 (struct flashchip * flash)
 {
-	char * bios = flash->virt_addr;
+	volatile char * bios = flash->virt_addr;
 	unsigned char id1, id2, id3;
 
 	*(bios + 0x5555) = 0xAA;
 	*(bios + 0x2AAA) = 0x55;
 	*(bios + 0x5555) = 0x90;
     
-	id1 = *(unsigned char *) bios;
-	id2 = *(unsigned char *) (bios + 0x01);
+	id1 = *(volatile unsigned char *) bios;
+	id2 = *(volatile unsigned char *) (bios + 0x01);
  
 	*bios = 0xF0;
 
-	usleep(10);
+	myusec_delay(10);
 
+	printf(__FUNCTION__ "id1 %d, id2 %d\n", id1, id2);
 	if (id1 == flash->manufacture_id && id2 == flash->model_id)
 		return 1;
 
@@ -52,7 +53,7 @@
 
 int erase_29f002 (struct flashchip * flash)
 {
-	char * bios = flash->virt_addr;
+	volatile char * bios = flash->virt_addr;
 
  again:
 	*(bios + 0x555) = 0xF0;
@@ -63,7 +64,7 @@
 	*(bios + 0x2AA) = 0x55;
 	*(bios + 0x555) = 0x10;
 
-	usleep(100);
+	myusec_delay(100);
 	toggle_ready_jedec(bios);
 
 	//   while ((*bios & 0x40) != 0x40)
@@ -85,11 +86,11 @@
 {
     int i;
     int total_size = flash->total_size * 1024, page_size = flash->page_size;
-    char * bios = flash->virt_addr;
-    char * dst = bios, * src = buf;
+    volatile char * bios = flash->virt_addr;
+    volatile char * dst = bios, * src = buf;
 
     *bios = 0xF0;
-    usleep(10);
+    myusec_delay(10);
     erase_29f002(flash);
     //*bios = 0xF0;
 #if 1
diff --git a/sst28sf040.c b/sst28sf040.c
index e0e5ea3..4042e8b 100644
--- a/sst28sf040.c
+++ b/sst28sf040.c
@@ -35,35 +35,35 @@
 #define RESET			0xFF
 #define READ_ID			0x90
 
-static __inline__ void protect_28sf040 (char * bios)
+static __inline__ void protect_28sf040 (volatile char * bios)
 {
 	/* ask compiler not to optimize this */
 	volatile unsigned char tmp;
 
-	tmp = *(unsigned char *) (bios + 0x1823);
-	tmp = *(unsigned char *) (bios + 0x1820);
-	tmp = *(unsigned char *) (bios + 0x1822);
-	tmp = *(unsigned char *) (bios + 0x0418);
-	tmp = *(unsigned char *) (bios + 0x041B);
-	tmp = *(unsigned char *) (bios + 0x0419);
-	tmp = *(unsigned char *) (bios + 0x040A);
+	tmp = *(volatile unsigned char *) (bios + 0x1823);
+	tmp = *(volatile unsigned char *) (bios + 0x1820);
+	tmp = *(volatile unsigned char *) (bios + 0x1822);
+	tmp = *(volatile unsigned char *) (bios + 0x0418);
+	tmp = *(volatile unsigned char *) (bios + 0x041B);
+	tmp = *(volatile unsigned char *) (bios + 0x0419);
+	tmp = *(volatile unsigned char *) (bios + 0x040A);
 }
 
-static __inline__ void unprotect_28sf040 (char * bios)
+static __inline__ void unprotect_28sf040 (volatile char * bios)
 {
 	/* ask compiler not to optimize this */
 	volatile unsigned char tmp;
 
-	tmp = *(unsigned char *) (bios + 0x1823);
-	tmp = *(unsigned char *) (bios + 0x1820);
-	tmp = *(unsigned char *) (bios + 0x1822);
-	tmp = *(unsigned char *) (bios + 0x0418);
-	tmp = *(unsigned char *) (bios + 0x041B);
-	tmp = *(unsigned char *) (bios + 0x0419);
-	tmp = *(unsigned char *) (bios + 0x041A);
+	tmp = *(volatile unsigned char *) (bios + 0x1823);
+	tmp = *(volatile unsigned char *) (bios + 0x1820);
+	tmp = *(volatile unsigned char *) (bios + 0x1822);
+	tmp = *(volatile unsigned char *) (bios + 0x0418);
+	tmp = *(volatile unsigned char *) (bios + 0x041B);
+	tmp = *(volatile unsigned char *) (bios + 0x0419);
+	tmp = *(volatile unsigned char *) (bios + 0x041A);
 }
 
-static __inline__ erase_sector_28sf040 (char * bios, unsigned long address)
+static __inline__ erase_sector_28sf040 (volatile char * bios, unsigned long address)
 {
 	*bios = AUTO_PG_ERASE1;
 	*(bios + address) = AUTO_PG_ERASE2;
@@ -72,8 +72,8 @@
 	toggle_ready_jedec(bios);
 }
 
-static __inline__ write_sector_28sf040(char * bios, unsigned char * src,
-				       unsigned char * dst, unsigned int page_size)
+static __inline__ write_sector_28sf040(volatile char * bios, unsigned char * src,
+				       volatile unsigned char * dst, unsigned int page_size)
 {
 	int i;
 
@@ -95,24 +95,25 @@
 
 int probe_28sf040 (struct flashchip * flash)
 {
-	char * bios = flash->virt_addr;
+	volatile char * bios = flash->virt_addr;
 	unsigned char  id1, id2, tmp;
 
 	/* save the value at the beginning of the Flash */
 	tmp = *bios;
 
 	*bios = RESET;
-	usleep(10);
+	myusec_delay(10);
 
 	*bios = READ_ID;
-	usleep(10);
-	id1 = *(unsigned char *) bios;
-	usleep(10);
-	id2 = *(unsigned char *) (bios + 0x01);
+	myusec_delay(10);
+	id1 = *(volatile unsigned char *) bios;
+	myusec_delay(10);
+	id2 = *(volatile unsigned char *) (bios + 0x01);
 
 	*bios = RESET;
-	usleep(10);
+	myusec_delay(10);
 
+	printf(__FUNCTION__ "id1 %d, id2 %d\n", id1, id2);
 	if (id1 == flash->manufacture_id && id2 == flash->model_id)
 		return 1;
 
@@ -123,14 +124,14 @@
 
 int erase_28sf040 (struct flashchip * flash)
 {
-	char * bios = flash->virt_addr;
+	volatile char * bios = flash->virt_addr;
 
 	unprotect_28sf040 (bios);
 	*bios = CHIP_ERASE;
 	*bios = CHIP_ERASE;
 	protect_28sf040 (bios);
 
-	usleep(10);
+	myusec_delay(10);
 	toggle_ready_jedec(bios);
 }
 
@@ -138,7 +139,7 @@
 {
 	int i;
 	int total_size = flash->total_size * 1024, page_size = flash->page_size;
-	char * bios = flash->virt_addr;
+	volatile char * bios = flash->virt_addr;
 
 	unprotect_28sf040 (bios);