All of lore.kernel.org
 help / color / mirror / Atom feed
* [U-Boot] [PATCH 1/1] TPM: STMicroelectronics u-boot driver I2C
@ 2013-05-15 13:58 Mathias leblanc
  2013-10-22 15:48 ` Simon Glass
  0 siblings, 1 reply; 13+ messages in thread
From: Mathias leblanc @ 2013-05-15 13:58 UTC (permalink / raw)
  To: u-boot

From: Mathias Leblanc <mathias.leblanc@st.com>

 * STMicroelectronics version 1.2.0, Copyright (C) 2013
 * This is free software, and you are welcome to redistribute it.

This is the u-boot driver for TPM chip from ST Microelectronics.

If you have a TPM security chip from STMicroelectronics working with
an I2C, read the README file and add the correct defines regarding
the tpm in the configuration file of your board.
This file is located in include/configs/your_board.h

The tpm command will be accessible from within uboot terminal.

Signed-off-by: Mathias Leblanc <mathias.leblanc@st.com>
---
 README                         |   14 +-
 common/cmd_tpm.c               |  122 ++++++++
 drivers/tpm/Makefile           |    1 +
 drivers/tpm/slb9635_i2c/tpm.c  |   20 ++
 drivers/tpm/slb9635_i2c/tpm.h  |    1 +
 drivers/tpm/tis_i2c.c          |   37 +++
 drivers/tpm/tpm_i2c_st.c       |  599 ++++++++++++++++++++++++++++++++++++++++
 include/configs/omap3_beagle.h |    8 +
 include/tpm.h                  |   18 ++
 9 files changed, 819 insertions(+), 1 deletion(-)
 create mode 100644 drivers/tpm/tpm_i2c_st.c

diff --git a/README b/README
index 0d37d56..a72b570 100644
--- a/README
+++ b/README
@@ -1208,7 +1208,7 @@ The following options need to be configured:
 			If this option is set, the driver enables cache flush.
 
 - TPM Support:
-		CONFIG_GENERIC_LPC_TPM
+		CONFIG_TPM
 		Support for generic parallel port TPM devices. Only one device
 		per system is supported at this time.
 
@@ -1217,6 +1217,18 @@ The following options need to be configured:
 			to. Contemporary x86 systems usually map it at
 			0xfed40000.
 
+		CONFIG_ST_TPM_I2C
+		Define to compile the ST TPM I2C DRIVER.
+
+		CONFIG_TPM_I2C_BUS
+		Define the bus number of the board.
+
+		CONFIG_TPM_I2C_ADDR
+		Define the address of the TPM.
+
+		CONFIG_CMD_TPM
+		Define to use some TPM u-boot commands.
+
 - USB Support:
 		At the moment only the UHCI host controller is
 		supported (PIP405, MIP405, MPC5200); define
diff --git a/common/cmd_tpm.c b/common/cmd_tpm.c
index 46fae18..fba1fe7 100644
--- a/common/cmd_tpm.c
+++ b/common/cmd_tpm.c
@@ -27,6 +27,14 @@
 #include <asm/unaligned.h>
 #include <linux/string.h>
 
+#define MAX_TRANSACTION_SIZE 30
+#define CHECK(exp) do {							\
+		int _rv = exp;						\
+		if (_rv) {						\
+			printf("CHECK: %s %d %x\n", #exp, __LINE__, _rv);\
+		}							\
+	} while (0)
+
 /**
  * Print a byte string in hexdecimal format, 16-bytes per line.
  *
@@ -546,6 +554,118 @@ static int do_tpm_nv_write(cmd_tbl_t *cmdtp, int flag,
 	return convert_return_code(err);
 }
 
+static int do_tpm_hash(cmd_tbl_t *cmdtp, int flag, int argc,
+char * const argv[])
+{
+	u8 tpm_buffer[MAX_TRANSACTION_SIZE];
+	u32 write_size, read_size;
+	char *p;
+	int rv = -1;
+	argc -= 1;
+	argv += 1;
+	uint8_t response[1024];
+	size_t rlength = MAX_TRANSACTION_SIZE;
+
+	u8 startup[] = {
+		0x00, 0xc1,
+		0x00, 0x00, 0x00, 0x0c,
+		0x00, 0x00, 0x00, 0x99,
+		0x00, 0x01
+	};
+
+	u8 selftestfull[] = {
+		0x00, 0xc1,
+		0x00, 0x00, 0x00, 0x0a,
+		0x00, 0x00, 0x00, 0x50
+	};
+
+	u8 readpcr17[] = {
+		0x00, 0xc1,
+		0x00, 0x00, 0x00, 0x0e,
+		0x00, 0x00, 0x00, 0x15,
+		0x00, 0x00, 0x00, 0x11
+	};
+
+	for (write_size = 0; write_size < argc; write_size++) {
+		u32 datum = kstrtoul(argv[write_size], &p, 0);
+		if (*p || (datum > 0xff)) {
+			printf("\n%s: bad data value\n\n", argv[write_size]);
+			cmd_usage(cmdtp);
+			return rv;
+		}
+		tpm_buffer[write_size] = (u8)datum;
+	}
+
+	if (tis_init()) {
+		puts("tis_init() failed!\n");
+		return -1;
+	}
+
+	if (tis_open()) {
+		puts("tis_open() failed!\n");
+		return -1;
+	}
+
+	rv = tis_sendrecv(startup, sizeof(startup), response, &rlength);
+	if (rv) {
+		printf("tpm test startup failed\n");
+		CHECK(tis_close());
+	}
+
+	rv = tis_sendrecv(selftestfull, sizeof(selftestfull), response,
+		 &rlength);
+	if (rv) {
+		printf("tpm test selftestfull failed\n");
+		CHECK(tis_close());
+	}
+
+	if (!
+	tis_sendrecv(readpcr17, sizeof(readpcr17), response, &read_size)) {
+			int i;
+			puts("TPM Read PCR 17:\n");
+			for (i = 10; i < read_size; i++)
+				printf(" %2.2x", response[i]);
+			puts("\n");
+			rv = 0;
+			} else {
+				printf("tpm test readpcr17 failed\n");
+				CHECK(tis_close());
+			}
+
+	read_size = sizeof(tpm_buffer);
+	if (!
+	tis_sendrecv_hash(tpm_buffer, write_size, tpm_buffer, &read_size)) {
+			int i;
+			puts("Got TPM Hash response:\n");
+			for (i = 0; i < read_size; i++)
+				printf(" %2.2x", tpm_buffer[i]);
+			puts("\n");
+			rv = 0;
+			} else {
+				puts("tpm hash command failed\n");
+				}
+
+	if (!
+	tis_sendrecv(readpcr17, sizeof(readpcr17), response, &read_size)) {
+			int i;
+			puts("TPM Read PCR 17 after hash:\n");
+			for (i = 10; i < read_size; i++)
+				printf(" %2.2x", response[i]);
+			puts("\n");
+			rv = 0;
+			} else {
+				printf("tpm test readpcr17 failed\n");
+				CHECK(tis_close());
+				}
+
+	if (tis_close()) {
+		puts("tis_close() failed!\n");
+		rv = -1;
+	}
+
+	return rv;
+}
+
 #define MAKE_TPM_CMD_ENTRY(cmd) \
 	U_BOOT_CMD_MKENT(cmd, 0, 1, do_tpm_ ## cmd, "", "")
 
@@ -590,6 +710,8 @@ static cmd_tbl_t tpm_commands[] = {
 			do_tpm_nv_read, "", ""),
 	U_BOOT_CMD_MKENT(nv_write, 0, 1,
 			do_tpm_nv_write, "", ""),
+	U_BOOT_CMD_MKENT(hash, 0, 1,
+			do_tpm_hash, "", ""),
 };
 
 static int do_tpm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
diff --git a/drivers/tpm/Makefile b/drivers/tpm/Makefile
index e8c159c..cbececf 100644
--- a/drivers/tpm/Makefile
+++ b/drivers/tpm/Makefile
@@ -28,6 +28,7 @@ $(shell mkdir -p $(obj)slb9635_i2c)
 COBJS-$(CONFIG_GENERIC_LPC_TPM) = generic_lpc_tpm.o
 COBJS-$(CONFIG_INFINEON_TPM_I2C) += tis_i2c.o slb9635_i2c/tpm.o
 COBJS-$(CONFIG_INFINEON_TPM_I2C) += slb9635_i2c/tpm_tis_i2c.o
+COBJS-$(CONFIG_ST_TPM_I2C) = tis_i2c.o tpm_i2c_st.o slb9635_i2c/tpm.o
 
 COBJS	:= $(COBJS-y)
 SRCS	:= $(COBJS:.o=.c)
diff --git a/drivers/tpm/slb9635_i2c/tpm.c b/drivers/tpm/slb9635_i2c/tpm.c
index 496c48e..c92bd06 100644
--- a/drivers/tpm/slb9635_i2c/tpm.c
+++ b/drivers/tpm/slb9635_i2c/tpm.c
@@ -444,6 +444,26 @@ int tpm_open(uint32_t dev_addr)
 	return rc;
 }
 
+ssize_t tpm_transmit_hash(const unsigned char *buf, size_t bufsiz)
+{
+	ssize_t rc;
+
+	struct tpm_chip *chip = &g_chip;
+
+	rc = chip->vendor.send_hash(chip, (u8 *)buf, bufsiz);
+	if (rc < 0) {
+		dev_err(chip->dev, "tpm_transmit: tpm_send: error %zd\n", rc);
+		goto out;
+	}
+
+	dbg_printf("out_recv: reading response...\n");
+	rc = chip->vendor.recv(chip, (u8 *)buf, TPM_BUFSIZE);
+	if (rc < 0)
+		dev_err(chip->dev, "tpm_transmit: tpm_recv: error %zd\n", rc);
+out:
+	return rc;
+}
+
 void tpm_close(void)
 {
 	if (g_chip.is_open) {
diff --git a/drivers/tpm/slb9635_i2c/tpm.h b/drivers/tpm/slb9635_i2c/tpm.h
index 9ddee86..88e0c07 100644
--- a/drivers/tpm/slb9635_i2c/tpm.h
+++ b/drivers/tpm/slb9635_i2c/tpm.h
@@ -64,6 +64,7 @@ struct tpm_vendor_specific {
 	int irq;
 	int (*recv) (struct tpm_chip *, u8 *, size_t);
 	int (*send) (struct tpm_chip *, u8 *, size_t);
+	int (*send_hash) (struct tpm_chip *, u8 *, size_t);
 	void (*cancel) (struct tpm_chip *);
 	 u8(*status) (struct tpm_chip *);
 	int locality;
diff --git a/drivers/tpm/tis_i2c.c b/drivers/tpm/tis_i2c.c
index e818fba..36ae544 100644
--- a/drivers/tpm/tis_i2c.c
+++ b/drivers/tpm/tis_i2c.c
@@ -82,8 +82,13 @@ static int tpm_decode_config(struct tpm *dev)
 	dev->i2c_bus = i2c_bus;
 	dev->slave_addr = fdtdec_get_addr(blob, node, "reg");
 #else
+	#ifdef CONFIG_INFINEON_TPM_I2C_BUS
 	dev->i2c_bus = CONFIG_INFINEON_TPM_I2C_BUS;
 	dev->slave_addr = CONFIG_INFINEON_TPM_I2C_ADDR;
+	#else
+	dev->i2c_bus = CONFIG_TPM_I2C_BUS;
+	dev->slave_addr = CONFIG_TPM_I2C_ADDR;
+	#endif
 #endif
 	return 0;
 }
@@ -179,3 +184,35 @@ int tis_sendrecv(const uint8_t *sendbuf, size_t sbuf_size,
 
 	return 0;
 }
+
+int tis_sendrecv_hash(const uint8_t *sendbuf, size_t sbuf_size,
+		uint8_t *recvbuf, size_t *rbuf_len)
+{
+	int len;
+	uint8_t buf[TPM_BUFSIZE];
+
+	if (!tpm.inited)
+		return -1;
+
+	if (sizeof(buf) < sbuf_size)
+		return -1;
+
+	memcpy(buf, sendbuf, sbuf_size);
+
+	if (tpm_select())
+		return -1;
+
+	len = tpm_transmit_hash(buf, sbuf_size);
+
+	tpm_deselect();
+
+	if (len < 10) {
+		*rbuf_len = 0;
+		return -1;
+	}
+
+	memcpy(recvbuf, buf, len);
+	*rbuf_len = len;
+
+	return 0;
+}
diff --git a/drivers/tpm/tpm_i2c_st.c b/drivers/tpm/tpm_i2c_st.c
new file mode 100644
index 0000000..16753f8
--- /dev/null
+++ b/drivers/tpm/tpm_i2c_st.c
@@ -0,0 +1,599 @@
+/*
+ * STMicroelectronics TPM I2C UBOOT Linux driver for TPM ST33ZP24
+ * Copyright (C) 2013  STMicroelectronics
+ *
+ * (c) Copyright 2013 Mathias Leblanc <mathias.leblanc@st.com>
+ * This file is released under the terms of GPL v2 and any later version
+ * See the file COPYING in the root directory of the source tree for details
+ *
+ * Description:
+ * Device driver for TCG/TCPA TPM (trusted platform module).
+ * Specifications at www.trustedcomputinggroup.org
+ *
+ * This device driver implements the TPM interface as defined in
+ * the TCG TPM Interface Spec version 1.2, revision 1.0 and the
+ * STMicroelectronics I2C Protocol Stack Specification version 1.2.0.
+ *
+ * It is based on the Linux I2C TPM driver from Peter Huewe, modified
+ * from the original tpm
+ * device drivers from Leendert van Dorn, Dave Safford, Reiner Sailer
+ * and Kyleen Hall.
+ *
+ * See file CREDITS for list of people who contributed to this
+ * project.
+ *
+ * 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, version 2 of the
+ * License.
+ *
+ * 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.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
+ * MA 02111-1307 USA
+ *
+ * @Author: Mathias Leblanc tpmsupport at st.com
+ *
+ * @File: tpm_i2c_st.c
+ *
+ */
+
+#include <common.h>
+#include <i2c.h>
+#include <linux/types.h>
+
+#include "slb9635_i2c/compatibility.h"
+#include "slb9635_i2c/tpm.h"
+
+/* max. buffer size supported by our tpm */
+#ifdef TPM_BUFSIZE
+#undef TPM_BUFSIZE
+#endif
+
+#define MINOR_NUM_I2C		224
+
+#define TPM_ACCESS			(0x0)
+#define TPM_STS				(0x18)
+#define TPM_HASH_END			(0x20)
+#define TPM_DATA_FIFO			(0x24)
+#define TPM_HASH_DATA			(0x24)
+#define TPM_HASH_START			(0x28)
+#define TPM_INTF_CAPABILITY		(0x14)
+#define TPM_INT_STATUS			(0x10)
+#define TPM_INT_ENABLE			(0x08)
+
+#define TPM_DUMMY_BYTE			0xAA
+#define TPM_WRITE_DIRECTION		0x80
+#define TPM_HEADER_SIZE			10
+#define TPM_BUFSIZE			2048
+
+#define LOCALITY0		0
+#define LOCALITY4		4
+
+struct st_tpm_hash {
+	int size;
+	u8 *data;
+};
+
+enum stm33zp24_access {
+	TPM_ACCESS_VALID = 0x80,
+	TPM_ACCESS_ACTIVE_LOCALITY = 0x20,
+	TPM_ACCESS_REQUEST_PENDING = 0x04,
+	TPM_ACCESS_REQUEST_USE = 0x02,
+};
+
+enum stm33zp24_status {
+	TPM_STS_VALID = 0x80,
+	TPM_STS_COMMAND_READY = 0x40,
+	TPM_STS_GO = 0x20,
+	TPM_STS_DATA_AVAIL = 0x10,
+	TPM_STS_DATA_EXPECT = 0x08,
+};
+
+enum stm33zp24_int_flags {
+	TPM_GLOBAL_INT_ENABLE = 0x80,
+	TPM_INTF_CMD_READY_INT = 0x080,
+	TPM_INTF_FIFO_AVALAIBLE_INT = 0x040,
+	TPM_INTF_WAKE_UP_READY_INT = 0x020,
+	TPM_INTF_LOC4SOFTRELEASE_INT = 0x008,
+	TPM_INTF_LOCALITY_CHANGE_INT = 0x004,
+	TPM_INTF_STS_VALID_INT = 0x002,
+	TPM_INTF_DATA_AVAIL_INT = 0x001,
+};
+
+enum tis_defaults {
+	TIS_SHORT_TIMEOUT = 750,	/* ms */
+	TIS_LONG_TIMEOUT = 2000,	/* 2 sec */
+};
+
+struct tpm_i2c_ST_dev {
+	uint addr;
+	u8 buf[TPM_BUFSIZE];
+};
+
+static struct tpm_i2c_ST_dev tpm_dev = {
+		/* Note: replace with defined addr from board configuration */
+		.addr = CONFIG_TPM_I2C_ADDR
+};
+
+/*
+ * write8_reg
+ * Send byte to the TIS register according to the ST33ZP24 I2C protocol.
+ * @param: tpm_register, the tpm tis register where the data should be written
+ * @param: tpm_data, the tpm_data to write inside the tpm_register
+ * @param: tpm_size, The length of the data
+ * @return: Returns zero in case of success else the negative error code.
+ */
+static int write8_reg(u8 addr, u8 tpm_register,
+		      u8 *tpm_data, u16 tpm_size)
+{
+	u8 data;
+	data = tpm_register;
+	memcpy(&(tpm_dev.buf[0]), &data, sizeof(data));
+	memcpy(&(tpm_dev.buf[0])+1, tpm_data, tpm_size);
+
+	return i2c_write(addr, 0, 0, &tpm_dev.buf[0],
+				tpm_size + 1);
+
+} /* write8_reg() */
+
+/*
+* read8_reg
+* Recv byte from the TIS register according to the ST33ZP24 I2C protocol.
+* @param: tpm_register, the tpm tis register where the data should be read
+* @param: tpm_data, the TPM response
+* @param: tpm_size, tpm TPM response size to read.
+* @return: Returns zero in case of success else the negative error code.
+*/
+static int read8_reg(u8 addr, u8 tpm_register,
+u8 *tpm_data, int tpm_size)
+{
+	u8 status = 0;
+	u8 data;
+	data = TPM_DUMMY_BYTE;
+	status = write8_reg(addr, tpm_register, &data, 1);
+	if (status == 0)
+		status = i2c_read(addr, 0, 0, tpm_data, tpm_size);
+return status;
+} /* read8_reg() */
+
+/*
+ * I2C_WRITE_DATA
+ * Send byte to the TIS register according to the ST33ZP24 I2C protocol.
+ * @param: client, the chip description
+ * @param: tpm_register, the tpm tis register where the data should be written
+ * @param: tpm_data, the tpm_data to write inside the tpm_register
+ * @param: tpm_size, The length of the data
+ * @return: Returns zero in case of success else the negative error code.
+ */
+#define I2C_WRITE_DATA(client, tpm_register, tpm_data, tpm_size)\
+	 (write8_reg(client, tpm_register | \
+	TPM_WRITE_DIRECTION, tpm_data, tpm_size))
+
+/*
+ * I2C_READ_DATA
+ * Recv byte from the TIS register according to the ST33ZP24 I2C protocol.
+ * @param: tpm, the chip description
+ * @param: tpm_register, the tpm tis register where the data should be read
+ * @param: tpm_data, the TPM response
+ * @param: tpm_size, tpm TPM response size to read.
+ * @return: Returns zero in case of success else the negative error code.
+ */
+#define I2C_READ_DATA(client, tpm_register, tpm_data, tpm_size)\
+	 (read8_reg(client, tpm_register, tpm_data, tpm_size))
+
+/*
+ * release_locality release the active locality
+ * @param: chip, the tpm chip description.
+ */
+static void release_locality(struct tpm_chip *chip)
+{
+	u8 data = TPM_ACCESS_ACTIVE_LOCALITY;
+
+	I2C_WRITE_DATA(tpm_dev.addr, TPM_ACCESS, &data, 1);
+}
+
+/*
+ * clear_interruption
+ * clear the TPM interrupt register.
+ * @param: tpm, the chip description
+ */
+static void clear_interruption(u8 addr)
+{
+	u8 interrupt;
+	I2C_READ_DATA(tpm_dev.addr, TPM_INT_STATUS, &interrupt, 1);
+	I2C_WRITE_DATA(tpm_dev.addr, TPM_INT_STATUS, &interrupt, 1);
+	I2C_READ_DATA(tpm_dev.addr, TPM_INT_STATUS, &interrupt, 1);
+} /* clear_interruption() */
+
+int wait_for_serirq_timeout(struct tpm_chip *chip, int condition,
+	 unsigned long timeout)
+{
+	int status = 2;
+
+	clear_interruption(tpm_dev.addr);
+	if (condition)
+		status = 1;
+
+	return status;
+}
+
+/*
+ * check_locality if the locality is active
+ * @param: chip, the tpm chip description
+ * @return: the active locality or -EACCESS.
+ */
+static int check_locality(struct tpm_chip *chip)
+{
+	u8 data;
+	u8 status;
+	status = I2C_READ_DATA(tpm_dev.addr, TPM_ACCESS, &data, 1);
+
+	if ((status == 0) && (data &
+		(TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID)) ==
+		(TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID))
+		return chip->vendor.locality;
+
+	return -EACCES;
+
+} /* check_locality() */
+
+/*
+ * request_locality request the TPM locality
+ * @param: chip, the chip description
+ * @return: the active locality or EACCESS.
+ */
+static int request_locality(struct tpm_chip *chip)
+{
+	unsigned long start, stop;
+	long rc;
+	u8 data;
+	if (check_locality(chip) == chip->vendor.locality)
+		return chip->vendor.locality;
+
+	data = TPM_ACCESS_REQUEST_USE;
+	rc = I2C_WRITE_DATA(tpm_dev.addr, TPM_ACCESS, &data, 1);
+	if (rc < 0)
+		goto end;
+
+	if (chip->vendor.irq) {
+		rc = wait_for_serirq_timeout(chip, (check_locality
+						       (chip) >= 0),
+						      chip->vendor.timeout_a);
+		if (rc > 0)
+			return chip->vendor.locality;
+	} else{
+	/* wait for locality activated */
+	start = get_timer(0);
+	stop = chip->vendor.timeout_a;
+		do {
+			if (check_locality(chip) >= 0)
+				return chip->vendor.locality;
+
+			msleep(TPM_TIMEOUT);
+		} while	 (get_timer(start) < stop);
+	}
+	rc = -EACCES;
+end:
+	return rc;
+} /* request_locality() */
+
+/*
+ * tpm_stm_i2c_cancel, cancel is not implemented.
+ * @param: chip, tpm_chip description.
+ */
+static void tpm_stm_i2c_cancel(struct tpm_chip *chip)
+{
+	u8 data;
+
+	data = TPM_STS_COMMAND_READY;
+	I2C_WRITE_DATA(tpm_dev.addr, TPM_STS, &data, 1);
+	if (chip->vendor.irq)
+		wait_for_serirq_timeout(chip, 1, chip->vendor.timeout_a);
+}	/* tpm_stm_i2c_cancel() */
+
+/*
+ * tpm_stm_spi_status return the TPM_STS register
+ * @param: chip, the tpm chip description
+ * @return: the TPM_STS register value.
+ */
+static u8 tpm_stm_i2c_status(struct tpm_chip *chip)
+{
+	u8 data;
+	I2C_READ_DATA(tpm_dev.addr, TPM_STS, &data, 1);
+	return data;
+}				/* tpm_stm_i2c_status() */
+
+/*
+ * get_burstcount return the burstcount address 0x19 0x1A
+ * @param: chip, the chip description
+ * return: the burstcount.
+ */
+static int get_burstcount(struct tpm_chip *chip)
+{
+	unsigned long start, stop;
+	int burstcnt, status;
+	u8 tpm_reg, temp;
+
+	/* wait for burstcount */
+	/* which timeout value, spec has 2 answers (c & d) */
+	start = get_timer(0);
+	stop = chip->vendor.timeout_d;
+	do {
+		tpm_reg = TPM_STS + 1;
+		status = I2C_READ_DATA(tpm_dev.addr, tpm_reg, &temp, 1);
+		if (status < 0)
+			goto end;
+
+		tpm_reg = tpm_reg + 1;
+		burstcnt = temp;
+		status = I2C_READ_DATA(tpm_dev.addr, tpm_reg, &temp, 1);
+		if (status < 0)
+			goto end;
+
+		burstcnt |= temp << 8;
+		if (burstcnt)
+			return burstcnt;
+
+		msleep(TPM_TIMEOUT);
+	} while (get_timer(start) < stop);
+
+end:
+	return -EBUSY;
+} /* get_burstcount() */
+
+
+/*
+ * recv_data receive data
+ * @param: chip, the tpm chip description
+ * @param: buf, the buffer where the data are received
+ * @param: count, the number of data to receive
+ * @return: the number of bytes read from TPM FIFO.
+ */
+static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count)
+{
+	int size = 0, burstcnt, len;
+
+	while (size < count) {
+		burstcnt = get_burstcount(chip);
+		len = count - size;
+		if ((len) > burstcnt)
+			len = burstcnt;
+	if (
+	I2C_READ_DATA(tpm_dev.addr, TPM_DATA_FIFO, buf + size, len) == 0)
+			size += len;
+		else
+			break;
+	}
+	return size;
+} /* recv_data() */
+
+/*
+ * tpm_stm_i2c_recv received TPM response through the I2C bus.
+ * @param: chip, tpm_chip description.
+ * @param: buf,	the buffer to store datas.
+ * @param: count, the number of bytes to send.
+ * @return: Returns zero in case of success else the negative error code.
+ */
+static int tpm_stm_i2c_recv(struct tpm_chip *chip, unsigned char *buf,
+			    size_t count)
+{
+	int size = 0;
+	int expected;
+
+	if (chip == NULL)
+		return -EBUSY;
+
+	if (count < TPM_HEADER_SIZE) {
+		size = -EIO;
+		goto out;
+	}
+
+	size = recv_data(chip, buf, TPM_HEADER_SIZE);
+	if (size < TPM_HEADER_SIZE) {
+		dev_err(chip->dev, "Unable to read header\n");
+		goto out;
+	}
+
+
+	expected = get_unaligned_be32(buf + TPM_RSP_SIZE_BYTE);
+	if (expected > count) {
+		size = -EIO;
+		goto out;
+	}
+
+	size += recv_data(chip, &buf[TPM_HEADER_SIZE],
+					expected - TPM_HEADER_SIZE);
+	if (size < expected) {
+		dev_err(chip->dev, "Unable to read remainder of result\n");
+		size = -ETIME;
+		goto out;
+	}
+
+out:
+	chip->vendor.cancel(chip);
+	release_locality(chip);
+	return size;
+} /* tpm_stm_i2c_recv() */
+
+/*
+ * tpm_stm_i2c_send send TPM commands through the I2C bus.
+ *
+ * @param: chip, tpm_chip description.
+ * @param: buf,	the buffer to send.
+ * @param: len, the number of bytes to send.
+ * @return: Returns zero in case of success else the negative error code.
+ */
+static int tpm_stm_i2c_send(struct tpm_chip *chip, u8 *buf,
+			    size_t len)
+{
+	u32 ret = 0,
+	    status,
+	    burstcnt = 0, i, size;
+	u8 data;
+
+	if (chip == NULL)
+		return -EBUSY;
+	if (len < TPM_HEADER_SIZE)
+		return -EBUSY;
+
+	ret = request_locality(chip);
+	if (ret < 0)
+		return ret;
+
+	status = tpm_stm_i2c_status(chip);
+	if ((status & TPM_STS_COMMAND_READY) == 0)
+		tpm_stm_i2c_cancel(chip);
+
+	for (i = 0; i < len - 1;) {
+		burstcnt = get_burstcount(chip);
+		size = len - i - 1;
+		if ((size) > burstcnt)
+			size = burstcnt;
+		ret = I2C_WRITE_DATA(tpm_dev.addr, TPM_DATA_FIFO, buf, size);
+		if (ret < 0)
+			goto out_err;
+
+		i += size;
+	}
+
+	status = tpm_stm_i2c_status(chip);
+	if ((status & TPM_STS_DATA_EXPECT) == 0) {
+		ret = -EIO;
+		goto out_err;
+	}
+
+	ret = I2C_WRITE_DATA(tpm_dev.addr, TPM_DATA_FIFO, buf + len - 1, 1);
+	if (ret < 0)
+		goto out_err;
+
+	status = tpm_stm_i2c_status(chip);
+	if ((status & TPM_STS_DATA_EXPECT) != 0) {
+		ret = -EIO;
+		goto out_err;
+	}
+
+	data = TPM_STS_GO;
+	I2C_WRITE_DATA(tpm_dev.addr, TPM_STS, &data, 1);
+
+	return len;
+out_err:
+	tpm_stm_i2c_cancel(chip);
+	release_locality(chip);
+	return ret;
+} /* tpm_stm_i2c_send() */
+
+/*
+ * tpm_stm_i2c_send_hash send TPM locality 4 hash datas through the I2C bus
+ * to update the PCR[17].
+ * @param: chip, the tpm_chip description.
+ * @param: buf,	the data buffer to send.
+ * @param: len, the number of bytes to send.
+ * @return: Returns zero in case of success else the negative error code.
+ */
+static int tpm_stm_i2c_send_hash(struct tpm_chip *chip, unsigned char *buf,
+			    size_t len)
+{
+	u32 ret = 0;
+	u8 data;
+
+	if (chip == NULL)
+		return -EBUSY;
+
+	release_locality(chip);
+
+	tpm_dev.addr = 0x1B;
+	chip->vendor.locality = LOCALITY4;
+
+	data = TPM_DUMMY_BYTE;
+	ret = I2C_WRITE_DATA(tpm_dev.addr, TPM_HASH_START, &data, 1);
+	if (ret < 0)
+		goto end;
+	ret = I2C_WRITE_DATA(tpm_dev.addr, TPM_DATA_FIFO, buf, len);
+	if (ret < 0)
+		goto end;
+
+end:
+	I2C_WRITE_DATA(tpm_dev.addr, TPM_HASH_END, &data, 1);
+	release_locality(chip);
+	chip->vendor.locality = LOCALITY0;
+	tpm_dev.addr = 0x13;
+	ret = request_locality(chip);
+	return ret;
+} /* tpm_stm_i2c_send_hash */
+
+static struct tpm_vendor_specific st_i2c_tpm = {
+	.send = tpm_stm_i2c_send,
+	.send_hash = tpm_stm_i2c_send_hash,
+	.recv = tpm_stm_i2c_recv,
+	.cancel = tpm_stm_i2c_cancel,
+	.status = tpm_stm_i2c_status,
+	.req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
+	.req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
+	.req_canceled = TPM_STS_COMMAND_READY,
+};
+
+/*
+ * tpm_vendor_init initialize the TPM device
+ * @param: dev_addr, the i2c address of the tpm.
+ * @return: 0 in case of success.
+ *	 -1 in other case.
+ */
+int tpm_vendor_init(uint32_t dev_addr)
+{
+	u32 vendor;
+	uint old_addr;
+	int rc = 0;
+	struct tpm_chip *chip;
+
+	old_addr = tpm_dev.addr;
+	if (dev_addr != 0)
+		tpm_dev.addr = dev_addr;
+
+	chip = tpm_register_hardware(&st_i2c_tpm);
+
+	if (chip < 0) {
+		rc = -ENODEV;
+		goto out_err;
+	}
+
+	/* Default timeouts */
+	chip->vendor.timeout_a = TIS_SHORT_TIMEOUT;
+	chip->vendor.timeout_b = TIS_LONG_TIMEOUT;
+	chip->vendor.timeout_c = TIS_SHORT_TIMEOUT;
+	chip->vendor.timeout_d = TIS_SHORT_TIMEOUT;
+
+	chip->vendor.locality = LOCALITY0;
+
+	if (request_locality(chip) != 0) {
+		rc = -ENODEV;
+		goto out_err;
+	}
+
+	vendor = be32_to_cpu(vendor);
+
+
+	dev_info(dev, "1.2 TPM STMicroelectronics");
+	/*
+	 * A timeout query to TPM can be placed here.
+	 * Standard timeout values are used so far
+	 */
+
+	return 0;
+
+out_err:
+	tpm_dev.addr = old_addr;
+	return rc;
+} /* tpm_vendor_init() */
+
+
+
+void tpm_vendor_cleanup(struct tpm_chip *chip)
+{
+	release_locality(chip);
+} /* tpm_vendor_cleanup() */
diff --git a/include/configs/omap3_beagle.h b/include/configs/omap3_beagle.h
index 48ce4c0..ef381f8 100644
--- a/include/configs/omap3_beagle.h
+++ b/include/configs/omap3_beagle.h
@@ -111,6 +111,14 @@
 #define STATUS_LED_BOOT			STATUS_LED_BIT
 #define STATUS_LED_GREEN		STATUS_LED_BIT1
 
+/* TPM */
+#define CONFIG_CMD_TPM
+#define CONFIG_TPM
+#define CONFIG_ST_TPM_DEBUG
+#define CONFIG_ST_TPM_I2C
+#define CONFIG_TPM_I2C_BUS	1
+#define CONFIG_TPM_I2C_ADDR	0x13
+
 /* Enable Multi Bus support for I2C */
 #define CONFIG_I2C_MULTI_BUS		1
 
diff --git a/include/tpm.h b/include/tpm.h
index 7219b73..36ba0bb 100644
--- a/include/tpm.h
+++ b/include/tpm.h
@@ -201,4 +201,22 @@ uint32_t tpm_physical_set_deactivated(uint8_t state);
 uint32_t tpm_get_capability(uint32_t cap_area, uint32_t sub_cap,
 		void *cap, size_t count);
 
+/*
+ * tis_sendrecv_hash()
+ *
+ * Send the requested data to the TPM for hash in LOC 4
+ * and then try to get its response
+ *
+ * @sendbuf - buffer of the data to hash
+ * @send_size size of the data to send
+ * @recvbuf - memory to save the response to
+ * @recv_len - pointer to the size of the response buffer
+ *
+ * Returns 0 on success (and places the number of response bytes at recv_len)
+ * or -1 on failure.
+ */
+int tis_sendrecv_hash(const uint8_t *sendbuf, size_t send_size,
+	 uint8_t *recvbuf,
+			size_t *recv_len);
+
 #endif /* __TPM_H */
-- 
1.7.9.5

^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [U-Boot] [PATCH 1/1] TPM: STMicroelectronics u-boot driver I2C
  2013-05-15 13:58 [U-Boot] [PATCH 1/1] TPM: STMicroelectronics u-boot driver I2C Mathias leblanc
@ 2013-10-22 15:48 ` Simon Glass
  2014-02-16 22:46   ` Simon Glass
  2014-02-17 22:23   ` Simon Glass
  0 siblings, 2 replies; 13+ messages in thread
From: Simon Glass @ 2013-10-22 15:48 UTC (permalink / raw)
  To: u-boot

Hi Mathias,

On Wed, May 15, 2013 at 7:58 AM, Mathias leblanc <mathias.leblanc@st.com> wrote:
> From: Mathias Leblanc <mathias.leblanc@st.com>
>
>  * STMicroelectronics version 1.2.0, Copyright (C) 2013
>  * This is free software, and you are welcome to redistribute it.
>
> This is the u-boot driver for TPM chip from ST Microelectronics.
>
> If you have a TPM security chip from STMicroelectronics working with
> an I2C, read the README file and add the correct defines regarding
> the tpm in the configuration file of your board.
> This file is located in include/configs/your_board.h
>
> The tpm command will be accessible from within uboot terminal.
>
> Signed-off-by: Mathias Leblanc <mathias.leblanc@st.com>

It would be good to apply this patch in this merge window. Can you
please do a few tidy-ups?

> ---
>  README                         |   14 +-
>  common/cmd_tpm.c               |  122 ++++++++
>  drivers/tpm/Makefile           |    1 +
>  drivers/tpm/slb9635_i2c/tpm.c  |   20 ++
>  drivers/tpm/slb9635_i2c/tpm.h  |    1 +
>  drivers/tpm/tis_i2c.c          |   37 +++
>  drivers/tpm/tpm_i2c_st.c       |  599 ++++++++++++++++++++++++++++++++++++++++
>  include/configs/omap3_beagle.h |    8 +

Please split the driver, command change (cmd_tpm.c) and the CONFIG
change (this file) into three separate patches.

