[coreboot-gerrit] New patch to review for coreboot: drivers/i2c/tpm: Add support for Atmel TPM (AT97SC3204)

Lee Leahy (leroy.p.leahy@intel.com) gerrit at coreboot.org
Tue Mar 14 02:35:15 CET 2017


Lee Leahy (leroy.p.leahy at intel.com) just uploaded a new patch set to gerrit, which you can find at https://review.coreboot.org/18800

-gerrit

commit b8e6531c2750abf449406eae264e7f483df0f7fa
Author: Lee Leahy <leroy.p.leahy at intel.com>
Date:   Thu Jan 5 17:59:38 2017 -0800

    drivers/i2c/tpm: Add support for Atmel TPM (AT97SC3204)
    
    The I2C interface for the Atmel AT97SC3204 TPM varies greatly from the
    existing I2C TPM support.  The Atmel part just passes the commands and
    responses from the TIS layer across the I2C interface.
    
    This code breaks the I2C transfers into multiple sections because the
    Quark SOC only has a 16 byte FIFO to handle the transfers.  This logic
    works here because the Atmel chip does not use a register address as
    part of the command/response in the I2C data format.  Additionally
    the Atmel chip does not care about the transfers being broken into
    multiple pieces.
    
    For I2C controllers which do not have this limitation, the Kconfig
    value CONFIG_DRIVER_I2C_MAX_DATA_BYTES was added.  Use this value
    to specify a larger value to breakup the transfers if necessary.
    Specifying a value of 0xffffffff to issue single I2C transactions
    for the command and response.
    
    TEST=Build and run on Galileo Gen2 with Crypto Shield and vboot enabled
    
    Change-Id: Ib2ef0ffdfc12b2fc11fe4c55b6414924d4b676dd
    Signed-off-by: Lee Leahy <leroy.p.leahy at intel.com>
---
 src/drivers/i2c/tpm/Kconfig      |  21 +++++-
 src/drivers/i2c/tpm/Makefile.inc |  13 ++--
 src/drivers/i2c/tpm/tis_atmel.c  | 150 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 179 insertions(+), 5 deletions(-)

diff --git a/src/drivers/i2c/tpm/Kconfig b/src/drivers/i2c/tpm/Kconfig
index d35eb75..bbd3fd8 100644
--- a/src/drivers/i2c/tpm/Kconfig
+++ b/src/drivers/i2c/tpm/Kconfig
@@ -2,24 +2,38 @@ config I2C_TPM
 	bool "I2C TPM"
 	depends on TPM || TPM2
 
+config MAINBOARD_HAS_I2C_TPM_ATMEL
+	bool
+	default n
+
 config MAINBOARD_HAS_I2C_TPM_CR50
 	bool
 	default n
 
 choice
 	prompt "I2C TPM Driver"
+	default I2C_TPM_ATMEL if MAINBOARD_HAS_I2C_TPM_ATMEL
 	default I2C_TPM_CR50 if MAINBOARD_HAS_I2C_TPM_CR50
-	default I2C_TPM_GENERIC if !MAINBOARD_HAS_I2C_TPM_CR50
+	default I2C_TPM_GENERIC if !MAINBOARD_HAS_I2C_TPM_CR50 && !MAINBOARD_HAS_I2C_TPM_ATMEL
 	depends on I2C_TPM
 
 config I2C_TPM_GENERIC
 	bool "Generic I2C TPM Driver"
 
+config I2C_TPM_ATMEL
+	bool "ATMEL I2C TPM Driver"
+
 config I2C_TPM_CR50
 	bool "CR50 I2C TPM Driver"
 
 endchoice
 
+config DRIVER_TIS_INFINEON
+	bool
+	depends on I2C_TPM
+	default n if MAINBOARD_HAS_I2C_TPM_ATMEL
+	default y
+
 config DRIVER_TPM_I2C_BUS
 	hex "I2C TPM chip bus"
 	default 0x9 # FIXME, workaround for Kconfig BS
