spi25: Integrate 4BA support

Allow 4-byte addresses for instructions usually used with 3-byte
addresses. Decide in which way the 4th byte will be communicated
based on the state of the chip (i.e. have we enabled 4BA mode)
and a new feature bit for an extended address register. If we
are not in 4BA mode and no extended address register is available
or the write to it fails, bail out.

We cache the state of 4BA mode and the extended address register
in the flashctx.

Change-Id: I644600beaab9a571b97b67f7516abe571d3460c1
Signed-off-by: Nico Huber <nico.h@gmx.de>
Reviewed-on: https://review.coreboot.org/22384
Reviewed-by: Stefan Reinauer <stefan.reinauer@coreboot.org>
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
diff --git a/flash.h b/flash.h
index b18d698..5cb7040 100644
--- a/flash.h
+++ b/flash.h
@@ -120,6 +120,8 @@
 #define FEATURE_OTP		(1 << 8)
 #define FEATURE_QPI		(1 << 9)
 #define FEATURE_4BA_SUPPORT	(1 << 10)
+#define FEATURE_4BA_EXT_ADDR	(1 << 11) /**< Regular 3-byte operations can be used by writing the most
+					       significant address byte into an extended address register. */
 
 enum test_state {
 	OK = 0,
@@ -236,6 +238,12 @@
 		bool verify_after_write;
 		bool verify_whole_chip;
 	} flags;
+	/* We cache the state of the extended address register (highest byte
+           of a 4BA for 3BA instructions) and the state of the 4BA mode here.
+           If possible, we enter 4BA mode early. If that fails, we make use
+           of the extended address register. */
+	int address_high_byte;
+	bool in_4ba_mode;
 };
 
 /* Timing used in probe routines. ZERO is -2 to differentiate between an unset
diff --git a/flashrom.c b/flashrom.c
index 12d7390..8849f63 100644
--- a/flashrom.c
+++ b/flashrom.c
@@ -2215,6 +2215,9 @@
 	if (flash->chip->unlock)
 		flash->chip->unlock(flash);
 
+	flash->address_high_byte = -1;
+	flash->in_4ba_mode = false;
+
 	/* Enable/disable 4-byte addressing mode if flash chip supports it */
 	if ((flash->chip->feature_bits & FEATURE_4BA_SUPPORT) &&
 	    flash->chip->four_bytes_addr_funcs.set_4ba) {
diff --git a/spi25.c b/spi25.c
index 84de75e..6940394 100644
--- a/spi25.c
+++ b/spi25.c
@@ -363,14 +363,37 @@
 	return result ? result : status;
 }
 
+static int spi_set_extended_address(struct flashctx *const flash, const uint8_t addr_high)
+{
+	if (flash->address_high_byte != addr_high &&
+	    spi_write_extended_address_register(flash, addr_high))
+		return -1;
+	flash->address_high_byte = addr_high;
+	return 0;
+}
+
 static int spi_prepare_address(struct flashctx *const flash,
 			       uint8_t cmd_buf[], const unsigned int addr)
 {
-	/* TODO: extend for 4BA */
-	cmd_buf[1] = (addr >> 16) & 0xff;
-	cmd_buf[2] = (addr >>  8) & 0xff;
-	cmd_buf[3] = (addr >>  0) & 0xff;
-	return 3;
+	if (flash->in_4ba_mode) {
+		cmd_buf[1] = (addr >> 24) & 0xff;
+		cmd_buf[2] = (addr >> 16) & 0xff;
+		cmd_buf[3] = (addr >>  8) & 0xff;
+		cmd_buf[4] = (addr >>  0) & 0xff;
+		return 4;
+	} else {
+		if (flash->chip->feature_bits & FEATURE_4BA_EXT_ADDR) {
+			if (spi_set_extended_address(flash, addr >> 24))
+				return -1;
+		} else {
+			if (addr >> 24)
+				return -1;
+		}
+		cmd_buf[1] = (addr >> 16) & 0xff;
+		cmd_buf[2] = (addr >>  8) & 0xff;
+		cmd_buf[3] = (addr >>  0) & 0xff;
+		return 3;
+	}
 }
 
 /**
diff --git a/spi4ba.c b/spi4ba.c
index a44e067..902f073 100644
--- a/spi4ba.c
+++ b/spi4ba.c
@@ -39,12 +39,17 @@
 /* Enter 4-bytes addressing mode (without sending WREN before) */
 int spi_enter_4ba_b7(struct flashctx *flash)
 {
+	int result;
 	const unsigned char cmd[JEDEC_ENTER_4_BYTE_ADDR_MODE_OUTSIZE] = { JEDEC_ENTER_4_BYTE_ADDR_MODE };
 
 	msg_trace("-> %s\n", __func__);
 
 	/* Switch to 4-bytes addressing mode */
-	return spi_send_command(flash, sizeof(cmd), 0, cmd, NULL);
+	result = spi_send_command(flash, sizeof(cmd), 0, cmd, NULL);
+	if (!result)
+		flash->in_4ba_mode = true;
+
+	return result;
 }
 
 /* Enter 4-bytes addressing mode with sending WREN before */
@@ -75,18 +80,25 @@
 	result = spi_send_multicommand(flash, cmds);
 	if (result)
 		msg_cerr("%s failed during command execution\n", __func__);
+	else
+		flash->in_4ba_mode = true;
 	return result;
 }
 
 /* Exit 4-bytes addressing mode (without sending WREN before) */
 int spi_exit_4ba_e9(struct flashctx *flash)
 {
+	int result;
 	const unsigned char cmd[JEDEC_EXIT_4_BYTE_ADDR_MODE_OUTSIZE] = { JEDEC_EXIT_4_BYTE_ADDR_MODE };
 
 	msg_trace("-> %s\n", __func__);
 
 	/* Switch to 3-bytes addressing mode  */
-	return spi_send_command(flash, sizeof(cmd), 0, cmd, NULL);
+	result = spi_send_command(flash, sizeof(cmd), 0, cmd, NULL);
+	if (!result)
+		flash->in_4ba_mode = false;
+
+	return result;
 }
 
 /* Exit 4-bytes addressing mode with sending WREN before */
@@ -115,9 +127,10 @@
 
 	/* Switch to 3-bytes addressing mode  */
 	result = spi_send_multicommand(flash, cmds);
-	if (result) {
+	if (result)
 		msg_cerr("%s failed during command execution\n", __func__);
-	}
+	else
+		flash->in_4ba_mode = false;
 	return result;
 }
 
diff --git a/spi4ba.h b/spi4ba.h
index 8a01792..a0316bc 100644
--- a/spi4ba.h
+++ b/spi4ba.h
@@ -114,5 +114,6 @@
 int spi_block_erase_5c_4ba_direct(struct flashctx *flash, unsigned int addr, unsigned int blocklen);
 int spi_block_erase_dc_4ba_direct(struct flashctx *flash, unsigned int addr, unsigned int blocklen);
 
+int spi_write_extended_address_register(struct flashctx *flash, uint8_t regdata);
 
 #endif /* __SPI_4BA_H__ */