>  include/tpm.h                  |   18 ++
>  9 files changed, 819 insertions(+), 1 deletion(-)
>  create mode 100644 drivers/tpm/tpm_i2c_st.c
>
> diff --git a/README b/README
> index 0d37d56..a72b570 100644
> --- a/README
> +++ b/README
> @@ -1208,7 +1208,7 @@ The following options need to be configured:
>                         If this option is set, the driver enables cache flush.
>
>  - TPM Support:
> -               CONFIG_GENERIC_LPC_TPM
> +               CONFIG_TPM
>                 Support for generic parallel port TPM devices. Only one device
>                 per system is supported at this time.
>
> @@ -1217,6 +1217,18 @@ The following options need to be configured:
>                         to. Contemporary x86 systems usually map it at
>                         0xfed40000.
>
> +               CONFIG_ST_TPM_I2C
> +               Define to compile the ST TPM I2C DRIVER.
> +
> +               CONFIG_TPM_I2C_BUS
> +               Define the bus number of the board.
> +
> +               CONFIG_TPM_I2C_ADDR
> +               Define the address of the TPM.
> +
> +               CONFIG_CMD_TPM
> +               Define to use some TPM u-boot commands.
> +
>  - USB Support:
>                 At the moment only the UHCI host controller is
>                 supported (PIP405, MIP405, MPC5200); define
> diff --git a/common/cmd_tpm.c b/common/cmd_tpm.c
> index 46fae18..fba1fe7 100644
> --- a/common/cmd_tpm.c
> +++ b/common/cmd_tpm.c
> @@ -27,6 +27,14 @@
>  #include <asm/unaligned.h>
>  #include <linux/string.h>
>
> +#define MAX_TRANSACTION_SIZE 30
> +#define CHECK(exp) do {                                                        \
> +               int _rv = exp;                                          \
> +               if (_rv) {                                              \
> +                       printf("CHECK: %s %d %x\n", #exp, __LINE__, _rv);\
> +               }                                                       \
> +       } while (0)
> +
>  /**
>   * Print a byte string in hexdecimal format, 16-bytes per line.
>   *
> @@ -546,6 +554,118 @@ static int do_tpm_nv_write(cmd_tbl_t *cmdtp, int flag,
>         return convert_return_code(err);
>  }
>
> +static int do_tpm_hash(cmd_tbl_t *cmdtp, int flag, int argc,
> +char * const argv[])
> +{
> +       u8 tpm_buffer[MAX_TRANSACTION_SIZE];
> +       u32 write_size, read_size;
> +       char *p;
> +       int rv = -1;
> +       argc -= 1;
> +       argv += 1;
> +       uint8_t response[1024];
> +       size_t rlength = MAX_TRANSACTION_SIZE;
> +
> +       u8 startup[] = {
> +               0x00, 0xc1,
> +               0x00, 0x00, 0x00, 0x0c,
> +               0x00, 0x00, 0x00, 0x99,
> +               0x00, 0x01
> +       };
> +
> +       u8 selftestfull[] = {
> +               0x00, 0xc1,
> +               0x00, 0x00, 0x00, 0x0a,
> +               0x00, 0x00, 0x00, 0x50
> +       };
> +
> +       u8 readpcr17[] = {
> +               0x00, 0xc1,
> +               0x00, 0x00, 0x00, 0x0e,
> +               0x00, 0x00, 0x00, 0x15,
> +               0x00, 0x00, 0x00, 0x11
> +       };
> +
> +       for (write_size = 0; write_size < argc; write_size++) {
> +               u32 datum = kstrtoul(argv[write_size], &p, 0);
> +               if (*p || (datum > 0xff)) {
> +                       printf("\n%s: bad data value\n\n", argv[write_size]);
> +                       cmd_usage(cmdtp);
> +                       return rv;
> +               }
> +               tpm_buffer[write_size] = (u8)datum;
> +       }
> +
> +       if (tis_init()) {
> +               puts("tis_init() failed!\n");
> +               return -1;
> +       }
> +
> +       if (tis_open()) {
> +               puts("tis_open() failed!\n");
> +               return -1;
> +       }
> +
> +       rv = tis_sendrecv(startup, sizeof(startup), response, &rlength);
> +       if (rv) {
> +               printf("tpm test startup failed\n");
> +               CHECK(tis_close());
> +       }
> +
> +       rv = tis_sendrecv(selftestfull, sizeof(selftestfull), response,
> +                &rlength);
> +       if (rv) {
> +               printf("tpm test selftestfull failed\n");
> +               CHECK(tis_close());
> +       }
> +
> +       if (!
> +       tis_sendrecv(readpcr17, sizeof(readpcr17), response, &read_size)) {
> +                       int i;

Blank line between declarations and body of function. Please fix globally.

> +                       puts("TPM Read PCR 17:\n");
> +                       for (i = 10; i < read_size; i++)
> +                               printf(" %2.2x", response[i]);
> +                       puts("\n");
> +                       rv = 0;
> +                       } else {

Suspect indenting here, please fix globally.

> +                               printf("tpm test readpcr17 failed\n");
> +                               CHECK(tis_close());
> +                       }
> +
> +       read_size = sizeof(tpm_buffer);
> +       if (!

Remove newline here. Please fix below also.

> +       tis_sendrecv_hash(tpm_buffer, write_size, tpm_buffer, &read_size)) {
> +                       int i;
> +                       puts("Got TPM Hash response:\n");
> +                       for (i = 0; i < read_size; i++)
> +                               printf(" %2.2x", tpm_buffer[i]);
> +                       puts("\n");
> +                       rv = 0;
> +                       } else {
> +                               puts("tpm hash command failed\n");
> +                               }
> +
> +       if (!
> +       tis_sendrecv(readpcr17, sizeof(readpcr17), response, &read_size)) {
> +                       int i;
> +                       puts("TPM Read PCR 17 after hash:\n");
> +                       for (i = 10; i < read_size; i++)
> +                               printf(" %2.2x", response[i]);
> +                       puts("\n");
> +                       rv = 0;
> +                       } else {
> +                               printf("tpm test readpcr17 failed\n");
> +                               CHECK(tis_close());
> +                               }
> +
> +       if (tis_close()) {
> +               puts("tis_close() failed!\n");
> +               rv = -1;
> +       }
> +
> +       return rv;
> +}
> +
>  #define MAKE_TPM_CMD_ENTRY(cmd) \
>         U_BOOT_CMD_MKENT(cmd, 0, 1, do_tpm_ ## cmd, "", "")
>
> @@ -590,6 +710,8 @@ static cmd_tbl_t tpm_commands[] = {
>                         do_tpm_nv_read, "", ""),
>         U_BOOT_CMD_MKENT(nv_write, 0, 1,
>                         do_tpm_nv_write, "", ""),
> +       U_BOOT_CMD_MKENT(hash, 0, 1,
> +                       do_tpm_hash, "", ""),
>  };
>
>  static int do_tpm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
> diff --git a/drivers/tpm/Makefile b/drivers/tpm/Makefile
> index e8c159c..cbececf 100644
> --- a/drivers/tpm/Makefile
> +++ b/drivers/tpm/Makefile
> @@ -28,6 +28,7 @@ $(shell mkdir -p $(obj)slb9635_i2c)
>  COBJS-$(CONFIG_GENERIC_LPC_TPM) = generic_lpc_tpm.o
>  COBJS-$(CONFIG_INFINEON_TPM_I2C) += tis_i2c.o slb9635_i2c/tpm.o
>  COBJS-$(CONFIG_INFINEON_TPM_I2C) += slb9635_i2c/tpm_tis_i2c.o
> +COBJS-$(CONFIG_ST_TPM_I2C) = tis_i2c.o tpm_i2c_st.o slb9635_i2c/tpm.o
>
>  COBJS  := $(COBJS-y)
>  SRCS   := $(COBJS:.o=.c)
> diff --git a/drivers/tpm/slb9635_i2c/tpm.c b/drivers/tpm/slb9635_i2c/tpm.c
> index 496c48e..c92bd06 100644
> --- a/drivers/tpm/slb9635_i2c/tpm.c
> +++ b/drivers/tpm/slb9635_i2c/tpm.c
> @@ -444,6 +444,26 @@ int tpm_open(uint32_t dev_addr)
>         return rc;
>  }
>
> +ssize_t tpm_transmit_hash(const unsigned char *buf, size_t bufsiz)
> +{
> +       ssize_t rc;
> +
> +       struct tpm_chip *chip = &g_chip;
> +
> +       rc = chip->vendor.send_hash(chip, (u8 *)buf, bufsiz);
> +       if (rc < 0) {
> +               dev_err(chip->dev, "tpm_transmit: tpm_send: error %zd\n", rc);
> +               goto out;
> +       }
> +
> +       dbg_printf("out_recv: reading response...\n");
> +       rc = chip->vendor.recv(chip, (u8 *)buf, TPM_BUFSIZE);
> +       if (rc < 0)
> +               dev_err(chip->dev, "tpm_transmit: tpm_recv: error %zd\n", rc);
> +out:
> +       return rc;
> +}
> +
>  void tpm_close(void)
>  {
>         if (g_chip.is_open) {
> diff --git a/drivers/tpm/slb9635_i2c/tpm.h b/drivers/tpm/slb9635_i2c/tpm.h
> index 9ddee86..88e0c07 100644
> --- a/drivers/tpm/slb9635_i2c/tpm.h
> +++ b/drivers/tpm/slb9635_i2c/tpm.h
> @@ -64,6 +64,7 @@ struct tpm_vendor_specific {
>         int irq;
>         int (*recv) (struct tpm_chip *, u8 *, size_t);
>         int (*send) (struct tpm_chip *, u8 *, size_t);
> +       int (*send_hash) (struct tpm_chip *, u8 *, size_t);
>         void (*cancel) (struct tpm_chip *);
>          u8(*status) (struct tpm_chip *);
>         int locality;
> diff --git a/drivers/tpm/tis_i2c.c b/drivers/tpm/tis_i2c.c
> index e818fba..36ae544 100644
> --- a/drivers/tpm/tis_i2c.c
> +++ b/drivers/tpm/tis_i2c.c
> @@ -82,8 +82,13 @@ static int tpm_decode_config(struct tpm *dev)
>         dev->i2c_bus = i2c_bus;
>         dev->slave_addr = fdtdec_get_addr(blob, node, "reg");
>  #else
> +       #ifdef CONFIG_INFINEON_TPM_I2C_BUS
>         dev->i2c_bus = CONFIG_INFINEON_TPM_I2C_BUS;
>         dev->slave_addr = CONFIG_INFINEON_TPM_I2C_ADDR;
> +       #else
> +       dev->i2c_bus = CONFIG_TPM_I2C_BUS;
> +       dev->slave_addr = CONFIG_TPM_I2C_ADDR;
> +       #endif
>  #endif
>         return 0;
>  }
> @@ -179,3 +184,35 @@ int tis_sendrecv(const uint8_t *sendbuf, size_t sbuf_size,
>
>         return 0;
>  }
> +
> +int tis_sendrecv_hash(const uint8_t *sendbuf, size_t sbuf_size,
> +               uint8_t *recvbuf, size_t *rbuf_len)
> +{
> +       int len;
> +       uint8_t buf[TPM_BUFSIZE];
> +
> +       if (!tpm.inited)
> +               return -1;
> +
> +       if (sizeof(buf) < sbuf_size)
> +               return -1;
> +
> +       memcpy(buf, sendbuf, sbuf_size);
> +
> +       if (tpm_select())
> +               return -1;
> +
> +       len = tpm_transmit_hash(buf, sbuf_size);
> +
> +       tpm_deselect();
> +
> +       if (len < 10) {
> +               *rbuf_len = 0;
> +               return -1;
> +       }
> +
> +       memcpy(recvbuf, buf, len);
> +       *rbuf_len = len;
> +
> +       return 0;
> +}
> diff --git a/drivers/tpm/tpm_i2c_st.c b/drivers/tpm/tpm_i2c_st.c
> new file mode 100644
> index 0000000..16753f8
> --- /dev/null
> +++ b/drivers/tpm/tpm_i2c_st.c
> @@ -0,0 +1,599 @@
> +/*
> + * STMicroelectronics TPM I2C UBOOT Linux driver for TPM ST33ZP24
> + * Copyright (C) 2013  STMicroelectronics
> + *
> + * (c) Copyright 2013 Mathias Leblanc <mathias.leblanc@st.com>
> + * This file is released under the terms of GPL v2 and any later version
> + * See the file COPYING in the root directory of the source tree for details
> + *
> + * Description:
> + * Device driver for TCG/TCPA TPM (trusted platform module).
> + * Specifications at www.trustedcomputinggroup.org
> + *
> + * This device driver implements the TPM interface as defined in
> + * the TCG TPM Interface Spec version 1.2, revision 1.0 and the
> + * STMicroelectronics I2C Protocol Stack Specification version 1.2.0.
> + *
> + * It is based on the Linux I2C TPM driver from Peter Huewe, modified
> + * from the original tpm
> + * device drivers from Leendert van Dorn, Dave Safford, Reiner Sailer
> + * and Kyleen Hall.
> + *
> + * See file CREDITS for list of people who contributed to this
> + * project.
> + *
> + * 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, version 2 of the
> + * License.
> + *
> + * 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.
> + *
> + * You should have received a copy of the GNU General Public License
> + * along with this program; if not, write to the Free Software
> + * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
> + * MA 02111-1307 USA
> + *
> + * @Author: Mathias Leblanc tpmsupport at st.com
> + *
> + * @File: tpm_i2c_st.c
> + *
> + */
> +
> +#include <common.h>
> +#include <i2c.h>
> +#include <linux/types.h>
> +
> +#include "slb9635_i2c/compatibility.h"
> +#include "slb9635_i2c/tpm.h"
> +
> +/* max. buffer size supported by our tpm */
> +#ifdef TPM_BUFSIZE
> +#undef TPM_BUFSIZE
> +#endif

What are these for?

> +
> +#define MINOR_NUM_I2C          224
> +
> +#define TPM_ACCESS                     (0x0)
> +#define TPM_STS                                (0x18)
> +#define TPM_HASH_END                   (0x20)
> +#define TPM_DATA_FIFO                  (0x24)
> +#define TPM_HASH_DATA                  (0x24)
> +#define TPM_HASH_START                 (0x28)
> +#define TPM_INTF_CAPABILITY            (0x14)
> +#define TPM_INT_STATUS                 (0x10)
> +#define TPM_INT_ENABLE                 (0x08)

Don't need () around these

> +
> +#define TPM_DUMMY_BYTE                 0xAA
> +#define TPM_WRITE_DIRECTION            0x80
> +#define TPM_HEADER_SIZE                        10
> +#define TPM_BUFSIZE                    2048
> +
> +#define LOCALITY0              0
> +#define LOCALITY4              4
> +
> +struct st_tpm_hash {
> +       int size;
> +       u8 *data;
> +};
> +
> +enum stm33zp24_access {
> +       TPM_ACCESS_VALID = 0x80,
> +       TPM_ACCESS_ACTIVE_LOCALITY = 0x20,
> +       TPM_ACCESS_REQUEST_PENDING = 0x04,
> +       TPM_ACCESS_REQUEST_USE = 0x02,
> +};
> +
> +enum stm33zp24_status {
> +       TPM_STS_VALID = 0x80,
> +       TPM_STS_COMMAND_READY = 0x40,
> +       TPM_STS_GO = 0x20,
> +       TPM_STS_DATA_AVAIL = 0x10,
> +       TPM_STS_DATA_EXPECT = 0x08,
> +};
> +
> +enum stm33zp24_int_flags {
> +       TPM_GLOBAL_INT_ENABLE = 0x80,
> +       TPM_INTF_CMD_READY_INT = 0x080,
> +       TPM_INTF_FIFO_AVALAIBLE_INT = 0x040,
> +       TPM_INTF_WAKE_UP_READY_INT = 0x020,
> +       TPM_INTF_LOC4SOFTRELEASE_INT = 0x008,
> +       TPM_INTF_LOCALITY_CHANGE_INT = 0x004,
> +       TPM_INTF_STS_VALID_INT = 0x002,
> +       TPM_INTF_DATA_AVAIL_INT = 0x001,
> +};
> +
> +enum tis_defaults {
> +       TIS_SHORT_TIMEOUT = 750,        /* ms */
> +       TIS_LONG_TIMEOUT = 2000,        /* 2 sec */
> +};
> +
> +struct tpm_i2c_ST_dev {
> +       uint addr;
> +       u8 buf[TPM_BUFSIZE];
> +};
> +
> +static struct tpm_i2c_ST_dev tpm_dev = {
> +               /* Note: replace with defined addr from board configuration */
> +               .addr = CONFIG_TPM_I2C_ADDR
> +};
> +
> +/*
> + * write8_reg
> + * Send byte to the TIS register according to the ST33ZP24 I2C protocol.
> + * @param: tpm_register, the tpm tis register where the data should be written
> + * @param: tpm_data, the tpm_data to write inside the tpm_register
> + * @param: tpm_size, The length of the data
> + * @return: Returns zero in case of success else the negative error code.
> + */
> +static int write8_reg(u8 addr, u8 tpm_register,
> +                     u8 *tpm_data, u16 tpm_size)
> +{
> +       u8 data;
> +       data = tpm_register;
> +       memcpy(&(tpm_dev.buf[0]), &data, sizeof(data));
> +       memcpy(&(tpm_dev.buf[0])+1, tpm_data, tpm_size);
> +
> +       return i2c_write(addr, 0, 0, &tpm_dev.buf[0],
> +                               tpm_size + 1);
> +
> +} /* write8_reg() */
> +
> +/*
> +* read8_reg
> +* Recv byte from the TIS register according to the ST33ZP24 I2C protocol.
> +* @param: tpm_register, the tpm tis register where the data should be read
> +* @param: tpm_data, the TPM response
> +* @param: tpm_size, tpm TPM response size to read.
> +* @return: Returns zero in case of success else the negative error code.
> +*/
> +static int read8_reg(u8 addr, u8 tpm_register,
> +u8 *tpm_data, int tpm_size)
> +{
> +       u8 status = 0;
> +       u8 data;
> +       data = TPM_DUMMY_BYTE;
> +       status = write8_reg(addr, tpm_register, &data, 1);
> +       if (status == 0)
> +               status = i2c_read(addr, 0, 0, tpm_data, tpm_size);
> +return status;
> +} /* read8_reg() */
> +
> +/*
> + * I2C_WRITE_DATA
> + * Send byte to the TIS register according to the ST33ZP24 I2C protocol.
> + * @param: client, the chip description
> + * @param: tpm_register, the tpm tis register where the data should be written
> + * @param: tpm_data, the tpm_data to write inside the tpm_register
> + * @param: tpm_size, The length of the data
> + * @return: Returns zero in case of success else the negative error code.
> + */
> +#define I2C_WRITE_DATA(client, tpm_register, tpm_data, tpm_size)\
> +        (write8_reg(client, tpm_register | \
> +       TPM_WRITE_DIRECTION, tpm_data, tpm_size))
> +
> +/*
> + * I2C_READ_DATA
> + * Recv byte from the TIS register according to the ST33ZP24 I2C protocol.
> + * @param: tpm, the chip description
> + * @param: tpm_register, the tpm tis register where the data should be read
> + * @param: tpm_data, the TPM response
> + * @param: tpm_size, tpm TPM response size to read.
> + * @return: Returns zero in case of success else the negative error code.
> + */
> +#define I2C_READ_DATA(client, tpm_register, tpm_data, tpm_size)\
> +        (read8_reg(client, tpm_register, tpm_data, tpm_size))

Either remove these macros or just make them static functions.

> +
> +/*
> + * release_locality release the active locality
> + * @param: chip, the tpm chip description.
> + */
> +static void release_locality(struct tpm_chip *chip)
> +{
> +       u8 data = TPM_ACCESS_ACTIVE_LOCALITY;
> +
> +       I2C_WRITE_DATA(tpm_dev.addr, TPM_ACCESS, &data, 1);
> +}
> +
> +/*
> + * clear_interruption
> + * clear the TPM interrupt register.
> + * @param: tpm, the chip description
> + */
> +static void clear_interruption(u8 addr)
> +{
> +       u8 interrupt;
> +       I2C_READ_DATA(tpm_dev.addr, TPM_INT_STATUS, &interrupt, 1);
> +       I2C_WRITE_DATA(tpm_dev.addr, TPM_INT_STATUS, &interrupt, 1);
> +       I2C_READ_DATA(tpm_dev.addr, TPM_INT_STATUS, &interrupt, 1);
> +} /* clear_interruption() */
> +
> +int wait_for_serirq_timeout(struct tpm_chip *chip, int condition,
> +        unsigned long timeout)
> +{
> +       int status = 2;
> +
> +       clear_interruption(tpm_dev.addr);
> +       if (condition)
> +               status = 1;
> +
> +       return status;
> +}
> +
> +/*
> + * check_locality if the locality is active
> + * @param: chip, the tpm chip description
> + * @return: the active locality or -EACCESS.
> + */
> +static int check_locality(struct tpm_chip *chip)
> +{
> +       u8 data;
> +       u8 status;
> +       status = I2C_READ_DATA(tpm_dev.addr, TPM_ACCESS, &data, 1);
> +
> +       if ((status == 0) && (data &
> +               (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID)) ==
> +               (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID))
> +               return chip->vendor.locality;
> +
> +       return -EACCES;
> +
> +} /* check_locality() */
> +
> +/*
> + * request_locality request the TPM locality
> + * @param: chip, the chip description
> + * @return: the active locality or EACCESS.
> + */
> +static int request_locality(struct tpm_chip *chip)
> +{
> +       unsigned long start, stop;
> +       long rc;
> +       u8 data;
> +       if (check_locality(chip) == chip->vendor.locality)
> +               return chip->vendor.locality;
> +
> +       data = TPM_ACCESS_REQUEST_USE;
> +       rc = I2C_WRITE_DATA(tpm_dev.addr, TPM_ACCESS, &data, 1);
> +       if (rc < 0)
> +               goto end;
> +
> +       if (chip->vendor.irq) {
> +               rc = wait_for_serirq_timeout(chip, (check_locality
> +                                                      (chip) >= 0),
> +                                                     chip->vendor.timeout_a);
> +               if (rc > 0)
> +                       return chip->vendor.locality;
> +       } else{
> +       /* wait for locality activated */
> +       start = get_timer(0);
> +       stop = chip->vendor.timeout_a;
> +               do {
> +                       if (check_locality(chip) >= 0)
> +                               return chip->vendor.locality;
> +
> +                       msleep(TPM_TIMEOUT);
> +               } while  (get_timer(start) < stop);
> +       }
> +       rc = -EACCES;
> +end:
> +       return rc;
> +} /* request_locality() */
> +
> +/*
> + * tpm_stm_i2c_cancel, cancel is not implemented.
> + * @param: chip, tpm_chip description.
> + */
> +static void tpm_stm_i2c_cancel(struct tpm_chip *chip)
> +{
> +       u8 data;
> +
> +       data = TPM_STS_COMMAND_READY;
> +       I2C_WRITE_DATA(tpm_dev.addr, TPM_STS, &data, 1);
> +       if (chip->vendor.irq)
> +               wait_for_serirq_timeout(chip, 1, chip->vendor.timeout_a);
> +}      /* tpm_stm_i2c_cancel() */
> +
> +/*
> + * tpm_stm_spi_status return the TPM_STS register
> + * @param: chip, the tpm chip description
> + * @return: the TPM_STS register value.
> + */
> +static u8 tpm_stm_i2c_status(struct tpm_chip *chip)
> +{
> +       u8 data;
> +       I2C_READ_DATA(tpm_dev.addr, TPM_STS, &data, 1);
> +       return data;
> +}                              /* tpm_stm_i2c_status() */
> +
> +/*
> + * get_burstcount return the burstcount address 0x19 0x1A
> + * @param: chip, the chip description
> + * return: the burstcount.
> + */
> +static int get_burstcount(struct tpm_chip *chip)
> +{
> +       unsigned long start, stop;
> +       int burstcnt, status;
> +       u8 tpm_reg, temp;
> +
> +       /* wait for burstcount */
> +       /* which timeout value, spec has 2 answers (c & d) */
> +       start = get_timer(0);
> +       stop = chip->vendor.timeout_d;
> +       do {
> +               tpm_reg = TPM_STS + 1;
> +               status = I2C_READ_DATA(tpm_dev.addr, tpm_reg, &temp, 1);
> +               if (status < 0)
> +                       goto end;
> +
> +               tpm_reg = tpm_reg + 1;
> +               burstcnt = temp;
> +               status = I2C_READ_DATA(tpm_dev.addr, tpm_reg, &temp, 1);
> +               if (status < 0)
> +                       goto end;
> +
> +               burstcnt |= temp << 8;
> +               if (burstcnt)
> +                       return burstcnt;
> +
> +               msleep(TPM_TIMEOUT);
> +       } while (get_timer(start) < stop);
> +
> +end:
> +       return -EBUSY;
> +} /* get_burstcount() */
> +
> +
> +/*
> + * recv_data receive data
> + * @param: chip, the tpm chip description
> + * @param: buf, the buffer where the data are received
> + * @param: count, the number of data to receive
> + * @return: the number of bytes read from TPM FIFO.
> + */
> +static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count)
> +{
> +       int size = 0, burstcnt, len;
> +
> +       while (size < count) {
> +               burstcnt = get_burstcount(chip);
> +               len = count - size;
> +               if ((len) > burstcnt)
> +                       len = burstcnt;
> +       if (
> +       I2C_READ_DATA(tpm_dev.addr, TPM_DATA_FIFO, buf + size, len) == 0)
> +                       size += len;
> +               else
> +                       break;
> +       }
> +       return size;
> +} /* recv_data() */
> +
> +/*
> + * tpm_stm_i2c_recv received TPM response through the I2C bus.
> + * @param: chip, tpm_chip description.
> + * @param: buf,        the buffer to store datas.
> + * @param: count, the number of bytes to send.
> + * @return: Returns zero in case of success else the negative error code.
> + */
> +static int tpm_stm_i2c_recv(struct tpm_chip *chip, unsigned char *buf,
> +                           size_t count)
> +{
> +       int size = 0;
> +       int expected;
> +
> +       if (chip == NULL)
> +               return -EBUSY;
> +
> +       if (count < TPM_HEADER_SIZE) {
> +               size = -EIO;
> +               goto out;
> +       }
> +
> +       size = recv_data(chip, buf, TPM_HEADER_SIZE);
> +       if (size < TPM_HEADER_SIZE) {
> +               dev_err(chip->dev, "Unable to read header\n");
> +               goto out;
> +       }
> +
> +
> +       expected = get_unaligned_be32(buf + TPM_RSP_SIZE_BYTE);
> +       if (expected > count) {
> +               size = -EIO;
> +               goto out;
> +       }
> +
> +       size += recv_data(chip, &buf[TPM_HEADER_SIZE],
> +                                       expected - TPM_HEADER_SIZE);
> +       if (size < expected) {
> +               dev_err(chip->dev, "Unable to read remainder of result\n");
> +               size = -ETIME;
> +               goto out;
> +       }
> +
> +out:
> +       chip->vendor.cancel(chip);
> +       release_locality(chip);
> +       return size;
> +} /* tpm_stm_i2c_recv() */
> +
> +/*
> + * tpm_stm_i2c_send send TPM commands through the I2C bus.
> + *
> + * @param: chip, tpm_chip description.
> + * @param: buf,        the buffer to send.
> + * @param: len, the number of bytes to send.
> + * @return: Returns zero in case of success else the negative error code.
> + */
> +static int tpm_stm_i2c_send(struct tpm_chip *chip, u8 *buf,
> +                           size_t len)
> +{
> +       u32 ret = 0,
> +           status,
> +           burstcnt = 0, i, size;
> +       u8 data;
> +
> +       if (chip == NULL)
> +               return -EBUSY;
> +       if (len < TPM_HEADER_SIZE)
> +               return -EBUSY;
> +
> +       ret = request_locality(chip);
> +       if (ret < 0)
> +               return ret;
> +
> +       status = tpm_stm_i2c_status(chip);
> +       if ((status & TPM_STS_COMMAND_READY) == 0)
> +               tpm_stm_i2c_cancel(chip);
> +
> +       for (i = 0; i < len - 1;) {
> +               burstcnt = get_burstcount(chip);
> +               size = len - i - 1;
> +               if ((size) > burstcnt)
> +                       size = burstcnt;
> +               ret = I2C_WRITE_DATA(tpm_dev.addr, TPM_DATA_FIFO, buf, size);
> +               if (ret < 0)
> +                       goto out_err;
> +
> +               i += size;
> +       }
> +
> +       status = tpm_stm_i2c_status(chip);
> +       if ((status & TPM_STS_DATA_EXPECT) == 0) {
> +               ret = -EIO;
> +               goto out_err;
> +       }
> +
> +       ret = I2C_WRITE_DATA(tpm_dev.addr, TPM_DATA_FIFO, buf + len - 1, 1);
> +       if (ret < 0)
> +               goto out_err;
> +
> +       status = tpm_stm_i2c_status(chip);
> +       if ((status & TPM_STS_DATA_EXPECT) != 0) {
> +               ret = -EIO;
> +               goto out_err;
> +       }
> +
> +       data = TPM_STS_GO;
> +       I2C_WRITE_DATA(tpm_dev.addr, TPM_STS, &data, 1);
> +
> +       return len;
> +out_err:
> +       tpm_stm_i2c_cancel(chip);
> +       release_locality(chip);

Add debug() on error path?

> +       return ret;
> +} /* tpm_stm_i2c_send() */
> +
> +/*
> + * tpm_stm_i2c_send_hash send TPM locality 4 hash datas through the I2C bus
> + * to update the PCR[17].
> + * @param: chip, the tpm_chip description.
> + * @param: buf,        the data buffer to send.
> + * @param: len, the number of bytes to send.
> + * @return: Returns zero in case of success else the negative error code.
> + */
> +static int tpm_stm_i2c_send_hash(struct tpm_chip *chip, unsigned char *buf,
> +                           size_t len)
> +{
> +       u32 ret = 0;
> +       u8 data;
> +
> +       if (chip == NULL)
> +               return -EBUSY;

Why does NULL mean busy? Shouldn't it be -ENODEV?

> +
> +       release_locality(chip);
> +
> +       tpm_dev.addr = 0x1B;
> +       chip->vendor.locality = LOCALITY4;
> +
> +       data = TPM_DUMMY_BYTE;
> +       ret = I2C_WRITE_DATA(tpm_dev.addr, TPM_HASH_START, &data, 1);
> +       if (ret < 0)
> +               goto end;
> +       ret = I2C_WRITE_DATA(tpm_dev.addr, TPM_DATA_FIFO, buf, len);
> +       if (ret < 0)
> +               goto end;
> +
> +end:
> +       I2C_WRITE_DATA(tpm_dev.addr, TPM_HASH_END, &data, 1);
> +       release_locality(chip);
> +       chip->vendor.locality = LOCALITY0;
> +       tpm_dev.addr = 0x13;
> +       ret = request_locality(chip);
> +       return ret;
> +} /* tpm_stm_i2c_send_hash */
> +
> +static struct tpm_vendor_specific st_i2c_tpm = {
> +       .send = tpm_stm_i2c_send,
> +       .send_hash = tpm_stm_i2c_send_hash,
> +       .recv = tpm_stm_i2c_recv,
> +       .cancel = tpm_stm_i2c_cancel,
> +       .status = tpm_stm_i2c_status,
> +       .req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
> +       .req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
> +       .req_canceled = TPM_STS_COMMAND_READY,
> +};
> +
> +/*
> + * tpm_vendor_init initialize the TPM device
> + * @param: dev_addr, the i2c address of the tpm.
> + * @return: 0 in case of success.
> + *      -1 in other case.
> + */
> +int tpm_vendor_init(uint32_t dev_addr)
> +{
> +       u32 vendor;
> +       uint old_addr;
> +       int rc = 0;
> +       struct tpm_chip *chip;
> +
> +       old_addr = tpm_dev.addr;
> +       if (dev_addr != 0)
> +               tpm_dev.addr = dev_addr;
> +
> +       chip = tpm_register_hardware(&st_i2c_tpm);
> +
> +       if (chip < 0) {
> +               rc = -ENODEV;
> +               goto out_err;
> +       }
> +
> +       /* Default timeouts */
> +       chip->vendor.timeout_a = TIS_SHORT_TIMEOUT;
> +       chip->vendor.timeout_b = TIS_LONG_TIMEOUT;
> +       chip->vendor.timeout_c = TIS_SHORT_TIMEOUT;
> +       chip->vendor.timeout_d = TIS_SHORT_TIMEOUT;
> +
> +       chip->vendor.locality = LOCALITY0;
> +
> +       if (request_locality(chip) != 0) {
> +               rc = -ENODEV;
> +               goto out_err;
> +       }
> +
> +       vendor = be32_to_cpu(vendor);
> +
> +
> +       dev_info(dev, "1.2 TPM STMicroelectronics");
> +       /*
> +        * A timeout query to TPM can be placed here.
> +        * Standard timeout values are used so far
> +        */
> +
> +       return 0;
> +
> +out_err:
> +       tpm_dev.addr = old_addr;
> +       return rc;
> +} /* tpm_vendor_init() */
> +
> +
> +
> +void tpm_vendor_cleanup(struct tpm_chip *chip)
> +{
> +       release_locality(chip);
> +} /* tpm_vendor_cleanup() */
> diff --git a/include/configs/omap3_beagle.h b/include/configs/omap3_beagle.h
> index 48ce4c0..ef381f8 100644
> --- a/include/configs/omap3_beagle.h
> +++ b/include/configs/omap3_beagle.h
> @@ -111,6 +111,14 @@
>  #define STATUS_LED_BOOT                        STATUS_LED_BIT
>  #define STATUS_LED_GREEN               STATUS_LED_BIT1
>
> +/* TPM */
> +#define CONFIG_CMD_TPM
> +#define CONFIG_TPM
> +#define CONFIG_ST_TPM_DEBUG
> +#define CONFIG_ST_TPM_I2C
> +#define CONFIG_TPM_I2C_BUS     1
> +#define CONFIG_TPM_I2C_ADDR    0x13
> +
>  /* Enable Multi Bus support for I2C */
>  #define CONFIG_I2C_MULTI_BUS           1
>
> diff --git a/include/tpm.h b/include/tpm.h
> index 7219b73..36ba0bb 100644
> --- a/include/tpm.h
> +++ b/include/tpm.h
> @@ -201,4 +201,22 @@ uint32_t tpm_physical_set_deactivated(uint8_t state);
>  uint32_t tpm_get_capability(uint32_t cap_area, uint32_t sub_cap,
>                 void *cap, size_t count);
>
> +/*
> + * tis_sendrecv_hash()
> + *
> + * Send the requested data to the TPM for hash in LOC 4
> + * and then try to get its response
> + *
> + * @sendbuf - buffer of the data to hash
> + * @send_size size of the data to send
> + * @recvbuf - memory to save the response to
> + * @recv_len - pointer to the size of the response buffer
> + *
> + * Returns 0 on success (and places the number of response bytes at recv_len)
> + * or -1 on failure.
> + */
> +int tis_sendrecv_hash(const uint8_t *sendbuf, size_t send_size,
> +        uint8_t *recvbuf,
> +                       size_t *recv_len);
> +
>  #endif /* __TPM_H */
> --
> 1.7.9.5
>


Regards,
Simon

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [U-Boot] [PATCH 1/1] TPM: STMicroelectronics u-boot driver I2C
  2013-10-22 15:48 ` Simon Glass
@ 2014-02-16 22:46   ` Simon Glass
  2014-02-16 22:47     ` Simon Glass
  2014-02-17 22:23   ` Simon Glass
  1 sibling, 1 reply; 13+ messages in thread
From: Simon Glass @ 2014-02-16 22:46 UTC (permalink / raw)
  To: u-boot

Hi,

On 22 October 2013 09:48, Simon Glass <sjg@chromium.org> wrote:
> Hi Mathias,
>
> On Wed, May 15, 2013 at 7:58 AM, Mathias leblanc <mathias.leblanc@st.com> wrote:
>> From: Mathias Leblanc <mathias.leblanc@st.com>
>>
>>  * STMicroelectronics version 1.2.0, Copyright (C) 2013
>>  * This is free software, and you are welcome to redistribute it.
>>
>> This is the u-boot driver for TPM chip from ST Microelectronics.
>>
>> If you have a TPM security chip from STMicroelectronics working with
>> an I2C, read the README file and add the correct defines regarding
>> the tpm in the configuration file of your board.
>> This file is located in include/configs/your_board.h
>>
>> The tpm command will be accessible from within uboot terminal.
>>
>> Signed-off-by: Mathias Leblanc <mathias.leblanc@st.com>
>
> It would be good to apply this patch in this merge window. Can you
> please do a few tidy-ups?

I have not had a response back - it seems that Mathias has moved on.
So I am dropping this patch. If anyone wants to pick it up and finish
it, please go ahead.

Regards,
Simon

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [U-Boot] [PATCH 1/1] TPM: STMicroelectronics u-boot driver I2C
  2014-02-16 22:46   ` Simon Glass
@ 2014-02-16 22:47     ` Simon Glass
  0 siblings, 0 replies; 13+ messages in thread
From: Simon Glass @ 2014-02-16 22:47 UTC (permalink / raw)
  To: u-boot

On 16 February 2014 15:46, Simon Glass <sjg@chromium.org> wrote:
> Hi,
>
> On 22 October 2013 09:48, Simon Glass <sjg@chromium.org> wrote:
>> Hi Mathias,
>>
>> On Wed, May 15, 2013 at 7:58 AM, Mathias leblanc <mathias.leblanc@st.com> wrote:
>>> From: Mathias Leblanc <mathias.leblanc@st.com>
>>>
>>>  * STMicroelectronics version 1.2.0, Copyright (C) 2013
>>>  * This is free software, and you are welcome to redistribute it.
>>>
>>> This is the u-boot driver for TPM chip from ST Microelectronics.
>>>
>>> If you have a TPM security chip from STMicroelectronics working with
>>> an I2C, read the README file and add the correct defines regarding
>>> the tpm in the configuration file of your board.
>>> This file is located in include/configs/your_board.h
>>>
>>> The tpm command will be accessible from within uboot terminal.
>>>
>>> Signed-off-by: Mathias Leblanc <mathias.leblanc@st.com>
>>
>> It would be good to apply this patch in this merge window. Can you
>> please do a few tidy-ups?
>
> I have not had a response back - it seems that Mathias has moved on.
> So I am dropping this patch. If anyone wants to pick it up and finish
> it, please go ahead.

Adding Benoit as suggested.

>
> Regards,
> Simon

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [U-Boot] [PATCH 1/1] TPM: STMicroelectronics u-boot driver I2C
  2013-10-22 15:48 ` Simon Glass
  2014-02-16 22:46   ` Simon Glass
@ 2014-02-17 22:23   ` Simon Glass
       [not found]     ` <C56FB217EE26FF4AB56DA86C1AB6A2724E40EAD190@SAFEX1MAIL3.st.com>
  1 sibling, 1 reply; 13+ messages in thread