@@ -44,3 +58,8 @@ config DRIVER_TPM_DISPLAY_TIS_BYTES
 	bool "TPM: Display the TIS transactions to I2C TPM chip"
 	default n
 	depends on I2C_TPM
+
+config DRIVER_I2C_MAX_DATA_BYTES
+	int
+	default 16
+	depends on I2C_TPM_ATMEL
diff --git a/src/drivers/i2c/tpm/Makefile.inc b/src/drivers/i2c/tpm/Makefile.inc
index 7fcfc78..c8912f7 100644
--- a/src/drivers/i2c/tpm/Makefile.inc
+++ b/src/drivers/i2c/tpm/Makefile.inc
@@ -1,8 +1,13 @@
 
-ramstage-$(CONFIG_I2C_TPM) += tis.c
-romstage-$(CONFIG_I2C_TPM) += tis.c
-verstage-$(CONFIG_I2C_TPM) += tis.c
-bootblock-$(CONFIG_I2C_TPM) += tis.c
+ramstage-$(CONFIG_DRIVER_TIS_INFINEON) += tis.c
+romstage-$(CONFIG_DRIVER_TIS_INFINEON) += tis.c
+verstage-$(CONFIG_DRIVER_TIS_INFINEON) += tis.c
+bootblock-$(CONFIG_DRIVER_TIS_INFINEON) += tis.c
+
+ramstage-$(CONFIG_MAINBOARD_HAS_I2C_TPM_ATMEL) += tis_atmel.c
+romstage-$(CONFIG_MAINBOARD_HAS_I2C_TPM_ATMEL) += tis_atmel.c
+verstage-$(CONFIG_MAINBOARD_HAS_I2C_TPM_ATMEL) += tis_atmel.c
+bootblock-$(CONFIG_MAINBOARD_HAS_I2C_TPM_ATMEL) += tis_atmel.c
 
 ramstage-$(CONFIG_I2C_TPM_GENERIC) += tpm.c
 romstage-$(CONFIG_I2C_TPM_GENERIC) += tpm.c
