tpm: report firmware version

Some devices allow to retrieve firmware version by reading the same 4
byte register repeatedly until the entire version string is read.

Let's print out TPM firmware version when available. Just in case
something goes wrong limit the version string length to 200 bytes.

CQ-DEPEND=CL:355701
BRANCH=none
BUG=chrome-os-partner:54723
TEST=built the new firmware and ran it on Gru, observed the following
     in the coreboot console log:

  Connected to device vid:did:rid of 1ae0:0028:00
  Firmware version: cr50_v1.1.4792-7a44484

Original-Commit-Id: 1f54a30cebe808abf1b09478b47924bb722a0ca6
Original-Change-Id: Idb069dabb80d34a0efdf04c3c40a42ab0c8a3f94
Original-Signed-off-by: Vadim Bendebury <vbendeb@chromium.org>
Original-Reviewed-on: https://chromium-review.googlesource.com/355704
Original-Reviewed-by: Scott Collyer <scollyer@chromium.org>

Squashed with:

tpm: use 4 byte quantities when retrieving firmware version

The CR50 device is capable of reporting its firmware version in 4 byte
quantities, but the recently introduced code retrieves the version one
byte at a time.

With this fix the version is retrieved in 4 byte chunks.

BRANCH=none
BUG=none
TEST=the version is still reported properly, as reported by the AP
     firmware console log:

localhost ~ # grep cr50 /sys/firmware/log
Firmware version: cr50_v1.1.4804-c64cf24
localhost ~ #

Original-Commit-Id: 3111537e7b66d8507b6608ef665e4cde76403818
Original-Change-Id: I04116881a30001e35e989e51ec1567263f9149a6
Original-Signed-off-by: Vadim Bendebury <vbendeb@chromium.org>
Original-Reviewed-on: https://chromium-review.googlesource.com/356542
Original-Reviewed-by: Andrey Pronin <apronin@chromium.org>

Change-Id: Ia9f13a5bf1c34292b866f57c0d14470fe6ca9853
Signed-off-by: Martin Roth <martinroth@chromium.org>
Reviewed-on: https://review.coreboot.org/15573
Tested-by: build bot (Jenkins)
Reviewed-by: Furquan Shaikh <furquan@google.com>
diff --git a/src/drivers/spi/tpm/tpm.c b/src/drivers/spi/tpm/tpm.c
index b02fc5f..4de62d9 100644
--- a/src/drivers/spi/tpm/tpm.c
+++ b/src/drivers/spi/tpm/tpm.c
@@ -32,6 +32,7 @@
 #define TPM_DATA_FIFO_REG (TPM_LOCALITY_0_SPI_BASE + 0x24)
 #define TPM_DID_VID_REG   (TPM_LOCALITY_0_SPI_BASE + 0xf00)
 #define TPM_RID_REG       (TPM_LOCALITY_0_SPI_BASE + 0xf04)
+#define TPM_FW_VER	  (TPM_LOCALITY_0_SPI_BASE + 0xf90)
 
 /* SPI Interface descriptor used by the driver. */
 struct tpm_spi_if {
@@ -355,6 +356,36 @@
 	printk(BIOS_INFO, "Connected to device vid:did:rid of %4.4x:%4.4x:%2.2x\n",
 	       tpm_info.vendor_id, tpm_info.device_id, tpm_info.revision);
 
+	/* Let's report device FW version if available. */
+	if (tpm_info.vendor_id == 0x1ae0) {
+		int chunk_count = 0;
+		uint32_t chunk = 0;
+		char vstr[sizeof(chunk) + 1];	/* room for 4 chars + zero */
+
+		printk(BIOS_INFO, "Firmware version: ");
+
+		/*
+		 * Does not really matter what's written, this just makes sure
+		 * the version is reported from the beginning.
+		 */
+		tpm2_write_reg(TPM_FW_VER, &chunk, sizeof(chunk));
+
+		/* Print it out in 4 byte chunks. */
+		vstr[sizeof(vstr) - 1] = 0;
+		do {
+			tpm2_read_reg(TPM_FW_VER, vstr, sizeof(chunk));
+			printk(BIOS_INFO, "%s", vstr);
+
+			/*
+			 * While string is not over, and no more than 200
+			 * characters.
+			 * This is likely result in one extra printk()
+			 * invocation with an empty string, not a big deal.
+			 */
+		} while (vstr[0] && (chunk_count++ < (200 / sizeof(chunk))));
+
+		printk(BIOS_INFO, "\n");
+	}
 	return 0;
 }