From: Simon Glass @ 2014-02-17 22:23 UTC (permalink / raw)
  To: u-boot

Hi Jean-Luc,

On 22 October 2013 09:48, Simon Glass <sjg@chromium.org> wrote:
> Hi Mathias,
>
> On Wed, May 15, 2013 at 7:58 AM, Mathias leblanc <mathias.leblanc@st.com> wrote:
>> From: Mathias Leblanc <mathias.leblanc@st.com>
>>
>>  * STMicroelectronics version 1.2.0, Copyright (C) 2013
>>  * This is free software, and you are welcome to redistribute it.
>>
>> This is the u-boot driver for TPM chip from ST Microelectronics.
>>
>> If you have a TPM security chip from STMicroelectronics working with
>> an I2C, read the README file and add the correct defines regarding
>> the tpm in the configuration file of your board.
>> This file is located in include/configs/your_board.h
>>
>> The tpm command will be accessible from within uboot terminal.
>>
>> Signed-off-by: Mathias Leblanc <mathias.leblanc@st.com>
>
> It would be good to apply this patch in this merge window. Can you
> please do a few tidy-ups?

This is the email with my comments.

>
>> ---
>>  README                         |   14 +-
>>  common/cmd_tpm.c               |  122 ++++++++
>>  drivers/tpm/Makefile           |    1 +
>>  drivers/tpm/slb9635_i2c/tpm.c  |   20 ++
>>  drivers/tpm/slb9635_i2c/tpm.h  |    1 +
>>  drivers/tpm/tis_i2c.c          |   37 +++
>>  drivers/tpm/tpm_i2c_st.c       |  599 ++++++++++++++++++++++++++++++++++++++++
>>  include/configs/omap3_beagle.h |    8 +
>
> Please split the driver, command change (cmd_tpm.c) and the CONFIG
> change (this file) into three separate patches.
>
>>  include/tpm.h                  |   18 ++
>>  9 files changed, 819 insertions(+), 1 deletion(-)
>>  create mode 100644 drivers/tpm/tpm_i2c_st.c
>>
>> diff --git a/README b/README
>> index 0d37d56..a72b570 100644
>> --- a/README
>> +++ b/README
>> @@ -1208,7 +1208,7 @@ The following options need to be configured:
>>                         If this option is set, the driver enables cache flush.
>>
>>  - TPM Support:
>> -               CONFIG_GENERIC_LPC_TPM
>> +               CONFIG_TPM
>>                 Support for generic parallel port TPM devices. Only one device
>>                 per system is supported at this time.
>>
>> @@ -1217,6 +1217,18 @@ The following options need to be configured:
>>                         to. Contemporary x86 systems usually map it at
>>                         0xfed40000.
>>
>> +               CONFIG_ST_TPM_I2C
>> +               Define to compile the ST TPM I2C DRIVER.
>> +
>> +               CONFIG_TPM_I2C_BUS
>> +               Define the bus number of the board.
>> +
>> +               CONFIG_TPM_I2C_ADDR
>> +               Define the address of the TPM.
>> +
>> +               CONFIG_CMD_TPM
>> +               Define to use some TPM u-boot commands.
>> +
>>  - USB Support:
>>                 At the moment only the UHCI host controller is
>>                 supported (PIP405, MIP405, MPC5200); define
>> diff --git a/common/cmd_tpm.c b/common/cmd_tpm.c
>> index 46fae18..fba1fe7 100644
>> --- a/common/cmd_tpm.c
>> +++ b/common/cmd_tpm.c
>> @@ -27,6 +27,14 @@
>>  #include <asm/unaligned.h>
>>  #include <linux/string.h>
>>
>> +#define MAX_TRANSACTION_SIZE 30
>> +#define CHECK(exp) do {                                                        \
>> +               int _rv = exp;                                          \
>> +               if (_rv) {                                              \
>> +                       printf("CHECK: %s %d %x\n", #exp, __LINE__, _rv);\
>> +               }                                                       \
>> +       } while (0)
>> +
>>  /**
>>   * Print a byte string in hexdecimal format, 16-bytes per line.
>>   *
>> @@ -546,6 +554,118 @@ static int do_tpm_nv_write(cmd_tbl_t *cmdtp, int flag,
>>         return convert_return_code(err);
>>  }
>>
>> +static int do_tpm_hash(cmd_tbl_t *cmdtp, int flag, int argc,
>> +char * const argv[])
>> +{
>> +       u8 tpm_buffer[MAX_TRANSACTION_SIZE];
>> +       u32 write_size, read_size;
>> +       char *p;
>> +       int rv = -1;
>> +       argc -= 1;
>> +       argv += 1;
>> +       uint8_t response[1024];
>> +       size_t rlength = MAX_TRANSACTION_SIZE;
>> +
>> +       u8 startup[] = {
>> +               0x00, 0xc1,
>> +               0x00, 0x00, 0x00, 0x0c,
>> +               0x00, 0x00, 0x00, 0x99,
>> +               0x00, 0x01
>> +       };
>> +
>> +       u8 selftestfull[] = {
>> +               0x00, 0xc1,
>> +               0x00, 0x00, 0x00, 0x0a,
>> +               0x00, 0x00, 0x00, 0x50
>> +       };
>> +
>> +       u8 readpcr17[] = {
>> +               0x00, 0xc1,
>> +               0x00, 0x00, 0x00, 0x0e,
>> +               0x00, 0x00, 0x00, 0x15,
>> +               0x00, 0x00, 0x00, 0x11
>> +       };
>> +
>> +       for (write_size = 0; write_size < argc; write_size++) {
>> +               u32 datum = kstrtoul(argv[write_size], &p, 0);
>> +               if (*p || (datum > 0xff)) {
>> +                       printf("\n%s: bad data value\n\n", argv[write_size]);
>> +                       cmd_usage(cmdtp);
>> +                       return rv;
>> +               }
>> +               tpm_buffer[write_size] = (u8)datum;
>> +       }
>> +
>> +       if (tis_init()) {
>> +               puts("tis_init() failed!\n");
>> +               return -1;
>> +       }
>> +
>> +       if (tis_open()) {
>> +               puts("tis_open() failed!\n");
>> +               return -1;
>> +       }
>> +
>> +       rv = tis_sendrecv(startup, sizeof(startup), response, &rlength);
>> +       if (rv) {
>> +               printf("tpm test startup failed\n");
>> +               CHECK(tis_close());
>> +       }
>> +
>> +       rv = tis_sendrecv(selftestfull, sizeof(selftestfull), response,
>> +                &rlength);
>> +       if (rv) {
>> +               printf("tpm test selftestfull failed\n");
>> +               CHECK(tis_close());
>> +       }
>> +
>> +       if (!
>> +       tis_sendrecv(readpcr17, sizeof(readpcr17), response, &read_size)) {
>> +                       int i;
>
> Blank line between declarations and body of function. Please fix globally.
>
>> +                       puts("TPM Read PCR 17:\n");
>> +                       for (i = 10; i < read_size; i++)
>> +                               printf(" %2.2x", response[i]);
>> +                       puts("\n");
>> +                       rv = 0;
>> +                       } else {
>
> Suspect indenting here, please fix globally.
>
>> +                               printf("tpm test readpcr17 failed\n");
>> +                               CHECK(tis_close());
>> +                       }
>> +
>> +       read_size = sizeof(tpm_buffer);
>> +       if (!
>
> Remove newline here. Please fix below also.
>
>> +       tis_sendrecv_hash(tpm_buffer, write_size, tpm_buffer, &read_size)) {
>> +                       int i;
>> +                       puts("Got TPM Hash response:\n");
>> +                       for (i = 0; i < read_size; i++)
>> +                               printf(" %2.2x", tpm_buffer[i]);
>> +                       puts("\n");
>> +                       rv = 0;
>> +                       } else {
>> +                               puts("tpm hash command failed\n");
>> +                               }
>> +
>> +       if (!
>> +       tis_sendrecv(readpcr17, sizeof(readpcr17), response, &read_size)) {
>> +                       int i;
>> +                       puts("TPM Read PCR 17 after hash:\n");
>> +                       for (i = 10; i < read_size; i++)
>> +                               printf(" %2.2x", response[i]);
>> +                       puts("\n");
>> +                       rv = 0;
>> +                       } else {
>> +                               printf("tpm test readpcr17 failed\n");
>> +                               CHECK(tis_close());
>> +                               }
>> +
>> +       if (tis_close()) {
>> +               puts("tis_close() failed!\n");
>> +               rv = -1;
>> +       }
>> +
>> +       return rv;
>> +}
>> +
>>  #define MAKE_TPM_CMD_ENTRY(cmd) \
>>         U_BOOT_CMD_MKENT(cmd, 0, 1, do_tpm_ ## cmd, "", "")
>>
>> @@ -590,6 +710,8 @@ static cmd_tbl_t tpm_commands[] = {
>>                         do_tpm_nv_read, "", ""),
>>         U_BOOT_CMD_MKENT(nv_write, 0, 1,
>>                         do_tpm_nv_write, "", ""),
>> +       U_BOOT_CMD_MKENT(hash, 0, 1,
>> +                       do_tpm_hash, "", ""),
>>  };
>>
>>  static int do_tpm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
>> diff --git a/drivers/tpm/Makefile b/drivers/tpm/Makefile
>> index e8c159c..cbececf 100644
>> --- a/drivers/tpm/Makefile
>> +++ b/drivers/tpm/Makefile
>> @@ -28,6 +28,7 @@ $(shell mkdir -p $(obj)slb9635_i2c)
>>  COBJS-$(CONFIG_GENERIC_LPC_TPM) = generic_lpc_tpm.o
>>  COBJS-$(CONFIG_INFINEON_TPM_I2C) += tis_i2c.o slb9635_i2c/tpm.o
>>  COBJS-$(CONFIG_INFINEON_TPM_I2C) += slb9635_i2c/tpm_tis_i2c.o
>> +COBJS-$(CONFIG_ST_TPM_I2C) = tis_i2c.o tpm_i2c_st.o slb9635_i2c/tpm.o
>>
>>  COBJS  := $(COBJS-y)
>>  SRCS   := $(COBJS:.o=.c)
>> diff --git a/drivers/tpm/slb9635_i2c/tpm.c b/drivers/tpm/slb9635_i2c/tpm.c
>> index 496c48e..c92bd06 100644
>> --- a/drivers/tpm/slb9635_i2c/tpm.c
>> +++ b/drivers/tpm/slb9635_i2c/tpm.c
>> @@ -444,6 +444,26 @@ int tpm_open(uint32_t dev_addr)
>>         return rc;
>>  }
>>
>> +ssize_t tpm_transmit_hash(const unsigned char *buf, size_t bufsiz)
>> +{
>> +       ssize_t rc;
>> +
>> +       struct tpm_chip *chip = &g_chip;
>> +
>> +       rc = chip->vendor.send_hash(chip, (u8 *)buf, bufsiz);
>> +       if (rc < 0) {
>> +               dev_err(chip->dev, "tpm_transmit: tpm_send: error %zd\n", rc);
>> +               goto out;
>> +       }
>> +
>> +       dbg_printf("out_recv: reading response...\n");
>> +       rc = chip->vendor.recv(chip, (u8 *)buf, TPM_BUFSIZE);
>> +       if (rc < 0)
>> +               dev_err(chip->dev, "tpm_transmit: tpm_recv: error %zd\n", rc);
>> +out:
>> +       return rc;
>> +}
>> +
>>  void tpm_close(void)
>>  {
>>         if (g_chip.is_open) {
>> diff --git a/drivers/tpm/slb9635_i2c/tpm.h b/drivers/tpm/slb9635_i2c/tpm.h
>> index 9ddee86..88e0c07 100644
>> --- a/drivers/tpm/slb9635_i2c/tpm.h
>> +++ b/drivers/tpm/slb9635_i2c/tpm.h
>> @@ -64,6 +64,7 @@ struct tpm_vendor_specific {
>>         int irq;
>>         int (*recv) (struct tpm_chip *, u8 *, size_t);
>>         int (*send) (struct tpm_chip *, u8 *, size_t);
>> +       int (*send_hash) (struct tpm_chip *, u8 *, size_t);
>>         void (*cancel) (struct tpm_chip *);
>>          u8(*status) (struct tpm_chip *);
>>         int locality;
>> diff --git a/drivers/tpm/tis_i2c.c b/drivers/tpm/tis_i2c.c
>> index e818fba..36ae544 100644
>> --- a/drivers/tpm/tis_i2c.c
>> +++ b/drivers/tpm/tis_i2c.c
>> @@ -82,8 +82,13 @@ static int tpm_decode_config(struct tpm *dev)
>>         dev->i2c_bus = i2c_bus;
>>         dev->slave_addr = fdtdec_get_addr(blob, node, "reg");
>>  #else
>> +       #ifdef CONFIG_INFINEON_TPM_I2C_BUS
>>         dev->i2c_bus = CONFIG_INFINEON_TPM_I2C_BUS;
>>         dev->slave_addr = CONFIG_INFINEON_TPM_I2C_ADDR;
>> +       #else
>> +       dev->i2c_bus = CONFIG_TPM_I2C_BUS;
>> +       dev->slave_addr = CONFIG_TPM_I2C_ADDR;
>> +       #endif
>>  #endif
>>         return 0;
>>  }
>> @@ -179,3 +184,35 @@ int tis_sendrecv(const uint8_t *sendbuf, size_t sbuf_size,
>>
>>         return 0;
>>  }
>> +
>> +int tis_sendrecv_hash(const uint8_t *sendbuf, size_t sbuf_size,
>> +               uint8_t *recvbuf, size_t *rbuf_len)
>> +{
>> +       int len;
>> +       uint8_t buf[TPM_BUFSIZE];
>> +
>> +       if (!tpm.inited)
>> +               return -1;
>> +
>> +       if (sizeof(buf) < sbuf_size)
>> +               return -1;
>> +
>> +       memcpy(buf, sendbuf, sbuf_size);
>> +
>> +       if (tpm_select())
>> +               return -1;
>> +
>> +       len = tpm_transmit_hash(buf, sbuf_size);
>> +
>> +       tpm_deselect();
>> +
>> +       if (len < 10) {
>> +               *rbuf_len = 0;
>> +               return -1;
>> +       }
>> +
>> +       memcpy(recvbuf, buf, len);
>> +       *rbuf_len = len;
>> +
>> +       return 0;
>> +}
>> diff --git a/drivers/tpm/tpm_i2c_st.c b/drivers/tpm/tpm_i2c_st.c
>> new file mode 100644
>> index 0000000..16753f8
>> --- /dev/null
>> +++ b/drivers/tpm/tpm_i2c_st.c
>> @@ -0,0 +1,599 @@
>> +/*
>> + * STMicroelectronics TPM I2C UBOOT Linux driver for TPM ST33ZP24
>> + * Copyright (C) 2013  STMicroelectronics
>> + *
>> + * (c) Copyright 2013 Mathias Leblanc <mathias.leblanc@st.com>
>> + * This file is released under the terms of GPL v2 and any later version
>> + * See the file COPYING in the root directory of the source tree for details
>> + *
>> + * Description:
>> + * Device driver for TCG/TCPA TPM (trusted platform module).
>> + * Specifications at www.trustedcomputinggroup.org
>> + *
>> + * This device driver implements the TPM interface as defined in
>> + * the TCG TPM Interface Spec version 1.2, revision 1.0 and the
>> + * STMicroelectronics I2C Protocol Stack Specification version 1.2.0.
>> + *
>> + * It is based on the Linux I2C TPM driver from Peter Huewe, modified
>> + * from the original tpm
>> + * device drivers from Leendert van Dorn, Dave Safford, Reiner Sailer
>> + * and Kyleen Hall.
>> + *
>> + * See file CREDITS for list of people who contributed to this
>> + * project.
>> + *
>> + * 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, version 2 of the
>> + * License.
>> + *
>> + * 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.
>> + *
>> + * You should have received a copy of the GNU General Public License
>> + * along with this program; if not, write to the Free Software
>> + * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
>> + * MA 02111-1307 USA
>> + *
>> + * @Author: Mathias Leblanc tpmsupport at st.com
>> + *
>> + * @File: tpm_i2c_st.c
>> + *
>> + */
>> +
>> +#include <common.h>
>> +#include <i2c.h>
>> +#include <linux/types.h>
>> +
>> +#include "slb9635_i2c/compatibility.h"
>> +#include "slb9635_i2c/tpm.h"
>> +
>> +/* max. buffer size supported by our tpm */
>> +#ifdef TPM_BUFSIZE
>> +#undef TPM_BUFSIZE
>> +#endif
>
> What are these for?
>
>> +
>> +#define MINOR_NUM_I2C          224
>> +
>> +#define TPM_ACCESS                     (0x0)
>> +#define TPM_STS                                (0x18)
>> +#define TPM_HASH_END                   (0x20)
>> +#define TPM_DATA_FIFO                  (0x24)
>> +#define TPM_HASH_DATA                  (0x24)
>> +#define TPM_HASH_START                 (0x28)
>> +#define TPM_INTF_CAPABILITY            (0x14)
>> +#define TPM_INT_STATUS                 (0x10)
>> +#define TPM_INT_ENABLE                 (0x08)
>
> Don't need () around these
>
>> +
>> +#define TPM_DUMMY_BYTE                 0xAA
>> +#define TPM_WRITE_DIRECTION            0x80
>> +#define TPM_HEADER_SIZE                        10
>> +#define TPM_BUFSIZE                    2048
>> +
>> +#define LOCALITY0              0
>> +#define LOCALITY4              4
>> +
>> +struct st_tpm_hash {
>> +       int size;
>> +       u8 *data;
>> +};
>> +
>> +enum stm33zp24_access {
>> +       TPM_ACCESS_VALID = 0x80,
>> +       TPM_ACCESS_ACTIVE_LOCALITY = 0x20,
>> +       TPM_ACCESS_REQUEST_PENDING = 0x04,
>> +       TPM_ACCESS_REQUEST_USE = 0x02,
>> +};
>> +
>> +enum stm33zp24_status {
>> +       TPM_STS_VALID = 0x80,
>> +       TPM_STS_COMMAND_READY = 0x40,
>> +       TPM_STS_GO = 0x20,
>> +       TPM_STS_DATA_AVAIL = 0x10,
>> +       TPM_STS_DATA_EXPECT = 0x08,
>> +};
>> +
>> +enum stm33zp24_int_flags {
>> +       TPM_GLOBAL_INT_ENABLE = 0x80,
>> +       TPM_INTF_CMD_READY_INT = 0x080,
>> +       TPM_INTF_FIFO_AVALAIBLE_INT = 0x040,
>> +       TPM_INTF_WAKE_UP_READY_INT = 0x020,
>> +       TPM_INTF_LOC4SOFTRELEASE_INT = 0x008,
>> +       TPM_INTF_LOCALITY_CHANGE_INT = 0x004,
>> +       TPM_INTF_STS_VALID_INT = 0x002,
>> +       TPM_INTF_DATA_AVAIL_INT = 0x001,
>> +};
>> +
>> +enum tis_defaults {
>> +       TIS_SHORT_TIMEOUT = 750,        /* ms */
>> +       TIS_LONG_TIMEOUT = 2000,        /* 2 sec */
>> +};
>> +
>> +struct tpm_i2c_ST_dev {
>> +       uint addr;
>> +       u8 buf[TPM_BUFSIZE];
>> +};
>> +
>> +static struct tpm_i2c_ST_dev tpm_dev = {
>> +               /* Note: replace with defined addr from board configuration */
>> +               .addr = CONFIG_TPM_I2C_ADDR
>> +};
>> +
>> +/*
>> + * write8_reg
>> + * Send byte to the TIS register according to the ST33ZP24 I2C protocol.
>> + * @param: tpm_register, the tpm tis register where the data should be written
>> + * @param: tpm_data, the tpm_data to write inside the tpm_register
>> + * @param: tpm_size, The length of the data
>> + * @return: Returns zero in case of success else the negative error code.
>> + */
>> +static int write8_reg(u8 addr, u8 tpm_register,
>> +                     u8 *tpm_data, u16 tpm_size)
>> +{
>> +       u8 data;
>> +       data = tpm_register;
>> +       memcpy(&(tpm_dev.buf[0]), &data, sizeof(data));
>> +       memcpy(&(tpm_dev.buf[0])+1, tpm_data, tpm_size);
>> +
>> +       return i2c_write(addr, 0, 0, &tpm_dev.buf[0],
>> +                               tpm_size + 1);
>> +
>> +} /* write8_reg() */
>> +
>> +/*
>> +* read8_reg
>> +* Recv byte from the TIS register according to the ST33ZP24 I2C protocol.
>> +* @param: tpm_register, the tpm tis register where the data should be read
>> +* @param: tpm_data, the TPM response
>> +* @param: tpm_size, tpm TPM response size to read.
>> +* @return: Returns zero in case of success else the negative error code.
>> +*/
>> +static int read8_reg(u8 addr, u8 tpm_register,
>> +u8 *tpm_data, int tpm_size)
>> +{
>> +       u8 status = 0;
>> +       u8 data;
>> +       data = TPM_DUMMY_BYTE;
>> +       status = write8_reg(addr, tpm_register, &data, 1);
>> +       if (status == 0)
>> +               status = i2c_read(addr, 0, 0, tpm_data, tpm_size);
>> +return status;
>> +} /* read8_reg() */
>> +
>> +/*
>> + * I2C_WRITE_DATA
>> + * Send byte to the TIS register according to the ST33ZP24 I2C protocol.
>> + * @param: client, the chip description
>> + * @param: tpm_register, the tpm tis register where the data should be written
>> + * @param: tpm_data, the tpm_data to write inside the tpm_register
>> + * @param: tpm_size, The length of the data
>> + * @return: Returns zero in case of success else the negative error code.
>> + */
>> +#define I2C_WRITE_DATA(client, tpm_register, tpm_data, tpm_size)\
>> +        (write8_reg(client, tpm_register | \
>> +       TPM_WRITE_DIRECTION, tpm_data, tpm_size))
>> +
>> +/*
>> + * I2C_READ_DATA
>> + * Recv byte from the TIS register according to the ST33ZP24 I2C protocol.
>> + * @param: tpm, the chip description
>> + * @param: tpm_register, the tpm tis register where the data should be read
>> + * @param: tpm_data, the TPM response
>> + * @param: tpm_size, tpm TPM response size to read.
>> + * @return: Returns zero in case of success else the negative error code.
>> + */
>> +#define I2C_READ_DATA(client, tpm_register, tpm_data, tpm_size)\
>> +        (read8_reg(client, tpm_register, tpm_data, tpm_size))
>
> Either remove these macros or just make them static functions.
>
>> +
>> +/*
>> + * release_locality release the active locality
>> + * @param: chip, the tpm chip description.
>> + */
>> +static void release_locality(struct tpm_chip *chip)
>> +{
>> +       u8 data = TPM_ACCESS_ACTIVE_LOCALITY;
>> +
>> +       I2C_WRITE_DATA(tpm_dev.addr, TPM_ACCESS, &data, 1);
>> +}
>> +
>> +/*
>> + * clear_interruption
>> + * clear the TPM interrupt register.
>> + * @param: tpm, the chip description
>> + */
>> +static void clear_interruption(u8 addr)
>> +{
>> +       u8 interrupt;
>> +       I2C_READ_DATA(tpm_dev.addr, TPM_INT_STATUS, &interrupt, 1);
>> +       I2C_WRITE_DATA(tpm_dev.addr, TPM_INT_STATUS, &interrupt, 1);
>> +       I2C_READ_DATA(tpm_dev.addr, TPM_INT_STATUS, &interrupt, 1);
>> +} /* clear_interruption() */
>> +
>> +int wait_for_serirq_timeout(struct tpm_chip *chip, int condition,
>> +        unsigned long timeout)
>> +{
>> +       int status = 2;
>> +
>> +       clear_interruption(tpm_dev.addr);
>> +       if (condition)
>> +               status = 1;
>> +
>> +       return status;
>> +}
>> +
>> +/*
>> + * check_locality if the locality is active
>> + * @param: chip, the tpm chip description
>> + * @return: the active locality or -EACCESS.
>> + */
>> +static int check_locality(struct tpm_chip *chip)
>> +{
>> +       u8 data;
>> +       u8 status;
>> +       status = I2C_READ_DATA(tpm_dev.addr, TPM_ACCESS, &data, 1);
>> +
>> +       if ((status == 0) && (data &
>> +               (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID)) ==
>> +               (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID))
>> +               return chip->vendor.locality;
>> +
>> +       return -EACCES;
>> +
>> +} /* check_locality() */
>> +
>> +/*
>> + * request_locality request the TPM locality
>> + * @param: chip, the chip description
>> + * @return: the active locality or EACCESS.
>> + */
>> +static int request_locality(struct tpm_chip *chip)
>> +{
>> +       unsigned long start, stop;
>> +       long rc;
>> +       u8 data;
>> +       if (check_locality(chip) == chip->vendor.locality)
>> +               return chip->vendor.locality;
>> +
>> +       data = TPM_ACCESS_REQUEST_USE;
>> +       rc = I2C_WRITE_DATA(tpm_dev.addr, TPM_ACCESS, &data, 1);
>> +       if (rc < 0)
>> +               goto end;
>> +
>> +       if (chip->vendor.irq) {
>> +               rc = wait_for_serirq_timeout(chip, (check_locality
>> +                                                      (chip) >= 0),
>> +                                                     chip->vendor.timeout_a);
>> +               if (rc > 0)
>> +                       return chip->vendor.locality;
>> +       } else{
>> +       /* wait for locality activated */
>> +       start = get_timer(0);
>> +       stop = chip->vendor.timeout_a;
>> +               do {
>> +                       if (check_locality(chip) >= 0)
>> +                               return chip->vendor.locality;
>> +
>> +                       msleep(TPM_TIMEOUT);
>> +               } while  (get_timer(start) < stop);
>> +       }
>> +       rc = -EACCES;
>> +end:
>> +       return rc;
>> +} /* request_locality() */
>> +
>> +/*
>> + * tpm_stm_i2c_cancel, cancel is not implemented.
>> + * @param: chip, tpm_chip description.
>> + */
>> +static void tpm_stm_i2c_cancel(struct tpm_chip *chip)
>> +{
>> +       u8 data;
>> +
>> +       data = TPM_STS_COMMAND_READY;
>> +       I2C_WRITE_DATA(tpm_dev.addr, TPM_STS, &data, 1);
>> +       if (chip->vendor.irq)
>> +               wait_for_serirq_timeout(chip, 1, chip->vendor.timeout_a);
>> +}      /* tpm_stm_i2c_cancel() */
>> +
>> +/*
>> + * tpm_stm_spi_status return the TPM_STS register
>> + * @param: chip, the tpm chip description
>> + * @return: the TPM_STS register value.
>> + */
>> +static u8 tpm_stm_i2c_status(struct tpm_chip *chip)
>> +{
>> +       u8 data;
>> +       I2C_READ_DATA(tpm_dev.addr, TPM_STS, &data, 1);
>> +       return data;
>> +}                              /* tpm_stm_i2c_status() */
>> +
>> +/*
>> + * get_burstcount return the burstcount address 0x19 0x1A
>> + * @param: chip, the chip description
>> + * return: the burstcount.
>> + */
>> +static int get_burstcount(struct tpm_chip *chip)
>> +{
>> +       unsigned long start, stop;
>> +       int burstcnt, status;
>> +       u8 tpm_reg, temp;
>> +
>> +       /* wait for burstcount */
>> +       /* which timeout value, spec has 2 answers (c & d) */
>> +       start = get_timer(0);
>> +       stop = chip->vendor.timeout_d;
>> +       do {
>> +               tpm_reg = TPM_STS + 1;
>> +               status = I2C_READ_DATA(tpm_dev.addr, tpm_reg, &temp, 1);
>> +               if (status < 0)
>> +                       goto end;
>> +
>> +               tpm_reg = tpm_reg + 1;
>> +               burstcnt = temp;
>> +               status = I2C_READ_DATA(tpm_dev.addr, tpm_reg, &temp, 1);
>> +               if (status < 0)
>> +                       goto end;
>> +
>> +               burstcnt |= temp << 8;
>> +               if (burstcnt)
>> +                       return burstcnt;
>> +
>> +               msleep(TPM_TIMEOUT);
>> +       } while (get_timer(start) < stop);
>> +
>> +end:
>> +       return -EBUSY;
>> +} /* get_burstcount() */
>> +
>> +
>> +/*
>> + * recv_data receive data
>> + * @param: chip, the tpm chip description
>> + * @param: buf, the buffer where the data are received
>> + * @param: count, the number of data to receive
>> + * @return: the number of bytes read from TPM FIFO.
>> + */
>> +static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count)
>> +{
>> +       int size = 0, burstcnt, len;
>> +
>> +       while (size < count) {
>> +               burstcnt = get_burstcount(chip);
>> +               len = count - size;
>> +               if ((len) > burstcnt)
>> +                       len = burstcnt;
>> +       if (
>> +       I2C_READ_DATA(tpm_dev.addr, TPM_DATA_FIFO, buf + size, len) == 0)
>> +                       size += len;
>> +               else
>> +                       break;
>> +       }
>> +       return size;
>> +} /* recv_data() */
>> +
>> +/*
>> + * tpm_stm_i2c_recv received TPM response through the I2C bus.
>> + * @param: chip, tpm_chip description.
>> + * @param: buf,        the buffer to store datas.
>> + * @param: count, the number of bytes to send.
>> + * @return: Returns zero in case of success else the negative error code.
>> + */
>> +static int tpm_stm_i2c_recv(struct tpm_chip *chip, unsigned char *buf,
>> +                           size_t count)
>> +{
>> +       int size = 0;
>> +       int expected;
>> +
>> +       if (chip == NULL)
>> +               return -EBUSY;
>> +
>> +       if (count < TPM_HEADER_SIZE) {
>> +               size = -EIO;
>> +               goto out;
>> +       }
>> +
>> +       size = recv_data(chip, buf, TPM_HEADER_SIZE);
>> +       if (size < TPM_HEADER_SIZE) {
>> +               dev_err(chip->dev, "Unable to read header\n");
>> +               goto out;
>> +       }
>> +
>> +
>> +       expected = get_unaligned_be32(buf + TPM_RSP_SIZE_BYTE);
>> +       if (expected > count) {
>> +               size = -EIO;
>> +               goto out;
>> +       }
>> +
>> +       size += recv_data(chip, &buf[TPM_HEADER_SIZE],
>> +                                       expected - TPM_HEADER_SIZE);
>> +       if (size < expected) {
>> +               dev_err(chip->dev, "Unable to read remainder of result\n");
>> +               size = -ETIME;
>> +               goto out;
>> +       }
>> +
>> +out:
>> +       chip->vendor.cancel(chip);
>> +       release_locality(chip);
>> +       return size;
>> +} /* tpm_stm_i2c_recv() */
>> +
>> +/*
>> + * tpm_stm_i2c_send send TPM commands through the I2C bus.
>> + *
>> + * @param: chip, tpm_chip description.
>> + * @param: buf,        the buffer to send.
>> + * @param: len, the number of bytes to send.
>> + * @return: Returns zero in case of success else the negative error code.
>> + */
>> +static int tpm_stm_i2c_send(struct tpm_chip *chip, u8 *buf,
>> +                           size_t len)
>> +{
>> +       u32 ret = 0,
>> +           status,
>> +           burstcnt = 0, i, size;
>> +       u8 data;
>> +
>> +       if (chip == NULL)
>> +               return -EBUSY;
>> +       if (len < TPM_HEADER_SIZE)
>> +               return -EBUSY;
>> +
>> +       ret = request_locality(chip);
>> +       if (ret < 0)
>> +               return ret;
>> +
>> +       status = tpm_stm_i2c_status(chip);
>> +       if ((status & TPM_STS_COMMAND_READY) == 0)
>> +               tpm_stm_i2c_cancel(chip);
>> +
>> +       for (i = 0; i < len - 1;) {
>> +               burstcnt = get_burstcount(chip);
>> +               size = len - i - 1;
>> +               if ((size) > burstcnt)
>> +                       size = burstcnt;
>> +               ret = I2C_WRITE_DATA(tpm_dev.addr, TPM_DATA_FIFO, buf, size);
>> +               if (ret < 0)
>> +                       goto out_err;
>> +
>> +               i += size;
>> +       }
>> +
>> +       status = tpm_stm_i2c_status(chip);
>> +       if ((status & TPM_STS_DATA_EXPECT) == 0) {
>> +               ret = -EIO;
>> +               goto out_err;
>> +       }
>> +
>> +       ret = I2C_WRITE_DATA(tpm_dev.addr, TPM_DATA_FIFO, buf + len - 1, 1);
>> +       if (ret < 0)
>> +               goto out_err;
>> +
>> +       status = tpm_stm_i2c_status(chip);
>> +       if ((status & TPM_STS_DATA_EXPECT) != 0) {
>> +               ret = -EIO;
>> +               goto out_err;
>> +       }
>> +
>> +       data = TPM_STS_GO;
>> +       I2C_WRITE_DATA(tpm_dev.addr, TPM_STS, &data, 1);
>> +
>> +       return len;
>> +out_err:
>> +       tpm_stm_i2c_cancel(chip);
>> +       release_locality(chip);
>
> Add debug() on error path?
>
>> +       return ret;
>> +} /* tpm_stm_i2c_send() */
>> +
>> +/*
>> + * tpm_stm_i2c_send_hash send TPM locality 4 hash datas through the I2C bus
>> + * to update the PCR[17].
>> + * @param: chip, the tpm_chip description.
>> + * @param: buf,        the data buffer to send.
>> + * @param: len, the number of bytes to send.
>> + * @return: Returns zero in case of success else the negative error code.
>> + */
>> +static int tpm_stm_i2c_send_hash(struct tpm_chip *chip, unsigned char *buf,
>> +                           size_t len)
>> +{
>> +       u32 ret = 0;
>> +       u8 data;
>> +
>> +       if (chip == NULL)
>> +               return -EBUSY;
>
> Why does NULL mean busy? Shouldn't it be -ENODEV?
>
>> +
>> +       release_locality(chip);
>> +
>> +       tpm_dev.addr = 0x1B;
>> +       chip->vendor.locality = LOCALITY4;
>> +
>> +       data = TPM_DUMMY_BYTE;
>> +       ret = I2C_WRITE_DATA(tpm_dev.addr, TPM_HASH_START, &data, 1);
>> +       if (ret < 0)
>> +               goto end;
>> +       ret = I2C_WRITE_DATA(tpm_dev.addr, TPM_DATA_FIFO, buf, len);
>> +       if (ret < 0)
>> +               goto end;
>> +
>> +end:
>> +       I2C_WRITE_DATA(tpm_dev.addr, TPM_HASH_END, &data, 1);
>> +       release_locality(chip);
>> +       chip->vendor.locality = LOCALITY0;
>> +       tpm_dev.addr = 0x13;
>> +       ret = request_locality(chip);
>> +       return ret;
>> +} /* tpm_stm_i2c_send_hash */
>> +
>> +static struct tpm_vendor_specific st_i2c_tpm = {
>> +       .send = tpm_stm_i2c_send,
>> +       .send_hash = tpm_stm_i2c_send_hash,
>> +       .recv = tpm_stm_i2c_recv,
>> +       .cancel = tpm_stm_i2c_cancel,
>> +       .status = tpm_stm_i2c_status,
>> +       .req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
>> +       .req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
>> +       .req_canceled = TPM_STS_COMMAND_READY,
>> +};
>> +
>> +/*
>> + * tpm_vendor_init initialize the TPM device
>> + * @param: dev_addr, the i2c address of the tpm.
>> + * @return: 0 in case of success.
>> + *      -1 in other case.
>> + */
>> +int tpm_vendor_init(uint32_t dev_addr)
>> +{
>> +       u32 vendor;
>> +       uint old_addr;
>> +       int rc = 0;
>> +       struct tpm_chip *chip;
>> +
>> +       old_addr = tpm_dev.addr;
>> +       if (dev_addr != 0)
>> +               tpm_dev.addr = dev_addr;
>> +
>> +       chip = tpm_register_hardware(&st_i2c_tpm);
>> +
>> +       if (chip < 0) {
>> +               rc = -ENODEV;
>> +               goto out_err;
>> +       }
>> +
>> +       /* Default timeouts */
>> +       chip->vendor.timeout_a = TIS_SHORT_TIMEOUT;
>> +       chip->vendor.timeout_b = TIS_LONG_TIMEOUT;
>> +       chip->vendor.timeout_c = TIS_SHORT_TIMEOUT;
>> +       chip->vendor.timeout_d = TIS_SHORT_TIMEOUT;
>> +
>> +       chip->vendor.locality = LOCALITY0;
>> +
>> +       if (request_locality(chip) != 0) {
>> +               rc = -ENODEV;
>> +               goto out_err;
>> +       }
>> +
>> +       vendor = be32_to_cpu(vendor);
>> +
>> +
>> +       dev_info(dev, "1.2 TPM STMicroelectronics");
>> +       /*
>> +        * A timeout query to TPM can be placed here.
>> +        * Standard timeout values are used so far
>> +        */
>> +
>> +       return 0;
>> +
>> +out_err:
>> +       tpm_dev.addr = old_addr;
>> +       return rc;
>> +} /* tpm_vendor_init() */
>> +
>> +
>> +
>> +void tpm_vendor_cleanup(struct tpm_chip *chip)
>> +{
>> +       release_locality(chip);
>> +} /* tpm_vendor_cleanup() */
>> diff --git a/include/configs/omap3_beagle.h b/include/configs/omap3_beagle.h
>> index 48ce4c0..ef381f8 100644
>> --- a/include/configs/omap3_beagle.h
>> +++ b/include/configs/omap3_beagle.h
>> @@ -111,6 +111,14 @@
>>  #define STATUS_LED_BOOT                        STATUS_LED_BIT
>>  #define STATUS_LED_GREEN               STATUS_LED_BIT1
>>
>> +/* TPM */
>> +#define CONFIG_CMD_TPM
>> +#define CONFIG_TPM
>> +#define CONFIG_ST_TPM_DEBUG
>> +#define CONFIG_ST_TPM_I2C
>> +#define CONFIG_TPM_I2C_BUS     1
>> +#define CONFIG_TPM_I2C_ADDR    0x13
>> +
>>  /* Enable Multi Bus support for I2C */
>>  #define CONFIG_I2C_MULTI_BUS           1
>>
>> diff --git a/include/tpm.h b/include/tpm.h
>> index 7219b73..36ba0bb 100644
>> --- a/include/tpm.h
>> +++ b/include/tpm.h
>> @@ -201,4 +201,22 @@ uint32_t tpm_physical_set_deactivated(uint8_t state);
>>  uint32_t tpm_get_capability(uint32_t cap_area, uint32_t sub_cap,
>>                 void *cap, size_t count);
>>
>> +/*
>> + * tis_sendrecv_hash()
>> + *
>> + * Send the requested data to the TPM for hash in LOC 4
>> + * and then try to get its response
>> + *
>> + * @sendbuf - buffer of the data to hash
>> + * @send_size size of the data to send
>> + * @recvbuf - memory to save the response to
>> + * @recv_len - pointer to the size of the response buffer
>> + *
>> + * Returns 0 on success (and places the number of response bytes at recv_len)
>> + * or -1 on failure.
>> + */
>> +int tis_sendrecv_hash(const uint8_t *sendbuf, size_t send_size,
>> +        uint8_t *recvbuf,
>> +                       size_t *recv_len);
>> +
>>  #endif /* __TPM_H */
>> --
>> 1.7.9.5

