Drop nr/opcode_index parameter from run_opcode and search the opmenu for the opcode instead

This is slightly slower (ha, ha), but works on boards with a locked
opmenu. Tested on ICH7 and works.

Corresponding to flashrom svn r336 and coreboot v2 svn r3721.

Signed-off-by: Stefan Reinauer <stepan@coresystems.de>
Acked-by: Carl-Daniel Hailfinger <c-d.hailfinger.devel.2006@gmx.net>
diff --git a/ichspi.c b/ichspi.c
index 095d53a..74ce3f0 100644
--- a/ichspi.c
+++ b/ichspi.c
@@ -148,7 +148,7 @@
 
 /* Common SPI functions */
 static int program_opcodes(OPCODES * op);
-static int run_opcode(uint8_t nr, OPCODE op, uint32_t offset,
+static int run_opcode(OPCODE op, uint32_t offset,
 		      uint8_t datalength, uint8_t * data);
 static int ich_spi_read_page(struct flashchip *flash, uint8_t * buf,
 			     int offset, int maxdata);
@@ -225,7 +225,7 @@
 	return 0;
 }
 
-static int ich7_run_opcode(uint8_t nr, OPCODE op, uint32_t offset,
+static int ich7_run_opcode(OPCODE op, uint32_t offset,
 			   uint8_t datalength, uint8_t * data, int maxdata)
 {
 	int write_cmd = 0;
@@ -233,6 +233,8 @@
 	uint32_t temp32 = 0;
 	uint16_t temp16;
 	uint32_t a;
+	uint64_t opmenu;
+	int opcode_index;
 
 	/* Is it a write command? */
 	if ((op.spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS)
@@ -280,7 +282,20 @@
 	}
 
 	/* Select opcode */
-	temp16 |= ((uint16_t) (nr & 0x07)) << 4;
+	opmenu = REGREAD32(ICH7_REG_OPMENU);
+	opmenu |= ((uint64_t)REGREAD32(ICH7_REG_OPMENU + 4)) << 32;
+
+	for (opcode_index=0; opcode_index<8; opcode_index++) {
+		if((opmenu & 0xff) == op.opcode) {
+			break;
+		}
+		opmenu >>= 8;
+	}
+	if (opcode_index == 8) {
+		printf_debug("Opcode %x not found.\n", op.opcode);
+		return 1;
+	}
+	temp16 |= ((uint16_t) (opcode_index & 0x07)) << 4;
 
 	/* Handle Atomic */
 	if (op.atomic != 0) {
@@ -328,13 +343,15 @@
 	return 0;
 }
 
-static int ich9_run_opcode(uint8_t nr, OPCODE op, uint32_t offset,
+static int ich9_run_opcode(OPCODE op, uint32_t offset,
 			   uint8_t datalength, uint8_t * data)
 {
 	int write_cmd = 0;
 	int timeout;
 	uint32_t temp32;
 	uint32_t a;
+	uint64_t opmenu;
+	int opcode_index;
 
 	/* Is it a write command? */
 	if ((op.spi_type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS)
@@ -383,7 +400,20 @@
 	}
 
 	/* Select opcode */
-	temp32 |= ((uint32_t) (nr & 0x07)) << (8 + 4);
+	opmenu = REGREAD32(ICH9_REG_OPMENU);
+	opmenu |= ((uint64_t)REGREAD32(ICH9_REG_OPMENU + 4)) << 32;
+
+	for (opcode_index=0; opcode_index<8; opcode_index++) {
+		if((opmenu & 0xff) == op.opcode) {
+			break;
+		}
+		opmenu >>= 8;
+	}
+	if (opcode_index == 8) {
+		printf_debug("Opcode %x not found.\n", op.opcode);
+		return 1;
+	}
+	temp32 |= ((uint32_t) (opcode_index & 0x07)) << (8 + 4);
 
 	/* Handle Atomic */
 	if (op.atomic != 0) {
@@ -431,16 +461,16 @@
 	return 0;
 }
 
-static int run_opcode(uint8_t nr, OPCODE op, uint32_t offset,
+static int run_opcode(OPCODE op, uint32_t offset,
 		      uint8_t datalength, uint8_t * data)
 {
 	switch (flashbus) {
 	case BUS_TYPE_VIA_SPI:
-		return ich7_run_opcode(nr, op, offset, datalength, data, 16);
+		return ich7_run_opcode(op, offset, datalength, data, 16);
 	case BUS_TYPE_ICH7_SPI:
-		return ich7_run_opcode(nr, op, offset, datalength, data, 64);
+		return ich7_run_opcode(op, offset, datalength, data, 64);
 	case BUS_TYPE_ICH9_SPI:
-		return ich9_run_opcode(nr, op, offset, datalength, data);
+		return ich9_run_opcode(op, offset, datalength, data);
 	default:
 		printf_debug("%s: unsupported chipset\n", __FUNCTION__);
 	}
@@ -453,7 +483,7 @@
 {
 	printf_debug("ich_spi_erase_block: offset=%d, sectors=%d\n", offset, 1);
 
-	if (run_opcode(2, curopcodes->opcode[2], offset, 0, NULL) != 0) {
+	if (run_opcode(curopcodes->opcode[2], offset, 0, NULL) != 0) {
 		printf_debug("Error erasing sector at 0x%x", offset);
 		return -1;
 	}
@@ -477,7 +507,7 @@
 		if (remaining < maxdata) {
 
 			if (run_opcode
-			    (1, curopcodes->opcode[1],
+			    (curopcodes->opcode[1],
 			     offset + (page_size - remaining), remaining,
 			     &buf[page_size - remaining]) != 0) {
 				printf_debug("Error reading");
@@ -486,7 +516,7 @@
 			remaining = 0;
 		} else {
 			if (run_opcode
-			    (1, curopcodes->opcode[1],
+			    (curopcodes->opcode[1],
 			     offset + (page_size - remaining), maxdata,
 			     &buf[page_size - remaining]) != 0) {
 				printf_debug("Error reading");
@@ -512,7 +542,7 @@
 	for (a = 0; a < page_size; a += maxdata) {
 		if (remaining < maxdata) {
 			if (run_opcode
-			    (0, curopcodes->opcode[0],
+			    (curopcodes->opcode[0],
 			     offset + (page_size - remaining), remaining,
 			     &bytes[page_size - remaining]) != 0) {
 				printf_debug("Error writing");
@@ -521,7 +551,7 @@
 			remaining = 0;
 		} else {
 			if (run_opcode
-			    (0, curopcodes->opcode[0],
+			    (curopcodes->opcode[0],
 			     offset + (page_size - remaining), maxdata,
 			     &bytes[page_size - remaining]) != 0) {
 				printf_debug("Error writing");
@@ -641,7 +671,7 @@
 		count = readcnt;
 	}
 
-	if (run_opcode(opcode_index, *opcode, addr, count, data) != 0) {
+	if (run_opcode(*opcode, addr, count, data) != 0) {
 		printf_debug("run OPCODE 0x%02x failed\n", opcode->opcode);
 		return 1;
 	}