Refine granularity handling in preparation of AT45DB series support

This is a backport of the upstream `commit 02437458` with some
minor additions to align more closely with master. Includes,
`commit c8305e` - Add support for 128 bytes write granularity.

BUG=chromium:478356
BRANCH=none
TEST=still builds

Change-Id: I8f7b5899a8ea2c75174de46e95d935d1bf4ab934
Signed-off-by: Edward O'Callaghan <quasisec@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/1484924
Reviewed-by: Stefan Reinauer <reinauer@chromium.org>
diff --git a/flash.h b/flash.h
index 79864bd..52553c6 100644
--- a/flash.h
+++ b/flash.h
@@ -269,11 +269,27 @@
 void tolower_string(char *str);
 
 /* flashrom.c */
+/*
+ * The following enum defines possible write granularities of flash chips. These tend to reflect the properties
+ * of the actual hardware not necesserily the write function(s) defined by the respective struct flashchip.
+ * The latter might (and should) be more precisely specified, e.g. they might bail out early if their execution
+ * would result in undefined chip contents.
+ */
 enum write_granularity {
-	write_gran_1bit,
-	write_gran_1byte,
-	write_gran_256bytes,
+	/* We assume 256 byte granularity by default. */
+	write_gran_256bytes = 0,/* If less than 256 bytes are written, the unwritten bytes are undefined. */
+	write_gran_1bit,	/* Each bit can be cleared individually. */
+	write_gran_1byte,	/* A byte can be written once. Further writes to an already written byte cause
+				 * its contents to be either undefined or to stay unchanged. */
+	write_gran_128bytes,	/* If less than 128 bytes are written, the unwritten bytes are undefined. */
+	write_gran_264bytes,	/* If less than 264 bytes are written, the unwritten bytes are undefined. */
+	write_gran_512bytes,	/* If less than 512 bytes are written, the unwritten bytes are undefined. */
+	write_gran_528bytes,	/* If less than 528 bytes are written, the unwritten bytes are undefined. */
+	write_gran_1024bytes,	/* If less than 1024 bytes are written, the unwritten bytes are undefined. */
+	write_gran_1056bytes,	/* If less than 1056 bytes are written, the unwritten bytes are undefined. */
+	write_gran_1byte_implicit_erase, /* EEPROMs and other chips with implicit erase and 1-byte writes. */
 };
+
 extern enum chipbustype buses_supported;
 extern enum flashrom_log_level verbose_screen;
 extern enum flashrom_log_level verbose_logfile;
diff --git a/flashrom.c b/flashrom.c
index 8a2a14a..c0b97c3 100644
--- a/flashrom.c
+++ b/flashrom.c
@@ -694,6 +694,24 @@
 	return ret;
 }
 
+/* Helper function for need_erase() that focuses on granularities of gran bytes. */
+static int need_erase_gran_bytes(const uint8_t *have, const uint8_t *want, unsigned int len,
+                                 unsigned int gran)
+{
+	unsigned int i, j, limit;
+	for (j = 0; j < len / gran; j++) {
+		limit = min (gran, len - j * gran);
+		/* Are 'have' and 'want' identical? */
+		if (!memcmp(have + j * gran, want + j * gran, limit))
+			continue;
+		/* have needs to be in erased state. */
+		for (i = 0; i < limit; i++)
+			if (have[j * gran + i] != 0xff)
+				return 1;
+	}
+	return 0;
+}
+
 /*
  * Check if the buffer @have can be programmed to the content of @want without
  * erasing. This is only possible if all chunks of size @gran are either kept
@@ -722,11 +740,7 @@
 		      unsigned int len, enum write_granularity gran)
 {
 	int result = 0;
-	unsigned int i, j, limit;
-	int erase_value = flash_erase_value(flash);
-
-	if (flash->chip->feature_bits & FEATURE_NO_ERASE)
-		return 0;
+	unsigned int i;
 
 	switch (gran) {
 	case write_gran_1bit:
@@ -738,26 +752,35 @@
 		break;
 	case write_gran_1byte:
 		for (i = 0; i < len; i++)
-			if ((have[i] != want[i]) && (have[i] != erase_value)) {
+			if ((have[i] != want[i]) && (have[i] != 0xff)) {
 				result = 1;
 				break;
 			}
 		break;
+	case write_gran_128bytes:
+		result = need_erase_gran_bytes(have, want, len, 128);
+		break;
 	case write_gran_256bytes:
-		for (j = 0; j < len / 256; j++) {
-			limit = min (256, len - j * 256);
-			/* Are 'have' and 'want' identical? */
-			if (!memcmp(have + j * 256, want + j * 256, limit))
-				continue;
-			/* have needs to be in erased state. */
-			for (i = 0; i < limit; i++)
-				if (have[j * 256 + i] != erase_value) {
-					result = 1;
-					break;
-				}
-			if (result)
-				break;
-		}
+		result = need_erase_gran_bytes(have, want, len, 256);
+		break;
+	case write_gran_264bytes:
+		result = need_erase_gran_bytes(have, want, len, 264);
+		break;
+	case write_gran_512bytes:
+		result = need_erase_gran_bytes(have, want, len, 512);
+		break;
+	case write_gran_528bytes:
+		result = need_erase_gran_bytes(have, want, len, 528);
+		break;
+	case write_gran_1024bytes:
+		result = need_erase_gran_bytes(have, want, len, 1024);
+		break;
+	case write_gran_1056bytes:
+		result = need_erase_gran_bytes(have, want, len, 1056);
+		break;
+	case write_gran_1byte_implicit_erase:
+		/* Do not erase, handle content changes from anything->0xff by writing 0xff. */
+		result = 0;
 		break;
 	default:
 		msg_cerr("%s: Unsupported granularity! Please report a bug at "
@@ -800,11 +823,30 @@
 	switch (gran) {
 	case write_gran_1bit:
 	case write_gran_1byte:
+	case write_gran_1byte_implicit_erase:
 		stride = 1;
 		break;
+	case write_gran_128bytes:
+		stride = 128;
+		break;
 	case write_gran_256bytes:
 		stride = 256;
 		break;
+	case write_gran_264bytes:
+		stride = 264;
+		break;
+	case write_gran_512bytes:
+		stride = 512;
+		break;
+	case write_gran_528bytes:
+		stride = 528;
+		break;
+	case write_gran_1024bytes:
+		stride = 1024;
+		break;
+	case write_gran_1056bytes:
+		stride = 1056;
+		break;
 	default:
 		msg_cerr("%s: Unsupported granularity! Please report a bug at "
 			 "flashrom@flashrom.org\n", __func__);