Regards,
Simon

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [U-Boot] [PATCH 1/1] TPM: STMicroelectronics u-boot driver I2C
       [not found]     ` <C56FB217EE26FF4AB56DA86C1AB6A2724E40EAD190@SAFEX1MAIL3.st.com>
@ 2014-03-31 18:31       ` Simon Glass
  0 siblings, 0 replies; 13+ messages in thread
From: Simon Glass @ 2014-03-31 18:31 UTC (permalink / raw)
  To: u-boot

Hi Jean-Luc,

On 31 March 2014 02:44, Jean-Luc BLANC <jean-luc.blanc@st.com> wrote:

> Hi Simon,
>
> I rewrote the patch. A complete different version will be pushed soon.
>

Sounds good, thanks. Please cc me when you are ready.

Regards,
Simon

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [U-Boot] [PATCH 1/1] TPM: STMicroelectronics u-boot driver I2C
  2013-03-22 16:28 Mathias leblanc
@ 2013-05-11 21:04 ` Simon Glass
  0 siblings, 0 replies; 13+ messages in thread
From: Simon Glass @ 2013-05-11 21:04 UTC (permalink / raw)
  To: u-boot

Hi Mathias,


On Fri, Mar 22, 2013 at 10:28 AM, Mathias leblanc
<mathias.leblanc@st.com> wrote:
> From: admin01 <admin01@admin01-desktop.(none)>
>
>  * STMicroelectronics version 1.2.0, Copyright (C) 2013
>  * STMicroelectronics comes with ABSOLUTELY NO WARRANTY.
>  * This is free software, and you are welcome to redistribute it
>  * under certain conditions.
>
> This is the driver for TPM chip from ST Microelectronics.
>
> If you have a TPM security chip from STMicroelectronics working with
> an I2C, read the README file and add the correct defines regarding
> the tpm in the configuration file of your board.
> This file is located in include/configs/your_board.h
>
> The driver will be accessible from within uboot terminal.

The TPM code in U-Boot has been updated, also there are some pending patches

Can you please rebase this patch on top of mainline plus the existing
pending patches?

http://patchwork.ozlabs.org/patch/236211/
http://patchwork.ozlabs.org/patch/236212/
http://patchwork.ozlabs.org/patch/236214/
http://patchwork.ozlabs.org/patch/236215/
http://patchwork.ozlabs.org/patch/236213/

If it helps I have put them in branch 'tpm' of u-boot-x86.git.

Regards,
Simon

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [U-Boot] [PATCH 1/1] TPM: STMicroelectronics u-boot driver I2C
  2013-04-22 15:55 Mathias leblanc
  2013-04-22 18:53 ` Tom Rini
@ 2013-04-23  2:33 ` Simon Glass
  1 sibling, 0 replies; 13+ messages in thread
From: Simon Glass @ 2013-04-23  2:33 UTC (permalink / raw)
  To: u-boot

Hi Mathias,

On Mon, Apr 22, 2013 at 8:55 AM, Mathias leblanc <mathias.leblanc@st.com> wrote:
> From: Mathias Leblanc <mathias.leblanc@st.com>
>
>  * STMicroelectronics version 1.2.0, Copyright (C) 2013
>  * This is free software, and you are welcome to redistribute it
>  * under certain conditions.
>
> This is the driver for TPM chip from ST Microelectronics.
>
> If you have a TPM security chip from STMicroelectronics working with
> an I2C, read the README file and add the correct defines regarding
> the tpm in the configuration file of your board.
> This file is located in include/configs/your_board.h
>
> The driver will be accessible from within uboot terminal.

Please can you take a look at current mainline TPM support?

Also there are some new patches on top of that:

http://patchwork.ozlabs.org/patch/236211/
http://patchwork.ozlabs.org/patch/236212/
http://patchwork.ozlabs.org/patch/236214/
http://patchwork.ozlabs.org/patch/236215/
http://patchwork.ozlabs.org/patch/236213/

Please take a look, and perhaps rebase on top of that?

Thanks for your efforts on this.

Regards,
Simon

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [U-Boot] [PATCH 1/1] TPM: STMicroelectronics u-boot driver I2C
  2013-04-22 15:55 Mathias leblanc
@ 2013-04-22 18:53 ` Tom Rini
  2013-04-23  2:33 ` Simon Glass
  1 sibling, 0 replies; 13+ messages in thread
From: Tom Rini @ 2013-04-22 18:53 UTC (permalink / raw)
  To: u-boot

On Mon, Apr 22, 2013 at 05:55:50PM +0200, Mathias leblanc wrote:

> From: Mathias Leblanc <mathias.leblanc@st.com>
> 
>  * STMicroelectronics version 1.2.0, Copyright (C) 2013
>  * This is free software, and you are welcome to redistribute it
>  * under certain conditions.

Just leave these lines out of the commit message.

> This is the driver for TPM chip from ST Microelectronics.
> 
> If you have a TPM security chip from STMicroelectronics working with
> an I2C, read the README file and add the correct defines regarding
> the tpm in the configuration file of your board.
> This file is located in include/configs/your_board.h
> 
> The driver will be accessible from within uboot terminal.

Please see
http://www.denx.de/wiki/view/U-Boot/Patches#Sending_updated_patch_versions
as this is the 3rd or 4th posting of these changes.

[snip]
> diff --git a/common/cmd_tpm.c b/common/cmd_tpm.c
> index 0970a6f..2b7bf35 100644
> --- a/common/cmd_tpm.c
> +++ b/common/cmd_tpm.c
> @@ -145,10 +145,177 @@ static int do_tpm_many(cmd_tbl_t *cmdtp, int flag,
>  	return rv;
>  }
>  
> +static int do_tpm_hash(cmd_tbl_t *cmdtp, int flag, int argc,
> +char * const argv[])

checkpatch warning there, please run tools/checkpatch.pl and fix all
errors.

> +	u8 startup[] = {
> +		0x00, 0xc1,
> +		0x00, 0x00, 0x00, 0x0c,
> +		0x00, 0x00, 0x00, 0x99,
> +		0x00, 0x01
> +	};
> +
> +	u8 selftestfull[] = {
> +		0x00, 0xc1,
> +		0x00, 0x00, 0x00, 0x0a,
> +		0x00, 0x00, 0x00, 0x50
> +	};
> +
> +	u8 readpcr17[] = {
> +		0x00, 0xc1,
> +		0x00, 0x00, 0x00, 0x0e,
> +		0x00, 0x00, 0x00, 0x15,
> +		0x00, 0x00, 0x00, 0x11
> +	};

Comment what these are and/or where they come from.

[snip]
> +	if (!
> +	tis_sendrecv(readpcr17, sizeof(readpcr17), &response, &read_size)) {

Funny place to break the line, split it on the tis_sendrecv params.  And
fix anything else like this too.


[snip]
> +	u8 startup[] = {
> +		0x00, 0xc1,
> +		0x00, 0x00, 0x00, 0x0c,
> +		0x00, 0x00, 0x00, 0x99,
> +		0x00, 0x01
> +	};
> +	u8 selftestfull[] = {
> +		0x00, 0xc1,
> +		0x00, 0x00, 0x00, 0x0a,
> +		0x00, 0x00, 0x00, 0x50
> +	};
> +	u8 getpermflags[] = {
> +		0x00, 0xc1,
> +		0x00, 0x00, 0x00, 0x16,
> +		0x00, 0x00, 0x00, 0x65,
> +		0x00, 0x00, 0x00, 0x04,
> +		0x00, 0x00, 0x00, 0x04,
> +		0x00, 0x00, 0x01, 0x08
> +	};

startup and selftestfull are the same as before?  Shouldn't duplicate
them then.

> +
> +
> +	CHECK(tis_init());
> +
> +

Too much whitespace around the line.

[snip]
> +/* extended error numbers from linux (see errno.h) */
> +#define	ECANCELED	125	/* Operation Canceled */

'#define<space>' please.

> +#define msleep(t) udelay((t)*1000)

We already have a few msleep compatibility defines.  Can you please do a
separate prepatch that adds msleep to <common.h> after the extern for
udelay?

> +
> +/* Timer frequency. Corresponds to msec timer resolution*/
> +#define HZ             1000

Please use CONFIG_SYS_HZ

> +++ b/drivers/tpm/tis_i2c.c
[snip]
> + *	[backport from https://github.com/theopolis/u-boot-sboot/
> + *		blob/master/drivers/tpm/tis_i2c.c]

You're giving the original author credit in the file already, yes?  If
not, please do, and drop these lines.

> +/* Define in board config */
> +#ifndef CONFIG_TPM_I2C_BUS
> +	#define CONFIG_TPM_I2C_BUS 0
> +	#define CONFIG_TPM_I2C_ADDR 0
> +#endif

No extra indentation.

[snip]
> +	if (i2c_probe(tpm.slave_addr) && i2c_probe(tpm.slave_addr)) {
> +		debug(
> +		"%s: fail to probe i2c addr 0x%x\n", __func__, tpm.slave_addr);

Split after __func__ instead.

> diff --git a/drivers/tpm/tpm.c b/drivers/tpm/tpm.c
[snip]

We have this as drivers/tpm/slb9635_i2c/tpm.c now, so I think we need to
be renaming the existing one, same with tpm.h

[snip]
> diff --git a/drivers/tpm/tpm_i2c_st.c b/drivers/tpm/tpm_i2c_st.c
[snip]
> +/*
> + * write8_reg
> + * Send byte to the TIS register according to the ST33ZP24 I2C protocol.
> + * @param: tpm_register, the tpm tis register where the data should be written
> + * @param: tpm_data, the tpm_data to write inside the tpm_register
> + * @param: tpm_size, The length of the data
> + * @return: Returns zero in case of success else the negative error code.
> + */
> +static int write8_reg(u8 addr, u8 tpm_register,
> +		      u8 *tpm_data, u16 tpm_size)
> +{
> +	u8 data;
> +	data = tpm_register;
> +	memcpy(&(tpm_dev.buf[0]), &data, sizeof(data));
> +	memcpy(&(tpm_dev.buf[0])+1, tpm_data, tpm_size);
> +
> +	return i2c_write(addr, 0, 0, &tpm_dev.buf[0],
> +				tpm_size + 1);
> +
> +} /* write8_reg() */

Since this is kerneldoc/etc style commenting, it should be /** at the
start, yes?  And we don't do comments like that at the end of a
function.  And since it is kerneldoc style, can you do a template for
them?  See doc/DocBook/  Thanks!

-- 
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: Digital signature
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20130422/ef503703/attachment.pgp>

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [U-Boot] [PATCH 1/1] TPM: STMicroelectronics u-boot driver I2C
@ 2013-04-22 15:55 Mathias leblanc
  2013-04-22 18:53 ` Tom Rini
  2013-04-23  2:33 ` Simon Glass
  0 siblings, 2 replies; 13+ messages in thread
From: Mathias leblanc @ 2013-04-22 15:55 UTC (permalink / raw)
  To: u-boot

From: Mathias Leblanc <mathias.leblanc@st.com>

 * STMicroelectronics version 1.2.0, Copyright (C) 2013
 * This is free software, and you are welcome to redistribute it
 * under certain conditions.

This is the driver for TPM chip from ST Microelectronics.

If you have a TPM security chip from STMicroelectronics working with
an I2C, read the README file and add the correct defines regarding
the tpm in the configuration file of your board.
This file is located in include/configs/your_board.h

The driver will be accessible from within uboot terminal.

Signed-off-by: Mathias Leblanc <mathias.leblanc@st.com>
---
 Makefile                       |    2 +-
 README                         |   14 +-
 common/cmd_tpm.c               |  170 +++++++++++-
 drivers/tpm/Makefile           |    1 +
 drivers/tpm/compatibility.h    |   52 ++++
 drivers/tpm/tis_i2c.c          |  248 +++++++++++++++++
 drivers/tpm/tpm.c              |  477 ++++++++++++++++++++++++++++++++
 drivers/tpm/tpm.h              |  170 +++++++++++
 drivers/tpm/tpm_i2c_st.c       |  599 ++++++++++++++++++++++++++++++++++++++++
 include/configs/coreboot.h     |    4 +-
 include/configs/omap3_beagle.h |    8 +
 include/tpm.h                  |   17 ++
 12 files changed, 1757 insertions(+), 5 deletions(-)
 create mode 100644 drivers/tpm/compatibility.h
 create mode 100644 drivers/tpm/tis_i2c.c
 create mode 100644 drivers/tpm/tpm.c
 create mode 100644 drivers/tpm/tpm.h
 create mode 100644 drivers/tpm/tpm_i2c_st.c

diff --git a/Makefile b/Makefile
index 12763ce..ef954a4 100644
--- a/Makefile
+++ b/Makefile
@@ -314,7 +314,7 @@ endif
 LIBS-y += drivers/rtc/librtc.o
 LIBS-y += drivers/serial/libserial.o
 LIBS-y += drivers/sound/libsound.o
-LIBS-$(CONFIG_GENERIC_LPC_TPM) += drivers/tpm/libtpm.o
+LIBS-$(CONFIG_TPM) += drivers/tpm/libtpm.o
 LIBS-y += drivers/twserial/libtws.o
 LIBS-y += drivers/usb/eth/libusb_eth.o
 LIBS-y += drivers/usb/gadget/libusb_gadget.o
diff --git a/README b/README
index 7f2506a..3b1b633 100644
--- a/README
+++ b/README
@@ -1201,7 +1201,7 @@ The following options need to be configured:
 			If this option is set, the driver enables cache flush.
 
 - TPM Support:
-		CONFIG_GENERIC_LPC_TPM
+		CONFIG_TPM
 		Support for generic parallel port TPM devices. Only one device
 		per system is supported at this time.
 
@@ -1210,6 +1210,18 @@ The following options need to be configured:
 			to. Contemporary x86 systems usually map it at
 			0xfed40000.
 
+		CONFIG_ST_TPM_I2C
+		Define to compile the ST TPM I2C DRIVER.
+
+		CONFIG_TPM_I2C_BUS
+		Define the bus number of the board.
+
+		CONFIG_TPM_I2C_ADDR
+		Define the address of the TPM.
+
+		CONFIG_CMD_TPM
+		Define to use some TPM u-boot commands.
+
 - USB Support:
 		At the moment only the UHCI host controller is
 		supported (PIP405, MIP405, MPC5200); define
diff --git a/common/cmd_tpm.c b/common/cmd_tpm.c
index 0970a6f..2b7bf35 100644
--- a/common/cmd_tpm.c
+++ b/common/cmd_tpm.c
@@ -145,10 +145,177 @@ static int do_tpm_many(cmd_tbl_t *cmdtp, int flag,
 	return rv;
 }
 
+static int do_tpm_hash(cmd_tbl_t *cmdtp, int flag, int argc,
+char * const argv[])
+{
+	u8 tpm_buffer[MAX_TRANSACTION_SIZE];
+	u32 write_size, read_size;
+	char *p;
+	int rv = -1;
+	argc -= 1;
+	argv += 1;
+	u8 response[MAX_TRANSACTION_SIZE];
+	u32 rlength = MAX_TRANSACTION_SIZE;
+
+	u8 startup[] = {
+		0x00, 0xc1,
+		0x00, 0x00, 0x00, 0x0c,
+		0x00, 0x00, 0x00, 0x99,
+		0x00, 0x01
+	};
+
+	u8 selftestfull[] = {
+		0x00, 0xc1,
+		0x00, 0x00, 0x00, 0x0a,
+		0x00, 0x00, 0x00, 0x50
+	};
+
+	u8 readpcr17[] = {
+		0x00, 0xc1,
+		0x00, 0x00, 0x00, 0x0e,
+		0x00, 0x00, 0x00, 0x15,
+		0x00, 0x00, 0x00, 0x11
+	};
+
+	for (write_size = 0; write_size < argc; write_size++) {
+		u32 datum = simple_strtoul(argv[write_size], &p, 0);
+		if (*p || (datum > 0xff)) {
+			printf("\n%s: bad data value\n\n", argv[write_size]);
+			cmd_usage(cmdtp);
+			return rv;
+		}
+		tpm_buffer[write_size] = (u8)datum;
+	}
+
+	if (tis_init()) {
+		puts("tis_init() failed!\n");
+		return -1;
+	}
+
+	if (tis_open()) {
+		puts("tis_open() failed!\n");
+		return -1;
+	}
+
+	rv = tis_sendrecv(startup, sizeof(startup), &response, &rlength);
+	if (rv) {
+		printf("tpm test startup failed\n");
+		CHECK(tis_close());
+	}
+
+	rv = tis_sendrecv(selftestfull, sizeof(selftestfull), &response,
+		 &rlength);
+	if (rv) {
+		printf("tpm test selftestfull failed\n");
+		CHECK(tis_close());
+	}
+
+	if (!
+	tis_sendrecv(readpcr17, sizeof(readpcr17), &response, &read_size)) {
+			int i;
+			puts("TPM Read PCR 17:\n");
+			for (i = 10; i < read_size; i++)
+				printf(" %2.2x", response[i]);
+			puts("\n");
+			rv = 0;
+			} else {
+				printf("tpm test readpcr17 failed\n");
+				CHECK(tis_close());
+			}
+
+	read_size = sizeof(tpm_buffer);
+	if (!
+	tis_sendrecv_hash(tpm_buffer, write_size, tpm_buffer, &read_size)) {
+			int i;
+			puts("Got TPM Hash response:\n");
+			for (i = 0; i < read_size; i++)
+				printf(" %2.2x", tpm_buffer[i]);
+			puts("\n");
+			rv = 0;
+			} else {
+				puts("tpm hash command failed\n");
+				}
+
+	if (!
+	tis_sendrecv(readpcr17, sizeof(readpcr17), &response, &read_size)) {
+			int i;
+			puts("TPM Read PCR 17 after hash:\n");
+			for (i = 10; i < read_size; i++)
+				printf(" %2.2x", response[i]);
+			puts("\n");
+			rv = 0;
+			} else {
+				printf("tpm test readpcr17 failed\n");
+				CHECK(tis_close());
+				}
+
+	if (tis_close()) {
+		puts("tis_close() failed!\n");
+		rv = -1;
+	}
+
+	return rv;
+}
+
+static int do_tpm_get_flag(void)
+{
+	int rv = 0;
+	u8 response[MAX_TRANSACTION_SIZE];
+	u32 rlength = MAX_TRANSACTION_SIZE;
+
+	u8 startup[] = {
+		0x00, 0xc1,
+		0x00, 0x00, 0x00, 0x0c,
+		0x00, 0x00, 0x00, 0x99,
+		0x00, 0x01
+	};
+	u8 selftestfull[] = {
+		0x00, 0xc1,
+		0x00, 0x00, 0x00, 0x0a,
+		0x00, 0x00, 0x00, 0x50
+	};
+	u8 getpermflags[] = {
+		0x00, 0xc1,
+		0x00, 0x00, 0x00, 0x16,
+		0x00, 0x00, 0x00, 0x65,
+		0x00, 0x00, 0x00, 0x04,
+		0x00, 0x00, 0x00, 0x04,
+		0x00, 0x00, 0x01, 0x08
+	};
+
+
+	CHECK(tis_init());
+
+
+	CHECK(tis_open());
+	rv = tis_sendrecv(startup, sizeof(startup), response, &rlength);
+	if (rv) {
+		printf("tpm test startup failed\n");
+		CHECK(tis_close());
+	}
+	rv = tis_sendrecv(selftestfull, sizeof(selftestfull), response,
+	 &rlength);
+	if (rv) {
+		printf("tpm test selftestfull failed\n");
+		CHECK(tis_close());
+	}
+	rv = tis_sendrecv(getpermflags, sizeof(getpermflags), response,
+	 &rlength);
+	if (rv) {
+		printf("tpm test getpermflags failed\n");
+		CHECK(tis_close());
+	}
+	CHECK(tis_close());
+	return rv;
+}
 
 static int do_tpm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 {
-	return do_tpm_many(cmdtp, flag, argc, argv, 0);
+	if (strcmp(argv[1], "hash"))
+		return do_tpm_hash(cmdtp, flag, argc, argv);
+	else if (strcmp(argv[1], "get_flag"))
+			return do_tpm_get_flag();
+		else
+			return do_tpm_many(cmdtp, flag, argc, argv, 0);
 }
 
 
diff --git a/drivers/tpm/Makefile b/drivers/tpm/Makefile
index be11c8b..802b54e 100644
--- a/drivers/tpm/Makefile
+++ b/drivers/tpm/Makefile
@@ -24,6 +24,7 @@ include $(TOPDIR)/config.mk
 LIB := $(obj)libtpm.o
 
 COBJS-$(CONFIG_GENERIC_LPC_TPM) = generic_lpc_tpm.o
+COBJS-$(CONFIG_ST_TPM_I2C) = tis_i2c.o tpm.o tpm_i2c_st.o
 
 COBJS	:= $(COBJS-y)
 SRCS	:= $(COBJS:.o=.c)
diff --git a/drivers/tpm/compatibility.h b/drivers/tpm/compatibility.h
new file mode 100644
index 0000000..c68a855
--- /dev/null
+++ b/drivers/tpm/compatibility.h
@@ -0,0 +1,52 @@
+/*
+ * Copyright (C) 2011 Infineon Technologies
+ *
+ * Authors:
+ * Peter Huewe <huewe.external@infineon.com>
+ *
+ * Version: 2.1.1
+ *
+ * See file CREDITS for list of people who contributed to this
+ * project.
+ *
+ * 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.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
+ * MA 02111-1307 USA
+ */
+
+#ifndef _COMPATIBILITY_H_
+#define _COMPATIBILITY_H_
+
+/* all includes from U-Boot */
+#include <linux/types.h>
+#include <linux/unaligned/be_byteshift.h>
+#include <asm-generic/errno.h>
+#include <compiler.h>
+#include <common.h>
+
+/* extended error numbers from linux (see errno.h) */
+#define	ECANCELED	125	/* Operation Canceled */
+
+#define msleep(t) udelay((t)*1000)
+
+/* Timer frequency. Corresponds to msec timer resolution*/
+#define HZ             1000
+
+
+#define dev_dbg(dev, format, arg...) debug(format, ##arg)
+#define dev_err(dev, format, arg...) printf(format, ##arg)
+#define dev_info(dev, format, arg...) debug(format, ##arg)
+#define dbg_printf debug
+
+#endif
diff --git a/drivers/tpm/tis_i2c.c b/drivers/tpm/tis_i2c.c
new file mode 100644
index 0000000..3b3740f
--- /dev/null
+++ b/drivers/tpm/tis_i2c.c
@@ -0,0 +1,248 @@
+/*
+ * Copyright (C) 2011 Infineon Technologies
+ *
+ * Authors:
+ * Peter Huewe <huewe.external@infineon.com>
+ *
+ * Description:
+ * Device driver for TCG/TCPA TPM (trusted platform module).
+ * Specifications at www.trustedcomputinggroup.org
+ *
+ * This device driver implements the TPM interface as defined in
+ * the TCG TPM Interface Spec version 1.2, revision 1.0 and the
+ * Infineon I2C Protocol Stack Specification v0.20.
+ *
+ * It is based on the Linux kernel driver tpm.c from Leendert van
+ * Dorn, Dave Safford, Reiner Sailer, and Kyleen Hall.
+ *
+ * Version: 2.1.1
+ *
+ * See file CREDITS for list of people who contributed to this
+ * project.
+ *
+ * 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, version 2 of the
+ * License.
+ *
+ * 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.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
+ * MA 02111-1307 USA
+ *
+ *Date:	Monday April 13
+ *
+ *	[backport from https://github.com/theopolis/u-boot-sboot/
+ *		blob/master/drivers/tpm/tis_i2c.c]
+ */
+
+#include <config.h>
+#include <common.h>
+#include <i2c.h>
+#include "tpm.h"
+
+DECLARE_GLOBAL_DATA_PTR;
+
+/* Define in board config */
+#ifndef CONFIG_TPM_I2C_BUS
+	#define CONFIG_TPM_I2C_BUS 0
+	#define CONFIG_TPM_I2C_ADDR 0
+#endif
+
+/* TPM configuration */
+struct tpm {
+	int i2c_bus;
+	int slave_addr;
+	char inited;
+	int old_bus;
+} tpm;
+
+
+static int tpm_select(void)
+{
+	int ret;
+
+	tpm.old_bus = i2c_get_bus_num();
+	if (tpm.old_bus != tpm.i2c_bus) {
+		ret = i2c_set_bus_num(tpm.i2c_bus);
+		if (ret) {
+			debug("%s: Fail to set i2c bus %d\n", __func__,
+			      tpm.i2c_bus);
+			return -1;
+		}
+	}
+	return 0;
+}
+
+static int tpm_deselect(void)
+{
+	int ret;
+
+	if (tpm.old_bus != i2c_get_bus_num()) {
+		ret = i2c_set_bus_num(tpm.old_bus);
+		if (ret) {
+			debug("%s: Fail to restore i2c bus %d\n",
+			      __func__, tpm.old_bus);
+			return -1;
+		}
+	}
+	tpm.old_bus = -1;
+	return 0;
+}
+
+/**
+ * Decode TPM configuration.
+ *
+ * @param dev	Returns a configuration of TPM device
+ * @return 0 if ok, -1 on error
+ */
+static int tpm_decode_config(struct tpm *dev)
+{
+#ifdef CONFIG_OF_CONTROL
+	const void *blob = gd->fdt_blob;
+	int node, parent;
+	int i2c_bus;
+
+	node = fdtdec_next_compatible(blob, 0, COMPAT_INFINEON_SLB9635_TPM);
+	if (node < 0) {
+		debug("%s: Node not found\n", __func__);
+		return -1;
+	}
+	parent = fdt_parent_offset(blob, node);
+	if (parent < 0) {
+			debug("%s: Cannot find node parent\n", __func__);
+			return -1;
+	}
+	i2c_bus = i2c_get_bus_num_fdt(blob, parent);
+	if (i2c_bus < 0)
+		return -1;
+	dev->i2c_bus = i2c_bus;
+	dev->slave_addr = fdtdec_get_addr(blob, node, "reg");
+#else
+	dev->i2c_bus = CONFIG_TPM_I2C_BUS;
+	dev->slave_addr = CONFIG_TPM_I2C_ADDR;
+#endif
+	return 0;
+}
+
+/* Methods used by cmd_tpm */
+int tis_init(void)
+{
+	if (tpm.inited)
+		return 0;
+	if (tpm_decode_config(&tpm))
+		return -1;
+	if (tpm_select())
+		return -1;
+	/*
+	 * Probe TPM twice; the first probing might fail because TPM is asleep,
+	 * and the probing can wake up TPM.
+	 */
+	if (i2c_probe(tpm.slave_addr) && i2c_probe(tpm.slave_addr)) {
+		debug(
+		"%s: fail to probe i2c addr 0x%x\n", __func__, tpm.slave_addr);
+		return -1;
+	}
+	tpm_deselect();
+	tpm.inited = 1;
+	return 0;
+}
+
+int tis_open(void)
+{
+	int rc;
+	if (!tpm.inited)
+		return -1;
+
+	if (tpm_select())
+		return -1;
+
+	rc = tpm_open(tpm.slave_addr);
+	tpm_deselect();
+
+	return rc;
+}
+
+int tis_close(void)
+{
+	if (!tpm.inited)
+		return -1;
+
+	if (tpm_select())
+		return -1;
+
+	tpm_close();
+
+	tpm_deselect();
+
+	return 0;
+}
+
+int tis_sendrecv_hash(const uint8_t *sendbuf, size_t sbuf_size,
+		uint8_t *recvbuf, size_t *rbuf_len)
+{
+	int len;
+	uint8_t buf[TPM_BUFSIZE];
+
+	if (!tpm.inited)
+		return -1;
+
+	if (sizeof(buf) < sbuf_size)
+		return -1;
+
+	memcpy(buf, sendbuf, sbuf_size);
+
+	if (tpm_select())
+		return -1;
+
+	len = tpm_transmit_hash(buf, sbuf_size);
+
+	tpm_deselect();
+
+	if (len < 10) {
+		*rbuf_len = 0;
+		return -1;
+	}
+
+	memcpy(recvbuf, buf, len);
+	*rbuf_len = len;
+
+	return 0;
+}
+
+int tis_sendrecv(const uint8_t *sendbuf, size_t sbuf_size,
+		uint8_t *recvbuf, size_t *rbuf_len)
+{
+	int len;
+	uint8_t buf[TPM_BUFSIZE];
+
+	if (!tpm.inited)
+		return -1;
+
+	if (sizeof(buf) < sbuf_size)
+		return -1;
+
+	memcpy(buf, sendbuf, sbuf_size);
+
+	if (tpm_select())
+		return -1;
+
+	len = tpm_transmit(buf, sbuf_size);
+
+	tpm_deselect();
+
+	if (len < 10) {
+		*rbuf_len = 0;
+		return -1;
+	}
+
+	memcpy(recvbuf, buf, len);
+	*rbuf_len = len;
+
+	return 0;
+}
diff --git a/drivers/tpm/tpm.c b/drivers/tpm/tpm.c
new file mode 100644
index 0000000..25d6157
--- /dev/null
+++ b/drivers/tpm/tpm.c
@@ -0,0 +1,477 @@
+/*
+ * Copyright (C) 2011 Infineon Technologies
+ *
+ * Authors:
+ * Peter Huewe <huewe.external@infineon.com>
+ *
+ * Description:
+ * Device driver for TCG/TCPA TPM (trusted platform module).
+ * Specifications at www.trustedcomputinggroup.org
+ *
+ * It is based on the Linux kernel driver tpm.c from Leendert van
+ * Dorn, Dave Safford, Reiner Sailer, and Kyleen Hall.
+ *
+ * Version: 2.1.1
+ *
+ * See file CREDITS for list of people who contributed to this
+ * project.
+ *
+ * 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, version 2 of the
+ * License.
+ *
+ * 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.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
+ * MA 02111-1307 USA
+ *
+ *Date:	Monday April 13
+ *
+ *	[backport from https://github.com/theopolis/u-boot-sboot/
+ *		blob/master/drivers/tpm/tpm.c]
+ */
+
+#include <malloc.h>
+#include "tpm.h"
+
+/* global structure for tpm chip data */
+struct tpm_chip g_chip;
+
+enum tpm_duration {
+	TPM_SHORT = 0,
+	TPM_MEDIUM = 1,
+	TPM_LONG = 2,
+	TPM_UNDEFINED,
+};
+
+#define TPM_MAX_ORDINAL 243
+#define TPM_MAX_PROTECTED_ORDINAL 12
+#define TPM_PROTECTED_ORDINAL_MASK 0xFF
+
+/*
+ * Array with one entry per ordinal defining the maximum amount
+ * of time the chip could take to return the result.  The ordinal
+ * designation of short, medium or long is defined in a table in
+ * TCG Specification TPM Main Part 2 TPM Structures Section 17. The
+ * values of the SHORT, MEDIUM, and LONG durations are retrieved
+ * from the chip during initialization with a call to tpm_get_timeouts.
+ */
+static const u8 tpm_protected_ordinal_duration[TPM_MAX_PROTECTED_ORDINAL] = {
+	TPM_UNDEFINED,		/* 0 */
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,		/* 5 */
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_SHORT,		/* 10 */
+	TPM_SHORT,
+};
+
+static const u8 tpm_ordinal_duration[TPM_MAX_ORDINAL] = {
+	TPM_UNDEFINED,		/* 0 */
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,		/* 5 */
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_SHORT,		/* 10 */
+	TPM_SHORT,
+	TPM_MEDIUM,
+	TPM_LONG,
+	TPM_LONG,
+	TPM_MEDIUM,		/* 15 */
+	TPM_SHORT,
+	TPM_SHORT,
+	TPM_MEDIUM,
+	TPM_LONG,
+	TPM_SHORT,		/* 20 */
+	TPM_SHORT,
+	TPM_MEDIUM,
+	TPM_MEDIUM,
+	TPM_MEDIUM,
+	TPM_SHORT,		/* 25 */
+	TPM_SHORT,
+	TPM_MEDIUM,
+	TPM_SHORT,
+	TPM_SHORT,
+	TPM_MEDIUM,		/* 30 */
+	TPM_LONG,
+	TPM_MEDIUM,
+	TPM_SHORT,
+	TPM_SHORT,
+	TPM_SHORT,		/* 35 */
+	TPM_MEDIUM,
+	TPM_MEDIUM,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_MEDIUM,		/* 40 */
+	TPM_LONG,
+	TPM_MEDIUM,
+	TPM_SHORT,
+	TPM_SHORT,
+	TPM_SHORT,		/* 45 */
+	TPM_SHORT,
+	TPM_SHORT,
+	TPM_SHORT,
+	TPM_LONG,
+	TPM_MEDIUM,		/* 50 */
+	TPM_MEDIUM,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,		/* 55 */
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_MEDIUM,		/* 60 */
+	TPM_MEDIUM,
+	TPM_MEDIUM,
+	TPM_SHORT,
+	TPM_SHORT,
+	TPM_MEDIUM,		/* 65 */
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_SHORT,		/* 70 */
+	TPM_SHORT,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,		/* 75 */
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_LONG,		/* 80 */
+	TPM_UNDEFINED,
+	TPM_MEDIUM,
+	TPM_LONG,
+	TPM_SHORT,
+	TPM_UNDEFINED,		/* 85 */
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_SHORT,		/* 90 */
+	TPM_SHORT,
+	TPM_SHORT,
+	TPM_SHORT,
+	TPM_SHORT,
+	TPM_UNDEFINED,		/* 95 */
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_MEDIUM,		/* 100 */
+	TPM_SHORT,
+	TPM_SHORT,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,		/* 105 */
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_SHORT,		/* 110 */
+	TPM_SHORT,
+	TPM_SHORT,
+	TPM_SHORT,
+	TPM_SHORT,
+	TPM_SHORT,		/* 115 */
+	TPM_SHORT,
+	TPM_SHORT,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_LONG,		/* 120 */
+	TPM_LONG,
+	TPM_MEDIUM,
+	TPM_UNDEFINED,
+	TPM_SHORT,
+	TPM_SHORT,		/* 125 */
+	TPM_SHORT,
+	TPM_LONG,
+	TPM_SHORT,
+	TPM_SHORT,
+	TPM_SHORT,		/* 130 */
+	TPM_MEDIUM,
+	TPM_UNDEFINED,
+	TPM_SHORT,
+	TPM_MEDIUM,
+	TPM_UNDEFINED,		/* 135 */
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_SHORT,		/* 140 */
+	TPM_SHORT,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,		/* 145 */
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_SHORT,		/* 150 */
+	TPM_MEDIUM,
+	TPM_MEDIUM,
+	TPM_SHORT,
+	TPM_SHORT,
+	TPM_UNDEFINED,		/* 155 */
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_SHORT,		/* 160 */
+	TPM_SHORT,
+	TPM_SHORT,
+	TPM_SHORT,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,		/* 165 */
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_LONG,		/* 170 */
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,		/* 175 */
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_MEDIUM,		/* 180 */
+	TPM_SHORT,
+	TPM_MEDIUM,
+	TPM_MEDIUM,
+	TPM_MEDIUM,
+	TPM_MEDIUM,		/* 185 */
+	TPM_SHORT,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,		/* 190 */
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,		/* 195 */
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_SHORT,		/* 200 */
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_SHORT,
+	TPM_SHORT,		/* 205 */
+	TPM_SHORT,
+	TPM_SHORT,
+	TPM_SHORT,
+	TPM_SHORT,
+	TPM_MEDIUM,		/* 210 */
+	TPM_UNDEFINED,
+	TPM_MEDIUM,
+	TPM_MEDIUM,
+	TPM_MEDIUM,
+	TPM_UNDEFINED,		/* 215 */
+	TPM_MEDIUM,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_SHORT,
+	TPM_SHORT,		/* 220 */
+	TPM_SHORT,
+	TPM_SHORT,
+	TPM_SHORT,
+	TPM_SHORT,
+	TPM_UNDEFINED,		/* 225 */
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_SHORT,		/* 230 */
+	TPM_LONG,
+	TPM_MEDIUM,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,		/* 235 */
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_SHORT,		/* 240 */
+	TPM_UNDEFINED,
+	TPM_MEDIUM,
+};
+
+/*
+ * Returns max number of milliseconds to wait
+ */
+unsigned long tpm_calc_ordinal_duration(struct tpm_chip *chip, u32 ordinal)
+{
+	int duration_idx = TPM_UNDEFINED;
+	int duration = 0;
+
+	if (ordinal < TPM_MAX_ORDINAL)
+		duration_idx = tpm_ordinal_duration[ordinal];
+	else if ((ordinal & TPM_PROTECTED_ORDINAL_MASK) <
+		 TPM_MAX_PROTECTED_ORDINAL)
+		duration_idx =
+		    tpm_protected_ordinal_duration[ordinal &
+						   TPM_PROTECTED_ORDINAL_MASK];
+
+	if (duration_idx != TPM_UNDEFINED)
+		duration = chip->vendor.duration[duration_idx];
+	if (duration <= 0)
+		return 2 * 60 * HZ; /*two minutes timeout*/
+	else
+		return duration;
+}
+
+ssize_t tpm_transmit_hash(const unsigned char *buf, size_t bufsiz)
+{
+	ssize_t rc;
+	u32 count, ordinal;
+	unsigned long start, stop;
+
+	struct tpm_chip *chip = &g_chip;
+
+	rc = chip->vendor.send_hash(chip, (u8 *)buf, bufsiz);
+	if (rc < 0) {
+		dev_err(chip->dev, "tpm_transmit: tpm_send: error %zd\n", rc);
+		goto out;
+	}
+
+out_recv:
+
+	dbg_printf("out_recv: reading response...\n");
+	rc = chip->vendor.recv(chip, (u8 *)buf, TPM_BUFSIZE);
+	if (rc < 0)
+		dev_err(chip->dev, "tpm_transmit: tpm_recv: error %zd\n", rc);
+out:
+	return rc;
+}
+
+ssize_t tpm_transmit(const unsigned char *buf, size_t bufsiz)
+{
+	ssize_t rc;
+	u32 count, ordinal;
+	unsigned long start, stop;
+
+	struct tpm_chip *chip = &g_chip;
+
+	/* switch endianess: big->little */
+	count = get_unaligned_be32(buf + TPM_CMD_SIZE_BYTE);
+	ordinal = get_unaligned_be32(buf + TPM_CMD_ORDINAL_BYTE);
+
+	if (count == 0) {
+		dev_err(chip->dev, "no data\n");
+		return -ENODATA;
+	}
+	if (count > bufsiz) {
+		dev_err(chip->dev,
+			"invalid count value %x %zx\n", count, bufsiz);
+		return -E2BIG;
+	}
+
+	rc = chip->vendor.send(chip, (u8 *)buf, count);
+	if (rc < 0) {
+		dev_err(chip->dev, "tpm_transmit: tpm_send: error %zd\n", rc);
+		goto out;
+	}
+
+	if (chip->vendor.irq)
+		goto out_recv;
+
+	start = get_timer(0);
+	stop = tpm_calc_ordinal_duration(chip, ordinal);
+	do {
+		dbg_printf("waiting for status...\n");
+		u8 status = chip->vendor.status(chip);
+		if ((status & chip->vendor.req_complete_mask) ==
+		    chip->vendor.req_complete_val) {
+			dbg_printf("...got it;\n");
+			goto out_recv;
+		}
+
+		if ((status == chip->vendor.req_canceled)) {
+			dev_err(chip->dev, "Operation Canceled\n");
+			rc = -ECANCELED;
+			goto out;
+		}
+		msleep(TPM_TIMEOUT);
+	} while (get_timer(start) < stop);
+
+	chip->vendor.cancel(chip);
+	dev_err(chip->dev, "Operation Timed out\n");
+	rc = -ETIME;
+	goto out;
+
+out_recv:
+
+	dbg_printf("out_recv: reading response...\n");
+	rc = chip->vendor.recv(chip, (u8 *)buf, TPM_BUFSIZE);
+	if (rc < 0)
+		dev_err(chip->dev, "tpm_transmit: tpm_recv: error %zd\n", rc);
+out:
+	return rc;
+}
+
+enum tpm_capabilities {
+	TPM_CAP_PROP = cpu_to_be32(5),
+};
+
+enum tpm_sub_capabilities {
+	TPM_CAP_PROP_TIS_TIMEOUT = cpu_to_be32(0x115),
+	TPM_CAP_PROP_TIS_DURATION = cpu_to_be32(0x120),
+};
+
+struct tpm_chip *tpm_register_hardware(const struct tpm_vendor_specific *entry)
+{
+	struct tpm_chip *chip;
+
+	/* Driver specific per-device data */
+	chip = &g_chip;
+	memcpy(&chip->vendor, entry, sizeof(struct tpm_vendor_specific));
+	chip->is_open = 1;
+
+	return chip;
+}
+
+int tpm_open(uint32_t dev_addr)
+{
+	int rc;
+	if (g_chip.is_open)
+		return -EBUSY;
+	rc = tpm_vendor_init(dev_addr);
+	if (rc < 0)
+		g_chip.is_open = 0;
+	return rc;
+}
+
+void tpm_close(void)
+{
+	if (g_chip.is_open) {
+		tpm_vendor_cleanup(&g_chip);
+		g_chip.is_open = 0;
+	}
+}
diff --git a/drivers/tpm/tpm.h b/drivers/tpm/tpm.h
new file mode 100644
index 0000000..e5ce16e
--- /dev/null
+++ b/drivers/tpm/tpm.h
@@ -0,0 +1,169 @@
+/*
+ * Copyright (C) 2011 Infineon Technologies
+ *
+ * Authors:
+ * Peter Huewe <huewe.external@infineon.com>
+ *
+ * Version: 2.1.1
+ *
+ * Description:
+ * Device driver for TCG/TCPA TPM (trusted platform module).
+ * Specifications at www.trustedcomputinggroup.org
+ *
+ * It is based on the Linux kernel driver tpm.c from Leendert van
+ * Dorn, Dave Safford, Reiner Sailer, and Kyleen Hall.
+ *
+ *
+ * See file CREDITS for list of people who contributed to this
+ * project.
+ *
+ * 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, version 2 of the
+ * License.
+ *
+ * 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.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
+ * MA 02111-1307 USA
+ *
+ *Date:	Monday April 13
+ *
+ *	[backport from https://github.com/theopolis/u-boot-sboot/
+ *		blob/master/drivers/tpm/tpm.h]
+ */
+
+#ifndef _TPM_H_
+#define _TPM_H_
+
+#include "compatibility.h"
+
+enum tpm_timeout {
+	TPM_TIMEOUT = 5,	/* msecs */
+};
+
+/* Size of external transmit buffer (used in tpm_transmit)*/
+#define TPM_BUFSIZE 4096
+#define TPM_ERROR_SIZE 10
+#define TPM_HEADER_SIZE 10
+
+/* Index of fields in TPM command buffer */
+#define TPM_CMD_SIZE_BYTE 2
+#define TPM_CMD_ORDINAL_BYTE 6
+
+/* Index of Count field in TPM response buffer */
+#define TPM_RSP_SIZE_BYTE 2
+#define TPM_RSP_RC_BYTE 6
+
+struct tpm_chip;
+
+struct tpm_vendor_specific {
+	const u8 req_complete_mask;
+	const u8 req_complete_val;
+	const u8 req_canceled;
+	int irq;
+	int (*recv) (struct tpm_chip *, u8 *, size_t);
+	int (*send) (struct tpm_chip *, u8 *, size_t);
+	int (*send_hash) (struct tpm_chip *, u8 *, size_t);
+	void (*cancel) (struct tpm_chip *);
+	 u8 (*status) (struct tpm_chip *);
+	int locality;
+	unsigned long timeout_a, timeout_b, timeout_c, timeout_d; /* msec */
+	unsigned long duration[3];	/* msec */
+};
+
+struct tpm_chip {
+	int is_open;
+	struct tpm_vendor_specific vendor;
+	/* testing */
+	u8 dev;
+};
+
+struct tpm_input_header {
+	__be16 tag;
+	__be32 length;
+	__be32 ordinal;
+} __packed;
+
+struct tpm_output_header {
+	__be16 tag;
+	__be32 length;
+	__be32 return_code;
+} __packed;
+
+struct timeout_t {
+	__be32 a;
+	__be32 b;
+	__be32 c;
+	__be32 d;
+} __packed;
+
+struct duration_t {
+	__be32 tpm_short;
+	__be32 tpm_medium;
+	__be32 tpm_long;
+} __packed;
+
+struct cap_t {
+	struct timeout_t timeout;
+	struct duration_t duration;
+} __packed;
+
+struct tpm_getcap_params_in {
+	__be32 cap;
+	__be32 subcap_size;
+	__be32 subcap;
+} __packed;
+
+struct tpm_getcap_params_out {
+	__be32 cap_size;
+	struct cap_t cap;
+} __packed;
+
+struct tpm_cmd_header {
+	struct tpm_input_header in;
+	struct tpm_output_header out;
+} __packed;
+
+struct tpm_cmd_params {
+	struct tpm_getcap_params_out getcap_out;
+	struct tpm_getcap_params_in getcap_in;
+} __packed;
+
+struct tpm_cmd_t {
+	struct tpm_cmd_header header;
+	struct tpm_cmd_params params;
+} __packed;
+
+
+/* ---------- Interface for TPM vendor ------------ */
+
+extern struct tpm_chip *tpm_register_hardware(
+	const struct tpm_vendor_specific *);
+
+extern int tpm_vendor_init(uint32_t dev_addr);
+
+extern void tpm_vendor_cleanup(struct tpm_chip *chip);
+
+/* ---------- Interface for TDDL ------------------- */
+
+/*
+ * if dev_addr != 0 - redefines TPM device address
+ * Returns < 0 on error, 0 on success.
+ */
+extern int tpm_open(uint32_t dev_addr);
+
+extern void tpm_close(void);
+
+/*
+ * Transmit bufsiz bytes out of buf to TPM and get results back in buf, too.
+ * Returns < 0 on error, 0 on success.
+ */
+extern ssize_t tpm_transmit(const unsigned char *buf, size_t bufsiz);
+
+#endif
diff --git a/drivers/tpm/tpm_i2c_st.c b/drivers/tpm/tpm_i2c_st.c
new file mode 100644
index 0000000..f630dbd
--- /dev/null
+++ b/drivers/tpm/tpm_i2c_st.c
@@ -0,0 +1,599 @@
+/*
+ * STMicroelectronics TPM I2C UBOOT Linux driver for TPM ST33ZP24
+ * Copyright (C) 2013  STMicroelectronics
+ *
+ * (c) Copyright 2013 Mathias Leblanc <mathias.leblanc@st.com>
+ * This file is released under the terms of GPL v2 and any later version
+ * See the file COPYING in the root directory of the source tree for details
+ *
+ * Description:
+ * Device driver for TCG/TCPA TPM (trusted platform module).
+ * Specifications at www.trustedcomputinggroup.org
+ *
+ * This device driver implements the TPM interface as defined in
+ * the TCG TPM Interface Spec version 1.2, revision 1.0 and the
+ * STMicroelectronics I2C Protocol Stack Specification version 1.2.0.
+ *
+ * It is based on the Linux I2C TPM driver from Peter Huewe, modified
+ * from the original tpm
+ * device drivers from Leendert van Dorn, Dave Safford, Reiner Sailer
+ * and Kyleen Hall.
+ *
+ * See file CREDITS for list of people who contributed to this
+ * project.
+ *
+ * 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, version 2 of the
+ * License.
+ *
+ * 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.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
+ * MA 02111-1307 USA
+ *
+ * @Author: Mathias Leblanc tpmsupport at st.com
+ *
+ * @File: tpm_i2c_st.c
+ *
+ */
+
+#include <common.h>
+#include <i2c.h>
+#include <linux/types.h>
+
+#include "compatibility.h"
+#include "tpm.h"
+
+/* max. buffer size supported by our tpm */
+#ifdef TPM_BUFSIZE
+#undef TPM_BUFSIZE
+#endif
+
+#define MINOR_NUM_I2C		224
+
+#define TPM_ACCESS			(0x0)
+#define TPM_STS				(0x18)
+#define TPM_HASH_END			(0x20)
+#define TPM_DATA_FIFO			(0x24)
+#define TPM_HASH_DATA			(0x24)
+#define TPM_HASH_START			(0x28)
+#define TPM_INTF_CAPABILITY		(0x14)
+#define TPM_INT_STATUS			(0x10)
+#define TPM_INT_ENABLE			(0x08)
+
+#define TPM_DUMMY_BYTE			0xAA
+#define TPM_WRITE_DIRECTION		0x80
+#define TPM_HEADER_SIZE			10
+#define TPM_BUFSIZE			2048
+
+#define LOCALITY0		0
+#define LOCALITY4		4
+
+struct st_tpm_hash {
+	int size;
+	u8 *data;
+};
+
+enum stm33zp24_access {
+	TPM_ACCESS_VALID = 0x80,
+	TPM_ACCESS_ACTIVE_LOCALITY = 0x20,
+	TPM_ACCESS_REQUEST_PENDING = 0x04,
+	TPM_ACCESS_REQUEST_USE = 0x02,
+};
+
+enum stm33zp24_status {
+	TPM_STS_VALID = 0x80,
+	TPM_STS_COMMAND_READY = 0x40,
+	TPM_STS_GO = 0x20,
+	TPM_STS_DATA_AVAIL = 0x10,
+	TPM_STS_DATA_EXPECT = 0x08,
+};
+
+enum stm33zp24_int_flags {
+	TPM_GLOBAL_INT_ENABLE = 0x80,
+	TPM_INTF_CMD_READY_INT = 0x080,
+	TPM_INTF_FIFO_AVALAIBLE_INT = 0x040,
+	TPM_INTF_WAKE_UP_READY_INT = 0x020,
+	TPM_INTF_LOC4SOFTRELEASE_INT = 0x008,
+	TPM_INTF_LOCALITY_CHANGE_INT = 0x004,
+	TPM_INTF_STS_VALID_INT = 0x002,
+	TPM_INTF_DATA_AVAIL_INT = 0x001,
+};
+
+enum tis_defaults {
+	TIS_SHORT_TIMEOUT = 750,	/* ms */
+	TIS_LONG_TIMEOUT = 2000,	/* 2 sec */
+};
+
+struct tpm_i2c_ST_dev {
+	uint addr;
+	u8 buf[TPM_BUFSIZE];
+};
+
+static struct tpm_i2c_ST_dev tpm_dev = {
+		/* Note: replace with defined addr from board configuration */
+		.addr = CONFIG_TPM_I2C_ADDR
+};
+
+/*
+ * write8_reg
+ * Send byte to the TIS register according to the ST33ZP24 I2C protocol.
+ * @param: tpm_register, the tpm tis register where the data should be written
+ * @param: tpm_data, the tpm_data to write inside the tpm_register
+ * @param: tpm_size, The length of the data
+ * @return: Returns zero in case of success else the negative error code.
+ */
+static int write8_reg(u8 addr, u8 tpm_register,
+		      u8 *tpm_data, u16 tpm_size)
+{
+	u8 data;
+	data = tpm_register;
+	memcpy(&(tpm_dev.buf[0]), &data, sizeof(data));
+	memcpy(&(tpm_dev.buf[0])+1, tpm_data, tpm_size);
+
+	return i2c_write(addr, 0, 0, &tpm_dev.buf[0],
+				tpm_size + 1);
+
+} /* write8_reg() */
+
+/*
+* read8_reg
+* Recv byte from the TIS register according to the ST33ZP24 I2C protocol.
+* @param: tpm_register, the tpm tis register where the data should be read
+* @param: tpm_data, the TPM response
+* @param: tpm_size, tpm TPM response size to read.
+* @return: Returns zero in case of success else the negative error code.
+*/
+static int read8_reg(u8 addr, u8 tpm_register,
+u8 *tpm_data, int tpm_size)
+{
+	u8 status = 0;
+	u8 data;
+	data = TPM_DUMMY_BYTE;
+	status = write8_reg(addr, tpm_register, &data, 1);
+	if (status == 0)
+		status = i2c_read(addr, 0, 0, tpm_data, tpm_size);
+return status;
+} /* read8_reg() */
+
+/*
+ * I2C_WRITE_DATA
+ * Send byte to the TIS register according to the ST33ZP24 I2C protocol.
+ * @param: client, the chip description
+ * @param: tpm_register, the tpm tis register where the data should be written
+ * @param: tpm_data, the tpm_data to write inside the tpm_register
+ * @param: tpm_size, The length of the data
+ * @return: Returns zero in case of success else the negative error code.
+ */
+#define I2C_WRITE_DATA(client, tpm_register, tpm_data, tpm_size)\
+	 (write8_reg(client, tpm_register | \
+	TPM_WRITE_DIRECTION, tpm_data, tpm_size))
+
+/*
+ * I2C_READ_DATA
+ * Recv byte from the TIS register according to the ST33ZP24 I2C protocol.
+ * @param: tpm, the chip description
+ * @param: tpm_register, the tpm tis register where the data should be read
+ * @param: tpm_data, the TPM response
+ * @param: tpm_size, tpm TPM response size to read.
+ * @return: Returns zero in case of success else the negative error code.
+ */
+#define I2C_READ_DATA(client, tpm_register, tpm_data, tpm_size)\
+	 (read8_reg(client, tpm_register, tpm_data, tpm_size))
+
+/*
+ * release_locality release the active locality
+ * @param: chip, the tpm chip description.
+ */
+static void release_locality(struct tpm_chip *chip)
+{
+	u8 data = TPM_ACCESS_ACTIVE_LOCALITY;
+
+	I2C_WRITE_DATA(tpm_dev.addr, TPM_ACCESS, &data, 1);
+}
+
+/*
+ * clear_interruption
+ * clear the TPM interrupt register.
+ * @param: tpm, the chip description
+ */
+static void clear_interruption(u8 addr)
+{
+	u8 interrupt;
+	I2C_READ_DATA(tpm_dev.addr, TPM_INT_STATUS, &interrupt, 1);
+	I2C_WRITE_DATA(tpm_dev.addr, TPM_INT_STATUS, &interrupt, 1);
+	I2C_READ_DATA(tpm_dev.addr, TPM_INT_STATUS, &interrupt, 1);
+} /* clear_interruption() */
+
+int wait_for_serirq_timeout(struct tpm_chip *chip, int condition,
+	 unsigned long timeout)
+{
+	int status = 2;
+
+	clear_interruption(tpm_dev.addr);
+	if (condition)
+		status = 1;
+
+	return status;
+}
+
+/*
+ * check_locality if the locality is active
+ * @param: chip, the tpm chip description
+ * @return: the active locality or -EACCESS.
+ */
+static int check_locality(struct tpm_chip *chip)
+{
+	u8 data;
+	u8 status;
+	status = I2C_READ_DATA(tpm_dev.addr, TPM_ACCESS, &data, 1);
+
+	if ((status == 0) && (data &
+		(TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID)) ==
+		(TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID))
+		return chip->vendor.locality;
+
+	return -EACCES;
+
+} /* check_locality() */
+
+/*
+ * request_locality request the TPM locality
+ * @param: chip, the chip description
+ * @return: the active locality or EACCESS.
+ */
+static int request_locality(struct tpm_chip *chip)
+{
+	unsigned long start, stop;
+	long rc;
+	u8 data;
+	if (check_locality(chip) == chip->vendor.locality)
+		return chip->vendor.locality;
+
+	data = TPM_ACCESS_REQUEST_USE;
+	rc = I2C_WRITE_DATA(tpm_dev.addr, TPM_ACCESS, &data, 1);
+	if (rc < 0)
+		goto end;
+
+	if (chip->vendor.irq) {
+		rc = wait_for_serirq_timeout(chip, (check_locality
+						       (chip) >= 0),
+						      chip->vendor.timeout_a);
+		if (rc > 0)
+			return chip->vendor.locality;
+	} else{
+	/* wait for locality activated */
+	start = get_timer(0);
+	stop = chip->vendor.timeout_a;
+		do {
+			if (check_locality(chip) >= 0)
+				return chip->vendor.locality;
+
+			msleep(TPM_TIMEOUT);
+		} while	 (get_timer(start) < stop);
+	}
+	rc = -EACCES;
+end:
+	return rc;
+} /* request_locality() */
+
+/*
+ * tpm_stm_i2c_cancel, cancel is not implemented.
+ * @param: chip, tpm_chip description.
+ */
+static void tpm_stm_i2c_cancel(struct tpm_chip *chip)
+{
+	u8 data;
+
+	data = TPM_STS_COMMAND_READY;
+	I2C_WRITE_DATA(tpm_dev.addr, TPM_STS, &data, 1);
+	if (chip->vendor.irq)
+		wait_for_serirq_timeout(chip, 1, chip->vendor.timeout_a);
+}	/* tpm_stm_i2c_cancel() */
+
+/*
+ * tpm_stm_spi_status return the TPM_STS register
+ * @param: chip, the tpm chip description
+ * @return: the TPM_STS register value.
+ */
+static u8 tpm_stm_i2c_status(struct tpm_chip *chip)
+{
+	u8 data;
+	I2C_READ_DATA(tpm_dev.addr, TPM_STS, &data, 1);
+	return data;
+}				/* tpm_stm_i2c_status() */
+
+/*
+ * get_burstcount return the burstcount address 0x19 0x1A
+ * @param: chip, the chip description
+ * return: the burstcount.
+ */
+static int get_burstcount(struct tpm_chip *chip)
+{
+	unsigned long start, stop;
+	int burstcnt, status;
+	u8 tpm_reg, temp;
+
+	/* wait for burstcount */
+	/* which timeout value, spec has 2 answers (c & d) */
+	start = get_timer(0);
+	stop = chip->vendor.timeout_d;
+	do {
+		tpm_reg = TPM_STS + 1;
+		status = I2C_READ_DATA(tpm_dev.addr, tpm_reg, &temp, 1);
+		if (status < 0)
+			goto end;
+
+		tpm_reg = tpm_reg + 1;
+		burstcnt = temp;
+		status = I2C_READ_DATA(tpm_dev.addr, tpm_reg, &temp, 1);
+		if (status < 0)
+			goto end;
+
+		burstcnt |= temp << 8;
+		if (burstcnt)
+			return burstcnt;
+
+		msleep(TPM_TIMEOUT);
+	} while (get_timer(start) < stop);
+
+end:
+	return -EBUSY;
+} /* get_burstcount() */
+
+
+/*
+ * recv_data receive data
+ * @param: chip, the tpm chip description
+ * @param: buf, the buffer where the data are received
+ * @param: count, the number of data to receive
+ * @return: the number of bytes read from TPM FIFO.
+ */
+static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count)
+{
+	int size = 0, burstcnt, len;
+
+	while (size < count) {
+		burstcnt = get_burstcount(chip);
+		len = count - size;
+		if ((len) > burstcnt)
+			len = burstcnt;
+	if (
+	I2C_READ_DATA(tpm_dev.addr, TPM_DATA_FIFO, buf + size, len) == 0)
+			size += len;
+		else
+			break;
+	}
+	return size;
+} /* recv_data() */
+
+/*
+ * tpm_stm_i2c_recv received TPM response through the I2C bus.
+ * @param: chip, tpm_chip description.
+ * @param: buf,	the buffer to store datas.
+ * @param: count, the number of bytes to send.
+ * @return: Returns zero in case of success else the negative error code.
+ */
+static int tpm_stm_i2c_recv(struct tpm_chip *chip, unsigned char *buf,
+			    size_t count)
+{
+	int size = 0;
+	int expected;
+
+	if (chip == NULL)
+		return -EBUSY;
+
+	if (count < TPM_HEADER_SIZE) {
+		size = -EIO;
+		goto out;
+	}
+
+	size = recv_data(chip, buf, TPM_HEADER_SIZE);
+	if (size < TPM_HEADER_SIZE) {
+		dev_err(chip->dev, "Unable to read header\n");
+		goto out;
+	}
+
+
+	expected = get_unaligned_be32(buf + TPM_RSP_SIZE_BYTE);
+	if (expected > count) {
+		size = -EIO;
+		goto out;
+	}
+
+	size += recv_data(chip, &buf[TPM_HEADER_SIZE],
+					expected - TPM_HEADER_SIZE);
+	if (size < expected) {
+		dev_err(chip->dev, "Unable to read remainder of result\n");
+		size = -ETIME;
+		goto out;
+	}
+
+out:
+	chip->vendor.cancel(chip);
+	release_locality(chip);
+	return size;
+} /* tpm_stm_i2c_recv() */
+
+/*
+ * tpm_stm_i2c_send send TPM commands through the I2C bus.
+ *
+ * @param: chip, tpm_chip description.
+ * @param: buf,	the buffer to send.
+ * @param: len, the number of bytes to send.
+ * @return: Returns zero in case of success else the negative error code.
+ */
+static int tpm_stm_i2c_send(struct tpm_chip *chip, u8 *buf,
+			    size_t len)
+{
+	u32 ret = 0,
+	    status,
+	    burstcnt = 0, i, size;
+	u8 data;
+
+	if (chip == NULL)
+		return -EBUSY;
+	if (len < TPM_HEADER_SIZE)
+		return -EBUSY;
+
+	ret = request_locality(chip);
+	if (ret < 0)
+		return ret;
+
+	status = tpm_stm_i2c_status(chip);
+	if ((status & TPM_STS_COMMAND_READY) == 0)
+		tpm_stm_i2c_cancel(chip);
+
+	for (i = 0; i < len - 1;) {
+		burstcnt = get_burstcount(chip);
+		size = len - i - 1;
+		if ((size) > burstcnt)
+			size = burstcnt;
+		ret = I2C_WRITE_DATA(tpm_dev.addr, TPM_DATA_FIFO, buf, size);
+		if (ret < 0)
+			goto out_err;
+
+		i += size;
+	}
+
+	status = tpm_stm_i2c_status(chip);
+	if ((status & TPM_STS_DATA_EXPECT) == 0) {
+		ret = -EIO;
+		goto out_err;
+	}
+
+	ret = I2C_WRITE_DATA(tpm_dev.addr, TPM_DATA_FIFO, buf + len - 1, 1);
+	if (ret < 0)
+		goto out_err;
+
+	status = tpm_stm_i2c_status(chip);
+	if ((status & TPM_STS_DATA_EXPECT) != 0) {
+		ret = -EIO;
+		goto out_err;
+	}
+
+	data = TPM_STS_GO;
+	I2C_WRITE_DATA(tpm_dev.addr, TPM_STS, &data, 1);
+
+	return len;
+out_err:
+	tpm_stm_i2c_cancel(chip);
+	release_locality(chip);
+	return ret;
+} /* tpm_stm_i2c_send() */
+
+/*
+ * tpm_stm_i2c_send_hash send TPM locality 4 hash datas through the I2C bus
+ * to update the PCR[17].
+ * @param: chip, the tpm_chip description.
+ * @param: buf,	the data buffer to send.
+ * @param: len, the number of bytes to send.
+ * @return: Returns zero in case of success else the negative error code.
+ */
+static int tpm_stm_i2c_send_hash(struct tpm_chip *chip, unsigned char *buf,
+			    size_t len)
+{
+	u32 ret = 0;
+	u8 data;
+
+	if (chip == NULL)
+		return -EBUSY;
+
+	release_locality(chip);
+
+	tpm_dev.addr = 0x1B;
+	chip->vendor.locality = LOCALITY4;
+
+	data = TPM_DUMMY_BYTE;
+	ret = I2C_WRITE_DATA(tpm_dev.addr, TPM_HASH_START, &data, 1);
+	if (ret < 0)
+		goto end;
+	ret = I2C_WRITE_DATA(tpm_dev.addr, TPM_DATA_FIFO, buf, len);
+	if (ret < 0)
+		goto end;
+
+end:
+	I2C_WRITE_DATA(tpm_dev.addr, TPM_HASH_END, &data, 1);
+	release_locality(chip);
+	chip->vendor.locality = LOCALITY0;
+	tpm_dev.addr = 0x13;
+	ret = request_locality(chip);
+	return ret;
+} /* tpm_stm_i2c_send_hash */
+
+static struct tpm_vendor_specific st_i2c_tpm = {
+	.send = tpm_stm_i2c_send,
+	.send_hash = tpm_stm_i2c_send_hash,
+	.recv = tpm_stm_i2c_recv,
+	.cancel = tpm_stm_i2c_cancel,
+	.status = tpm_stm_i2c_status,
+	.req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
+	.req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
+	.req_canceled = TPM_STS_COMMAND_READY,
+};
+
+/*
+ * tpm_vendor_init initialize the TPM device
+ * @param: dev_addr, the i2c address of the tpm.
+ * @return: 0 in case of success.
+ *	 -1 in other case.
+ */
+int tpm_vendor_init(uint32_t dev_addr)
+{
+	u32 vendor;
+	uint old_addr;
+	int rc = 0;
+	struct tpm_chip *chip;
+
+	old_addr = tpm_dev.addr;
+	if (dev_addr != 0)
+		tpm_dev.addr = dev_addr;
+
+	chip = tpm_register_hardware(&st_i2c_tpm);
+
+	if (chip < 0) {
+		rc = -ENODEV;
+		goto out_err;
+	}
+
+	/* Default timeouts */
+	chip->vendor.timeout_a = TIS_SHORT_TIMEOUT;
+	chip->vendor.timeout_b = TIS_LONG_TIMEOUT;
+	chip->vendor.timeout_c = TIS_SHORT_TIMEOUT;
+	chip->vendor.timeout_d = TIS_SHORT_TIMEOUT;
+
+	chip->vendor.locality = LOCALITY0;
+
+	if (request_locality(chip) != 0) {
+		rc = -ENODEV;
+		goto out_err;
+	}
+
+	vendor = be32_to_cpu(vendor);
+
+
+	dev_info(dev, "1.2 TPM STMicroelectronics");
+	/*
+	 * A timeout query to TPM can be placed here.
+	 * Standard timeout values are used so far
+	 */
+
+	return 0;
+
+out_err:
+	tpm_dev.addr = old_addr;
+	return rc;
+} /* tpm_vendor_init() */
+
+
+
+void tpm_vendor_cleanup(struct tpm_chip *chip)
+{
+	release_locality(chip);
+} /* tpm_vendor_cleanup() */
diff --git a/include/configs/coreboot.h b/include/configs/coreboot.h
index 87daf62..fda6512 100644
--- a/include/configs/coreboot.h
+++ b/include/configs/coreboot.h
@@ -76,8 +76,8 @@
 					 CONFIG_SYS_SCSI_MAX_LUN)
 #endif
 
-/* Generic TPM interfaced through LPC bus */
-#define CONFIG_GENERIC_LPC_TPM
+/* Generic TPM interfaced through LPC / I2C bus */
+#define CONFIG_TPM
 #define CONFIG_TPM_TIS_BASE_ADDRESS        0xfed40000
 
 /*-----------------------------------------------------------------------
diff --git a/include/configs/omap3_beagle.h b/include/configs/omap3_beagle.h
index 48ce4c0..ef381f8 100644
--- a/include/configs/omap3_beagle.h
+++ b/include/configs/omap3_beagle.h
@@ -111,6 +111,14 @@
 #define STATUS_LED_BOOT			STATUS_LED_BIT
 #define STATUS_LED_GREEN		STATUS_LED_BIT1
 
+/* TPM */
+#define CONFIG_CMD_TPM
+#define CONFIG_TPM
+#define CONFIG_ST_TPM_DEBUG
+#define CONFIG_ST_TPM_I2C
+#define CONFIG_TPM_I2C_BUS	1
+#define CONFIG_TPM_I2C_ADDR	0x13
+
 /* Enable Multi Bus support for I2C */
 #define CONFIG_I2C_MULTI_BUS		1
 
diff --git a/include/tpm.h b/include/tpm.h
index 6b21e9c..77a1514 100644
--- a/include/tpm.h
+++ b/include/tpm.h
@@ -68,4 +68,21 @@ int tis_close(void);
 int tis_sendrecv(const uint8_t *sendbuf, size_t send_size, uint8_t *recvbuf,
 			size_t *recv_len);
 
+/*
+ * tis_sendrecv_hash()
+ *
+ * Send the requested data to the TPM for hash in LOC 4
+ * and then try to get its response
+ *
+ * @sendbuf - buffer of the data to hash
+ * @send_size size of the data to send
+ * @recvbuf - memory to save the response to
+ * @recv_len - pointer to the size of the response buffer
+ *
+ * Returns 0 on success (and places the number of response bytes at recv_len)
+ * or -1 on failure.
+ */
+int tis_sendrecv_hash(const uint8_t *sendbuf, size_t send_size,
+	 uint8_t *recvbuf,
+			size_t *recv_len);
+
 #endif /* _INCLUDE_TPM_H_ */
-- 
1.7.1

^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [U-Boot] [PATCH 1/1] TPM: STMicroelectronics u-boot driver I2C
  2013-04-08 14:03 Mathias leblanc
@ 2013-04-08 18:25 ` Wolfgang Denk
  0 siblings, 0 replies; 13+ messages in thread