diff --git a/src/drivers/i2c/tpm/tis_atmel.c b/src/drivers/i2c/tpm/tis_atmel.c
new file mode 100644
index 0000000..bc9d140
--- /dev/null
+++ b/src/drivers/i2c/tpm/tis_atmel.c
@@ -0,0 +1,150 @@
+/*
+ * Copyright (C) 2017 Intel Corporation
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but without any warranty; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#include <arch/early_variables.h>
+#include <stdint.h>
+#include <string.h>
+#include <assert.h>
+#include <console/console.h>
+#include <delay.h>
+#include <device/i2c.h>
+#include <endian.h>
+#include <lib.h>
+#include <swab.h>
+#include <tpm.h>
+#include <timer.h>
+
+#define RECV_TIMEOUT            (1 * 1000)  /* 1 second */
+#define XMIT_TIMEOUT            (1 * 1000)  /* 1 second */
+#define SLEEP_DURATION          1000        /* microseconds */
+
+struct tpm_output_header {
+	uint16_t tag;
+	uint32_t length;
+	uint32_t return_code;
+} __attribute__ ((packed));
+
+int tis_open(void)
+{
+return 0;
+}
+
+int tis_close(void)
+{
+return 0;
+}
+
+int tis_init(void)
+{
+return 0;
+}
+
+long start_time CAR_GLOBAL;
+
+int tis_sendrecv(const uint8_t *sendbuf, size_t sbuf_size,
+		uint8_t *recvbuf, size_t *rbuf_len)
+{
+	size_t hdr_bytes;
+	struct tpm_output_header *header;
+	long *init_time;
+	size_t max_recv_bytes;
+	size_t recv_bytes;
+	int status;
+	struct stopwatch sw;
+
+	init_time = car_get_var_ptr(&start_time);
+	if (IS_ENABLED(CONFIG_DRIVER_TPM_DISPLAY_TIS_BYTES)) {
+		/* Display a time stamp */
+		stopwatch_init(&sw);
+		if (init_time == 0)
+			*init_time = sw.start.microseconds;
+		sw.start.microseconds = *init_time;
+		long time_usec = stopwatch_duration_usecs(&sw);
+		printk(BIOS_DEBUG, "%6d.%03d mSec\n",
+			(int)(time_usec / 1000), (int)(time_usec % 1000));
+
+		/* Display the TPM command */
+		printk(BIOS_DEBUG, "TPM Command: 0x%02x%02x%02x%02x\n",
+			sendbuf[6], sendbuf[7], sendbuf[8], sendbuf[9]);
+		hexdump(sendbuf, sbuf_size);
+	}
+
+	/* Send the command to the TPM */
+	stopwatch_init_msecs_expire(&sw, XMIT_TIMEOUT);
+	while (1) {
+		status = i2c_write_raw(CONFIG_DRIVER_TPM_I2C_BUS,
+			CONFIG_DRIVER_TPM_I2C_ADDR, (uint8_t *)sendbuf,
+			sbuf_size);
+		if ((status < 0) && (!stopwatch_expired(&sw)))
+			continue;
+		if (status < 0)
+			return status;
+		break;
+	}
+
+	/* Read the TPM response header */
+	max_recv_bytes = *rbuf_len;
+	ASSERT(max_recv_bytes >= sizeof(*header));
+	hdr_bytes = sizeof(*header);
+	header = (struct tpm_output_header *)recvbuf;
+	stopwatch_init_msecs_expire(&sw, RECV_TIMEOUT);
+	do {
+		status = i2c_read_raw(CONFIG_DRIVER_TPM_I2C_BUS,
+			CONFIG_DRIVER_TPM_I2C_ADDR, recvbuf, hdr_bytes);
+		if (status > 0)
+			break;
+		udelay(SLEEP_DURATION);
+	} while (!stopwatch_expired(&sw));
+	if (status != sizeof(*header))
+		return -1;
+
+	if (IS_ENABLED(CONFIG_DRIVER_TPM_DISPLAY_TIS_BYTES)) {
+		/* Display a time stamp */
+		stopwatch_init(&sw);
+		sw.start.microseconds = *init_time;
+		long time_usec = stopwatch_duration_usecs(&sw);
+		printk(BIOS_DEBUG, "%6d.%03d mSec\n",
+			(int)(time_usec / 1000), (int)(time_usec % 1000));
+	}
+
+	/* Determine the number of bytes remaining */
+	recv_bytes = min(swab32(header->length), max_recv_bytes);
+
+	/* Determine if there is additional response data */
+	if (recv_bytes > hdr_bytes) {
+		/* Display the TPM response */
+		if (IS_ENABLED(CONFIG_DRIVER_TPM_DISPLAY_TIS_BYTES)) {
+			hexdump(recvbuf, hdr_bytes);
+		}
+
+		/* Read the full TPM response */
+		status = i2c_read_raw(CONFIG_DRIVER_TPM_I2C_BUS,
+			CONFIG_DRIVER_TPM_I2C_ADDR, recvbuf, recv_bytes);
+		if (status < 0)
+			return status;
+	}
+
+	/* Return the number of bytes received */
+	*rbuf_len = status;
+
+	/* Display the TPM response */
+	if (IS_ENABLED(CONFIG_DRIVER_TPM_DISPLAY_TIS_BYTES)) {
+		printk(BIOS_DEBUG, "TPM Response: 0x%02x%02x%02x%02x\n",
+			recvbuf[6], recvbuf[7], recvbuf[8], recvbuf[9]);
+		hexdump(recvbuf, *rbuf_len);
+	}
+
+	/* Successful transfer */
+	return 0;
+}



More information about the coreboot-gerrit mailing list