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/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;
 }