From: Wolfgang Denk @ 2013-04-08 18:25 UTC (permalink / raw)
  To: u-boot

Dear Mathias leblanc,

In message <1365429818-2090-1-git-send-email-mathias.leblanc@st.com> you wrote:
> 
>  * STMicroelectronics version 1.2.0, Copyright (C) 2013

Who exactly is the copyright holder?

>  * STMicroelectronics comes with ABSOLUTELY NO WARRANTY.
>  * This is free software, and you are welcome to redistribute it
>  * under certain conditions.

And these would be what exactly?

Sorry, this makes no sense.  Such statements must be precise to the
finest detail , and they belong into the code, and not into the commit
message.

In the next step, please run your patch through checkpatch and fix the
reported issues.

Please make sure to get rid of all these "__attribute__ ((packed))"
stuff.  Make sure that structs are properly aligned, and use explicit
conversion where and if necessary.


> diff --git a/Makefile b/Makefile
> index 12763ce..ef954a4 100644
> --- a/Makefile
> +++ b/Makefile
> @@ -314,7 +314,7 @@ endif
>  LIBS-y += drivers/rtc/librtc.o
>  LIBS-y += drivers/serial/libserial.o
>  LIBS-y += drivers/sound/libsound.o
> -LIBS-$(CONFIG_GENERIC_LPC_TPM) += drivers/tpm/libtpm.o
> +LIBS-$(CONFIG_TPM) += drivers/tpm/libtpm.o

If you change this, you must also update the realted documentation (in
the README) and _all_ related code (I think you missed at least
include/configs/coreboot.h).

ALso please make sure to add the documentation to the proper groups,
i. e. CONFIG_CMD_TPM should be added to the CONFIG_CMD_* group.

> +	if (tis_init()) {
> +		puts("tis_init() failed!\n");
> +		return -1;
> +	}
> +
> +	if (tis_open()) {
> +		puts("tis_open() failed!\n");
> +		return -1;

Can we have some better diagnostics, for example including the rason
for the failure?

> +	rv = tis_sendrecv(Startup, sizeof(Startup), &response, &rlength);
> +	if (rv) {
> +		printf("tpm test Startup failed\n");
> +		CHECK(tis_close());
> +	}
> +
> +	rv = tis_sendrecv(SelfTestFull, sizeof(SelfTestFull), &response,

Startup, SelfTestFull, ...

We do not allow CamelCase indentifiers.

> +U_BOOT_CMD(tpm_hash, MAX_TRANSACTION_SIZE, 1, do_tpm_hash,
...
> +U_BOOT_CMD(tpm_get_flag, 1, 1, do_tpm_get_flag,

Instead of adding a number of different commands, this should be
implemented as sub-commands under a common one, i. e. make this:

	tpm_hash     --> tpm hash ...
	tpm_get_flag --> tpm get_flag ...

[eventually there is even a better name than "get_flag"?]

...
> +++ b/drivers/tpm/tis_i2c.c
> @@ -0,0 +1,211 @@
> +/*
> + * Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
> + * Use of this source code is governed by a BSD-style license that can be
> + * found in the LICENSE file.

This is not acceptable.  Please be precise here.  We need to know
exacly which license terms apply.  Please see item # 1 at
http://www.denx.de/wiki/view/U-Boot/Patches#Notes  for details.


> diff --git a/drivers/tpm/tpm.c b/drivers/tpm/tpm.c
> new file mode 100644
> index 0000000..3f4f66e
> --- /dev/null
> +++ b/drivers/tpm/tpm.c
> @@ -0,0 +1,472 @@
> +/*
> + * Copyright (C) 2011 Infineon Technologies
> + *
> + * Authors:
> + * Peter Huewe <huewe.external@infineon.com>
> + *
> + * Description:
> + * Device driver for TCG/TCPA TPM (trusted platform module).
> + * Specifications at www.trustedcomputinggroup.org
> + *
> + * It is based on the Linux kernel driver tpm.c from Leendert van
> + * Dorn, Dave Safford, Reiner Sailer, and Kyleen Hall.

Do you have a more precise reference where the code is coming from?
What we would like to see is described in bullet # 4 at
http://www.denx.de/wiki/view/U-Boot/Patches#Attributing_Code_Copyrights_Sign


Best regards,

Wolfgang Denk

-- 
DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
It's hard to think of you as the end result of millions of  years  of
evolution.

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [U-Boot] [PATCH 1/1] TPM: STMicroelectronics u-boot driver I2C
@ 2013-04-08 14:03 Mathias leblanc
  2013-04-08 18:25 ` Wolfgang Denk
  0 siblings, 1 reply; 13+ messages in thread
From: Mathias leblanc @ 2013-04-08 14:03 UTC (permalink / raw)
  To: u-boot

From: Mathias Leblanc <mathias.leblanc@st.com>

 * STMicroelectronics version 1.2.0, Copyright (C) 2013
 * STMicroelectronics comes with ABSOLUTELY NO WARRANTY.
 * This is free software, and you are welcome to redistribute it
 * under certain conditions.

This is the driver for TPM chip from ST Microelectronics.

If you have a TPM security chip from STMicroelectronics working with
an I2C, read the README file and add the correct defines regarding
the tpm in the configuration file of your board.
This file is located in include/configs/your_board.h

The driver will be accessible from within uboot terminal.

Signed-off-by: Mathias Leblanc <mathias.leblanc@st.com>
---
 Makefile                       |    2 +-
 README                         |   12 +
 common/cmd_tpm.c               |  176 ++++++++++++
 drivers/tpm/Makefile           |    1 +
 drivers/tpm/compatibility.h    |   52 ++++
 drivers/tpm/tis_i2c.c          |  211 ++++++++++++++
 drivers/tpm/tpm.c              |  472 +++++++++++++++++++++++++++++++
 drivers/tpm/tpm.h              |  164 +++++++++++
 drivers/tpm/tpm_i2c_st.c       |  601 ++++++++++++++++++++++++++++++++++++++++
 include/configs/omap3_beagle.h |    8 +
 include/tpm.h                  |   16 +
 11 files changed, 1714 insertions(+), 1 deletions(-)
 create mode 100644 drivers/tpm/compatibility.h
 create mode 100644 drivers/tpm/tis_i2c.c
 create mode 100644 drivers/tpm/tpm.c
 create mode 100644 drivers/tpm/tpm.h
 create mode 100644 drivers/tpm/tpm_i2c_st.c

diff --git a/Makefile b/Makefile
index 12763ce..ef954a4 100644
--- a/Makefile
+++ b/Makefile
@@ -314,7 +314,7 @@ endif
 LIBS-y += drivers/rtc/librtc.o
 LIBS-y += drivers/serial/libserial.o
 LIBS-y += drivers/sound/libsound.o
-LIBS-$(CONFIG_GENERIC_LPC_TPM) += drivers/tpm/libtpm.o
+LIBS-$(CONFIG_TPM) += drivers/tpm/libtpm.o
 LIBS-y += drivers/twserial/libtws.o
 LIBS-y += drivers/usb/eth/libusb_eth.o
 LIBS-y += drivers/usb/gadget/libusb_gadget.o
diff --git a/README b/README
index 7f2506a..0e497d1 100644
--- a/README
+++ b/README
@@ -1210,6 +1210,18 @@ The following options need to be configured:
 			to. Contemporary x86 systems usually map it at
 			0xfed40000.
 
+		CONFIG_ST_TPM_I2C
+		Define to compile the ST TPM I2C DRIVER.
+
+		CONFIG_TPM_I2C_BUS
+		Define the bus number of the board.
+
+		CONFIG_TPM_I2C_ADDR
+		Define the address of the TPM.
+
+		CONFIG_CMD_TPM
+		Define to use some TPM u-boot commands.
+
 - USB Support:
 		At the moment only the UHCI host controller is
 		supported (PIP405, MIP405, MPC5200); define
diff --git a/common/cmd_tpm.c b/common/cmd_tpm.c
index 0970a6f..5d4f485 100644
--- a/common/cmd_tpm.c
+++ b/common/cmd_tpm.c
@@ -158,6 +158,182 @@ U_BOOT_CMD(tpm, MAX_TRANSACTION_SIZE, 1, do_tpm,
 	   "device and read the response"
 );
 
+static int do_tpm_hash(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+{
+	u8 tpm_buffer[MAX_TRANSACTION_SIZE];
+	u32 write_size, read_size;
+	char *p;
+	int rv = -1;
+	argc -= 1;
+	argv += 1;
+	u8 response[MAX_TRANSACTION_SIZE];
+	u32 rlength = MAX_TRANSACTION_SIZE;
+
+	u8 Startup[] = {
+		0x00, 0xc1,
+		0x00, 0x00, 0x00, 0x0c,
+		0x00, 0x00, 0x00, 0x99,
+		0x00, 0x01
+	};
+
+	u8 SelfTestFull[] = {
+		0x00, 0xc1,
+		0x00, 0x00, 0x00, 0x0a,
+		0x00, 0x00, 0x00, 0x50
+	};
+
+	u8 ReadPcr17[] = {
+		0x00, 0xc1,
+		0x00, 0x00, 0x00, 0x0e,
+		0x00, 0x00, 0x00, 0x15,
+		0x00, 0x00, 0x00, 0x11
+	};
+
+	for (write_size = 0; write_size < argc; write_size++) {
+		u32 datum = simple_strtoul(argv[write_size], &p, 0);
+		if (*p || (datum > 0xff)) {
+			printf("\n%s: bad data value\n\n", argv[write_size]);
+			cmd_usage(cmdtp);
+			return rv;
+		}
+		tpm_buffer[write_size] = (u8)datum;
+	}
+
+	if (tis_init()) {
+		puts("tis_init() failed!\n");
+		return -1;
+	}
+
+	if (tis_open()) {
+		puts("tis_open() failed!\n");
+		return -1;
+	}
+
+	rv = tis_sendrecv(Startup, sizeof(Startup), &response, &rlength);
+	if (rv) {
+		printf("tpm test Startup failed\n");
+		CHECK(tis_close());
+	}
+
+	rv = tis_sendrecv(SelfTestFull, sizeof(SelfTestFull), &response,
+		 &rlength);
+	if (rv) {
+		printf("tpm test SelfTestFull failed\n");
+		CHECK(tis_close());
+	}
+
+	if (!tis_sendrecv(ReadPcr17, sizeof(ReadPcr17), &response,
+	 &read_size)) {
+		int i;
+		puts("TPM Read PCR 17:\n");
+		for (i = 10; i < read_size; i++)
+			printf(" %2.2x", response[i]);
+		puts("\n");
+		rv = 0;
+	} else {
+		printf("tpm test ReadPcr17 failed\n");
+		CHECK(tis_close());
+	}
+
+	read_size = sizeof(tpm_buffer);
+	if (!tis_sendrecv_hash(tpm_buffer, write_size, tpm_buffer,
+	 &read_size)) {
+		int i;
+		puts("Got TPM Hash response:\n");
+		for (i = 0; i < read_size; i++)
+			printf(" %2.2x", tpm_buffer[i]);
+		puts("\n");
+		rv = 0;
+	} else {
+		puts("tpm hash command failed\n");
+	}
+
+	if (!tis_sendrecv(ReadPcr17, sizeof(ReadPcr17), &response,
+	 &read_size)) {
+		int i;
+		puts("TPM Read PCR 17 after hash:\n");
+		for (i = 10; i < read_size; i++)
+			printf(" %2.2x", response[i]);
+		puts("\n");
+		rv = 0;
+	} else {
+		printf("tpm test ReadPcr17 failed\n");
+		CHECK(tis_close());
+	}
+
+	if (tis_close()) {
+		puts("tis_close() failed!\n");
+		rv = -1;
+	}
+
+	return rv;
+}
+
+
+U_BOOT_CMD(tpm_hash, MAX_TRANSACTION_SIZE, 1, do_tpm_hash,
+	   "<byte> [<byte> ...]   - write data to hash and read response",
+	   "send arbitrary data to the TPM "
+	   "device and read the response."
+	   "ex: tpm 53 01 75 06 00 08 24 05 00 08 00 01"
+);
+
+static int do_tpm_get_flag(void)
+{
+	int rv = 0;
+	u8 response[MAX_TRANSACTION_SIZE];
+	u32 rlength = MAX_TRANSACTION_SIZE;
+
+	u8 Startup[] = {
+		0x00, 0xc1,
+		0x00, 0x00, 0x00, 0x0c,
+		0x00, 0x00, 0x00, 0x99,
+		0x00, 0x01
+	};
+	u8 SelfTestFull[] = {
+		0x00, 0xc1,
+		0x00, 0x00, 0x00, 0x0a,
+		0x00, 0x00, 0x00, 0x50
+	};
+	u8 GetPermFlags[] = {
+		0x00, 0xc1,
+		0x00, 0x00, 0x00, 0x16,
+		0x00, 0x00, 0x00, 0x65,
+		0x00, 0x00, 0x00, 0x04,
+		0x00, 0x00, 0x00, 0x04,
+		0x00, 0x00, 0x01, 0x08
+	};
+
+
+	CHECK(tis_init());
+
+
+	CHECK(tis_open());
+	rv = tis_sendrecv(Startup, sizeof(Startup), response, &rlength);
+	if (rv) {
+		printf("tpm test Startup failed\n");
+		CHECK(tis_close());
+	}
+	rv = tis_sendrecv(SelfTestFull, sizeof(SelfTestFull), response,
+	 &rlength);
+	if (rv) {
+		printf("tpm test SelfTestFull failed\n");
+		CHECK(tis_close());
+	}
+	rv = tis_sendrecv(GetPermFlags, sizeof(GetPermFlags), response,
+	 &rlength);
+	if (rv) {
+		printf("tpm test GetPermFlags failed\n");
+		CHECK(tis_close());
+	}
+	CHECK(tis_close());
+	return rv;
+}
+
+
+U_BOOT_CMD(tpm_get_flag, 1, 1, do_tpm_get_flag,
+	   "start tpm get flag command", ""
+);
+
 static int do_tpm_stress(cmd_tbl_t *cmdtp, int flag,
 			 int argc, char * const argv[])
 {
diff --git a/drivers/tpm/Makefile b/drivers/tpm/Makefile
index be11c8b..802b54e 100644
--- a/drivers/tpm/Makefile
+++ b/drivers/tpm/Makefile
@@ -24,6 +24,7 @@ include $(TOPDIR)/config.mk
 LIB := $(obj)libtpm.o
 
 COBJS-$(CONFIG_GENERIC_LPC_TPM) = generic_lpc_tpm.o
+COBJS-$(CONFIG_ST_TPM_I2C) = tis_i2c.o tpm.o tpm_i2c_st.o
 
 COBJS	:= $(COBJS-y)
 SRCS	:= $(COBJS:.o=.c)
diff --git a/drivers/tpm/compatibility.h b/drivers/tpm/compatibility.h
new file mode 100644
index 0000000..c68a855
--- /dev/null
+++ b/drivers/tpm/compatibility.h
@@ -0,0 +1,52 @@
+/*
+ * Copyright (C) 2011 Infineon Technologies
+ *
+ * Authors:
+ * Peter Huewe <huewe.external@infineon.com>
+ *
+ * Version: 2.1.1
+ *
+ * See file CREDITS for list of people who contributed to this
+ * project.
+ *
+ * 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.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
+ * MA 02111-1307 USA
+ */
+
+#ifndef _COMPATIBILITY_H_
+#define _COMPATIBILITY_H_
+
+/* all includes from U-Boot */
+#include <linux/types.h>
+#include <linux/unaligned/be_byteshift.h>
+#include <asm-generic/errno.h>
+#include <compiler.h>
+#include <common.h>
+
+/* extended error numbers from linux (see errno.h) */
+#define	ECANCELED	125	/* Operation Canceled */
+
+#define msleep(t) udelay((t)*1000)
+
+/* Timer frequency. Corresponds to msec timer resolution*/
+#define HZ             1000
+
+
+#define dev_dbg(dev, format, arg...) debug(format, ##arg)
+#define dev_err(dev, format, arg...) printf(format, ##arg)
+#define dev_info(dev, format, arg...) debug(format, ##arg)
+#define dbg_printf debug
+
+#endif
diff --git a/drivers/tpm/tis_i2c.c b/drivers/tpm/tis_i2c.c
new file mode 100644
index 0000000..a494608
--- /dev/null
+++ b/drivers/tpm/tis_i2c.c
@@ -0,0 +1,211 @@
+/*
+ * Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include <config.h>
+#include <common.h>
+#include <i2c.h>
+#include "tpm.h"
+
+DECLARE_GLOBAL_DATA_PTR;
+
+/* Define in board config */
+#ifndef CONFIG_TPM_I2C_BUS
+	#define CONFIG_TPM_I2C_BUS 0
+	#define CONFIG_TPM_I2C_ADDR 0
+#endif
+
+/* TPM configuration */
+struct tpm {
+	int i2c_bus;
+	int slave_addr;
+	char inited;
+	int old_bus;
+} tpm;
+
+
+static int tpm_select(void)
+{
+	int ret;
+
+	tpm.old_bus = i2c_get_bus_num();
+	if (tpm.old_bus != tpm.i2c_bus) {
+		ret = i2c_set_bus_num(tpm.i2c_bus);
+		if (ret) {
+			debug("%s: Fail to set i2c bus %d\n", __func__,
+			      tpm.i2c_bus);
+			return -1;
+		}
+	}
+	return 0;
+}
+
+static int tpm_deselect(void)
+{
+	int ret;
+
+	if (tpm.old_bus != i2c_get_bus_num()) {
+		ret = i2c_set_bus_num(tpm.old_bus);
+		if (ret) {
+			debug("%s: Fail to restore i2c bus %d\n",
+			      __func__, tpm.old_bus);
+			return -1;
+		}
+	}
+	tpm.old_bus = -1;
+	return 0;
+}
+
+/**
+ * Decode TPM configuration.
+ *
+ * @param dev	Returns a configuration of TPM device
+ * @return 0 if ok, -1 on error
+ */
+static int tpm_decode_config(struct tpm *dev)
+{
+#ifdef CONFIG_OF_CONTROL
+	const void *blob = gd->fdt_blob;
+	int node, parent;
+	int i2c_bus;
+
+	node = fdtdec_next_compatible(blob, 0, COMPAT_INFINEON_SLB9635_TPM);
+	if (node < 0) {
+		debug("%s: Node not found\n", __func__);
+		return -1;
+	}
+	parent = fdt_parent_offset(blob, node);
+	if (parent < 0) {
+		debug("%s: Cannot find node parent\n", __func__);
+		return -1;
+	}
+	i2c_bus = i2c_get_bus_num_fdt(blob, parent);
+	if (i2c_bus < 0)
+		return -1;
+	dev->i2c_bus = i2c_bus;
+	dev->slave_addr = fdtdec_get_addr(blob, node, "reg");
+#else
+	dev->i2c_bus = CONFIG_TPM_I2C_BUS;
+	dev->slave_addr = CONFIG_TPM_I2C_ADDR;
+#endif
+	return 0;
+}
+
+/* Methods used by cmd_tpm */
+int tis_init(void)
+{
+	if (tpm.inited)
+		return 0;
+	if (tpm_decode_config(&tpm))
+		return -1;
+	if (tpm_select())
+		return -1;
+	/*
+	 * Probe TPM twice; the first probing might fail because TPM is asleep,
+	 * and the probing can wake up TPM.
+	 */
+	if (i2c_probe(tpm.slave_addr) && i2c_probe(tpm.slave_addr)) {
+		debug("%s: fail to probe i2c addr 0x%x\n", __func__,
+				tpm.slave_addr);
+		return -1;
+	}
+	tpm_deselect();
+	tpm.inited = 1;
+	return 0;
+}
+
+int tis_open(void)
+{
+	int rc;
+	if (!tpm.inited)
+		return -1;
+
+	if (tpm_select())
+		return -1;
+
+	rc = tpm_open(tpm.slave_addr);
+	tpm_deselect();
+
+	return rc;
+}
+
+int tis_close(void)
+{
+	if (!tpm.inited)
+		return -1;
+
+	if (tpm_select())
+		return -1;
+
+	tpm_close();
+
+	tpm_deselect();
+
+	return 0;
+}
+
+int tis_sendrecv_hash(const uint8_t *sendbuf, size_t sbuf_size,
+		uint8_t *recvbuf, size_t *rbuf_len)
+{
+	int len;
+	uint8_t buf[TPM_BUFSIZE];
+
+	if (!tpm.inited)
+		return -1;
+
+	if (sizeof(buf) < sbuf_size)
+		return -1;
+
+	memcpy(buf, sendbuf, sbuf_size);
+
+	if (tpm_select())
+		return -1;
+
+	len = tpm_transmit_hash(buf, sbuf_size);
+
+	tpm_deselect();
+
+	if (len < 10) {
+		*rbuf_len = 0;
+		return -1;
+	}
+
+	memcpy(recvbuf, buf, len);
+	*rbuf_len = len;
+
+	return 0;
+}
+
+int tis_sendrecv(const uint8_t *sendbuf, size_t sbuf_size,
+		uint8_t *recvbuf, size_t *rbuf_len)
+{
+	int len;
+	uint8_t buf[TPM_BUFSIZE];
+
+	if (!tpm.inited)
+		return -1;
+
+	if (sizeof(buf) < sbuf_size)
+		return -1;
+
+	memcpy(buf, sendbuf, sbuf_size);
+
+	if (tpm_select())
+		return -1;
+
+	len = tpm_transmit(buf, sbuf_size);
+
+	tpm_deselect();
+
+	if (len < 10) {
+		*rbuf_len = 0;
+		return -1;
+	}
+
+	memcpy(recvbuf, buf, len);
+	*rbuf_len = len;
+
+	return 0;
+}
diff --git a/drivers/tpm/tpm.c b/drivers/tpm/tpm.c
new file mode 100644
index 0000000..3f4f66e
--- /dev/null
+++ b/drivers/tpm/tpm.c
@@ -0,0 +1,472 @@
+/*
+ * Copyright (C) 2011 Infineon Technologies
+ *
+ * Authors:
+ * Peter Huewe <huewe.external@infineon.com>
+ *
+ * Description:
+ * Device driver for TCG/TCPA TPM (trusted platform module).
+ * Specifications at www.trustedcomputinggroup.org
+ *
+ * It is based on the Linux kernel driver tpm.c from Leendert van
+ * Dorn, Dave Safford, Reiner Sailer, and Kyleen Hall.
+ *
+ * Version: 2.1.1
+ *
+ * See file CREDITS for list of people who contributed to this
+ * project.
+ *
+ * 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, version 2 of the
+ * License.
+ *
+ * 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.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
+ * MA 02111-1307 USA
+ */
+
+#include <malloc.h>
+#include "tpm.h"
+
+/* global structure for tpm chip data */
+struct tpm_chip g_chip;
+
+enum tpm_duration {
+	TPM_SHORT = 0,
+	TPM_MEDIUM = 1,
+	TPM_LONG = 2,
+	TPM_UNDEFINED,
+};
+
+#define TPM_MAX_ORDINAL 243
+#define TPM_MAX_PROTECTED_ORDINAL 12
+#define TPM_PROTECTED_ORDINAL_MASK 0xFF
+
+/*
+ * Array with one entry per ordinal defining the maximum amount
+ * of time the chip could take to return the result.  The ordinal
+ * designation of short, medium or long is defined in a table in
+ * TCG Specification TPM Main Part 2 TPM Structures Section 17. The
+ * values of the SHORT, MEDIUM, and LONG durations are retrieved
+ * from the chip during initialization with a call to tpm_get_timeouts.
+ */
+static const u8 tpm_protected_ordinal_duration[TPM_MAX_PROTECTED_ORDINAL] = {
+	TPM_UNDEFINED,		/* 0 */
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,		/* 5 */
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_SHORT,		/* 10 */
+	TPM_SHORT,
+};
+
+static const u8 tpm_ordinal_duration[TPM_MAX_ORDINAL] = {
+	TPM_UNDEFINED,		/* 0 */
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,		/* 5 */
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_SHORT,		/* 10 */
+	TPM_SHORT,
+	TPM_MEDIUM,
+	TPM_LONG,
+	TPM_LONG,
+	TPM_MEDIUM,		/* 15 */
+	TPM_SHORT,
+	TPM_SHORT,
+	TPM_MEDIUM,
+	TPM_LONG,
+	TPM_SHORT,		/* 20 */
+	TPM_SHORT,
+	TPM_MEDIUM,
+	TPM_MEDIUM,
+	TPM_MEDIUM,
+	TPM_SHORT,		/* 25 */
+	TPM_SHORT,
+	TPM_MEDIUM,
+	TPM_SHORT,
+	TPM_SHORT,
+	TPM_MEDIUM,		/* 30 */
+	TPM_LONG,
+	TPM_MEDIUM,
+	TPM_SHORT,
+	TPM_SHORT,
+	TPM_SHORT,		/* 35 */
+	TPM_MEDIUM,
+	TPM_MEDIUM,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_MEDIUM,		/* 40 */
+	TPM_LONG,
+	TPM_MEDIUM,
+	TPM_SHORT,
+	TPM_SHORT,
+	TPM_SHORT,		/* 45 */
+	TPM_SHORT,
+	TPM_SHORT,
+	TPM_SHORT,
+	TPM_LONG,
+	TPM_MEDIUM,		/* 50 */
+	TPM_MEDIUM,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,		/* 55 */
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_MEDIUM,		/* 60 */
+	TPM_MEDIUM,
+	TPM_MEDIUM,
+	TPM_SHORT,
+	TPM_SHORT,
+	TPM_MEDIUM,		/* 65 */
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_SHORT,		/* 70 */
+	TPM_SHORT,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,		/* 75 */
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_LONG,		/* 80 */
+	TPM_UNDEFINED,
+	TPM_MEDIUM,
+	TPM_LONG,
+	TPM_SHORT,
+	TPM_UNDEFINED,		/* 85 */
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_SHORT,		/* 90 */
+	TPM_SHORT,
+	TPM_SHORT,
+	TPM_SHORT,
+	TPM_SHORT,
+	TPM_UNDEFINED,		/* 95 */
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_MEDIUM,		/* 100 */
+	TPM_SHORT,
+	TPM_SHORT,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,		/* 105 */
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_SHORT,		/* 110 */
+	TPM_SHORT,
+	TPM_SHORT,
+	TPM_SHORT,
+	TPM_SHORT,
+	TPM_SHORT,		/* 115 */
+	TPM_SHORT,
+	TPM_SHORT,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_LONG,		/* 120 */
+	TPM_LONG,
+	TPM_MEDIUM,
+	TPM_UNDEFINED,
+	TPM_SHORT,
+	TPM_SHORT,		/* 125 */
+	TPM_SHORT,
+	TPM_LONG,
+	TPM_SHORT,
+	TPM_SHORT,
+	TPM_SHORT,		/* 130 */
+	TPM_MEDIUM,
+	TPM_UNDEFINED,
+	TPM_SHORT,
+	TPM_MEDIUM,
+	TPM_UNDEFINED,		/* 135 */
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_SHORT,		/* 140 */
+	TPM_SHORT,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,		/* 145 */
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_SHORT,		/* 150 */
+	TPM_MEDIUM,
+	TPM_MEDIUM,
+	TPM_SHORT,
+	TPM_SHORT,
+	TPM_UNDEFINED,		/* 155 */
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_SHORT,		/* 160 */
+	TPM_SHORT,
+	TPM_SHORT,
+	TPM_SHORT,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,		/* 165 */
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_LONG,		/* 170 */
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,		/* 175 */
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_MEDIUM,		/* 180 */
+	TPM_SHORT,
+	TPM_MEDIUM,
+	TPM_MEDIUM,
+	TPM_MEDIUM,
+	TPM_MEDIUM,		/* 185 */
+	TPM_SHORT,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,		/* 190 */
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,		/* 195 */
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_SHORT,		/* 200 */
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_SHORT,
+	TPM_SHORT,		/* 205 */
+	TPM_SHORT,
+	TPM_SHORT,
+	TPM_SHORT,
+	TPM_SHORT,
+	TPM_MEDIUM,		/* 210 */
+	TPM_UNDEFINED,
+	TPM_MEDIUM,
+	TPM_MEDIUM,
+	TPM_MEDIUM,
+	TPM_UNDEFINED,		/* 215 */
+	TPM_MEDIUM,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_SHORT,
+	TPM_SHORT,		/* 220 */
+	TPM_SHORT,
+	TPM_SHORT,
+	TPM_SHORT,
+	TPM_SHORT,
+	TPM_UNDEFINED,		/* 225 */
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_SHORT,		/* 230 */
+	TPM_LONG,
+	TPM_MEDIUM,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,		/* 235 */
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_SHORT,		/* 240 */
+	TPM_UNDEFINED,
+	TPM_MEDIUM,
+};
+
+/*
+ * Returns max number of milliseconds to wait
+ */
+unsigned long tpm_calc_ordinal_duration(struct tpm_chip *chip, u32 ordinal)
+{
+	int duration_idx = TPM_UNDEFINED;
+	int duration = 0;
+
+	if (ordinal < TPM_MAX_ORDINAL)
+		duration_idx = tpm_ordinal_duration[ordinal];
+	else if ((ordinal & TPM_PROTECTED_ORDINAL_MASK) <
+		 TPM_MAX_PROTECTED_ORDINAL)
+		duration_idx =
+		    tpm_protected_ordinal_duration[ordinal &
+						   TPM_PROTECTED_ORDINAL_MASK];
+
+	if (duration_idx != TPM_UNDEFINED)
+		duration = chip->vendor.duration[duration_idx];
+	if (duration <= 0)
+		return 2 * 60 * HZ; /*two minutes timeout*/
+	else
+		return duration;
+}
+
+ssize_t tpm_transmit_hash(const unsigned char *buf, size_t bufsiz)
+{
+	ssize_t rc;
+	u32 count, ordinal;
+	unsigned long start, stop;
+
+	struct tpm_chip *chip = &g_chip;
+
+	rc = chip->vendor.send_hash(chip, (u8 *) buf, bufsiz);
+	if (rc < 0) {
+		dev_err(chip->dev, "tpm_transmit: tpm_send: error %zd\n", rc);
+		goto out;
+	}
+
+out_recv:
+
+	dbg_printf("out_recv: reading response...\n");
+	rc = chip->vendor.recv(chip, (u8 *) buf, TPM_BUFSIZE);
+	if (rc < 0)
+		dev_err(chip->dev, "tpm_transmit: tpm_recv: error %zd\n", rc);
+out:
+	return rc;
+}
+
+ssize_t tpm_transmit(const unsigned char *buf, size_t bufsiz)
+{
+	ssize_t rc;
+	u32 count, ordinal;
+	unsigned long start, stop;
+
+	struct tpm_chip *chip = &g_chip;
+
+	/* switch endianess: big->little */
+	count = get_unaligned_be32(buf + TPM_CMD_SIZE_BYTE);
+	ordinal = get_unaligned_be32(buf + TPM_CMD_ORDINAL_BYTE);
+
+	if (count == 0) {
+		dev_err(chip->dev, "no data\n");
+		return -ENODATA;
+	}
+	if (count > bufsiz) {
+		dev_err(chip->dev,
+			"invalid count value %x %zx\n", count, bufsiz);
+		return -E2BIG;
+	}
+
+	rc = chip->vendor.send(chip, (u8 *) buf, count);
+	if (rc < 0) {
+		dev_err(chip->dev, "tpm_transmit: tpm_send: error %zd\n", rc);
+		goto out;
+	}
+
+	if (chip->vendor.irq)
+		goto out_recv;
+
+	start = get_timer(0);
+	stop = tpm_calc_ordinal_duration(chip, ordinal);
+	do {
+		dbg_printf("waiting for status...\n");
+		u8 status = chip->vendor.status(chip);
+		if ((status & chip->vendor.req_complete_mask) ==
+		    chip->vendor.req_complete_val) {
+			dbg_printf("...got it;\n");
+			goto out_recv;
+		}
+
+		if ((status == chip->vendor.req_canceled)) {
+			dev_err(chip->dev, "Operation Canceled\n");
+			rc = -ECANCELED;
+			goto out;
+		}
+		msleep(TPM_TIMEOUT);
+	} while (get_timer(start) < stop);
+
+	chip->vendor.cancel(chip);
+	dev_err(chip->dev, "Operation Timed out\n");
+	rc = -ETIME;
+	goto out;
+
+out_recv:
+
+	dbg_printf("out_recv: reading response...\n");
+	rc = chip->vendor.recv(chip, (u8 *) buf, TPM_BUFSIZE);
+	if (rc < 0)
+		dev_err(chip->dev, "tpm_transmit: tpm_recv: error %zd\n", rc);
+out:
+	return rc;
+}
+
+enum tpm_capabilities {
+	TPM_CAP_PROP = cpu_to_be32(5),
+};
+
+enum tpm_sub_capabilities {
+	TPM_CAP_PROP_TIS_TIMEOUT = cpu_to_be32(0x115),
+	TPM_CAP_PROP_TIS_DURATION = cpu_to_be32(0x120),
+};
+
+struct tpm_chip *tpm_register_hardware(const struct tpm_vendor_specific *entry)
+{
+	struct tpm_chip *chip;
+
+	/* Driver specific per-device data */
+	chip = &g_chip;
+	memcpy(&chip->vendor, entry, sizeof(struct tpm_vendor_specific));
+	chip->is_open = 1;
+
+	return chip;
+}
+
+int tpm_open(uint32_t dev_addr)
+{
+	int rc;
+	if (g_chip.is_open)
+		return -EBUSY;
+	rc = tpm_vendor_init(dev_addr);
+	if (rc < 0)
+		g_chip.is_open = 0;
+	return rc;
+}
+
+void tpm_close(void)
+{
+	if (g_chip.is_open) {
+		tpm_vendor_cleanup(&g_chip);
+		g_chip.is_open = 0;
+	}
+}
diff --git a/drivers/tpm/tpm.h b/drivers/tpm/tpm.h
new file mode 100644
index 0000000..151c584
--- /dev/null
+++ b/drivers/tpm/tpm.h
@@ -0,0 +1,164 @@
+/*
+ * Copyright (C) 2011 Infineon Technologies
+ *
+ * Authors:
+ * Peter Huewe <huewe.external@infineon.com>
+ *
+ * Version: 2.1.1
+ *
+ * Description:
+ * Device driver for TCG/TCPA TPM (trusted platform module).
+ * Specifications at www.trustedcomputinggroup.org
+ *
+ * It is based on the Linux kernel driver tpm.c from Leendert van
+ * Dorn, Dave Safford, Reiner Sailer, and Kyleen Hall.
+ *
+ *
+ * See file CREDITS for list of people who contributed to this
+ * project.
+ *
+ * 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, version 2 of the
+ * License.
+ *
+ * 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.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
+ * MA 02111-1307 USA
+ */
+
+#ifndef _TPM_H_
+#define _TPM_H_
+
+#include "compatibility.h"
+
+enum tpm_timeout {
+	TPM_TIMEOUT = 5,	/* msecs */
+};
+
+/* Size of external transmit buffer (used in tpm_transmit)*/
+#define TPM_BUFSIZE 4096
+#define TPM_ERROR_SIZE 10
+#define TPM_HEADER_SIZE 10
+
+/* Index of fields in TPM command buffer */
+#define TPM_CMD_SIZE_BYTE 2
+#define TPM_CMD_ORDINAL_BYTE 6
+
+/* Index of Count field in TPM response buffer */
+#define TPM_RSP_SIZE_BYTE 2
+#define TPM_RSP_RC_BYTE 6
+
+struct tpm_chip;
+
+struct tpm_vendor_specific {
+	const u8 req_complete_mask;
+	const u8 req_complete_val;
+	const u8 req_canceled;
+	int irq;
+	int (*recv) (struct tpm_chip *, u8 *, size_t);
+	int (*send) (struct tpm_chip *, u8 *, size_t);
+	int (*send_hash) (struct tpm_chip *, u8 *, size_t);
+	void (*cancel) (struct tpm_chip *);
+	 u8 (*status) (struct tpm_chip *);
+	int locality;
+	unsigned long timeout_a, timeout_b, timeout_c, timeout_d; /* msec */
+	unsigned long duration[3];	/* msec */
+};
+
+struct tpm_chip {
+	int is_open;
+	struct tpm_vendor_specific vendor;
+	/* testing */
+	u8 dev;
+};
+
+struct tpm_input_header {
+	__be16 tag;
+	__be32 length;
+	__be32 ordinal;
+} __attribute__ ((packed));
+
+struct tpm_output_header {
+	__be16 tag;
+	__be32 length;
+	__be32 return_code;
+} __attribute__ ((packed));
+
+struct timeout_t {
+	__be32 a;
+	__be32 b;
+	__be32 c;
+	__be32 d;
+} __attribute__ ((packed));
+
+struct duration_t {
+	__be32 tpm_short;
+	__be32 tpm_medium;
+	__be32 tpm_long;
+} __attribute__ ((packed));
+
+typedef union {
+	struct timeout_t timeout;
+	struct duration_t duration;
+} cap_t;
+
+struct tpm_getcap_params_in {
+	__be32 cap;
+	__be32 subcap_size;
+	__be32 subcap;
+} __attribute__ ((packed));
+
+struct tpm_getcap_params_out {
+	__be32 cap_size;
+	cap_t cap;
+} __attribute__ ((packed));
+
+typedef union {
+	struct tpm_input_header in;
+	struct tpm_output_header out;
+} tpm_cmd_header;
+
+typedef union {
+	struct tpm_getcap_params_out getcap_out;
+	struct tpm_getcap_params_in getcap_in;
+} tpm_cmd_params;
+
+struct tpm_cmd_t {
+	tpm_cmd_header header;
+	tpm_cmd_params params;
+} __attribute__ ((packed));
+
+
+/* ---------- Interface for TPM vendor ------------ */
+
+extern struct tpm_chip *tpm_register_hardware(
+	const struct tpm_vendor_specific *);
+
+extern int tpm_vendor_init(uint32_t dev_addr);
+
+extern void tpm_vendor_cleanup(struct tpm_chip *chip);
+
+/* ---------- Interface for TDDL ------------------- */
+
+/*
+ * if dev_addr != 0 - redefines TPM device address
+ * Returns < 0 on error, 0 on success.
+ */
+extern int tpm_open(uint32_t dev_addr);
+
+extern void tpm_close(void);
+
+/*
+ * Transmit bufsiz bytes out of buf to TPM and get results back in buf, too.
+ * Returns < 0 on error, 0 on success.
+ */
+extern ssize_t tpm_transmit(const unsigned char *buf, size_t bufsiz);
+
+#endif
diff --git a/drivers/tpm/tpm_i2c_st.c b/drivers/tpm/tpm_i2c_st.c
new file mode 100644
index 0000000..cb716c5
--- /dev/null
+++ b/drivers/tpm/tpm_i2c_st.c
@@ -0,0 +1,601 @@
+/*
+ * STMicroelectronics TPM I2C UBOOT Linux driver for TPM ST33ZP24
+ * Copyright (C) 2013  STMicroelectronics
+ *
+ * Description:
+ * Device driver for TCG/TCPA TPM (trusted platform module).
+ * Specifications at www.trustedcomputinggroup.org
+ *
+ * This device driver implements the TPM interface as defined in
+ * the TCG TPM Interface Spec version 1.2, revision 1.0 and the
+ * STMicroelectronics I2C Protocol Stack Specification version 1.2.0.
+ *
+ * It is based on the Linux kernel driver tpm.c from Leendert van
+ * Dorn, Dave Safford, Reiner Sailer, and Kyleen Hall.
+ *
+ * See file CREDITS for list of people who contributed to this
+ * project.
+ *
+ * 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, version 2 of the
+ * License.
+ *
+ * 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.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
+ * MA 02111-1307 USA
+ *
+ * STMicroelectronics version 1.2.0, Copyright (C) 2013
+ * STMicroelectronics comes with ABSOLUTELY NO WARRANTY.
+ * This is free software, and you are welcome to redistribute it
+ * under certain conditions.
+ *
+ * @Author: Mathias Leblanc tpmsupport@st.com
+ *
+ * @File: tpm_i2c_st.c
+ *
+ */
+
+#include <common.h>
+#include <i2c.h>
+#include <linux/types.h>
+
+#include "compatibility.h"
+#include "tpm.h"
+
+/* max. buffer size supported by our tpm */
+#ifdef TPM_BUFSIZE
+#undef TPM_BUFSIZE
+#endif
+
+#define MINOR_NUM_I2C		224
+
+#define TPM_ACCESS			(0x0)
+#define TPM_STS				(0x18)
+#define TPM_HASH_END			(0x20)
+#define TPM_DATA_FIFO			(0x24)
+#define TPM_HASH_DATA			(0x24)
+#define TPM_HASH_START			(0x28)
+#define TPM_INTF_CAPABILITY		(0x14)
+#define TPM_INT_STATUS			(0x10)
+#define TPM_INT_ENABLE			(0x08)
+
+#define TPM_DUMMY_BYTE			0xAA
+#define TPM_WRITE_DIRECTION		0x80
+#define TPM_HEADER_SIZE			10
+#define TPM_BUFSIZE			2048
+
+#define LOCALITY0		0
+#define LOCALITY4		4
+
+struct st_tpm_hash {
+	int size;
+	u8 *data;
+};
+
+enum stm33zp24_access {
+	TPM_ACCESS_VALID = 0x80,
+	TPM_ACCESS_ACTIVE_LOCALITY = 0x20,
+	TPM_ACCESS_REQUEST_PENDING = 0x04,
+	TPM_ACCESS_REQUEST_USE = 0x02,
+};
+
+enum stm33zp24_status {
+	TPM_STS_VALID = 0x80,
+	TPM_STS_COMMAND_READY = 0x40,
+	TPM_STS_GO = 0x20,
+	TPM_STS_DATA_AVAIL = 0x10,
+	TPM_STS_DATA_EXPECT = 0x08,
+};
+
+enum stm33zp24_int_flags {
+	TPM_GLOBAL_INT_ENABLE = 0x80,
+	TPM_INTF_CMD_READY_INT = 0x080,
+	TPM_INTF_FIFO_AVALAIBLE_INT = 0x040,
+	TPM_INTF_WAKE_UP_READY_INT = 0x020,
+	TPM_INTF_LOC4SOFTRELEASE_INT = 0x008,
+	TPM_INTF_LOCALITY_CHANGE_INT = 0x004,
+	TPM_INTF_STS_VALID_INT = 0x002,
+	TPM_INTF_DATA_AVAIL_INT = 0x001,
+};
+
+enum tis_defaults {
+	TIS_SHORT_TIMEOUT = 750,	/* ms */
+	TIS_LONG_TIMEOUT = 2000,	/* 2 sec */
+};
+
+struct tpm_i2c_ST_dev {
+	uint addr;
+	u8 buf[TPM_BUFSIZE];
+};
+
+static struct tpm_i2c_ST_dev tpm_dev = {
+		/* Note: replace with defined addr from board configuration */
+		.addr = CONFIG_TPM_I2C_ADDR
+};
+
+/*
+ * write8_reg
+ * Send byte to the TIS register according to the ST33ZP24 I2C protocol.
+ * @param: tpm_register, the tpm tis register where the data should be written
+ * @param: tpm_data, the tpm_data to write inside the tpm_register
+ * @param: tpm_size, The length of the data
+ * @return: Returns negative errno, or else the number of bytes written.
+ */
+static int write8_reg(u8 addr, u8 tpm_register,
+		      u8 *tpm_data, u16 tpm_size)
+{
+	u8 data;
+	data = tpm_register;
+	memcpy(&(tpm_dev.buf[0]), &data, sizeof(data));
+	memcpy(&(tpm_dev.buf[0])+1, tpm_data, tpm_size);
+
+	return i2c_write(addr, 0, 0, &tpm_dev.buf[0],
+				tpm_size + 1);
+
+} /* write8_reg() */
+
+/*
+* read8_reg
+* Recv byte from the TIS register according to the ST33ZP24 I2C protocol.
+* @param: tpm_register, the tpm tis register where the data should be read
+* @param: tpm_data, the TPM response
+* @param: tpm_size, tpm TPM response size to read.
+* @return: number of byte read successfully: should be one if success.
+*/
+static int read8_reg(u8 addr, u8 tpm_register,
+u8 *tpm_data, int tpm_size)
+{
+	u8 status = 0;
+	u8 data;
+	data = TPM_DUMMY_BYTE;
+	status = write8_reg(addr, tpm_register, &data, 1);
+	if (status == 0)
+		status = i2c_read(addr, 0, 0, tpm_data, tpm_size);
+return status;
+} /* read8_reg() */
+
+/*
+ * I2C_WRITE_DATA
+ * Send byte to the TIS register according to the ST33ZP24 I2C protocol.
+ * @param: client, the chip description
+ * @param: tpm_register, the tpm tis register where the data should be written
+ * @param: tpm_data, the tpm_data to write inside the tpm_register
+ * @param: tpm_size, The length of the data
+ * @return: number of byte written successfully: should be one if success.
+ */
+#define I2C_WRITE_DATA(client, tpm_register, tpm_data, tpm_size)\
+	 (write8_reg(client, tpm_register | \
+	TPM_WRITE_DIRECTION, tpm_data, tpm_size))
+
+/*
+ * I2C_READ_DATA
+ * Recv byte from the TIS register according to the ST33ZP24 I2C protocol.
+ * @param: tpm, the chip description
+ * @param: tpm_register, the tpm tis register where the data should be read
+ * @param: tpm_data, the TPM response
+ * @param: tpm_size, tpm TPM response size to read.
+ * @return: number of byte read successfully: should be one if success.
+ */
+#define I2C_READ_DATA(client, tpm_register, tpm_data, tpm_size)\
+	 (read8_reg(client, tpm_register, tpm_data, tpm_size))
+
+/*
+ * release_locality release the active locality
+ * @param: chip, the tpm chip description.
+ */
+static void release_locality(struct tpm_chip *chip)
+{
+	u8 data = TPM_ACCESS_ACTIVE_LOCALITY;
+
+	I2C_WRITE_DATA(tpm_dev.addr, TPM_ACCESS, &data, 1);
+}
+
+/*
+ * clear_interruption
+ * clear the TPM interrupt register.
+ * @param: tpm, the chip description
+ */
+static void clear_interruption(u8 addr)
+{
+	u8 interrupt;
+	I2C_READ_DATA(tpm_dev.addr, TPM_INT_STATUS, &interrupt, 1);
+	I2C_WRITE_DATA(tpm_dev.addr, TPM_INT_STATUS, &interrupt, 1);
+	I2C_READ_DATA(tpm_dev.addr, TPM_INT_STATUS, &interrupt, 1);
+} /* clear_interruption() */
+
+int wait_for_serirq_timeout(struct tpm_chip *chip, int condition,
+	 unsigned long timeout)
+{
+	int status = 2;
+
+	clear_interruption(tpm_dev.addr);
+	if (condition)
+		status = 1;
+
+	return status;
+}
+
+/*
+ * check_locality if the locality is active
+ * @param: chip, the tpm chip description
+ * @return: the active locality or -EACCESS.
+ */
+static int check_locality(struct tpm_chip *chip)
+{
+	u8 data;
+	u8 status;
+	status = I2C_READ_DATA(tpm_dev.addr, TPM_ACCESS, &data, 1);
+
+	if ((status == 0) && (data &
+		(TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID)) ==
+		(TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID))
+		return chip->vendor.locality;
+
+	return -EACCES;
+
+} /* check_locality() */
+
+/*
+ * request_locality request the TPM locality
+ * @param: chip, the chip description
+ * @return: the active locality or EACCESS.
+ */
+static int request_locality(struct tpm_chip *chip)
+{
+	unsigned long start, stop;
+	long rc;
+	u8 data;
+	if (check_locality(chip) == chip->vendor.locality)
+		return chip->vendor.locality;
+
+	data = TPM_ACCESS_REQUEST_USE;
+	rc = I2C_WRITE_DATA(tpm_dev.addr, TPM_ACCESS, &data, 1);
+	if (rc < 0)
+		goto end;
+
+	if (chip->vendor.irq) {
+		rc = wait_for_serirq_timeout(chip, (check_locality
+						       (chip) >= 0),
+						      chip->vendor.timeout_a);
+		if (rc > 0)
+			return chip->vendor.locality;
+	} else{
+	/* wait for locality activated */
+	start = get_timer(0);
+	stop = chip->vendor.timeout_a;
+		do {
+			if (check_locality(chip) >= 0)
+				return chip->vendor.locality;
+
+			msleep(TPM_TIMEOUT);
+		} while	 (get_timer(start) < stop);
+	}
+	rc = -EACCES;
+end:
+	return rc;
+} /* request_locality() */
+
+/*
+ * tpm_stm_i2c_cancel, cancel is not implemented.
+ * @param: chip, tpm_chip description.
+ */
+static void tpm_stm_i2c_cancel(struct tpm_chip *chip)
+{
+	u8 data;
+
+	data = TPM_STS_COMMAND_READY;
+	I2C_WRITE_DATA(tpm_dev.addr, TPM_STS, &data, 1);
+	if (chip->vendor.irq)
+		wait_for_serirq_timeout(chip, 1, chip->vendor.timeout_a);
+}	/* tpm_stm_i2c_cancel() */
+
+/*
+ * tpm_stm_spi_status return the TPM_STS register
+ * @param: chip, the tpm chip description
+ * @return: the TPM_STS register value.
+ */
+static u8 tpm_stm_i2c_status(struct tpm_chip *chip)
+{
+	u8 data;
+	I2C_READ_DATA(tpm_dev.addr, TPM_STS, &data, 1);
+	return data;
+}				/* tpm_stm_i2c_status() */
+
+/*
+ * get_burstcount return the burstcount address 0x19 0x1A
+ * @param: chip, the chip description
+ * return: the burstcount.
+ */
+static int get_burstcount(struct tpm_chip *chip)
+{
+	unsigned long start, stop;
+	int burstcnt, status;
+	u8 tpm_reg, temp;
+
+	/* wait for burstcount */
+	/* which timeout value, spec has 2 answers (c & d) */
+	start = get_timer(0);
+	stop = chip->vendor.timeout_d;
+	do {
+		tpm_reg = TPM_STS + 1;
+		status = I2C_READ_DATA(tpm_dev.addr, tpm_reg, &temp, 1);
+		if (status < 0)
+			goto end;
+
+		tpm_reg = tpm_reg + 1;
+		burstcnt = temp;
+		status = I2C_READ_DATA(tpm_dev.addr, tpm_reg, &temp, 1);
+		if (status < 0)
+			goto end;
+
+		burstcnt |= temp << 8;
+		if (burstcnt)
+			return burstcnt;
+
+		msleep(TPM_TIMEOUT);
+	} while (get_timer(start) < stop);
+
+end:
+	return -EBUSY;
+} /* get_burstcount() */
+
+
+/*
+ * recv_data receive data
+ * @param: chip, the tpm chip description
+ * @param: buf, the buffer where the data are received
+ * @param: count, the number of data to receive
+ * @return: the number of bytes read from TPM FIFO.
+ */
+static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count)
+{
+	int size = 0, burstcnt, len;
+
+	while (size < count) {
+		burstcnt = get_burstcount(chip);
+		len = count - size;
+		if ((len) > burstcnt)
+			len = burstcnt;
+		if (I2C_READ_DATA(tpm_dev.addr, TPM_DATA_FIFO,
+		 buf + size, len) == 0)
+			size += len;
+		else
+			break;
+	}
+	return size;
+} /* recv_data() */
+
+/*
+ * tpm_stm_i2c_recv received TPM response through the I2C bus.
+ * @param: chip, tpm_chip description.
+ * @param: buf,	the buffer to store datas.
+ * @param: count, the number of bytes to send.
+ * @return: In case of success the number of bytes received.
+ *		In other case, a < 0 value describing the issue.
+ */
+static int tpm_stm_i2c_recv(struct tpm_chip *chip, unsigned char *buf,
+			    size_t count)
+{
+	int size = 0;
+	int expected;
+
+	if (chip == NULL)
+		return -EBUSY;
+
+	if (count < TPM_HEADER_SIZE) {
+		size = -EIO;
+		goto out;
+	}
+
+	size = recv_data(chip, buf, TPM_HEADER_SIZE);
+	if (size < TPM_HEADER_SIZE) {
+		dev_err(chip->dev, "Unable to read header\n");
+		goto out;
+	}
+
+
+	expected = get_unaligned_be32(buf + TPM_RSP_SIZE_BYTE);
+	if (expected > count) {
+		size = -EIO;
+		goto out;
+	}
+
+	size += recv_data(chip, &buf[TPM_HEADER_SIZE],
+					expected - TPM_HEADER_SIZE);
+	if (size < expected) {
+		dev_err(chip->dev, "Unable to read remainder of result\n");
+		size = -ETIME;
+		goto out;
+	}
+
+out:
+	chip->vendor.cancel(chip);
+	release_locality(chip);
+	return size;
+} /* tpm_stm_i2c_recv() */
+
+/*
+ * tpm_stm_i2c_send send TPM commands through the I2C bus.
+ *
+ * @param: chip, tpm_chip description.
+ * @param: buf,	the buffer to send.
+ * @param: len, the number of bytes to send.
+ * @return: In case of success the number of bytes sent.
+ *			In other case, a < 0 value describing the issue.
+ */
+static int tpm_stm_i2c_send(struct tpm_chip *chip, u8 *buf,
+			    size_t len)
+{
+	u32 ret = 0,
+	    status,
+	    burstcnt = 0, i, size;
+	u8 data;
+
+	if (chip == NULL)
+		return -EBUSY;
+	if (len < TPM_HEADER_SIZE)
+		return -EBUSY;
+
+	ret = request_locality(chip);
+	if (ret < 0)
+		return ret;
+
+	status = tpm_stm_i2c_status(chip);
+	if ((status & TPM_STS_COMMAND_READY) == 0)
+		tpm_stm_i2c_cancel(chip);
+
+	for (i = 0 ; i < len - 1 ;) {
+		burstcnt = get_burstcount(chip);
+		size = len - i - 1;
+		if ((size) > burstcnt)
+			size = burstcnt;
+		ret = I2C_WRITE_DATA(tpm_dev.addr, TPM_DATA_FIFO, buf, size);
+		if (ret < 0)
+			goto out_err;
+
+		i += size;
+	}
+
+	status = tpm_stm_i2c_status(chip);
+	if ((status & TPM_STS_DATA_EXPECT) == 0) {
+		ret = -EIO;
+		goto out_err;
+	}
+
+	ret = I2C_WRITE_DATA(tpm_dev.addr, TPM_DATA_FIFO, buf + len - 1, 1);
+	if (ret < 0)
+		goto out_err;
+
+	status = tpm_stm_i2c_status(chip);
+	if ((status & TPM_STS_DATA_EXPECT) != 0) {
+		ret = -EIO;
+		goto out_err;
+	}
+
+	data = TPM_STS_GO;
+	I2C_WRITE_DATA(tpm_dev.addr, TPM_STS, &data, 1);
+
+	return len;
+out_err:
+	tpm_stm_i2c_cancel(chip);
+	release_locality(chip);
+	return ret;
+} /* tpm_stm_i2c_send() */
+
+/*
+ * tpm_stm_i2c_send_hash send TPM locality 4 hash datas through the I2C bus
+ * to update the PCR[17].
+ * @param: chip, the tpm_chip description.
+ * @param: buf,	the data buffer to send.
+ * @param: len, the number of bytes to send.
+ * @return: In case of success the number of bytes sent.
+ *			In other case, a < 0 value describing the issue.
+ */
+static int tpm_stm_i2c_send_hash(struct tpm_chip *chip, unsigned char *buf,
+			    size_t len)
+{
+	u32 ret = 0;
+	u8 data;
+
+	if (chip == NULL)
+		return -EBUSY;
+
+	release_locality(chip);
+
+	tpm_dev.addr = 0x1B;
+	chip->vendor.locality = LOCALITY4;
+
+	data = TPM_DUMMY_BYTE;
+	ret = I2C_WRITE_DATA(tpm_dev.addr, TPM_HASH_START, &data, 1);
+	if (ret < 0)
+		goto end;
+	ret = I2C_WRITE_DATA(tpm_dev.addr, TPM_DATA_FIFO, buf, len);
+	if (ret < 0)
+		goto end;
+
+end:
+	I2C_WRITE_DATA(tpm_dev.addr, TPM_HASH_END, &data, 1);
+	release_locality(chip);
+	chip->vendor.locality = LOCALITY0;
+	tpm_dev.addr = 0x13;
+	ret = request_locality(chip);
+	return ret;
+} /* tpm_stm_i2c_send_hash */
+
+static struct tpm_vendor_specific st_i2c_tpm = {
+	.send = tpm_stm_i2c_send,
+	.send_hash = tpm_stm_i2c_send_hash,
+	.recv = tpm_stm_i2c_recv,
+	.cancel = tpm_stm_i2c_cancel,
+	.status = tpm_stm_i2c_status,
+	.req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
+	.req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
+	.req_canceled = TPM_STS_COMMAND_READY,
+};
+
+/*
+ * tpm_vendor_init initialize the TPM device
+ * @param: dev_addr, the i2c address of the tpm.
+ * @return: 0 in case of success.
+ *	 -1 in other case.
+ */
+int tpm_vendor_init(uint32_t dev_addr)
+{
+	u32 vendor;
+	uint old_addr;
+	int rc = 0;
+	struct tpm_chip *chip;
+
+	old_addr = tpm_dev.addr;
+	if (dev_addr != 0)
+		tpm_dev.addr = dev_addr;
+
+	chip = tpm_register_hardware(&st_i2c_tpm);
+
+	if (chip < 0) {
+		rc = -ENODEV;
+		goto out_err;
+	}
+
+	/* Default timeouts */
+	chip->vendor.timeout_a = TIS_SHORT_TIMEOUT;
+	chip->vendor.timeout_b = TIS_LONG_TIMEOUT;
+	chip->vendor.timeout_c = TIS_SHORT_TIMEOUT;
+	chip->vendor.timeout_d = TIS_SHORT_TIMEOUT;
+
+	chip->vendor.locality = LOCALITY0;
+
+	if (request_locality(chip) != 0) {
+		rc = -ENODEV;
+		goto out_err;
+	}
+
+	vendor = be32_to_cpu(vendor);
+
+
+	dev_info(dev, "1.2 TPM STMicroelectronics");
+	/*
+	 * A timeout query to TPM can be placed here.
+	 * Standard timeout values are used so far
+	 */
+
+	return 0;
+
+out_err:
+	tpm_dev.addr = old_addr;
+	return rc;
+} /* tpm_vendor_init() */
+
+
+
+void tpm_vendor_cleanup(struct tpm_chip *chip)
+{
+	release_locality(chip);
+} /* tpm_vendor_cleanup() */
diff --git a/include/configs/omap3_beagle.h b/include/configs/omap3_beagle.h
index 48ce4c0..ef381f8 100644
--- a/include/configs/omap3_beagle.h
+++ b/include/configs/omap3_beagle.h
@@ -111,6 +111,14 @@
 #define STATUS_LED_BOOT			STATUS_LED_BIT
 #define STATUS_LED_GREEN		STATUS_LED_BIT1
 
+/* TPM */
+#define CONFIG_CMD_TPM
+#define CONFIG_TPM
+#define CONFIG_ST_TPM_DEBUG
+#define CONFIG_ST_TPM_I2C
+#define CONFIG_TPM_I2C_BUS	1
+#define CONFIG_TPM_I2C_ADDR	0x13
+
 /* Enable Multi Bus support for I2C */
 #define CONFIG_I2C_MULTI_BUS		1
 
diff --git a/include/tpm.h b/include/tpm.h
index 6b21e9c..9074885 100644
--- a/include/tpm.h
+++ b/include/tpm.h
@@ -68,4 +68,20 @@ int tis_close(void);
 int tis_sendrecv(const uint8_t *sendbuf, size_t send_size, uint8_t *recvbuf,
 			size_t *recv_len);
 
+/*
+ * tis_sendrecv_hash()
+ *
+ * Send the requested data to the TPM for hash in LOC 4
+ * and then try to get its response
+ *
+ * @sendbuf - buffer of the data to hash
+ * @send_size size of the data to send
+ * @recvbuf - memory to save the response to
+ * @recv_len - pointer to the size of the response buffer
+ *
+ * Returns 0 on success (and places the number of response bytes at recv_len)
+ * or -1 on failure.
+ */
+int tis_sendrecv_hash(const uint8_t *sendbuf, size_t send_size, uint8_t *recvbuf,
+			size_t *recv_len);
 #endif /* _INCLUDE_TPM_H_ */
-- 
1.7.1

^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [U-Boot] [PATCH 1/1] TPM: STMicroelectronics u-boot driver I2C
@ 2013-03-22 16:28 Mathias leblanc
  2013-05-11 21:04 ` Simon Glass
  0 siblings, 1 reply; 13+ messages in thread
From: Mathias leblanc @ 2013-03-22 16:28 UTC (permalink / raw)
  To: u-boot

From: admin01 <admin01@admin01-desktop.(none)>

 * STMicroelectronics version 1.2.0, Copyright (C) 2013
 * STMicroelectronics comes with ABSOLUTELY NO WARRANTY.
 * This is free software, and you are welcome to redistribute it
 * under certain conditions.

This is the driver for TPM chip from ST Microelectronics.

If you have a TPM security chip from STMicroelectronics working with
an I2C, read the README file and add the correct defines regarding
the tpm in the configuration file of your board.
This file is located in include/configs/your_board.h

The driver will be accessible from within uboot terminal.

Signed-off-by: Mathias leblanc <admin01@admin01-desktop.(none)>
---
 Makefile                       |    2 +-
 README                         |   12 +
 common/cmd_tpm.c               |  176 ++++++++++++
 drivers/tpm/Makefile           |    1 +
 drivers/tpm/compatibility.h    |   52 ++++
 drivers/tpm/tis_i2c.c          |  211 ++++++++++++++
 drivers/tpm/tpm.c              |  472 +++++++++++++++++++++++++++++++
 drivers/tpm/tpm.h              |  164 +++++++++++
 drivers/tpm/tpm_i2c_st.c       |  601 ++++++++++++++++++++++++++++++++++++++++
 include/configs/omap3_beagle.h |    8 +
 include/tpm.h                  |   16 +
 11 files changed, 1714 insertions(+), 1 deletions(-)
 create mode 100644 drivers/tpm/compatibility.h
 create mode 100644 drivers/tpm/tis_i2c.c
 create mode 100644 drivers/tpm/tpm.c
 create mode 100644 drivers/tpm/tpm.h
 create mode 100644 drivers/tpm/tpm_i2c_st.c

diff --git a/Makefile b/Makefile
index 12763ce..ef954a4 100644
--- a/Makefile
+++ b/Makefile
@@ -314,7 +314,7 @@ endif
 LIBS-y += drivers/rtc/librtc.o
 LIBS-y += drivers/serial/libserial.o
 LIBS-y += drivers/sound/libsound.o
-LIBS-$(CONFIG_GENERIC_LPC_TPM) += drivers/tpm/libtpm.o
+LIBS-$(CONFIG_TPM) += drivers/tpm/libtpm.o
 LIBS-y += drivers/twserial/libtws.o
 LIBS-y += drivers/usb/eth/libusb_eth.o
 LIBS-y += drivers/usb/gadget/libusb_gadget.o
diff --git a/README b/README
index 7f2506a..0e497d1 100644
--- a/README
+++ b/README
@@ -1210,6 +1210,18 @@ The following options need to be configured:
 			to. Contemporary x86 systems usually map it at
 			0xfed40000.
 
+		CONFIG_ST_TPM_I2C
+		Define to compile the ST TPM I2C DRIVER.
+
+		CONFIG_TPM_I2C_BUS
+		Define the bus number of the board.
+
+		CONFIG_TPM_I2C_ADDR
+		Define the address of the TPM.
+
+		CONFIG_CMD_TPM
+		Define to use some TPM u-boot commands.
+
 - USB Support:
 		At the moment only the UHCI host controller is
 		supported (PIP405, MIP405, MPC5200); define
diff --git a/common/cmd_tpm.c b/common/cmd_tpm.c
index 0970a6f..5d4f485 100644
--- a/common/cmd_tpm.c
+++ b/common/cmd_tpm.c
@@ -158,6 +158,182 @@ U_BOOT_CMD(tpm, MAX_TRANSACTION_SIZE, 1, do_tpm,
 	   "device and read the response"
 );
 
+static int do_tpm_hash(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+{
+	u8 tpm_buffer[MAX_TRANSACTION_SIZE];
+	u32 write_size, read_size;
+	char *p;
+	int rv = -1;
+	argc -= 1;
+	argv += 1;
+	u8 response[MAX_TRANSACTION_SIZE];
+	u32 rlength = MAX_TRANSACTION_SIZE;
+
+	u8 Startup[] = {
+		0x00, 0xc1,
+		0x00, 0x00, 0x00, 0x0c,
+		0x00, 0x00, 0x00, 0x99,
+		0x00, 0x01
+	};
+
+	u8 SelfTestFull[] = {
+		0x00, 0xc1,
+		0x00, 0x00, 0x00, 0x0a,
+		0x00, 0x00, 0x00, 0x50
+	};
+
+	u8 ReadPcr17[] = {
+		0x00, 0xc1,
+		0x00, 0x00, 0x00, 0x0e,
+		0x00, 0x00, 0x00, 0x15,
+		0x00, 0x00, 0x00, 0x11
+	};
+
+	for (write_size = 0; write_size < argc; write_size++) {
+		u32 datum = simple_strtoul(argv[write_size], &p, 0);
+		if (*p || (datum > 0xff)) {
+			printf("\n%s: bad data value\n\n", argv[write_size]);
+			cmd_usage(cmdtp);
+			return rv;
+		}
+		tpm_buffer[write_size] = (u8)datum;
+	}
+
+	if (tis_init()) {
+		puts("tis_init() failed!\n");
+		return -1;
+	}
+
+	if (tis_open()) {
+		puts("tis_open() failed!\n");
+		return -1;
+	}
+
+	rv = tis_sendrecv(Startup, sizeof(Startup), &response, &rlength);
+	if (rv) {
+		printf("tpm test Startup failed\n");
+		CHECK(tis_close());
+	}
+
+	rv = tis_sendrecv(SelfTestFull, sizeof(SelfTestFull), &response,
+		 &rlength);
+	if (rv) {
+		printf("tpm test SelfTestFull failed\n");
+		CHECK(tis_close());
+	}
+
+	if (!tis_sendrecv(ReadPcr17, sizeof(ReadPcr17), &response,
+	 &read_size)) {
+		int i;
+		puts("TPM Read PCR 17:\n");
+		for (i = 10; i < read_size; i++)
+			printf(" %2.2x", response[i]);
+		puts("\n");
+		rv = 0;
+	} else {
+		printf("tpm test ReadPcr17 failed\n");
+		CHECK(tis_close());
+	}
+
+	read_size = sizeof(tpm_buffer);
+	if (!tis_sendrecv_hash(tpm_buffer, write_size, tpm_buffer,
+	 &read_size)) {
+		int i;
+		puts("Got TPM Hash response:\n");
+		for (i = 0; i < read_size; i++)
+			printf(" %2.2x", tpm_buffer[i]);
+		puts("\n");
+		rv = 0;
+	} else {
+		puts("tpm hash command failed\n");
+	}
+
+	if (!tis_sendrecv(ReadPcr17, sizeof(ReadPcr17), &response,
+	 &read_size)) {
+		int i;
+		puts("TPM Read PCR 17 after hash:\n");
+		for (i = 10; i < read_size; i++)
+			printf(" %2.2x", response[i]);
+		puts("\n");
+		rv = 0;
+	} else {
+		printf("tpm test ReadPcr17 failed\n");
+		CHECK(tis_close());
+	}
+
+	if (tis_close()) {
+		puts("tis_close() failed!\n");
+		rv = -1;
+	}
+
+	return rv;
+}
+
+
+U_BOOT_CMD(tpm_hash, MAX_TRANSACTION_SIZE, 1, do_tpm_hash,
+	   "<byte> [<byte> ...]   - write data to hash and read response",
+	   "send arbitrary data to the TPM "
+	   "device and read the response."
+	   "ex: tpm 53 01 75 06 00 08 24 05 00 08 00 01"
+);
+
+static int do_tpm_get_flag(void)
+{
+	int rv = 0;
+	u8 response[MAX_TRANSACTION_SIZE];
+	u32 rlength = MAX_TRANSACTION_SIZE;
+
+	u8 Startup[] = {
+		0x00, 0xc1,
+		0x00, 0x00, 0x00, 0x0c,
+		0x00, 0x00, 0x00, 0x99,
+		0x00, 0x01
+	};
+	u8 SelfTestFull[] = {
+		0x00, 0xc1,
+		0x00, 0x00, 0x00, 0x0a,
+		0x00, 0x00, 0x00, 0x50
+	};
+	u8 GetPermFlags[] = {
+		0x00, 0xc1,
+		0x00, 0x00, 0x00, 0x16,
+		0x00, 0x00, 0x00, 0x65,
+		0x00, 0x00, 0x00, 0x04,
+		0x00, 0x00, 0x00, 0x04,
+		0x00, 0x00, 0x01, 0x08
+	};
+
+
+	CHECK(tis_init());
+
+
+	CHECK(tis_open());
+	rv = tis_sendrecv(Startup, sizeof(Startup), response, &rlength);
+	if (rv) {
+		printf("tpm test Startup failed\n");
+		CHECK(tis_close());
+	}
+	rv = tis_sendrecv(SelfTestFull, sizeof(SelfTestFull), response,
+	 &rlength);
+	if (rv) {
+		printf("tpm test SelfTestFull failed\n");
+		CHECK(tis_close());
+	}
+	rv = tis_sendrecv(GetPermFlags, sizeof(GetPermFlags), response,
+	 &rlength);
+	if (rv) {
+		printf("tpm test GetPermFlags failed\n");
+		CHECK(tis_close());
+	}
+	CHECK(tis_close());
+	return rv;
+}
+
+
+U_BOOT_CMD(tpm_get_flag, 1, 1, do_tpm_get_flag,
+	   "start tpm get flag command", ""
+);
+
 static int do_tpm_stress(cmd_tbl_t *cmdtp, int flag,
 			 int argc, char * const argv[])
 {
diff --git a/drivers/tpm/Makefile b/drivers/tpm/Makefile
index be11c8b..802b54e 100644
--- a/drivers/tpm/Makefile
+++ b/drivers/tpm/Makefile
@@ -24,6 +24,7 @@ include $(TOPDIR)/config.mk
 LIB := $(obj)libtpm.o
 
 COBJS-$(CONFIG_GENERIC_LPC_TPM) = generic_lpc_tpm.o
+COBJS-$(CONFIG_ST_TPM_I2C) = tis_i2c.o tpm.o tpm_i2c_st.o
 
 COBJS	:= $(COBJS-y)
 SRCS	:= $(COBJS:.o=.c)
diff --git a/drivers/tpm/compatibility.h b/drivers/tpm/compatibility.h
new file mode 100644
index 0000000..c68a855
--- /dev/null
+++ b/drivers/tpm/compatibility.h
@@ -0,0 +1,52 @@
+/*
+ * Copyright (C) 2011 Infineon Technologies
+ *
+ * Authors:
+ * Peter Huewe <huewe.external@infineon.com>
+ *
+ * Version: 2.1.1
+ *
+ * See file CREDITS for list of people who contributed to this
+ * project.
+ *
+ * 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.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
+ * MA 02111-1307 USA
+ */
+
+#ifndef _COMPATIBILITY_H_
+#define _COMPATIBILITY_H_
+
+/* all includes from U-Boot */
+#include <linux/types.h>
+#include <linux/unaligned/be_byteshift.h>
+#include <asm-generic/errno.h>
+#include <compiler.h>
+#include <common.h>
+
+/* extended error numbers from linux (see errno.h) */
+#define	ECANCELED	125	/* Operation Canceled */
+
+#define msleep(t) udelay((t)*1000)
+
+/* Timer frequency. Corresponds to msec timer resolution*/
+#define HZ             1000
+
+
+#define dev_dbg(dev, format, arg...) debug(format, ##arg)
+#define dev_err(dev, format, arg...) printf(format, ##arg)
+#define dev_info(dev, format, arg...) debug(format, ##arg)
+#define dbg_printf debug
+
+#endif
diff --git a/drivers/tpm/tis_i2c.c b/drivers/tpm/tis_i2c.c
new file mode 100644
index 0000000..a494608
--- /dev/null
+++ b/drivers/tpm/tis_i2c.c
@@ -0,0 +1,211 @@
+/*
+ * Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include <config.h>
+#include <common.h>
+#include <i2c.h>
+#include "tpm.h"
+
+DECLARE_GLOBAL_DATA_PTR;
+
+/* Define in board config */
+#ifndef CONFIG_TPM_I2C_BUS
+	#define CONFIG_TPM_I2C_BUS 0
+	#define CONFIG_TPM_I2C_ADDR 0
+#endif
+
+/* TPM configuration */
+struct tpm {
+	int i2c_bus;
+	int slave_addr;
+	char inited;
+	int old_bus;
+} tpm;
+
+
+static int tpm_select(void)
+{
+	int ret;
+
+	tpm.old_bus = i2c_get_bus_num();
+	if (tpm.old_bus != tpm.i2c_bus) {
+		ret = i2c_set_bus_num(tpm.i2c_bus);
+		if (ret) {
+			debug("%s: Fail to set i2c bus %d\n", __func__,
+			      tpm.i2c_bus);
+			return -1;
+		}
+	}
+	return 0;
+}
+
+static int tpm_deselect(void)
+{
+	int ret;
+
+	if (tpm.old_bus != i2c_get_bus_num()) {
+		ret = i2c_set_bus_num(tpm.old_bus);
+		if (ret) {
+			debug("%s: Fail to restore i2c bus %d\n",
+			      __func__, tpm.old_bus);
+			return -1;
+		}
+	}
+	tpm.old_bus = -1;
+	return 0;
+}
+
+/**
+ * Decode TPM configuration.
+ *
+ * @param dev	Returns a configuration of TPM device
+ * @return 0 if ok, -1 on error
+ */
+static int tpm_decode_config(struct tpm *dev)
+{
+#ifdef CONFIG_OF_CONTROL
+	const void *blob = gd->fdt_blob;
+	int node, parent;
+	int i2c_bus;
+
+	node = fdtdec_next_compatible(blob, 0, COMPAT_INFINEON_SLB9635_TPM);
+	if (node < 0) {
+		debug("%s: Node not found\n", __func__);
+		return -1;
+	}
+	parent = fdt_parent_offset(blob, node);
+	if (parent < 0) {
+		debug("%s: Cannot find node parent\n", __func__);
+		return -1;
+	}
+	i2c_bus = i2c_get_bus_num_fdt(blob, parent);
+	if (i2c_bus < 0)
+		return -1;
+	dev->i2c_bus = i2c_bus;
+	dev->slave_addr = fdtdec_get_addr(blob, node, "reg");
+#else
+	dev->i2c_bus = CONFIG_TPM_I2C_BUS;
+	dev->slave_addr = CONFIG_TPM_I2C_ADDR;
+#endif
+	return 0;
+}
+
+/* Methods used by cmd_tpm */
+int tis_init(void)
+{
+	if (tpm.inited)
+		return 0;
+	if (tpm_decode_config(&tpm))
+		return -1;
+	if (tpm_select())
+		return -1;
+	/*
+	 * Probe TPM twice; the first probing might fail because TPM is asleep,
+	 * and the probing can wake up TPM.
+	 */
+	if (i2c_probe(tpm.slave_addr) && i2c_probe(tpm.slave_addr)) {
+		debug("%s: fail to probe i2c addr 0x%x\n", __func__,
+				tpm.slave_addr);
+		return -1;
+	}
+	tpm_deselect();
+	tpm.inited = 1;
+	return 0;
+}
+
+int tis_open(void)
+{
+	int rc;
+	if (!tpm.inited)
+		return -1;
+
+	if (tpm_select())
+		return -1;
+
+	rc = tpm_open(tpm.slave_addr);
+	tpm_deselect();
+
+	return rc;
+}
+
+int tis_close(void)
+{
+	if (!tpm.inited)
+		return -1;
+
+	if (tpm_select())
+		return -1;
+
+	tpm_close();
+
+	tpm_deselect();
+
+	return 0;
+}
+
+int tis_sendrecv_hash(const uint8_t *sendbuf, size_t sbuf_size,
+		uint8_t *recvbuf, size_t *rbuf_len)
+{
+	int len;
+	uint8_t buf[TPM_BUFSIZE];
+
+	if (!tpm.inited)
+		return -1;
+
+	if (sizeof(buf) < sbuf_size)
+		return -1;
+
+	memcpy(buf, sendbuf, sbuf_size);
+
+	if (tpm_select())
+		return -1;
+
+	len = tpm_transmit_hash(buf, sbuf_size);
+
+	tpm_deselect();
+
+	if (len < 10) {
+		*rbuf_len = 0;
+		return -1;
+	}
+
+	memcpy(recvbuf, buf, len);
+	*rbuf_len = len;
+
+	return 0;
+}
+
+int tis_sendrecv(const uint8_t *sendbuf, size_t sbuf_size,
+		uint8_t *recvbuf, size_t *rbuf_len)
+{
+	int len;
+	uint8_t buf[TPM_BUFSIZE];
+
+	if (!tpm.inited)
+		return -1;
+
+	if (sizeof(buf) < sbuf_size)
+		return -1;
+
+	memcpy(buf, sendbuf, sbuf_size);
+
+	if (tpm_select())
+		return -1;
+
+	len = tpm_transmit(buf, sbuf_size);
+
+	tpm_deselect();
+
+	if (len < 10) {
+		*rbuf_len = 0;
+		return -1;
+	}
+
+	memcpy(recvbuf, buf, len);
+	*rbuf_len = len;
+
+	return 0;
+}
diff --git a/drivers/tpm/tpm.c b/drivers/tpm/tpm.c
new file mode 100644
index 0000000..3f4f66e
--- /dev/null
+++ b/drivers/tpm/tpm.c
@@ -0,0 +1,472 @@
+/*
+ * Copyright (C) 2011 Infineon Technologies
+ *
+ * Authors:
+ * Peter Huewe <huewe.external@infineon.com>
+ *
+ * Description:
+ * Device driver for TCG/TCPA TPM (trusted platform module).
+ * Specifications at www.trustedcomputinggroup.org
+ *
+ * It is based on the Linux kernel driver tpm.c from Leendert van
+ * Dorn, Dave Safford, Reiner Sailer, and Kyleen Hall.
+ *
+ * Version: 2.1.1
+ *
+ * See file CREDITS for list of people who contributed to this
+ * project.
+ *
+ * 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, version 2 of the
+ * License.
+ *
+ * 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.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
+ * MA 02111-1307 USA
+ */
+
+#include <malloc.h>
+#include "tpm.h"
+
+/* global structure for tpm chip data */
+struct tpm_chip g_chip;
+
+enum tpm_duration {
+	TPM_SHORT = 0,
+	TPM_MEDIUM = 1,
+	TPM_LONG = 2,
+	TPM_UNDEFINED,
+};
+
+#define TPM_MAX_ORDINAL 243
+#define TPM_MAX_PROTECTED_ORDINAL 12
+#define TPM_PROTECTED_ORDINAL_MASK 0xFF
+
+/*
+ * Array with one entry per ordinal defining the maximum amount
+ * of time the chip could take to return the result.  The ordinal
+ * designation of short, medium or long is defined in a table in
+ * TCG Specification TPM Main Part 2 TPM Structures Section 17. The
+ * values of the SHORT, MEDIUM, and LONG durations are retrieved
+ * from the chip during initialization with a call to tpm_get_timeouts.
+ */
+static const u8 tpm_protected_ordinal_duration[TPM_MAX_PROTECTED_ORDINAL] = {
+	TPM_UNDEFINED,		/* 0 */
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,		/* 5 */
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_SHORT,		/* 10 */
+	TPM_SHORT,
+};
+
+static const u8 tpm_ordinal_duration[TPM_MAX_ORDINAL] = {
+	TPM_UNDEFINED,		/* 0 */
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,		/* 5 */
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_SHORT,		/* 10 */
+	TPM_SHORT,
+	TPM_MEDIUM,
+	TPM_LONG,
+	TPM_LONG,
+	TPM_MEDIUM,		/* 15 */
+	TPM_SHORT,
+	TPM_SHORT,
+	TPM_MEDIUM,
+	TPM_LONG,
+	TPM_SHORT,		/* 20 */
+	TPM_SHORT,
+	TPM_MEDIUM,
+	TPM_MEDIUM,
+	TPM_MEDIUM,
+	TPM_SHORT,		/* 25 */
+	TPM_SHORT,
+	TPM_MEDIUM,
+	TPM_SHORT,
+	TPM_SHORT,
+	TPM_MEDIUM,		/* 30 */
+	TPM_LONG,
+	TPM_MEDIUM,
+	TPM_SHORT,
+	TPM_SHORT,
+	TPM_SHORT,		/* 35 */
+	TPM_MEDIUM,
+	TPM_MEDIUM,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_MEDIUM,		/* 40 */
+	TPM_LONG,
+	TPM_MEDIUM,
+	TPM_SHORT,
+	TPM_SHORT,
+	TPM_SHORT,		/* 45 */
+	TPM_SHORT,
+	TPM_SHORT,
+	TPM_SHORT,
+	TPM_LONG,
+	TPM_MEDIUM,		/* 50 */
+	TPM_MEDIUM,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,		/* 55 */
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_MEDIUM,		/* 60 */
+	TPM_MEDIUM,
+	TPM_MEDIUM,
+	TPM_SHORT,
+	TPM_SHORT,
+	TPM_MEDIUM,		/* 65 */
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_SHORT,		/* 70 */
+	TPM_SHORT,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,		/* 75 */
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_LONG,		/* 80 */
+	TPM_UNDEFINED,
+	TPM_MEDIUM,
+	TPM_LONG,
+	TPM_SHORT,
+	TPM_UNDEFINED,		/* 85 */
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_SHORT,		/* 90 */
+	TPM_SHORT,
+	TPM_SHORT,
+	TPM_SHORT,
+	TPM_SHORT,
+	TPM_UNDEFINED,		/* 95 */
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_MEDIUM,		/* 100 */
+	TPM_SHORT,
+	TPM_SHORT,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,		/* 105 */
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_SHORT,		/* 110 */
+	TPM_SHORT,
+	TPM_SHORT,
+	TPM_SHORT,
+	TPM_SHORT,
+	TPM_SHORT,		/* 115 */
+	TPM_SHORT,
+	TPM_SHORT,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_LONG,		/* 120 */
+	TPM_LONG,
+	TPM_MEDIUM,
+	TPM_UNDEFINED,
+	TPM_SHORT,
+	TPM_SHORT,		/* 125 */
+	TPM_SHORT,
+	TPM_LONG,
+	TPM_SHORT,
+	TPM_SHORT,
+	TPM_SHORT,		/* 130 */
+	TPM_MEDIUM,
+	TPM_UNDEFINED,
+	TPM_SHORT,
+	TPM_MEDIUM,
+	TPM_UNDEFINED,		/* 135 */
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_SHORT,		/* 140 */
+	TPM_SHORT,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,		/* 145 */
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_SHORT,		/* 150 */
+	TPM_MEDIUM,
+	TPM_MEDIUM,
+	TPM_SHORT,
+	TPM_SHORT,
+	TPM_UNDEFINED,		/* 155 */
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_SHORT,		/* 160 */
+	TPM_SHORT,
+	TPM_SHORT,
+	TPM_SHORT,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,		/* 165 */
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_LONG,		/* 170 */
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,		/* 175 */
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_MEDIUM,		/* 180 */
+	TPM_SHORT,
+	TPM_MEDIUM,
+	TPM_MEDIUM,
+	TPM_MEDIUM,
+	TPM_MEDIUM,		/* 185 */
+	TPM_SHORT,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,		/* 190 */
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,		/* 195 */
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_SHORT,		/* 200 */
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_SHORT,
+	TPM_SHORT,		/* 205 */
+	TPM_SHORT,
+	TPM_SHORT,
+	TPM_SHORT,
+	TPM_SHORT,
+	TPM_MEDIUM,		/* 210 */
+	TPM_UNDEFINED,
+	TPM_MEDIUM,
+	TPM_MEDIUM,
+	TPM_MEDIUM,
+	TPM_UNDEFINED,		/* 215 */
+	TPM_MEDIUM,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_SHORT,
+	TPM_SHORT,		/* 220 */
+	TPM_SHORT,
+	TPM_SHORT,
+	TPM_SHORT,
+	TPM_SHORT,
+	TPM_UNDEFINED,		/* 225 */
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_SHORT,		/* 230 */
+	TPM_LONG,
+	TPM_MEDIUM,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,		/* 235 */
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_UNDEFINED,
+	TPM_SHORT,		/* 240 */
+	TPM_UNDEFINED,
+	TPM_MEDIUM,
+};
+
+/*
+ * Returns max number of milliseconds to wait
+ */
+unsigned long tpm_calc_ordinal_duration(struct tpm_chip *chip, u32 ordinal)
+{
+	int duration_idx = TPM_UNDEFINED;
+	int duration = 0;
+
+	if (ordinal < TPM_MAX_ORDINAL)
+		duration_idx = tpm_ordinal_duration[ordinal];
+	else if ((ordinal & TPM_PROTECTED_ORDINAL_MASK) <
+		 TPM_MAX_PROTECTED_ORDINAL)
+		duration_idx =
+		    tpm_protected_ordinal_duration[ordinal &
+						   TPM_PROTECTED_ORDINAL_MASK];
+
+	if (duration_idx != TPM_UNDEFINED)
+		duration = chip->vendor.duration[duration_idx];
+	if (duration <= 0)
+		return 2 * 60 * HZ; /*two minutes timeout*/
+	else
+		return duration;
+}
+
+ssize_t tpm_transmit_hash(const unsigned char *buf, size_t bufsiz)
+{
+	ssize_t rc;
+	u32 count, ordinal;
+	unsigned long start, stop;
+
+	struct tpm_chip *chip = &g_chip;
+
+	rc = chip->vendor.send_hash(chip, (u8 *) buf, bufsiz);
+	if (rc < 0) {
+		dev_err(chip->dev, "tpm_transmit: tpm_send: error %zd\n", rc);
+		goto out;
+	}
+
+out_recv:
+
+	dbg_printf("out_recv: reading response...\n");
+	rc = chip->vendor.recv(chip, (u8 *) buf, TPM_BUFSIZE);
+	if (rc < 0)
+		dev_err(chip->dev, "tpm_transmit: tpm_recv: error %zd\n", rc);
+out:
+	return rc;
+}
+
+ssize_t tpm_transmit(const unsigned char *buf, size_t bufsiz)
+{
+	ssize_t rc;
+	u32 count, ordinal;
+	unsigned long start, stop;
+
+	struct tpm_chip *chip = &g_chip;
+
+	/* switch endianess: big->little */
+	count = get_unaligned_be32(buf + TPM_CMD_SIZE_BYTE);
+	ordinal = get_unaligned_be32(buf + TPM_CMD_ORDINAL_BYTE);
+
+	if (count == 0) {
+		dev_err(chip->dev, "no data\n");
+		return -ENODATA;
+	}
+	if (count > bufsiz) {
+		dev_err(chip->dev,
+			"invalid count value %x %zx\n", count, bufsiz);
+		return -E2BIG;
+	}
+
+	rc = chip->vendor.send(chip, (u8 *) buf, count);
+	if (rc < 0) {
+		dev_err(chip->dev, "tpm_transmit: tpm_send: error %zd\n", rc);
+		goto out;
+	}
+
+	if (chip->vendor.irq)
+		goto out_recv;
+
+	start = get_timer(0);
+	stop = tpm_calc_ordinal_duration(chip, ordinal);
+	do {
+		dbg_printf("waiting for status...\n");
+		u8 status = chip->vendor.status(chip);
+		if ((status & chip->vendor.req_complete_mask) ==
+		    chip->vendor.req_complete_val) {
+			dbg_printf("...got it;\n");
+			goto out_recv;
+		}
+
+		if ((status == chip->vendor.req_canceled)) {
+			dev_err(chip->dev, "Operation Canceled\n");
+			rc = -ECANCELED;
+			goto out;
+		}
+		msleep(TPM_TIMEOUT);
+	} while (get_timer(start) < stop);
+
+	chip->vendor.cancel(chip);
+	dev_err(chip->dev, "Operation Timed out\n");
+	rc = -ETIME;
+	goto out;
+
+out_recv:
+
+	dbg_printf("out_recv: reading response...\n");
+	rc = chip->vendor.recv(chip, (u8 *) buf, TPM_BUFSIZE);
+	if (rc < 0)
+		dev_err(chip->dev, "tpm_transmit: tpm_recv: error %zd\n", rc);
+out:
+	return rc;
+}
+
+enum tpm_capabilities {
+	TPM_CAP_PROP = cpu_to_be32(5),
+};
+
+enum tpm_sub_capabilities {
+	TPM_CAP_PROP_TIS_TIMEOUT = cpu_to_be32(0x115),
+	TPM_CAP_PROP_TIS_DURATION = cpu_to_be32(0x120),
+};
+
+struct tpm_chip *tpm_register_hardware(const struct tpm_vendor_specific *entry)
+{
+	struct tpm_chip *chip;
+
+	/* Driver specific per-device data */
+	chip = &g_chip;
+	memcpy(&chip->vendor, entry, sizeof(struct tpm_vendor_specific));
+	chip->is_open = 1;
+
+	return chip;
+}
+
+int tpm_open(uint32_t dev_addr)
+{
+	int rc;
+	if (g_chip.is_open)
+		return -EBUSY;
+	rc = tpm_vendor_init(dev_addr);
+	if (rc < 0)
+		g_chip.is_open = 0;
+	return rc;
+}
+
+void tpm_close(void)
+{
+	if (g_chip.is_open) {
+		tpm_vendor_cleanup(&g_chip);
+		g_chip.is_open = 0;
+	}
+}
diff --git a/drivers/tpm/tpm.h b/drivers/tpm/tpm.h
new file mode 100644
index 0000000..151c584
--- /dev/null
+++ b/drivers/tpm/tpm.h
@@ -0,0 +1,164 @@
+/*
+ * Copyright (C) 2011 Infineon Technologies
+ *
+ * Authors:
+ * Peter Huewe <huewe.external@infineon.com>
+ *
+ * Version: 2.1.1
+ *
+ * Description:
+ * Device driver for TCG/TCPA TPM (trusted platform module).
+ * Specifications at www.trustedcomputinggroup.org
+ *
+ * It is based on the Linux kernel driver tpm.c from Leendert van
+ * Dorn, Dave Safford, Reiner Sailer, and Kyleen Hall.
+ *
+ *
+ * See file CREDITS for list of people who contributed to this
+ * project.
+ *
+ * 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, version 2 of the
+ * License.
+ *
+ * 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.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
+ * MA 02111-1307 USA
+ */
+
+#ifndef _TPM_H_
+#define _TPM_H_
+
+#include "compatibility.h"
+
+enum tpm_timeout {
+	TPM_TIMEOUT = 5,	/* msecs */
+};
+
+/* Size of external transmit buffer (used in tpm_transmit)*/
+#define TPM_BUFSIZE 4096
+#define TPM_ERROR_SIZE 10
+#define TPM_HEADER_SIZE 10
+
+/* Index of fields in TPM command buffer */
+#define TPM_CMD_SIZE_BYTE 2
+#define TPM_CMD_ORDINAL_BYTE 6
+
+/* Index of Count field in TPM response buffer */
+#define TPM_RSP_SIZE_BYTE 2
+#define TPM_RSP_RC_BYTE 6
+
+struct tpm_chip;
+
+struct tpm_vendor_specific {
+	const u8 req_complete_mask;
+	const u8 req_complete_val;
+	const u8 req_canceled;
+	int irq;
+	int (*recv) (struct tpm_chip *, u8 *, size_t);
+	int (*send) (struct tpm_chip *, u8 *, size_t);
+	int (*send_hash) (struct tpm_chip *, u8 *, size_t);
+	void (*cancel) (struct tpm_chip *);
+	 u8 (*status) (struct tpm_chip *);
+	int locality;
+	unsigned long timeout_a, timeout_b, timeout_c, timeout_d; /* msec */
+	unsigned long duration[3];	/* msec */
+};
+
+struct tpm_chip {
+	int is_open;
+	struct tpm_vendor_specific vendor;
+	/* testing */
+	u8 dev;
+};
+
+struct tpm_input_header {
+	__be16 tag;
+	__be32 length;
+	__be32 ordinal;
+} __attribute__ ((packed));
+
+struct tpm_output_header {
+	__be16 tag;
+	__be32 length;
+	__be32 return_code;
+} __attribute__ ((packed));
+
+struct timeout_t {
+	__be32 a;
+	__be32 b;
+	__be32 c;
+	__be32 d;
+} __attribute__ ((packed));
+
+struct duration_t {
+	__be32 tpm_short;
+	__be32 tpm_medium;
+	__be32 tpm_long;
+} __attribute__ ((packed));
+
+typedef union {
+	struct timeout_t timeout;
+	struct duration_t duration;
+} cap_t;
+
+struct tpm_getcap_params_in {
+	__be32 cap;
+	__be32 subcap_size;
+	__be32 subcap;
+} __attribute__ ((packed));
+
+struct tpm_getcap_params_out {
+	__be32 cap_size;
+	cap_t cap;
+} __attribute__ ((packed));
+
+typedef union {
+	struct tpm_input_header in;
+	struct tpm_output_header out;
+} tpm_cmd_header;
+
+typedef union {
+	struct tpm_getcap_params_out getcap_out;
+	struct tpm_getcap_params_in getcap_in;
+} tpm_cmd_params;
+
+struct tpm_cmd_t {
+	tpm_cmd_header header;
+	tpm_cmd_params params;
+} __attribute__ ((packed));
+
+
+/* ---------- Interface for TPM vendor ------------ */
+
+extern struct tpm_chip *tpm_register_hardware(
+	const struct tpm_vendor_specific *);
+
+extern int tpm_vendor_init(uint32_t dev_addr);
+
+extern void tpm_vendor_cleanup(struct tpm_chip *chip);
+
+/* ---------- Interface for TDDL ------------------- */
+
+/*
+ * if dev_addr != 0 - redefines TPM device address
+ * Returns < 0 on error, 0 on success.
+ */
+extern int tpm_open(uint32_t dev_addr);
+
+extern void tpm_close(void);
+
+/*
+ * Transmit bufsiz bytes out of buf to TPM and get results back in buf, too.
+ * Returns < 0 on error, 0 on success.
+ */
+extern ssize_t tpm_transmit(const unsigned char *buf, size_t bufsiz);
+
+#endif
diff --git a/drivers/tpm/tpm_i2c_st.c b/drivers/tpm/tpm_i2c_st.c
new file mode 100644
index 0000000..cb716c5
--- /dev/null
+++ b/drivers/tpm/tpm_i2c_st.c
@@ -0,0 +1,601 @@
+/*
+ * STMicroelectronics TPM I2C UBOOT Linux driver for TPM ST33ZP24
+ * Copyright (C) 2013  STMicroelectronics
+ *
+ * Description:
+ * Device driver for TCG/TCPA TPM (trusted platform module).
+ * Specifications at www.trustedcomputinggroup.org
+ *
+ * This device driver implements the TPM interface as defined in
+ * the TCG TPM Interface Spec version 1.2, revision 1.0 and the
+ * STMicroelectronics I2C Protocol Stack Specification version 1.2.0.
+ *
+ * It is based on the Linux kernel driver tpm.c from Leendert van
+ * Dorn, Dave Safford, Reiner Sailer, and Kyleen Hall.
+ *
+ * See file CREDITS for list of people who contributed to this
+ * project.
+ *
+ * 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, version 2 of the
+ * License.
+ *
+ * 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.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
+ * MA 02111-1307 USA
+ *
+ * STMicroelectronics version 1.2.0, Copyright (C) 2013
+ * STMicroelectronics comes with ABSOLUTELY NO WARRANTY.
+ * This is free software, and you are welcome to redistribute it
+ * under certain conditions.
+ *
+ * @Author: Mathias Leblanc tpmsupport@st.com
+ *
+ * @File: tpm_i2c_st.c
+ *
+ */
+
+#include <common.h>
+#include <i2c.h>
+#include <linux/types.h>
+
+#include "compatibility.h"
+#include "tpm.h"
+
+/* max. buffer size supported by our tpm */
+#ifdef TPM_BUFSIZE
+#undef TPM_BUFSIZE
+#endif
+
+#define MINOR_NUM_I2C		224
+
+#define TPM_ACCESS			(0x0)
+#define TPM_STS				(0x18)
+#define TPM_HASH_END			(0x20)
+#define TPM_DATA_FIFO			(0x24)
+#define TPM_HASH_DATA			(0x24)
+#define TPM_HASH_START			(0x28)
+#define TPM_INTF_CAPABILITY		(0x14)
+#define TPM_INT_STATUS			(0x10)
+#define TPM_INT_ENABLE			(0x08)
+
+#define TPM_DUMMY_BYTE			0xAA
+#define TPM_WRITE_DIRECTION		0x80
+#define TPM_HEADER_SIZE			10
+#define TPM_BUFSIZE			2048
+
+#define LOCALITY0		0
+#define LOCALITY4		4
+
+struct st_tpm_hash {
+	int size;
+	u8 *data;
+};
+
+enum stm33zp24_access {
+	TPM_ACCESS_VALID = 0x80,
+	TPM_ACCESS_ACTIVE_LOCALITY = 0x20,
+	TPM_ACCESS_REQUEST_PENDING = 0x04,
+	TPM_ACCESS_REQUEST_USE = 0x02,
+};
+
+enum stm33zp24_status {
+	TPM_STS_VALID = 0x80,
+	TPM_STS_COMMAND_READY = 0x40,
+	TPM_STS_GO = 0x20,
+	TPM_STS_DATA_AVAIL = 0x10,
+	TPM_STS_DATA_EXPECT = 0x08,
+};
+
+enum stm33zp24_int_flags {
+	TPM_GLOBAL_INT_ENABLE = 0x80,
+	TPM_INTF_CMD_READY_INT = 0x080,
+	TPM_INTF_FIFO_AVALAIBLE_INT = 0x040,
+	TPM_INTF_WAKE_UP_READY_INT = 0x020,
+	TPM_INTF_LOC4SOFTRELEASE_INT = 0x008,
+	TPM_INTF_LOCALITY_CHANGE_INT = 0x004,
+	TPM_INTF_STS_VALID_INT = 0x002,
+	TPM_INTF_DATA_AVAIL_INT = 0x001,
+};
+
+enum tis_defaults {
+	TIS_SHORT_TIMEOUT = 750,	/* ms */
+	TIS_LONG_TIMEOUT = 2000,	/* 2 sec */
+};
+
+struct tpm_i2c_ST_dev {
+	uint addr;
+	u8 buf[TPM_BUFSIZE];
+};
+
+static struct tpm_i2c_ST_dev tpm_dev = {
+		/* Note: replace with defined addr from board configuration */
+		.addr = CONFIG_TPM_I2C_ADDR
+};
+
+/*
+ * write8_reg
+ * Send byte to the TIS register according to the ST33ZP24 I2C protocol.
+ * @param: tpm_register, the tpm tis register where the data should be written
+ * @param: tpm_data, the tpm_data to write inside the tpm_register
+ * @param: tpm_size, The length of the data
+ * @return: Returns negative errno, or else the number of bytes written.
+ */
+static int write8_reg(u8 addr, u8 tpm_register,
+		      u8 *tpm_data, u16 tpm_size)
+{
+	u8 data;
+	data = tpm_register;
+	memcpy(&(tpm_dev.buf[0]), &data, sizeof(data));
+	memcpy(&(tpm_dev.buf[0])+1, tpm_data, tpm_size);
+
+	return i2c_write(addr, 0, 0, &tpm_dev.buf[0],
+				tpm_size + 1);
+
+} /* write8_reg() */
+
+/*
+* read8_reg
+* Recv byte from the TIS register according to the ST33ZP24 I2C protocol.
+* @param: tpm_register, the tpm tis register where the data should be read
+* @param: tpm_data, the TPM response
+* @param: tpm_size, tpm TPM response size to read.
+* @return: number of byte read successfully: should be one if success.
+*/
+static int read8_reg(u8 addr, u8 tpm_register,
+u8 *tpm_data, int tpm_size)
+{
+	u8 status = 0;
+	u8 data;
+	data = TPM_DUMMY_BYTE;
+	status = write8_reg(addr, tpm_register, &data, 1);
+	if (status == 0)
+		status = i2c_read(addr, 0, 0, tpm_data, tpm_size);
+return status;
+} /* read8_reg() */
+
+/*
+ * I2C_WRITE_DATA
+ * Send byte to the TIS register according to the ST33ZP24 I2C protocol.
+ * @param: client, the chip description
+ * @param: tpm_register, the tpm tis register where the data should be written
+ * @param: tpm_data, the tpm_data to write inside the tpm_register
+ * @param: tpm_size, The length of the data
+ * @return: number of byte written successfully: should be one if success.
+ */
+#define I2C_WRITE_DATA(client, tpm_register, tpm_data, tpm_size)\
+	 (write8_reg(client, tpm_register | \
+	TPM_WRITE_DIRECTION, tpm_data, tpm_size))
+
+/*
+ * I2C_READ_DATA
+ * Recv byte from the TIS register according to the ST33ZP24 I2C protocol.
+ * @param: tpm, the chip description
+ * @param: tpm_register, the tpm tis register where the data should be read
+ * @param: tpm_data, the TPM response
+ * @param: tpm_size, tpm TPM response size to read.
+ * @return: number of byte read successfully: should be one if success.
+ */
+#define I2C_READ_DATA(client, tpm_register, tpm_data, tpm_size)\
+	 (read8_reg(client, tpm_register, tpm_data, tpm_size))
+
+/*
+ * release_locality release the active locality
+ * @param: chip, the tpm chip description.
+ */
+static void release_locality(struct tpm_chip *chip)
+{
+	u8 data = TPM_ACCESS_ACTIVE_LOCALITY;
+
+	I2C_WRITE_DATA(tpm_dev.addr, TPM_ACCESS, &data, 1);
+}
+
+/*
+ * clear_interruption
+ * clear the TPM interrupt register.
+ * @param: tpm, the chip description
+ */
+static void clear_interruption(u8 addr)
+{
+	u8 interrupt;
+	I2C_READ_DATA(tpm_dev.addr, TPM_INT_STATUS, &interrupt, 1);
+	I2C_WRITE_DATA(tpm_dev.addr, TPM_INT_STATUS, &interrupt, 1);
+	I2C_READ_DATA(tpm_dev.addr, TPM_INT_STATUS, &interrupt, 1);
+} /* clear_interruption() */
+
+int wait_for_serirq_timeout(struct tpm_chip *chip, int condition,
+	 unsigned long timeout)
+{
+	int status = 2;
+
+	clear_interruption(tpm_dev.addr);
+	if (condition)
+		status = 1;
+
+	return status;
+}
+
+/*
+ * check_locality if the locality is active
+ * @param: chip, the tpm chip description
+ * @return: the active locality or -EACCESS.
+ */
+static int check_locality(struct tpm_chip *chip)
+{
+	u8 data;
+	u8 status;
+	status = I2C_READ_DATA(tpm_dev.addr, TPM_ACCESS, &data, 1);
+
+	if ((status == 0) && (data &
+		(TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID)) ==
+		(TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID))
+		return chip->vendor.locality;
+
+	return -EACCES;
+
+} /* check_locality() */
+
+/*
+ * request_locality request the TPM locality
+ * @param: chip, the chip description
+ * @return: the active locality or EACCESS.
+ */
+static int request_locality(struct tpm_chip *chip)
+{
+	unsigned long start, stop;
+	long rc;
+	u8 data;
+	if (check_locality(chip) == chip->vendor.locality)
+		return chip->vendor.locality;
+
+	data = TPM_ACCESS_REQUEST_USE;
+	rc = I2C_WRITE_DATA(tpm_dev.addr, TPM_ACCESS, &data, 1);
+	if (rc < 0)
+		goto end;
+
+	if (chip->vendor.irq) {
+		rc = wait_for_serirq_timeout(chip, (check_locality
+						       (chip) >= 0),
+						      chip->vendor.timeout_a);
+		if (rc > 0)
+			return chip->vendor.locality;
+	} else{
+	/* wait for locality activated */
+	start = get_timer(0);
+	stop = chip->vendor.timeout_a;
+		do {
+			if (check_locality(chip) >= 0)
+				return chip->vendor.locality;
+
+			msleep(TPM_TIMEOUT);
+		} while	 (get_timer(start) < stop);
+	}
+	rc = -EACCES;
+end:
+	return rc;
+} /* request_locality() */
+
+/*
+ * tpm_stm_i2c_cancel, cancel is not implemented.
+ * @param: chip, tpm_chip description.
+ */
+static void tpm_stm_i2c_cancel(struct tpm_chip *chip)
+{
+	u8 data;
+
+	data = TPM_STS_COMMAND_READY;
+	I2C_WRITE_DATA(tpm_dev.addr, TPM_STS, &data, 1);
+	if (chip->vendor.irq)
+		wait_for_serirq_timeout(chip, 1, chip->vendor.timeout_a);
+}	/* tpm_stm_i2c_cancel() */
+
+/*
+ * tpm_stm_spi_status return the TPM_STS register
+ * @param: chip, the tpm chip description
+ * @return: the TPM_STS register value.
+ */
+static u8 tpm_stm_i2c_status(struct tpm_chip *chip)
+{
+	u8 data;
+	I2C_READ_DATA(tpm_dev.addr, TPM_STS, &data, 1);
+	return data;
+}				/* tpm_stm_i2c_status() */
+
+/*
+ * get_burstcount return the burstcount address 0x19 0x1A
+ * @param: chip, the chip description
+ * return: the burstcount.
+ */
+static int get_burstcount(struct tpm_chip *chip)
+{
+	unsigned long start, stop;
+	int burstcnt, status;
+	u8 tpm_reg, temp;
+
+	/* wait for burstcount */
+	/* which timeout value, spec has 2 answers (c & d) */
+	start = get_timer(0);
+	stop = chip->vendor.timeout_d;
+	do {
+		tpm_reg = TPM_STS + 1;
+		status = I2C_READ_DATA(tpm_dev.addr, tpm_reg, &temp, 1);
+		if (status < 0)
+			goto end;
+
+		tpm_reg = tpm_reg + 1;
+		burstcnt = temp;
+		status = I2C_READ_DATA(tpm_dev.addr, tpm_reg, &temp, 1);
+		if (status < 0)
+			goto end;
+
+		burstcnt |= temp << 8;
+		if (burstcnt)
+			return burstcnt;
+
+		msleep(TPM_TIMEOUT);
+	} while (get_timer(start) < stop);
+
+end:
+	return -EBUSY;
+} /* get_burstcount() */
+
+
+/*
+ * recv_data receive data
+ * @param: chip, the tpm chip description
+ * @param: buf, the buffer where the data are received
+ * @param: count, the number of data to receive
+ * @return: the number of bytes read from TPM FIFO.
+ */
+static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count)
+{
+	int size = 0, burstcnt, len;
+
+	while (size < count) {
+		burstcnt = get_burstcount(chip);
+		len = count - size;
+		if ((len) > burstcnt)
+			len = burstcnt;
+		if (I2C_READ_DATA(tpm_dev.addr, TPM_DATA_FIFO,
+		 buf + size, len) == 0)
+			size += len;
+		else
+			break;
+	}
+	return size;
+} /* recv_data() */
+
+/*
+ * tpm_stm_i2c_recv received TPM response through the I2C bus.
+ * @param: chip, tpm_chip description.
+ * @param: buf,	the buffer to store datas.
+ * @param: count, the number of bytes to send.
+ * @return: In case of success the number of bytes received.
+ *		In other case, a < 0 value describing the issue.
+ */
+static int tpm_stm_i2c_recv(struct tpm_chip *chip, unsigned char *buf,
+			    size_t count)
+{
+	int size = 0;
+	int expected;
+
+	if (chip == NULL)
+		return -EBUSY;
+
+	if (count < TPM_HEADER_SIZE) {
+		size = -EIO;
+		goto out;
+	}
+
+	size = recv_data(chip, buf, TPM_HEADER_SIZE);
+	if (size < TPM_HEADER_SIZE) {
+		dev_err(chip->dev, "Unable to read header\n");
+		goto out;
+	}
+
+
+	expected = get_unaligned_be32(buf + TPM_RSP_SIZE_BYTE);
+	if (expected > count) {
+		size = -EIO;
+		goto out;
+	}
+
+	size += recv_data(chip, &buf[TPM_HEADER_SIZE],
+					expected - TPM_HEADER_SIZE);
+	if (size < expected) {
+		dev_err(chip->dev, "Unable to read remainder of result\n");
+		size = -ETIME;
+		goto out;
+	}
+
+out:
+	chip->vendor.cancel(chip);
+	release_locality(chip);
+	return size;
+} /* tpm_stm_i2c_recv() */
+
+/*
+ * tpm_stm_i2c_send send TPM commands through the I2C bus.
+ *
+ * @param: chip, tpm_chip description.
+ * @param: buf,	the buffer to send.
+ * @param: len, the number of bytes to send.
+ * @return: In case of success the number of bytes sent.
+ *			In other case, a < 0 value describing the issue.
+ */
+static int tpm_stm_i2c_send(struct tpm_chip *chip, u8 *buf,
+			    size_t len)
+{
+	u32 ret = 0,
+	    status,
+	    burstcnt = 0, i, size;
+	u8 data;
+
+	if (chip == NULL)
+		return -EBUSY;
+	if (len < TPM_HEADER_SIZE)
+		return -EBUSY;
+
+	ret = request_locality(chip);
+	if (ret < 0)
+		return ret;
+
+	status = tpm_stm_i2c_status(chip);
+	if ((status & TPM_STS_COMMAND_READY) == 0)
+		tpm_stm_i2c_cancel(chip);
+
+	for (i = 0 ; i < len - 1 ;) {
+		burstcnt = get_burstcount(chip);
+		size = len - i - 1;
+		if ((size) > burstcnt)
+			size = burstcnt;
+		ret = I2C_WRITE_DATA(tpm_dev.addr, TPM_DATA_FIFO, buf, size);
+		if (ret < 0)
+			goto out_err;
+
+		i += size;
+	}
+
+	status = tpm_stm_i2c_status(chip);
+	if ((status & TPM_STS_DATA_EXPECT) == 0) {
+		ret = -EIO;
+		goto out_err;
+	}
+
+	ret = I2C_WRITE_DATA(tpm_dev.addr, TPM_DATA_FIFO, buf + len - 1, 1);
+	if (ret < 0)
+		goto out_err;
+
+	status = tpm_stm_i2c_status(chip);
+	if ((status & TPM_STS_DATA_EXPECT) != 0) {
+		ret = -EIO;
+		goto out_err;
+	}
+
+	data = TPM_STS_GO;
+	I2C_WRITE_DATA(tpm_dev.addr, TPM_STS, &data, 1);
+
+	return len;
+out_err:
+	tpm_stm_i2c_cancel(chip);
+	release_locality(chip);
+	return ret;
+} /* tpm_stm_i2c_send() */
+
+/*
+ * tpm_stm_i2c_send_hash send TPM locality 4 hash datas through the I2C bus
+ * to update the PCR[17].
+ * @param: chip, the tpm_chip description.
+ * @param: buf,	the data buffer to send.
+ * @param: len, the number of bytes to send.
+ * @return: In case of success the number of bytes sent.
+ *			In other case, a < 0 value describing the issue.
+ */
+static int tpm_stm_i2c_send_hash(struct tpm_chip *chip, unsigned char *buf,
+			    size_t len)
+{
+	u32 ret = 0;
+	u8 data;
+
+	if (chip == NULL)
+		return -EBUSY;
+
+	release_locality(chip);
+
+	tpm_dev.addr = 0x1B;
+	chip->vendor.locality = LOCALITY4;
+
+	data = TPM_DUMMY_BYTE;
+	ret = I2C_WRITE_DATA(tpm_dev.addr, TPM_HASH_START, &data, 1);
+	if (ret < 0)
+		goto end;
+	ret = I2C_WRITE_DATA(tpm_dev.addr, TPM_DATA_FIFO, buf, len);
+	if (ret < 0)
+		goto end;
+
+end:
+	I2C_WRITE_DATA(tpm_dev.addr, TPM_HASH_END, &data, 1);
+	release_locality(chip);
+	chip->vendor.locality = LOCALITY0;
+	tpm_dev.addr = 0x13;
+	ret = request_locality(chip);
+	return ret;
+} /* tpm_stm_i2c_send_hash */
+
+static struct tpm_vendor_specific st_i2c_tpm = {
+	.send = tpm_stm_i2c_send,
+	.send_hash = tpm_stm_i2c_send_hash,
+	.recv = tpm_stm_i2c_recv,
+	.cancel = tpm_stm_i2c_cancel,
+	.status = tpm_stm_i2c_status,
+	.req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
+	.req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
+	.req_canceled = TPM_STS_COMMAND_READY,
+};
+
+/*
+ * tpm_vendor_init initialize the TPM device
+ * @param: dev_addr, the i2c address of the tpm.
+ * @return: 0 in case of success.
+ *	 -1 in other case.
+ */
+int tpm_vendor_init(uint32_t dev_addr)
+{
+	u32 vendor;
+	uint old_addr;
+	int rc = 0;
+	struct tpm_chip *chip;
+
+	old_addr = tpm_dev.addr;
+	if (dev_addr != 0)
+		tpm_dev.addr = dev_addr;
+
+	chip = tpm_register_hardware(&st_i2c_tpm);
+
+	if (chip < 0) {
+		rc = -ENODEV;
+		goto out_err;
+	}
+
+	/* Default timeouts */
+	chip->vendor.timeout_a = TIS_SHORT_TIMEOUT;
+	chip->vendor.timeout_b = TIS_LONG_TIMEOUT;
+	chip->vendor.timeout_c = TIS_SHORT_TIMEOUT;
+	chip->vendor.timeout_d = TIS_SHORT_TIMEOUT;
+
+	chip->vendor.locality = LOCALITY0;
+
+	if (request_locality(chip) != 0) {
+		rc = -ENODEV;
+		goto out_err;
+	}
+
+	vendor = be32_to_cpu(vendor);
+
+
+	dev_info(dev, "1.2 TPM STMicroelectronics");
+	/*
+	 * A timeout query to TPM can be placed here.
+	 * Standard timeout values are used so far
+	 */
+
+	return 0;
+
+out_err:
+	tpm_dev.addr = old_addr;
+	return rc;
+} /* tpm_vendor_init() */
+
+
+
+void tpm_vendor_cleanup(struct tpm_chip *chip)
+{
+	release_locality(chip);
+} /* tpm_vendor_cleanup() */
diff --git a/include/configs/omap3_beagle.h b/include/configs/omap3_beagle.h
index 48ce4c0..ef381f8 100644
--- a/include/configs/omap3_beagle.h
+++ b/include/configs/omap3_beagle.h
@@ -111,6 +111,14 @@
 #define STATUS_LED_BOOT			STATUS_LED_BIT
 #define STATUS_LED_GREEN		STATUS_LED_BIT1
 
+/* TPM */
+#define CONFIG_CMD_TPM
+#define CONFIG_TPM
+#define CONFIG_ST_TPM_DEBUG
+#define CONFIG_ST_TPM_I2C
+#define CONFIG_TPM_I2C_BUS	1
+#define CONFIG_TPM_I2C_ADDR	0x13
+
 /* Enable Multi Bus support for I2C */
 #define CONFIG_I2C_MULTI_BUS		1
 
diff --git a/include/tpm.h b/include/tpm.h
index 6b21e9c..9074885 100644
--- a/include/tpm.h
+++ b/include/tpm.h
@@ -68,4 +68,20 @@ int tis_close(void);
 int tis_sendrecv(const uint8_t *sendbuf, size_t send_size, uint8_t *recvbuf,
 			size_t *recv_len);
 
+/*
+ * tis_sendrecv_hash()
+ *
+ * Send the requested data to the TPM for hash in LOC 4
+ * and then try to get its response
+ *
+ * @sendbuf - buffer of the data to hash
+ * @send_size size of the data to send
+ * @recvbuf - memory to save the response to
+ * @recv_len - pointer to the size of the response buffer
+ *
+ * Returns 0 on success (and places the number of response bytes at recv_len)
+ * or -1 on failure.
+ */
+int tis_sendrecv_hash(const uint8_t *sendbuf, size_t send_size, uint8_t *recvbuf,
+			size_t *recv_len);
 #endif /* _INCLUDE_TPM_H_ */
-- 
1.7.1

^ permalink raw reply related	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2014-03-31 18:31 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-05-15 13:58 [U-Boot] [PATCH 1/1] TPM: STMicroelectronics u-boot driver I2C Mathias leblanc
2013-10-22 15:48 ` Simon Glass
2014-02-16 22:46   ` Simon Glass
2014-02-16 22:47     ` Simon Glass
2014-02-17 22:23   ` Simon Glass
     [not found]     ` <C56FB217EE26FF4AB56DA86C1AB6A2724E40EAD190@SAFEX1MAIL3.st.com>
2014-03-31 18:31       ` Simon Glass
  -- strict thread matches above, loose matches on Subject: below --
2013-04-22 15:55 Mathias leblanc
2013-04-22 18:53 ` Tom Rini
2013-04-23  2:33 ` Simon Glass
2013-04-08 14:03 Mathias leblanc
2013-04-08 18:25 ` Wolfgang Denk
2013-03-22 16:28 Mathias leblanc
2013-05-11 21:04 ` Simon Glass

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.