mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH 1/1] TPM: STMicroelectronics st33 driver SPI
@ 2013-05-07 10:16 Matthias Leblanc
       [not found] ` <-7985513476024686594@unknownmsgid>
  0 siblings, 1 reply; 21+ messages in thread
From: Matthias Leblanc @ 2013-05-07 10:16 UTC (permalink / raw)
  To: Kent Yoder, Rajiv Andrade, Marcel Selhorst, Sirrix AG, tpmdd-devel
  Cc: linux-kernel, Jean-Luc Blanc, Mathias Leblanc

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

 * STMicroelectronics version 1.2.0, Copyright (C) 2010
 * 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 SPI, in menuconfig or .config choose the tpm driver on
device --> tpm and activate the protocol of your choice before compiling
the kernel.
The driver will be accessible from within Linux.

Tested on linux x86/x64 and beagleboard REV B & XM REV C

Changelog:

date: 07-05-2013
Remove unused variables
Remove tpm_st33_spi_exit function that cause compilation error

Signed-off-by: Mathias Leblanc <mathias.leblanc@st.com>
---
 drivers/char/tpm/Kconfig            |   10 +
 drivers/char/tpm/Makefile           |    1 +
 drivers/char/tpm/tpm_spi_stm_st33.c |  939 +++++++++++++++++++++++++++++++++++
 drivers/char/tpm/tpm_spi_stm_st33.h |   61 +++
 4 files changed, 1011 insertions(+), 0 deletions(-)
 create mode 100644 drivers/char/tpm/tpm_spi_stm_st33.c
 create mode 100644 drivers/char/tpm/tpm_spi_stm_st33.h

diff --git a/drivers/char/tpm/Kconfig b/drivers/char/tpm/Kconfig
index dbfd564..2fc1254 100644
--- a/drivers/char/tpm/Kconfig
+++ b/drivers/char/tpm/Kconfig
@@ -91,4 +91,14 @@ config TCG_ST33_I2C
         To compile this driver as a module, choose M here; the module will be
         called tpm_stm_st33_i2c.
 
+config TCG_ST33_SPI
+       tristate "STMicroelectronics ST33 SPI"
+       depends on SPI
+       depends on GPIOLIB
+       ---help---
+       If you have a TPM security chip from STMicroelectronics working with
+       an SPI bus say Yes and it will be accessible from within Linux.
+       To compile this driver as a module, choose M here; the module will be
+       called tpm_stm_st33_spi.
+
 endif # TCG_TPM
diff --git a/drivers/char/tpm/Makefile b/drivers/char/tpm/Makefile
index a3736c9..a1db3dd 100644
--- a/drivers/char/tpm/Makefile
+++ b/drivers/char/tpm/Makefile
@@ -18,3 +18,4 @@ obj-$(CONFIG_TCG_ATMEL) += tpm_atmel.o
 obj-$(CONFIG_TCG_INFINEON) += tpm_infineon.o
 obj-$(CONFIG_TCG_IBMVTPM) += tpm_ibmvtpm.o
 obj-$(CONFIG_TCG_ST33_I2C) += tpm_i2c_stm_st33.o
+obj-$(CONFIG_TCG_ST33_SPI) += tpm_spi_stm_st33.o
diff --git a/drivers/char/tpm/tpm_spi_stm_st33.c b/drivers/char/tpm/tpm_spi_stm_st33.c
new file mode 100644
index 0000000..9279dc0
--- /dev/null
+++ b/drivers/char/tpm/tpm_spi_stm_st33.c
@@ -0,0 +1,939 @@
+/*
+ * STMicroelectronics TPM SPI Linux driver for TPM ST33ZP24
+ * Copyright (C) 2009, 2010  STMicroelectronics
+ *
+ * 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.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * STMicroelectronics version 1.2.0, Copyright (C) 2010
+ * STMicroelectronics comes with ABSOLUTELY NO WARRANTY.
+ * This is free software, and you are welcome to redistribute it
+ * under certain conditions.
+ *
+ * @Author: Christophe RICARD tpmsupport@st.com
+ *
+ * @File: tpm_stm_st33_spi.c
+ *
+ * @Synopsis:
+ *	09/15/2010:	First shot driver tpm_tis driver for lpc is
+ *			used as model.
+ */
+
+#include <linux/kernel.h>
+#include <linux/init.h>
+#include <linux/spi/spi.h>
+#include <linux/wait.h>
+#include <linux/string.h>
+#include <linux/interrupt.h>
+#include <linux/spinlock.h>
+#include <linux/sysfs.h>
+#include <linux/gpio.h>
+#include <linux/uaccess.h>
+#include <linux/slab.h>
+#include <linux/sched.h>
+
+#include "tpm.h"
+
+#include "tpm_spi_stm_st33.h"
+
+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 */
+};
+
+
+static inline int spi_read_write(struct spi_device *spi,
+	 struct spi_transfer xfer) {
+	struct spi_message msg;
+	spi_message_init(&msg);
+	spi_message_add_tail(&xfer, &msg);
+	return spi_sync(spi, &msg);
+}
+
+/*
+ * spi_write8_reg
+ * Send byte to the TIS register according to the ST33ZP24 SPI protocol.
+ * @param: tpm, 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: should be zero if success else a negative error code.
+ */
+static int spi_write8_reg(struct tpm_chip *tpm, u8 tpm_register,
+		      u8 *tpm_data, u16 tpm_size)
+{
+	u8 data;
+	int total_length = 0, nbr_dummy_bytes;
+	int value = 0;
+	struct spi_device *dev = (struct spi_device *)tpm->vendor.iobase;
+	struct st33zp24_platform_data *platform_data = dev->dev.platform_data;
+	u8 *data_buffer = platform_data->tpm_spi_buffer[0];
+	struct spi_transfer xfer = {
+		.tx_buf	 = data_buffer,
+		.rx_buf	 = platform_data->tpm_spi_buffer[1],
+	};
+
+	u8 latency = platform_data->latency + tpm_size + 1;
+
+	/* Pre-Header */
+	data = TPM_WRITE_DIRECTION | tpm->vendor.locality;
+	memcpy(data_buffer + total_length, &data, sizeof(data));
+	total_length++;
+	data = tpm_register;
+	memcpy(data_buffer + total_length, &data, sizeof(data));
+	total_length++;
+
+	if (tpm_size > 0 &&
+	     (tpm_register == TPM_DATA_FIFO ||
+	      tpm_register == TPM_HASH_DATA)) {
+		data = (tpm_size >> 8) & 0x00ff;
+		data_buffer[total_length++] = data;
+		data = tpm_size & 0x00ff;
+		data_buffer[total_length++] = data;
+		latency -= 2;
+	}
+
+	memcpy(&data_buffer[total_length], tpm_data, tpm_size);
+	total_length += tpm_size;
+
+	nbr_dummy_bytes = platform_data->latency + 1;
+	memset(&data_buffer[total_length], TPM_DUMMY_BYTE,
+					 platform_data->latency + 1);
+
+	xfer.len = total_length;
+
+	value = spi_read_write(dev, xfer);
+
+	if (value == 0) {
+		nbr_dummy_bytes = total_length - 1 - nbr_dummy_bytes;
+		while (nbr_dummy_bytes < total_length &&
+			 ((u8 *)xfer.rx_buf)[nbr_dummy_bytes] == 0)
+					nbr_dummy_bytes++;
+
+		if (((u8 *)xfer.rx_buf)[nbr_dummy_bytes] != 0)
+			value = ((u8 *)xfer.rx_buf)[nbr_dummy_bytes];
+	}
+
+	return value;
+} /* spi_write8_reg() */
+
+/*
+ * spi_read8_reg
+ * Recv byte from the TIS register according to the ST33ZP24 SPI 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: should be zero if success else a negative error code.
+ */
+static u8 spi_read8_reg(struct tpm_chip *tpm, u8 tpm_register,
+		    u8 *tpm_data, u16 tpm_size)
+{
+	u8 data;
+	int total_length = 0, nbr_dummy_bytes;
+	int value = 0;
+	struct spi_device *dev = (struct spi_device *)tpm->vendor.iobase;
+	struct st33zp24_platform_data *platform_data = dev->dev.platform_data;
+	u8 *data_buffer = platform_data->tpm_spi_buffer[0];
+	struct spi_transfer xfer = {
+		.tx_buf	 = data_buffer,
+		.rx_buf	 = platform_data->tpm_spi_buffer[1],
+	};
+
+	/* Pre-Header */
+
+	data = tpm->vendor.locality;
+	memcpy(data_buffer + total_length, &data, sizeof(data));
+	total_length++;
+	data = tpm_register;
+	memcpy(data_buffer + total_length, &data, sizeof(data));
+	total_length++;
+
+	nbr_dummy_bytes = platform_data->latency + 1;
+	memset(&data_buffer[total_length], TPM_DUMMY_BYTE,
+					 platform_data->latency + 1);
+
+	xfer.len = total_length;
+
+	/* header + status byte + size of the data + status byte */
+	value = spi_read_write(dev, xfer);
+
+	if (tpm_size > 0 && value == 0) {
+		nbr_dummy_bytes = 2;
+		for (; nbr_dummy_bytes < total_length &&
+			 ((u8 *)xfer.rx_buf)[nbr_dummy_bytes] == 0;
+			 nbr_dummy_bytes++)
+			;
+
+		if (nbr_dummy_bytes + 1 < total_length) {
+			value = ((u8 *)xfer.rx_buf)[nbr_dummy_bytes];
+
+			if (tpm_size > 0)
+				memcpy(tpm_data, xfer.rx_buf +
+							 nbr_dummy_bytes + 1,
+								 tpm_size);
+		}
+	}
+	return value;
+} /* spi_read8_reg() */
+
+/*
+ * clear_interruption
+ * clear the TPM interrupt register.
+ * @param: tpm, the chip description
+ */
+static void clear_interruption(struct tpm_chip *tpm)
+{
+	u8 interrupt;
+	spi_read8_reg(tpm, TPM_INT_STATUS, &interrupt, 1);
+	/* Clear interrupts handled with TPM_EOI */
+	spi_write8_reg(tpm, TPM_INT_STATUS, &interrupt, 1);
+	spi_read8_reg(tpm, TPM_INT_STATUS, &interrupt, 1);
+} /* clear_interruption() */
+
+/*
+ * _wait_for_serirq_timeout
+ * @param: tpm, the chip description
+ * @param: timeout, the timeout of the interrupt
+ * @return: the status of the interruption.
+ */
+unsigned long wait_for_serirq_timeout(struct tpm_chip *chip, bool condition,
+				 unsigned long timeout)
+{
+	unsigned long status;
+	struct spi_device *client;
+	struct st33zp24_platform_data *pin_infos;
+
+	client = (struct spi_device *)chip->vendor.iobase;
+	pin_infos = client->dev.platform_data;
+
+	status = wait_for_completion_interruptible_timeout(
+				&pin_infos->irq_detection, timeout);
+	if (status > 0)
+		enable_irq(gpio_to_irq(pin_infos->io_serirq));
+	gpio_direction_input(pin_infos->io_serirq);
+
+	if (!status) {
+		status = -EBUSY;
+		goto wait_end;
+	}
+	clear_interruption(chip);
+	if (condition)
+		status = 1;
+
+wait_end:
+	return status;
+}
+
+
+/*
+ * tpm_stm_spi_cancel, cancel is not implemented.
+ * @param: chip, the tpm chip description as specified in
+ * driver/char/tpm/tpm.h.
+ */
+static void tpm_stm_spi_cancel(struct tpm_chip *chip)
+{
+	u8 data = TPM_STS_COMMAND_READY;
+
+	/* this causes the current command to be aborted */
+	spi_write8_reg(chip, TPM_STS, &data, 1);
+} /* tpm_stm_spi_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_spi_status(struct tpm_chip *chip)
+{
+	u8 data;
+
+	spi_read8_reg(chip, TPM_STS, &data, 1);
+	return data;
+} /* tpm_stm_spi_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 = spi_read8_reg(chip, TPM_ACCESS, &data, 1);
+	if (status && (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 stop;
+	long rc;
+	u8 data;
+
+	/* Check locality */
+	if (check_locality(chip) == chip->vendor.locality)
+		return chip->vendor.locality;
+
+	/* Request locality */
+	data = TPM_ACCESS_REQUEST_USE;
+	rc = spi_write8_reg(chip, TPM_ACCESS, &data, 1);
+	if (rc < 0)
+		goto end;
+
+	/* wait for burstcount */
+	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 {
+		stop = jiffies + chip->vendor.timeout_a;
+		do {
+			if (check_locality(chip) >= 0)
+				return chip->vendor.locality;
+			msleep(TPM_TIMEOUT);
+		} while (time_before(jiffies, stop));
+	}
+end:
+	return -EACCES;
+} /* request_locality() */
+
+/*
+ * release_locality release the active locality
+ * @param: chip, the tpm chip description.
+ */
+static void release_locality(struct tpm_chip *chip)
+{
+	u8 data;
+
+	data = TPM_ACCESS_ACTIVE_LOCALITY;
+	spi_write8_reg(chip, TPM_ACCESS, &data, 1);
+} /* release_locality()*/
+
+/*
+ * 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 stop;
+	u32 burstcnt, status;
+	u8 tpm_reg, temp;
+
+	/* wait for burstcount */
+	/* which timeout value, spec has 2 answers (c & d) */
+	stop = jiffies + chip->vendor.timeout_d;
+	do {
+		tpm_reg = TPM_STS + 1;
+		status = spi_read8_reg(chip, tpm_reg, &temp, 1);
+		if (status < 0)
+			goto end;
+
+		tpm_reg = tpm_reg + 1;
+		burstcnt = temp;
+		status = spi_read8_reg(chip, tpm_reg, &temp, 1);
+		if (status < 0)
+			goto end;
+
+		burstcnt |= temp << 8;
+		if (burstcnt)
+			return burstcnt;
+		msleep(TPM_TIMEOUT);
+	} while (time_before(jiffies, stop));
+
+end:
+	return -EBUSY;
+} /* get_burstcount() */
+
+/*
+ * wait_for_stat wait for a TPM_STS value
+ * @param: chip, the tpm chip description
+ * @param: mask, the value mask to wait
+ * @param: timeout, the timeout
+ * @param: queue, the wait queue.
+ * @return: the tpm status, 0 if success, -ETIME if timeout is reached.
+ */
+static int wait_for_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout,
+			 wait_queue_head_t *queue)
+{
+	unsigned long stop;
+	long rc;
+	u8 status;
+
+	/* check current status */
+	status = tpm_stm_spi_status(chip);
+	if (!chip->vendor.irq && (status & mask) == mask)
+		return 0;
+
+	if (chip->vendor.irq) {
+		rc = wait_for_serirq_timeout(chip, ((tpm_stm_spi_status
+						    (chip) & mask) ==
+						     mask), timeout);
+		if (rc > 0)
+			return 0;
+	} else {
+		stop = jiffies + timeout;
+		do {
+			msleep(TPM_TIMEOUT);
+			status = tpm_stm_spi_status(chip);
+			if ((status & mask) == mask)
+				return 0;
+		} while (time_before(jiffies, stop));
+	}
+	return -ETIME;
+} /* wait_for_stat() */
+
+/*
+ * 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 tpm status, 0 if success, -ETIME if timeout is reached.
+ */
+static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count)
+{
+	u32 size = 0, burstcnt, status, len;
+
+	while (size < count &&
+	       wait_for_stat(chip,
+			     TPM_STS_DATA_AVAIL | TPM_STS_VALID,
+			     chip->vendor.timeout_c,
+			     &chip->vendor.read_queue)
+						== 0) {
+		burstcnt = get_burstcount(chip);
+		len = min_t(int, burstcnt, count - size);
+		status = spi_read8_reg(chip, TPM_DATA_FIFO, buf + size, len);
+		if (status < 0)
+			return status;
+
+
+		size += len;
+	}
+	return size;
+}
+
+/*
+ * tpm_ioserirq_handler the serirq irq handler
+ * @param: irq, the tpm chip description
+ * @param: dev_id, the description of the chip
+ * @return: the status of the handler.
+ */
+static irqreturn_t tpm_ioserirq_handler(int irq, void *dev_id)
+{
+	struct tpm_chip *chip = dev_id;
+	struct spi_device *client;
+	struct st33zp24_platform_data *pin_infos;
+
+	disable_irq_nosync(irq);
+
+	client = (struct spi_device *)chip->vendor.iobase;
+	pin_infos = client->dev.platform_data;
+
+	complete(&pin_infos->irq_detection);
+	return IRQ_HANDLED;
+} /* tpm_ioserirq_handler() */
+
+/*
+ * tpm_stm_spi_send send TPM commands through the SPI bus.
+ * @param: chip, the tpm chip description
+ * @param: buf, the tpm command buffer
+ * @param: len, the tpm command size
+ * @return: should be zero if success else the negative error code.
+ */
+static int tpm_stm_spi_send(struct tpm_chip *chip, unsigned char *buf,
+			    size_t len)
+{
+	u32 ret = 0, status, burstcnt = 0, i, size = 0;
+	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_spi_status(chip);
+	if ((status & TPM_STS_COMMAND_READY) == 0) {
+		tpm_stm_spi_cancel(chip);
+		if (wait_for_stat
+		    (chip, TPM_STS_COMMAND_READY, chip->vendor.timeout_b,
+		     &chip->vendor.int_queue) < 0) {
+			ret = -ETIME;
+			goto out_err;
+		}
+	}
+
+	for (i = 0; i < len - 1;) {
+		burstcnt = get_burstcount(chip);
+		size = min_t(int, len - i - 1, burstcnt);
+		ret = spi_write8_reg(chip, TPM_DATA_FIFO, buf, size);
+		if (ret < 0)
+			goto out_err;
+		i += size;
+	}
+
+	status = tpm_stm_spi_status(chip);
+	if ((status & TPM_STS_DATA_EXPECT) == 0) {
+		ret = -EIO;
+		goto out_err;
+	}
+
+	/* write last byte */
+	spi_write8_reg(chip, TPM_DATA_FIFO, buf + len - 1, 1);
+
+	status = tpm_stm_spi_status(chip);
+	if ((status & TPM_STS_DATA_EXPECT) != 0) {
+		ret = -EIO;
+		goto out_err;
+	}
+
+	/* go and do it */
+	data = TPM_STS_GO;
+	ret = spi_write8_reg(chip, TPM_STS, &data, 1);
+	if (ret < 0)
+		goto out_err;
+
+	return len;
+out_err:
+	tpm_stm_spi_cancel(chip);
+	release_locality(chip);
+	return ret;
+}
+
+/*
+ * tpm_stm_spi_recv received TPM response through the SPI bus.
+ * @param: chip, the tpm chip description
+ * @param: buf, the tpm command buffer
+ * @param: len, the tpm command size
+* @return: should be zero if success else the negative error code.
+ */
+static int tpm_stm_spi_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);
+
+	/* read first 10 bytes, including tag, paramsize, and result */
+	if (size < TPM_HEADER_SIZE) {
+		dev_err(chip->dev, "Unable to read header\n");
+		goto out;
+	}
+
+	expected = be32_to_cpu(*(__be32 *)(buf + 2));
+	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;
+}
+
+
+static const struct file_operations tpm_st33_spi_fops = {
+	.owner = THIS_MODULE,
+	.llseek = no_llseek,
+	.read = tpm_read,
+	.write = tpm_write,
+	.open = tpm_open,
+	.release = tpm_release,
+};
+
+static DEVICE_ATTR(pubek, S_IRUGO, tpm_show_pubek, NULL);
+static DEVICE_ATTR(pcrs, S_IRUGO, tpm_show_pcrs, NULL);
+static DEVICE_ATTR(enabled, S_IRUGO, tpm_show_enabled, NULL);
+static DEVICE_ATTR(active, S_IRUGO, tpm_show_active, NULL);
+static DEVICE_ATTR(owned, S_IRUGO, tpm_show_owned, NULL);
+static DEVICE_ATTR(temp_deactivated, S_IRUGO, tpm_show_temp_deactivated, NULL);
+static DEVICE_ATTR(caps, S_IRUGO, tpm_show_caps_1_2, NULL);
+static DEVICE_ATTR(cancel, S_IWUSR | S_IWGRP, NULL, tpm_store_cancel);
+
+static struct attribute *stm_tpm_attrs[] = {
+	&dev_attr_pubek.attr,
+	&dev_attr_pcrs.attr,
+	&dev_attr_enabled.attr,
+	&dev_attr_active.attr,
+	&dev_attr_owned.attr,
+	&dev_attr_temp_deactivated.attr,
+	&dev_attr_caps.attr,
+	&dev_attr_cancel.attr, NULL,
+};
+
+static struct attribute_group stm_tpm_attr_grp = {
+	.attrs = stm_tpm_attrs
+};
+
+static struct tpm_vendor_specific st_spi_tpm = {
+	.send = tpm_stm_spi_send,
+	.recv = tpm_stm_spi_recv,
+	.cancel = tpm_stm_spi_cancel,
+	.status = tpm_stm_spi_status,
+	.attr_group = &stm_tpm_attr_grp,
+	.miscdev = {.fops = &tpm_st33_spi_fops,},
+};
+
+static int evaluate_latency(struct tpm_chip *chip)
+{
+	int latency = 0;
+	struct spi_device *dev = (struct spi_device *)chip->vendor.iobase;
+	struct st33zp24_platform_data *platform_data = dev->dev.platform_data;
+	int status = 0;
+	u8 data;
+
+	for (; status == 0x00 && latency < MAX_SPI_LATENCY; latency++) {
+		platform_data->latency = latency;
+		status = spi_read8_reg(chip, TPM_INTF_CAPABILITY, &data, 1);
+	}
+	return latency - 1;
+} /* evaluate_latency() */
+
+static int interrupts;
+module_param(interrupts, int, 0444);
+MODULE_PARM_DESC(interrupts, "Enable interrupts");
+
+static int power_mgt = 1;
+module_param(power_mgt, int, 0444);
+MODULE_PARM_DESC(power_mgt, "Power Management");
+
+/*
+ * tpm_st33_spi_probe initialize the TPM device
+ * @param: client, the spi_device drescription (TPM SPI description).
+ * @param: id, the spi_device_id struct.
+ * @return: 0 in case of success.
+ *	 -1 in other case.
+ */
+static int
+tpm_st33_spi_probe(struct spi_device *dev)
+{
+	u32 err = 0;
+	u8 intmask;
+	struct tpm_chip *chip;
+	struct st33zp24_platform_data *platform_data;
+
+	/* Check SPI platform functionnalities */
+	if (dev == NULL) {
+		pr_info("dev is NULL. exiting.\n");
+		err = -ENODEV;
+		goto end;
+	}
+
+	chip = tpm_register_hardware(&dev->dev, &st_spi_tpm);
+	if (!chip) {
+		err = -ENODEV;
+		goto end;
+	}
+
+	/* Allocation of SPI buffers MISO and MOSI		*/
+	/* Size is as follow:					*/
+	/* Request burstcount value  = 0x800 = 2048		*/
+	/* +							*/
+	/* Response burstcount value = 0x400 = 1024		*/
+	/* +							*/
+	/* At least:						*/
+	/* 1 byte for direction/locality			*/
+	/* 1 byte tpm tis register				*/
+	/* 2 bytes spi data length (for request only)		*/
+	/* 2 latency bytes					*/
+	/* 1 status byte					*/
+	/* = 2048 + 1024 + 7					*/
+	/* We reserved 2048 + 1024 + 20 in case latency byte	*/
+	/* change						*/
+	platform_data = dev->dev.platform_data;
+	platform_data->tpm_spi_buffer[0] =
+	    kmalloc((TPM_BUFSIZE + (TPM_BUFSIZE / 2) +
+				 TPM_DIGEST_SIZE) * sizeof(u8), GFP_KERNEL);
+	if (platform_data->tpm_spi_buffer[0] == NULL) {
+		err = -ENOMEM;
+		goto _tpm_clean_answer;
+	}
+	platform_data->tpm_spi_buffer[1] =
+	    kmalloc((TPM_BUFSIZE + (TPM_BUFSIZE / 2) +
+				 TPM_DIGEST_SIZE) * sizeof(u8) , GFP_KERNEL);
+	if (platform_data->tpm_spi_buffer[1] == NULL) {
+		err = -ENOMEM;
+		goto _tpm_clean_response;
+	}
+
+	chip->vendor.iobase = dev;
+
+	/* Default timeouts */
+	chip->vendor.timeout_a = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
+	chip->vendor.timeout_b = msecs_to_jiffies(TIS_LONG_TIMEOUT);
+	chip->vendor.timeout_c = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
+	chip->vendor.timeout_d = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
+	chip->vendor.locality = LOCALITY0;
+
+	if (power_mgt) {
+		err = gpio_request(platform_data->io_lpcpd, "TPM IO_LPCPD");
+		if (err)
+			goto _gpio_init1;
+		gpio_set_value(platform_data->io_lpcpd, 1);
+	}
+
+	platform_data->latency = evaluate_latency(chip);
+	if (platform_data->latency <= 0x00) {
+		err = -ENODEV;
+		goto _gpio_init1;
+	}
+
+	/* Enable interrupt */
+	/* Register GPIO pin through generic Linux GPIO API */
+	if (interrupts) {
+		init_completion(&platform_data->irq_detection);
+		if (request_locality(chip) != LOCALITY0) {
+			err = -ENODEV;
+			goto _tpm_clean_response;
+		}
+		err = gpio_request(platform_data->io_serirq, "TPM IO_SERIRQ");
+		if (err)
+			goto _gpio_init2;
+
+		/* Clear all existing */
+		clear_interruption(chip);
+		err = request_irq(gpio_to_irq(platform_data->io_serirq),
+							&tpm_ioserirq_handler,
+				IRQF_TRIGGER_HIGH | IRQF_SHARED,
+				"TPM SERIRQ management", chip);
+		if (err < 0) {
+			pr_info("TPM SERIRQ signals %d not available\n",
+			gpio_to_irq(platform_data->io_serirq));
+			goto _irq_set;
+		}
+		err = spi_read8_reg(chip, TPM_INT_ENABLE, &intmask, 1);
+		if (err < 0)
+			goto _irq_set;
+
+		intmask |= TPM_INTF_CMD_READY_INT
+			|  TPM_INTF_FIFO_AVALAIBLE_INT
+			|  TPM_INTF_WAKE_UP_READY_INT
+			|  TPM_INTF_LOC4SOFTRELEASE_INT
+			|  TPM_INTF_LOCALITY_CHANGE_INT
+			|  TPM_INTF_STS_VALID_INT
+			|  TPM_INTF_DATA_AVAIL_INT;
+
+		err = spi_write8_reg(chip, TPM_INT_ENABLE, &intmask, 1);
+		if (err < 0)
+			goto _irq_set;
+
+		intmask = TPM_GLOBAL_INT_ENABLE;
+		err = spi_write8_reg(chip, TPM_INT_ENABLE + 3, &intmask, 1);
+		if (err < 0)
+			goto _irq_set;
+
+		err = spi_read8_reg(chip, TPM_INT_STATUS, &intmask, 1);
+		if (err < 0)
+			goto _irq_set;
+
+		chip->vendor.irq = interrupts;
+
+		tpm_gen_interrupt(chip);
+	}
+
+	tpm_get_timeouts(chip);
+
+	/* attach chip datas to client */
+	spi_set_drvdata(dev, chip);
+	platform_data->bchipf = false;
+
+	pr_info("TPM SPI Initialized\n");
+	return 0;
+_irq_set:
+	free_irq(gpio_to_irq(platform_data->io_serirq), chip);
+_gpio_init2:
+	if (platform_data && interrupts)
+		gpio_free(platform_data->io_serirq);
+_gpio_init1:
+	if (platform_data && power_mgt)
+		gpio_free(platform_data->io_lpcpd);
+_tpm_clean_response:
+	tpm_remove_hardware(chip->dev);
+	if (platform_data->tpm_spi_buffer[1] != NULL) {
+		kfree(platform_data->tpm_spi_buffer[1]);
+		platform_data->tpm_spi_buffer[1] = NULL;
+	}
+_tpm_clean_answer:
+	if (platform_data->tpm_spi_buffer[0] != NULL) {
+		kfree(platform_data->tpm_spi_buffer[0]);
+		platform_data->tpm_spi_buffer[0] = NULL;
+	}
+
+	platform_data->bchipf = true;
+end:
+	pr_info("TPM SPI initialisation fail\n");
+	return err;
+}
+
+/*
+ * tpm_st33_spi_remove remove the TPM device
+ * @param: client, the spi_device drescription (TPM SPI description).
+		clear_bit(0, &chip->is_open);
+ * @return: 0 in case of success.
+ */
+static int tpm_st33_spi_remove(struct spi_device *client)
+{
+	struct tpm_chip *chip = (struct tpm_chip *)spi_get_drvdata(client);
+	struct st33zp24_platform_data *pin_infos =
+		((struct spi_device *)chip->vendor.iobase)->dev.platform_data;
+
+	if (pin_infos != NULL) {
+		gpio_free(pin_infos->io_lpcpd);
+
+		/* Check if chip has been previously clean */
+		if (pin_infos->bchipf != true)
+			tpm_remove_hardware(chip->dev);
+		if (pin_infos->tpm_spi_buffer[1] != NULL) {
+			kfree(pin_infos->tpm_spi_buffer[1]);
+			pin_infos->tpm_spi_buffer[1] = NULL;
+		}
+		if (pin_infos->tpm_spi_buffer[0] != NULL) {
+			kfree(pin_infos->tpm_spi_buffer[0]);
+			pin_infos->tpm_spi_buffer[0] = NULL;
+		}
+	}
+
+	return 0;
+}
+
+#ifdef CONFIG_PM_SLEEP
+/*
+ * tpm_st33_spi_pm_suspend suspend the TPM device
+ * Added: Work around when suspend and no tpm application is running, suspend
+ * may fail because chip->data_buffer is not set (only set in tpm_open in Linux
+ * TPM core)
+ * @return: 0 in case of success.
+ */
+static int tpm_st33_spi_pm_suspend(struct device *dev)
+{
+	struct tpm_chip *chip = dev_get_drvdata(dev);
+	struct st33zp24_platform_data *pin_infos = dev->platform_data;
+	int ret = 0;
+
+	if (power_mgt)
+		gpio_set_value(pin_infos->io_lpcpd, 0);
+	else {
+		if (chip->data_buffer == NULL)
+			chip->data_buffer = pin_infos->tpm_spi_buffer[0];
+		ret = tpm_pm_suspend(dev);
+		}
+	return ret;
+}				/* tpm_st33_spi_suspend() */
+
+/*
+ * tpm_st33_spi_pm_resume resume the TPM device
+ * @param: spi, the spi_device drescription (TPM SPI description).
+ * @return: 0 in case of success.
+ */
+static int tpm_st33_spi_pm_resume(struct device *dev)
+{
+	struct tpm_chip *chip = dev_get_drvdata(dev);
+	struct st33zp24_platform_data *pin_infos = dev->platform_data;
+
+	int ret = 0;
+	if (power_mgt) {
+		gpio_set_value(pin_infos->io_lpcpd, 1);
+		ret = wait_for_serirq_timeout(chip,
+					 (chip->vendor.status(chip) &&
+					  TPM_STS_VALID) == TPM_STS_VALID,
+					  chip->vendor.timeout_b);
+	} else {
+		if (chip->data_buffer == NULL)
+			chip->data_buffer = pin_infos->tpm_spi_buffer[0];
+		ret = tpm_pm_resume(dev);
+		if (!ret)
+			tpm_do_selftest(chip);
+	}
+	return ret;
+} /* tpm_st33_spi_pm_resume() */
+#endif
+
+static SIMPLE_DEV_PM_OPS(tpm_st33_spi_ops, tpm_st33_spi_pm_suspend,
+	 tpm_st33_spi_pm_resume);
+static struct spi_driver tpm_st33_spi_driver = {
+	.driver = {
+		   .owner = THIS_MODULE,
+		   .name = TPM_ST33_SPI,
+		   .pm = &tpm_st33_spi_ops,
+		   },
+	.probe = tpm_st33_spi_probe,
+	.remove = tpm_st33_spi_remove,
+};
+
+module_spi_driver(tpm_st33_spi_driver);
+
+MODULE_AUTHOR("Christophe Ricard (tpmsupport@st.com)");
+MODULE_DESCRIPTION("STM TPM SPI ST33 Driver");
+MODULE_VERSION("1.2.0");
+MODULE_LICENSE("GPL");
diff --git a/drivers/char/tpm/tpm_spi_stm_st33.h b/drivers/char/tpm/tpm_spi_stm_st33.h
new file mode 100644
index 0000000..4ae5ba6
--- /dev/null
+++ b/drivers/char/tpm/tpm_spi_stm_st33.h
@@ -0,0 +1,61 @@
+/*
+ * STMicroelectronics TPM SPI Linux driver for TPM ST33NP18
+ * Copyright (C) 2009, 2010  STMicroelectronics
+ *
+ * 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.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * STMicroelectronics version 1.2.0, Copyright (C) 2010
+ * STMicroelectronics comes with ABSOLUTELY NO WARRANTY.
+ * This is free software, and you are welcome to redistribute it
+ * under certain conditions.
+ *
+ * @Author: Christophe RICARD tpmsupport@st.com
+ *
+ * @File: tpm_spi_stm_st33.h
+ *
+ * @Date: 09/15/2010
+ */
+#ifndef __STM_ST33_TPM_SPI_MAIN_H__
+#define __STM_ST33_TPM_SPI_MAIN_H__
+
+#define TPM_ACCESS			(0x0)
+#define TPM_STS				(0x18)
+#define TPM_DATA_FIFO			(0x24)
+#define TPM_HASH_DATA			(0x24)
+#define TPM_INTF_CAPABILITY		(0x14)
+#define TPM_INT_STATUS			(0x10)
+#define TPM_INT_ENABLE			(0x08)
+
+#define TPM_DUMMY_BYTE			0x00
+#define TPM_WRITE_DIRECTION		0x80
+#define TPM_HEADER_SIZE			10
+#define TPM_BUFSIZE			2048
+
+#define LOCALITY0		0
+
+#define MAX_SPI_LATENCY		15
+
+#define TPM_ST33_SPI		"st33zp24_spi"
+
+struct st33zp24_platform_data {
+	int io_serirq;
+	int io_lpcpd;
+	int latency;
+	bool bchipf;
+	u8 *tpm_spi_buffer[2]; /* 0 Request 1 Response */
+	struct completion irq_detection;
+};
+
+#endif /* __STM_ST33_TPM_SPI_MAIN_H__ */
-- 
1.7.1


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

* Re: [tpmdd-devel] [PATCH 1/1] TPM: STMicroelectronics st33 driver SPI
       [not found] ` <-7985513476024686594@unknownmsgid>
@ 2013-05-10 15:06   ` Kent Yoder
  2013-05-13 15:30     ` Mathias LEBLANC
  0 siblings, 1 reply; 21+ messages in thread
From: Kent Yoder @ 2013-05-10 15:06 UTC (permalink / raw)
  To: Matthias Leblanc
  Cc: Kent Yoder, Rajiv Andrade, Marcel Selhorst, Sirrix AG,
	TPM Device Driver List, Jean-Luc Blanc, linux-kernel

On Fri, May 10, 2013 at 8:38 AM, Kent Yoder <shpedoikal@gmail.com> wrote:
> Hi Mathias,

Sorry, this note got sent before I was done somehow...

A few compile warnings left:

drivers/char/tpm/tpm_spi_stm_st33.c: In function ‘tpm_stm_spi_status’:
drivers/char/tpm/tpm_spi_stm_st33.c:289:2: error: ‘data’ is used
uninitialized in this function [-Werror=uninitialized]
drivers/char/tpm/tpm_spi_stm_st33.c: In function ‘get_burstcount’:
drivers/char/tpm/tpm_spi_stm_st33.c:386:12: error: ‘temp’ is used
uninitialized in this function [-Werror=uninitialized]
drivers/char/tpm/tpm_spi_stm_st33.c: In function ‘check_locality’:
drivers/char/tpm/tpm_spi_stm_st33.c:305:22: error: ‘data’ may be used
uninitialized in this function [-Werror=uninitialized]

And smatch warnings:

drivers/char/tpm/tpm_spi_stm_st33.c:247 wait_for_serirq_timeout()
error: 'wait_for_completion_interruptible_timeout()' returns negative
and 'status' is unsigned
drivers/char/tpm/tpm_spi_stm_st33.c:382 get_burstcount() warn:
unsigned 'status' is never less than zero.
drivers/char/tpm/tpm_spi_stm_st33.c:388 get_burstcount() warn:
unsigned 'status' is never less than zero.
drivers/char/tpm/tpm_spi_stm_st33.c:459 recv_data() warn: unsigned
'status' is never less than zero.
drivers/char/tpm/tpm_spi_stm_st33.c:508 tpm_stm_spi_send() warn:
unsigned 'ret' is never less than zero.
drivers/char/tpm/tpm_spi_stm_st33.c:526 tpm_stm_spi_send() warn:
unsigned 'ret' is never less than zero.
drivers/char/tpm/tpm_spi_stm_st33.c:549 tpm_stm_spi_send() warn:
unsigned 'ret' is never less than zero.
drivers/char/tpm/tpm_spi_stm_st33.c:772 tpm_st33_spi_probe() warn:
unsigned 'err' is never less than zero.
drivers/char/tpm/tpm_spi_stm_st33.c:778 tpm_st33_spi_probe() warn:
unsigned 'err' is never less than zero.
drivers/char/tpm/tpm_spi_stm_st33.c:790 tpm_st33_spi_probe() warn:
unsigned 'err' is never less than zero.
drivers/char/tpm/tpm_spi_stm_st33.c:795 tpm_st33_spi_probe() warn:
unsigned 'err' is never less than zero.
drivers/char/tpm/tpm_spi_stm_st33.c:799 tpm_st33_spi_probe() warn:
unsigned 'err' is never less than zero.
drivers/char/tpm/tpm_spi_stm_st33.c:818 tpm_st33_spi_probe() warn:
variable dereferenced before check 'platform_data' (see line 717)

And again this patch is getting its last 2 lines clipped in
tpm_spi_stm_st33.c.  Please look below for where to put the ChangeLog
info.

Just as an FYI, I probably am not going to reply to repeat anything
I've said above for future submissions.  We're really going around in
circles here. :-/

Kent

> On Tue, May 07, 2013 at 12:16:05PM +0200, Matthias Leblanc wrote:
>> From: Mathias Leblanc <mathias.leblanc@st.com>
>>
>>  * STMicroelectronics version 1.2.0, Copyright (C) 2010
>>  * 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 SPI, in menuconfig or .config choose the tpm driver on
>> device --> tpm and activate the protocol of your choice before compiling
>> the kernel.
>> The driver will be accessible from within Linux.
>>
>> Tested on linux x86/x64 and beagleboard REV B & XM REV C
>>
>> Changelog:
>>
>> date: 07-05-2013
>> Remove unused variables
>> Remove tpm_st33_spi_exit function that cause compilation error
>>
>> Signed-off-by: Mathias Leblanc <mathias.leblanc@st.com>
>> ---
>
>   ChangeLog goes in here.
>
>>  drivers/char/tpm/Kconfig            |   10 +
>>  drivers/char/tpm/Makefile           |    1 +
>>  drivers/char/tpm/tpm_spi_stm_st33.c |  939 +++++++++++++++++++++++++++++++++++
>>  drivers/char/tpm/tpm_spi_stm_st33.h |   61 +++
>>  4 files changed, 1011 insertions(+), 0 deletions(-)
>>  create mode 100644 drivers/char/tpm/tpm_spi_stm_st33.c
>>  create mode 100644 drivers/char/tpm/tpm_spi_stm_st33.h
>>
>> diff --git a/drivers/char/tpm/Kconfig b/drivers/char/tpm/Kconfig
>> index dbfd564..2fc1254 100644
>> --- a/drivers/char/tpm/Kconfig
>> +++ b/drivers/char/tpm/Kconfig
>> @@ -91,4 +91,14 @@ config TCG_ST33_I2C
>>          To compile this driver as a module, choose M here; the module will be
>>          called tpm_stm_st33_i2c.
>>
>> +config TCG_ST33_SPI
>> +       tristate "STMicroelectronics ST33 SPI"
>> +       depends on SPI
>> +       depends on GPIOLIB
>> +       ---help---
>> +       If you have a TPM security chip from STMicroelectronics working with
>> +       an SPI bus say Yes and it will be accessible from within Linux.
>> +       To compile this driver as a module, choose M here; the module will be
>> +       called tpm_stm_st33_spi.
>> +
>>  endif # TCG_TPM
>> diff --git a/drivers/char/tpm/Makefile b/drivers/char/tpm/Makefile
>> index a3736c9..a1db3dd 100644
>> --- a/drivers/char/tpm/Makefile
>> +++ b/drivers/char/tpm/Makefile
>> @@ -18,3 +18,4 @@ obj-$(CONFIG_TCG_ATMEL) += tpm_atmel.o
>>  obj-$(CONFIG_TCG_INFINEON) += tpm_infineon.o
>>  obj-$(CONFIG_TCG_IBMVTPM) += tpm_ibmvtpm.o
>>  obj-$(CONFIG_TCG_ST33_I2C) += tpm_i2c_stm_st33.o
>> +obj-$(CONFIG_TCG_ST33_SPI) += tpm_spi_stm_st33.o
>> diff --git a/drivers/char/tpm/tpm_spi_stm_st33.c b/drivers/char/tpm/tpm_spi_stm_st33.c
>> new file mode 100644
>> index 0000000..9279dc0
>> --- /dev/null
>> +++ b/drivers/char/tpm/tpm_spi_stm_st33.c
>> @@ -0,0 +1,939 @@
>> +/*
>> + * STMicroelectronics TPM SPI Linux driver for TPM ST33ZP24
>> + * Copyright (C) 2009, 2010  STMicroelectronics
>> + *
>> + * 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.,
>> + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
>> + *
>> + * STMicroelectronics version 1.2.0, Copyright (C) 2010
>> + * STMicroelectronics comes with ABSOLUTELY NO WARRANTY.
>> + * This is free software, and you are welcome to redistribute it
>> + * under certain conditions.
>> + *
>> + * @Author: Christophe RICARD tpmsupport@st.com
>> + *
>> + * @File: tpm_stm_st33_spi.c
>> + *
>> + * @Synopsis:
>> + *   09/15/2010:     First shot driver tpm_tis driver for lpc is
>> + *                   used as model.
>> + */
>> +
>> +#include <linux/kernel.h>
>> +#include <linux/init.h>
>> +#include <linux/spi/spi.h>
>> +#include <linux/wait.h>
>> +#include <linux/string.h>
>> +#include <linux/interrupt.h>
>> +#include <linux/spinlock.h>
>> +#include <linux/sysfs.h>
>> +#include <linux/gpio.h>
>> +#include <linux/uaccess.h>
>> +#include <linux/slab.h>
>> +#include <linux/sched.h>
>> +
>> +#include "tpm.h"
>> +
>> +#include "tpm_spi_stm_st33.h"
>> +
>> +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 */
>> +};
>> +
>> +
>> +static inline int spi_read_write(struct spi_device *spi,
>> +      struct spi_transfer xfer) {
>> +     struct spi_message msg;
>> +     spi_message_init(&msg);
>> +     spi_message_add_tail(&xfer, &msg);
>> +     return spi_sync(spi, &msg);
>> +}
>> +
>> +/*
>> + * spi_write8_reg
>> + * Send byte to the TIS register according to the ST33ZP24 SPI protocol.
>> + * @param: tpm, 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: should be zero if success else a negative error code.
>> + */
>> +static int spi_write8_reg(struct tpm_chip *tpm, u8 tpm_register,
>> +                   u8 *tpm_data, u16 tpm_size)
>> +{
>> +     u8 data;
>> +     int total_length = 0, nbr_dummy_bytes;
>> +     int value = 0;
>> +     struct spi_device *dev = (struct spi_device *)tpm->vendor.iobase;
>> +     struct st33zp24_platform_data *platform_data = dev->dev.platform_data;
>> +     u8 *data_buffer = platform_data->tpm_spi_buffer[0];
>> +     struct spi_transfer xfer = {
>> +             .tx_buf  = data_buffer,
>> +             .rx_buf  = platform_data->tpm_spi_buffer[1],
>> +     };
>> +
>> +     u8 latency = platform_data->latency + tpm_size + 1;
>> +
>> +     /* Pre-Header */
>> +     data = TPM_WRITE_DIRECTION | tpm->vendor.locality;
>> +     memcpy(data_buffer + total_length, &data, sizeof(data));
>> +     total_length++;
>> +     data = tpm_register;
>> +     memcpy(data_buffer + total_length, &data, sizeof(data));
>> +     total_length++;
>> +
>> +     if (tpm_size > 0 &&
>> +          (tpm_register == TPM_DATA_FIFO ||
>> +           tpm_register == TPM_HASH_DATA)) {
>> +             data = (tpm_size >> 8) & 0x00ff;
>> +             data_buffer[total_length++] = data;
>> +             data = tpm_size & 0x00ff;
>> +             data_buffer[total_length++] = data;
>> +             latency -= 2;
>> +     }
>> +
>> +     memcpy(&data_buffer[total_length], tpm_data, tpm_size);
>> +     total_length += tpm_size;
>> +
>> +     nbr_dummy_bytes = platform_data->latency + 1;
>> +     memset(&data_buffer[total_length], TPM_DUMMY_BYTE,
>> +                                      platform_data->latency + 1);
>> +
>> +     xfer.len = total_length;
>> +
>> +     value = spi_read_write(dev, xfer);
>> +
>> +     if (value == 0) {
>> +             nbr_dummy_bytes = total_length - 1 - nbr_dummy_bytes;
>> +             while (nbr_dummy_bytes < total_length &&
>> +                      ((u8 *)xfer.rx_buf)[nbr_dummy_bytes] == 0)
>> +                                     nbr_dummy_bytes++;
>> +
>> +             if (((u8 *)xfer.rx_buf)[nbr_dummy_bytes] != 0)
>> +                     value = ((u8 *)xfer.rx_buf)[nbr_dummy_bytes];
>> +     }
>> +
>> +     return value;
>> +} /* spi_write8_reg() */
>> +
>> +/*
>> + * spi_read8_reg
>> + * Recv byte from the TIS register according to the ST33ZP24 SPI 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: should be zero if success else a negative error code.
>> + */
>> +static u8 spi_read8_reg(struct tpm_chip *tpm, u8 tpm_register,
>> +                 u8 *tpm_data, u16 tpm_size)
>> +{
>> +     u8 data;
>> +     int total_length = 0, nbr_dummy_bytes;
>> +     int value = 0;
>> +     struct spi_device *dev = (struct spi_device *)tpm->vendor.iobase;
>> +     struct st33zp24_platform_data *platform_data = dev->dev.platform_data;
>> +     u8 *data_buffer = platform_data->tpm_spi_buffer[0];
>> +     struct spi_transfer xfer = {
>> +             .tx_buf  = data_buffer,
>> +             .rx_buf  = platform_data->tpm_spi_buffer[1],
>> +     };
>> +
>> +     /* Pre-Header */
>> +
>> +     data = tpm->vendor.locality;
>> +     memcpy(data_buffer + total_length, &data, sizeof(data));
>> +     total_length++;
>> +     data = tpm_register;
>> +     memcpy(data_buffer + total_length, &data, sizeof(data));
>> +     total_length++;
>> +
>> +     nbr_dummy_bytes = platform_data->latency + 1;
>> +     memset(&data_buffer[total_length], TPM_DUMMY_BYTE,
>> +                                      platform_data->latency + 1);
>> +
>> +     xfer.len = total_length;
>> +
>> +     /* header + status byte + size of the data + status byte */
>> +     value = spi_read_write(dev, xfer);
>> +
>> +     if (tpm_size > 0 && value == 0) {
>> +             nbr_dummy_bytes = 2;
>> +             for (; nbr_dummy_bytes < total_length &&
>> +                      ((u8 *)xfer.rx_buf)[nbr_dummy_bytes] == 0;
>> +                      nbr_dummy_bytes++)
>> +                     ;
>> +
>> +             if (nbr_dummy_bytes + 1 < total_length) {
>> +                     value = ((u8 *)xfer.rx_buf)[nbr_dummy_bytes];
>> +
>> +                     if (tpm_size > 0)
>> +                             memcpy(tpm_data, xfer.rx_buf +
>> +                                                      nbr_dummy_bytes + 1,
>> +                                                              tpm_size);
>> +             }
>> +     }
>> +     return value;
>> +} /* spi_read8_reg() */
>> +
>> +/*
>> + * clear_interruption
>> + * clear the TPM interrupt register.
>> + * @param: tpm, the chip description
>> + */
>> +static void clear_interruption(struct tpm_chip *tpm)
>> +{
>> +     u8 interrupt;
>> +     spi_read8_reg(tpm, TPM_INT_STATUS, &interrupt, 1);
>> +     /* Clear interrupts handled with TPM_EOI */
>> +     spi_write8_reg(tpm, TPM_INT_STATUS, &interrupt, 1);
>> +     spi_read8_reg(tpm, TPM_INT_STATUS, &interrupt, 1);
>> +} /* clear_interruption() */
>> +
>> +/*
>> + * _wait_for_serirq_timeout
>> + * @param: tpm, the chip description
>> + * @param: timeout, the timeout of the interrupt
>> + * @return: the status of the interruption.
>> + */
>> +unsigned long wait_for_serirq_timeout(struct tpm_chip *chip, bool condition,
>> +                              unsigned long timeout)
>> +{
>> +     unsigned long status;
>> +     struct spi_device *client;
>> +     struct st33zp24_platform_data *pin_infos;
>> +
>> +     client = (struct spi_device *)chip->vendor.iobase;
>> +     pin_infos = client->dev.platform_data;
>> +
>> +     status = wait_for_completion_interruptible_timeout(
>> +                             &pin_infos->irq_detection, timeout);
>> +     if (status > 0)
>> +             enable_irq(gpio_to_irq(pin_infos->io_serirq));
>> +     gpio_direction_input(pin_infos->io_serirq);
>> +
>> +     if (!status) {
>> +             status = -EBUSY;
>> +             goto wait_end;
>> +     }
>> +     clear_interruption(chip);
>> +     if (condition)
>> +             status = 1;
>> +
>> +wait_end:
>> +     return status;
>> +}
>> +
>> +
>> +/*
>> + * tpm_stm_spi_cancel, cancel is not implemented.
>> + * @param: chip, the tpm chip description as specified in
>> + * driver/char/tpm/tpm.h.
>> + */
>> +static void tpm_stm_spi_cancel(struct tpm_chip *chip)
>> +{
>> +     u8 data = TPM_STS_COMMAND_READY;
>> +
>> +     /* this causes the current command to be aborted */
>> +     spi_write8_reg(chip, TPM_STS, &data, 1);
>> +} /* tpm_stm_spi_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_spi_status(struct tpm_chip *chip)
>> +{
>> +     u8 data;
>> +
>> +     spi_read8_reg(chip, TPM_STS, &data, 1);
>> +     return data;
>> +} /* tpm_stm_spi_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 = spi_read8_reg(chip, TPM_ACCESS, &data, 1);
>> +     if (status && (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 stop;
>> +     long rc;
>> +     u8 data;
>> +
>> +     /* Check locality */
>> +     if (check_locality(chip) == chip->vendor.locality)
>> +             return chip->vendor.locality;
>> +
>> +     /* Request locality */
>> +     data = TPM_ACCESS_REQUEST_USE;
>> +     rc = spi_write8_reg(chip, TPM_ACCESS, &data, 1);
>> +     if (rc < 0)
>> +             goto end;
>> +
>> +     /* wait for burstcount */
>> +     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 {
>> +             stop = jiffies + chip->vendor.timeout_a;
>> +             do {
>> +                     if (check_locality(chip) >= 0)
>> +                             return chip->vendor.locality;
>> +                     msleep(TPM_TIMEOUT);
>> +             } while (time_before(jiffies, stop));
>> +     }
>> +end:
>> +     return -EACCES;
>> +} /* request_locality() */
>> +
>> +/*
>> + * release_locality release the active locality
>> + * @param: chip, the tpm chip description.
>> + */
>> +static void release_locality(struct tpm_chip *chip)
>> +{
>> +     u8 data;
>> +
>> +     data = TPM_ACCESS_ACTIVE_LOCALITY;
>> +     spi_write8_reg(chip, TPM_ACCESS, &data, 1);
>> +} /* release_locality()*/
>> +
>> +/*
>> + * 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 stop;
>> +     u32 burstcnt, status;
>> +     u8 tpm_reg, temp;
>> +
>> +     /* wait for burstcount */
>> +     /* which timeout value, spec has 2 answers (c & d) */
>> +     stop = jiffies + chip->vendor.timeout_d;
>> +     do {
>> +             tpm_reg = TPM_STS + 1;
>> +             status = spi_read8_reg(chip, tpm_reg, &temp, 1);
>> +             if (status < 0)
>> +                     goto end;
>> +
>> +             tpm_reg = tpm_reg + 1;
>> +             burstcnt = temp;
>> +             status = spi_read8_reg(chip, tpm_reg, &temp, 1);
>> +             if (status < 0)
>> +                     goto end;
>> +
>> +             burstcnt |= temp << 8;
>> +             if (burstcnt)
>> +                     return burstcnt;
>> +             msleep(TPM_TIMEOUT);
>> +     } while (time_before(jiffies, stop));
>> +
>> +end:
>> +     return -EBUSY;
>> +} /* get_burstcount() */
>> +
>> +/*
>> + * wait_for_stat wait for a TPM_STS value
>> + * @param: chip, the tpm chip description
>> + * @param: mask, the value mask to wait
>> + * @param: timeout, the timeout
>> + * @param: queue, the wait queue.
>> + * @return: the tpm status, 0 if success, -ETIME if timeout is reached.
>> + */
>> +static int wait_for_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout,
>> +                      wait_queue_head_t *queue)
>> +{
>> +     unsigned long stop;
>> +     long rc;
>> +     u8 status;
>> +
>> +     /* check current status */
>> +     status = tpm_stm_spi_status(chip);
>> +     if (!chip->vendor.irq && (status & mask) == mask)
>> +             return 0;
>> +
>> +     if (chip->vendor.irq) {
>> +             rc = wait_for_serirq_timeout(chip, ((tpm_stm_spi_status
>> +                                                 (chip) & mask) ==
>> +                                                  mask), timeout);
>> +             if (rc > 0)
>> +                     return 0;
>> +     } else {
>> +             stop = jiffies + timeout;
>> +             do {
>> +                     msleep(TPM_TIMEOUT);
>> +                     status = tpm_stm_spi_status(chip);
>> +                     if ((status & mask) == mask)
>> +                             return 0;
>> +             } while (time_before(jiffies, stop));
>> +     }
>> +     return -ETIME;
>> +} /* wait_for_stat() */
>> +
>> +/*
>> + * 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 tpm status, 0 if success, -ETIME if timeout is reached.
>> + */
>> +static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count)
>> +{
>> +     u32 size = 0, burstcnt, status, len;
>> +
>> +     while (size < count &&
>> +            wait_for_stat(chip,
>> +                          TPM_STS_DATA_AVAIL | TPM_STS_VALID,
>> +                          chip->vendor.timeout_c,
>> +                          &chip->vendor.read_queue)
>> +                                             == 0) {
>> +             burstcnt = get_burstcount(chip);
>> +             len = min_t(int, burstcnt, count - size);
>> +             status = spi_read8_reg(chip, TPM_DATA_FIFO, buf + size, len);
>> +             if (status < 0)
>> +                     return status;
>> +
>> +
>> +             size += len;
>> +     }
>> +     return size;
>> +}
>> +
>> +/*
>> + * tpm_ioserirq_handler the serirq irq handler
>> + * @param: irq, the tpm chip description
>> + * @param: dev_id, the description of the chip
>> + * @return: the status of the handler.
>> + */
>> +static irqreturn_t tpm_ioserirq_handler(int irq, void *dev_id)
>> +{
>> +     struct tpm_chip *chip = dev_id;
>> +     struct spi_device *client;
>> +     struct st33zp24_platform_data *pin_infos;
>> +
>> +     disable_irq_nosync(irq);
>> +
>> +     client = (struct spi_device *)chip->vendor.iobase;
>> +     pin_infos = client->dev.platform_data;
>> +
>> +     complete(&pin_infos->irq_detection);
>> +     return IRQ_HANDLED;
>> +} /* tpm_ioserirq_handler() */
>> +
>> +/*
>> + * tpm_stm_spi_send send TPM commands through the SPI bus.
>> + * @param: chip, the tpm chip description
>> + * @param: buf, the tpm command buffer
>> + * @param: len, the tpm command size
>> + * @return: should be zero if success else the negative error code.
>> + */
>> +static int tpm_stm_spi_send(struct tpm_chip *chip, unsigned char *buf,
>> +                         size_t len)
>> +{
>> +     u32 ret = 0, status, burstcnt = 0, i, size = 0;
>> +     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_spi_status(chip);
>> +     if ((status & TPM_STS_COMMAND_READY) == 0) {
>> +             tpm_stm_spi_cancel(chip);
>> +             if (wait_for_stat
>> +                 (chip, TPM_STS_COMMAND_READY, chip->vendor.timeout_b,
>> +                  &chip->vendor.int_queue) < 0) {
>> +                     ret = -ETIME;
>> +                     goto out_err;
>> +             }
>> +     }
>> +
>> +     for (i = 0; i < len - 1;) {
>> +             burstcnt = get_burstcount(chip);
>> +             size = min_t(int, len - i - 1, burstcnt);
>> +             ret = spi_write8_reg(chip, TPM_DATA_FIFO, buf, size);
>> +             if (ret < 0)
>> +                     goto out_err;
>> +             i += size;
>> +     }
>> +
>> +     status = tpm_stm_spi_status(chip);
>> +     if ((status & TPM_STS_DATA_EXPECT) == 0) {
>> +             ret = -EIO;
>> +             goto out_err;
>> +     }
>> +
>> +     /* write last byte */
>> +     spi_write8_reg(chip, TPM_DATA_FIFO, buf + len - 1, 1);
>> +
>> +     status = tpm_stm_spi_status(chip);
>> +     if ((status & TPM_STS_DATA_EXPECT) != 0) {
>> +             ret = -EIO;
>> +             goto out_err;
>> +     }
>> +
>> +     /* go and do it */
>> +     data = TPM_STS_GO;
>> +     ret = spi_write8_reg(chip, TPM_STS, &data, 1);
>> +     if (ret < 0)
>> +             goto out_err;
>> +
>> +     return len;
>> +out_err:
>> +     tpm_stm_spi_cancel(chip);
>> +     release_locality(chip);
>> +     return ret;
>> +}
>> +
>> +/*
>> + * tpm_stm_spi_recv received TPM response through the SPI bus.
>> + * @param: chip, the tpm chip description
>> + * @param: buf, the tpm command buffer
>> + * @param: len, the tpm command size
>> +* @return: should be zero if success else the negative error code.
>> + */
>> +static int tpm_stm_spi_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);
>> +
>> +     /* read first 10 bytes, including tag, paramsize, and result */
>> +     if (size < TPM_HEADER_SIZE) {
>> +             dev_err(chip->dev, "Unable to read header\n");
>> +             goto out;
>> +     }
>> +
>> +     expected = be32_to_cpu(*(__be32 *)(buf + 2));
>> +     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;
>> +}
>> +
>> +
>> +static const struct file_operations tpm_st33_spi_fops = {
>> +     .owner = THIS_MODULE,
>> +     .llseek = no_llseek,
>> +     .read = tpm_read,
>> +     .write = tpm_write,
>> +     .open = tpm_open,
>> +     .release = tpm_release,
>> +};
>> +
>> +static DEVICE_ATTR(pubek, S_IRUGO, tpm_show_pubek, NULL);
>> +static DEVICE_ATTR(pcrs, S_IRUGO, tpm_show_pcrs, NULL);
>> +static DEVICE_ATTR(enabled, S_IRUGO, tpm_show_enabled, NULL);
>> +static DEVICE_ATTR(active, S_IRUGO, tpm_show_active, NULL);
>> +static DEVICE_ATTR(owned, S_IRUGO, tpm_show_owned, NULL);
>> +static DEVICE_ATTR(temp_deactivated, S_IRUGO, tpm_show_temp_deactivated, NULL);
>> +static DEVICE_ATTR(caps, S_IRUGO, tpm_show_caps_1_2, NULL);
>> +static DEVICE_ATTR(cancel, S_IWUSR | S_IWGRP, NULL, tpm_store_cancel);
>> +
>> +static struct attribute *stm_tpm_attrs[] = {
>> +     &dev_attr_pubek.attr,
>> +     &dev_attr_pcrs.attr,
>> +     &dev_attr_enabled.attr,
>> +     &dev_attr_active.attr,
>> +     &dev_attr_owned.attr,
>> +     &dev_attr_temp_deactivated.attr,
>> +     &dev_attr_caps.attr,
>> +     &dev_attr_cancel.attr, NULL,
>> +};
>> +
>> +static struct attribute_group stm_tpm_attr_grp = {
>> +     .attrs = stm_tpm_attrs
>> +};
>> +
>> +static struct tpm_vendor_specific st_spi_tpm = {
>> +     .send = tpm_stm_spi_send,
>> +     .recv = tpm_stm_spi_recv,
>> +     .cancel = tpm_stm_spi_cancel,
>> +     .status = tpm_stm_spi_status,
>> +     .attr_group = &stm_tpm_attr_grp,
>> +     .miscdev = {.fops = &tpm_st33_spi_fops,},
>> +};
>> +
>> +static int evaluate_latency(struct tpm_chip *chip)
>> +{
>> +     int latency = 0;
>> +     struct spi_device *dev = (struct spi_device *)chip->vendor.iobase;
>> +     struct st33zp24_platform_data *platform_data = dev->dev.platform_data;
>> +     int status = 0;
>> +     u8 data;
>> +
>> +     for (; status == 0x00 && latency < MAX_SPI_LATENCY; latency++) {
>> +             platform_data->latency = latency;
>> +             status = spi_read8_reg(chip, TPM_INTF_CAPABILITY, &data, 1);
>> +     }
>> +     return latency - 1;
>> +} /* evaluate_latency() */
>> +
>> +static int interrupts;
>> +module_param(interrupts, int, 0444);
>> +MODULE_PARM_DESC(interrupts, "Enable interrupts");
>> +
>> +static int power_mgt = 1;
>> +module_param(power_mgt, int, 0444);
>> +MODULE_PARM_DESC(power_mgt, "Power Management");
>> +
>> +/*
>> + * tpm_st33_spi_probe initialize the TPM device
>> + * @param: client, the spi_device drescription (TPM SPI description).
>> + * @param: id, the spi_device_id struct.
>> + * @return: 0 in case of success.
>> + *    -1 in other case.
>> + */
>> +static int
>> +tpm_st33_spi_probe(struct spi_device *dev)
>> +{
>> +     u32 err = 0;
>> +     u8 intmask;
>> +     struct tpm_chip *chip;
>> +     struct st33zp24_platform_data *platform_data;
>> +
>> +     /* Check SPI platform functionnalities */
>> +     if (dev == NULL) {
>> +             pr_info("dev is NULL. exiting.\n");
>> +             err = -ENODEV;
>> +             goto end;
>> +     }
>> +
>> +     chip = tpm_register_hardware(&dev->dev, &st_spi_tpm);
>> +     if (!chip) {
>> +             err = -ENODEV;
>> +             goto end;
>> +     }
>> +
>> +     /* Allocation of SPI buffers MISO and MOSI              */
>> +     /* Size is as follow:                                   */
>> +     /* Request burstcount value  = 0x800 = 2048             */
>> +     /* +                                                    */
>> +     /* Response burstcount value = 0x400 = 1024             */
>> +     /* +                                                    */
>> +     /* At least:                                            */
>> +     /* 1 byte for direction/locality                        */
>> +     /* 1 byte tpm tis register                              */
>> +     /* 2 bytes spi data length (for request only)           */
>> +     /* 2 latency bytes                                      */
>> +     /* 1 status byte                                        */
>> +     /* = 2048 + 1024 + 7                                    */
>> +     /* We reserved 2048 + 1024 + 20 in case latency byte    */
>> +     /* change                                               */
>> +     platform_data = dev->dev.platform_data;
>> +     platform_data->tpm_spi_buffer[0] =
>> +         kmalloc((TPM_BUFSIZE + (TPM_BUFSIZE / 2) +
>> +                              TPM_DIGEST_SIZE) * sizeof(u8), GFP_KERNEL);
>> +     if (platform_data->tpm_spi_buffer[0] == NULL) {
>> +             err = -ENOMEM;
>> +             goto _tpm_clean_answer;
>> +     }
>> +     platform_data->tpm_spi_buffer[1] =
>> +         kmalloc((TPM_BUFSIZE + (TPM_BUFSIZE / 2) +
>> +                              TPM_DIGEST_SIZE) * sizeof(u8) , GFP_KERNEL);
>> +     if (platform_data->tpm_spi_buffer[1] == NULL) {
>> +             err = -ENOMEM;
>> +             goto _tpm_clean_response;
>> +     }
>> +
>> +     chip->vendor.iobase = dev;
>> +
>> +     /* Default timeouts */
>> +     chip->vendor.timeout_a = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
>> +     chip->vendor.timeout_b = msecs_to_jiffies(TIS_LONG_TIMEOUT);
>> +     chip->vendor.timeout_c = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
>> +     chip->vendor.timeout_d = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
>> +     chip->vendor.locality = LOCALITY0;
>> +
>> +     if (power_mgt) {
>> +             err = gpio_request(platform_data->io_lpcpd, "TPM IO_LPCPD");
>> +             if (err)
>> +                     goto _gpio_init1;
>> +             gpio_set_value(platform_data->io_lpcpd, 1);
>> +     }
>> +
>> +     platform_data->latency = evaluate_latency(chip);
>> +     if (platform_data->latency <= 0x00) {
>> +             err = -ENODEV;
>> +             goto _gpio_init1;
>> +     }
>> +
>> +     /* Enable interrupt */
>> +     /* Register GPIO pin through generic Linux GPIO API */
>> +     if (interrupts) {
>> +             init_completion(&platform_data->irq_detection);
>> +             if (request_locality(chip) != LOCALITY0) {
>> +                     err = -ENODEV;
>> +                     goto _tpm_clean_response;
>> +             }
>> +             err = gpio_request(platform_data->io_serirq, "TPM IO_SERIRQ");
>> +             if (err)
>> +                     goto _gpio_init2;
>> +
>> +             /* Clear all existing */
>> +             clear_interruption(chip);
>> +             err = request_irq(gpio_to_irq(platform_data->io_serirq),
>> +                                                     &tpm_ioserirq_handler,
>> +                             IRQF_TRIGGER_HIGH | IRQF_SHARED,
>> +                             "TPM SERIRQ management", chip);
>> +             if (err < 0) {
>> +                     pr_info("TPM SERIRQ signals %d not available\n",
>> +                     gpio_to_irq(platform_data->io_serirq));
>> +                     goto _irq_set;
>> +             }
>> +             err = spi_read8_reg(chip, TPM_INT_ENABLE, &intmask, 1);
>> +             if (err < 0)
>> +                     goto _irq_set;
>> +
>> +             intmask |= TPM_INTF_CMD_READY_INT
>> +                     |  TPM_INTF_FIFO_AVALAIBLE_INT
>> +                     |  TPM_INTF_WAKE_UP_READY_INT
>> +                     |  TPM_INTF_LOC4SOFTRELEASE_INT
>> +                     |  TPM_INTF_LOCALITY_CHANGE_INT
>> +                     |  TPM_INTF_STS_VALID_INT
>> +                     |  TPM_INTF_DATA_AVAIL_INT;
>> +
>> +             err = spi_write8_reg(chip, TPM_INT_ENABLE, &intmask, 1);
>> +             if (err < 0)
>> +                     goto _irq_set;
>> +
>> +             intmask = TPM_GLOBAL_INT_ENABLE;
>> +             err = spi_write8_reg(chip, TPM_INT_ENABLE + 3, &intmask, 1);
>> +             if (err < 0)
>> +                     goto _irq_set;
>> +
>> +             err = spi_read8_reg(chip, TPM_INT_STATUS, &intmask, 1);
>> +             if (err < 0)
>> +                     goto _irq_set;
>> +
>> +             chip->vendor.irq = interrupts;
>> +
>> +             tpm_gen_interrupt(chip);
>> +     }
>> +
>> +     tpm_get_timeouts(chip);
>> +
>> +     /* attach chip datas to client */
>> +     spi_set_drvdata(dev, chip);
>> +     platform_data->bchipf = false;
>> +
>> +     pr_info("TPM SPI Initialized\n");
>> +     return 0;
>> +_irq_set:
>> +     free_irq(gpio_to_irq(platform_data->io_serirq), chip);
>> +_gpio_init2:
>> +     if (platform_data && interrupts)
>> +             gpio_free(platform_data->io_serirq);
>> +_gpio_init1:
>> +     if (platform_data && power_mgt)
>> +             gpio_free(platform_data->io_lpcpd);
>> +_tpm_clean_response:
>> +     tpm_remove_hardware(chip->dev);
>> +     if (platform_data->tpm_spi_buffer[1] != NULL) {
>> +             kfree(platform_data->tpm_spi_buffer[1]);
>> +             platform_data->tpm_spi_buffer[1] = NULL;
>> +     }
>> +_tpm_clean_answer:
>> +     if (platform_data->tpm_spi_buffer[0] != NULL) {
>> +             kfree(platform_data->tpm_spi_buffer[0]);
>> +             platform_data->tpm_spi_buffer[0] = NULL;
>> +     }
>> +
>> +     platform_data->bchipf = true;
>> +end:
>> +     pr_info("TPM SPI initialisation fail\n");
>> +     return err;
>> +}
>> +
>> +/*
>> + * tpm_st33_spi_remove remove the TPM device
>> + * @param: client, the spi_device drescription (TPM SPI description).
>> +             clear_bit(0, &chip->is_open);
>> + * @return: 0 in case of success.
>> + */
>> +static int tpm_st33_spi_remove(struct spi_device *client)
>> +{
>> +     struct tpm_chip *chip = (struct tpm_chip *)spi_get_drvdata(client);
>> +     struct st33zp24_platform_data *pin_infos =
>> +             ((struct spi_device *)chip->vendor.iobase)->dev.platform_data;
>> +
>> +     if (pin_infos != NULL) {
>> +             gpio_free(pin_infos->io_lpcpd);
>> +
>> +             /* Check if chip has been previously clean */
>> +             if (pin_infos->bchipf != true)
>> +                     tpm_remove_hardware(chip->dev);
>> +             if (pin_infos->tpm_spi_buffer[1] != NULL) {
>> +                     kfree(pin_infos->tpm_spi_buffer[1]);
>> +                     pin_infos->tpm_spi_buffer[1] = NULL;
>> +             }
>> +             if (pin_infos->tpm_spi_buffer[0] != NULL) {
>> +                     kfree(pin_infos->tpm_spi_buffer[0]);
>> +                     pin_infos->tpm_spi_buffer[0] = NULL;
>> +             }
>> +     }
>> +
>> +     return 0;
>> +}
>> +
>> +#ifdef CONFIG_PM_SLEEP
>> +/*
>> + * tpm_st33_spi_pm_suspend suspend the TPM device
>> + * Added: Work around when suspend and no tpm application is running, suspend
>> + * may fail because chip->data_buffer is not set (only set in tpm_open in Linux
>> + * TPM core)
>> + * @return: 0 in case of success.
>> + */
>> +static int tpm_st33_spi_pm_suspend(struct device *dev)
>> +{
>> +     struct tpm_chip *chip = dev_get_drvdata(dev);
>> +     struct st33zp24_platform_data *pin_infos = dev->platform_data;
>> +     int ret = 0;
>> +
>> +     if (power_mgt)
>> +             gpio_set_value(pin_infos->io_lpcpd, 0);
>> +     else {
>> +             if (chip->data_buffer == NULL)
>> +                     chip->data_buffer = pin_infos->tpm_spi_buffer[0];
>> +             ret = tpm_pm_suspend(dev);
>> +             }
>> +     return ret;
>> +}                            /* tpm_st33_spi_suspend() */
>> +
>> +/*
>> + * tpm_st33_spi_pm_resume resume the TPM device
>> + * @param: spi, the spi_device drescription (TPM SPI description).
>> + * @return: 0 in case of success.
>> + */
>> +static int tpm_st33_spi_pm_resume(struct device *dev)
>> +{
>> +     struct tpm_chip *chip = dev_get_drvdata(dev);
>> +     struct st33zp24_platform_data *pin_infos = dev->platform_data;
>> +
>> +     int ret = 0;
>> +     if (power_mgt) {
>> +             gpio_set_value(pin_infos->io_lpcpd, 1);
>> +             ret = wait_for_serirq_timeout(chip,
>> +                                      (chip->vendor.status(chip) &&
>> +                                       TPM_STS_VALID) == TPM_STS_VALID,
>> +                                       chip->vendor.timeout_b);
>> +     } else {
>> +             if (chip->data_buffer == NULL)
>> +                     chip->data_buffer = pin_infos->tpm_spi_buffer[0];
>> +             ret = tpm_pm_resume(dev);
>> +             if (!ret)
>> +                     tpm_do_selftest(chip);
>> +     }
>> +     return ret;
>> +} /* tpm_st33_spi_pm_resume() */
>> +#endif
>> +
>> +static SIMPLE_DEV_PM_OPS(tpm_st33_spi_ops, tpm_st33_spi_pm_suspend,
>> +      tpm_st33_spi_pm_resume);
>> +static struct spi_driver tpm_st33_spi_driver = {
>> +     .driver = {
>> +                .owner = THIS_MODULE,
>> +                .name = TPM_ST33_SPI,
>> +                .pm = &tpm_st33_spi_ops,
>> +                },
>> +     .probe = tpm_st33_spi_probe,
>> +     .remove = tpm_st33_spi_remove,
>> +};
>> +
>> +module_spi_driver(tpm_st33_spi_driver);
>> +
>> +MODULE_AUTHOR("Christophe Ricard (tpmsupport@st.com)");
>> +MODULE_DESCRIPTION("STM TPM SPI ST33 Driver");
>> +MODULE_VERSION("1.2.0");
>> +MODULE_LICENSE("GPL");
>> diff --git a/drivers/char/tpm/tpm_spi_stm_st33.h b/drivers/char/tpm/tpm_spi_stm_st33.h
>> new file mode 100644
>> index 0000000..4ae5ba6
>> --- /dev/null
>> +++ b/drivers/char/tpm/tpm_spi_stm_st33.h
>> @@ -0,0 +1,61 @@
>> +/*
>> + * STMicroelectronics TPM SPI Linux driver for TPM ST33NP18
>> + * Copyright (C) 2009, 2010  STMicroelectronics
>> + *
>> + * 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.,
>> + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
>> + *
>> + * STMicroelectronics version 1.2.0, Copyright (C) 2010
>> + * STMicroelectronics comes with ABSOLUTELY NO WARRANTY.
>> + * This is free software, and you are welcome to redistribute it
>> + * under certain conditions.
>> + *
>> + * @Author: Christophe RICARD tpmsupport@st.com
>> + *
>> + * @File: tpm_spi_stm_st33.h
>> + *
>> + * @Date: 09/15/2010
>> + */
>> +#ifndef __STM_ST33_TPM_SPI_MAIN_H__
>> +#define __STM_ST33_TPM_SPI_MAIN_H__
>> +
>> +#define TPM_ACCESS                   (0x0)
>> +#define TPM_STS                              (0x18)
>> +#define TPM_DATA_FIFO                        (0x24)
>> +#define TPM_HASH_DATA                        (0x24)
>> +#define TPM_INTF_CAPABILITY          (0x14)
>> +#define TPM_INT_STATUS                       (0x10)
>> +#define TPM_INT_ENABLE                       (0x08)
>> +
>> +#define TPM_DUMMY_BYTE                       0x00
>> +#define TPM_WRITE_DIRECTION          0x80
>> +#define TPM_HEADER_SIZE                      10
>> +#define TPM_BUFSIZE                  2048
>> +
>> +#define LOCALITY0            0
>> +
>> +#define MAX_SPI_LATENCY              15
>> +
>> +#define TPM_ST33_SPI         "st33zp24_spi"
>> +
>> +struct st33zp24_platform_data {
>> +     int io_serirq;
>> +     int io_lpcpd;
>> +     int latency;
>> +     bool bchipf;
>> +     u8 *tpm_spi_buffer[2]; /* 0 Request 1 Response */
>> +     struct completion irq_detection;
>> +};
>> +
>> +#endif /* __STM_ST33_TPM_SPI_MAIN_H__ */
>> --
>> 1.7.1
>>
>>
>> ------------------------------------------------------------------------------
>> Learn Graph Databases - Download FREE O'Reilly Book
>> "Graph Databases" is the definitive new guide to graph databases and
>> their applications. This 200-page book is written by three acclaimed
>> leaders in the field. The early access version is available now.
>> Download your free book today! http://p.sf.net/sfu/neotech_d2d_may
>> _______________________________________________
>> tpmdd-devel mailing list
>> tpmdd-devel@lists.sourceforge.net
>> https://lists.sourceforge.net/lists/listinfo/tpmdd-devel

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

* RE: [tpmdd-devel] [PATCH 1/1] TPM: STMicroelectronics st33 driver SPI
  2013-05-10 15:06   ` [tpmdd-devel] " Kent Yoder
@ 2013-05-13 15:30     ` Mathias LEBLANC
  2013-05-13 15:56       ` Kent Yoder
  0 siblings, 1 reply; 21+ messages in thread
From: Mathias LEBLANC @ 2013-05-13 15:30 UTC (permalink / raw)
  To: Kent Yoder
  Cc: Kent Yoder, Rajiv Andrade, Marcel Selhorst, Sirrix AG,
	TPM Device Driver List, Jean-Luc BLANC, linux-kernel

Hi Kent,

I have tried different compilation command and I haven't these errors.
What's the command you do to generate these warning?
However, these warning will be corrected in the next patch and the others too.

Regards,

Mathias Leblanc

-----Original Message-----
From: Kent Yoder [mailto:shpedoikal@gmail.com]
Sent: 10 May, 2013 17:07
To: Mathias LEBLANC
Cc: Kent Yoder; Rajiv Andrade; Marcel Selhorst; Sirrix AG; TPM Device Driver List; Jean-Luc BLANC; linux-kernel@vger.kernel.org
Subject: Re: [tpmdd-devel] [PATCH 1/1] TPM: STMicroelectronics st33 driver SPI

On Fri, May 10, 2013 at 8:38 AM, Kent Yoder <shpedoikal@gmail.com> wrote:
> Hi Mathias,

Sorry, this note got sent before I was done somehow...

A few compile warnings left:

drivers/char/tpm/tpm_spi_stm_st33.c: In function 'tpm_stm_spi_status':
drivers/char/tpm/tpm_spi_stm_st33.c:289:2: error: 'data' is used uninitialized in this function [-Werror=uninitialized]
drivers/char/tpm/tpm_spi_stm_st33.c: In function 'get_burstcount':
drivers/char/tpm/tpm_spi_stm_st33.c:386:12: error: 'temp' is used uninitialized in this function [-Werror=uninitialized]
drivers/char/tpm/tpm_spi_stm_st33.c: In function 'check_locality':
drivers/char/tpm/tpm_spi_stm_st33.c:305:22: error: 'data' may be used uninitialized in this function [-Werror=uninitialized]

And smatch warnings:

drivers/char/tpm/tpm_spi_stm_st33.c:247 wait_for_serirq_timeout()
error: 'wait_for_completion_interruptible_timeout()' returns negative and 'status' is unsigned
drivers/char/tpm/tpm_spi_stm_st33.c:382 get_burstcount() warn:
unsigned 'status' is never less than zero.
drivers/char/tpm/tpm_spi_stm_st33.c:388 get_burstcount() warn:
unsigned 'status' is never less than zero.
drivers/char/tpm/tpm_spi_stm_st33.c:459 recv_data() warn: unsigned 'status' is never less than zero.
drivers/char/tpm/tpm_spi_stm_st33.c:508 tpm_stm_spi_send() warn:
unsigned 'ret' is never less than zero.
drivers/char/tpm/tpm_spi_stm_st33.c:526 tpm_stm_spi_send() warn:
unsigned 'ret' is never less than zero.
drivers/char/tpm/tpm_spi_stm_st33.c:549 tpm_stm_spi_send() warn:
unsigned 'ret' is never less than zero.
drivers/char/tpm/tpm_spi_stm_st33.c:772 tpm_st33_spi_probe() warn:
unsigned 'err' is never less than zero.
drivers/char/tpm/tpm_spi_stm_st33.c:778 tpm_st33_spi_probe() warn:
unsigned 'err' is never less than zero.
drivers/char/tpm/tpm_spi_stm_st33.c:790 tpm_st33_spi_probe() warn:
unsigned 'err' is never less than zero.
drivers/char/tpm/tpm_spi_stm_st33.c:795 tpm_st33_spi_probe() warn:
unsigned 'err' is never less than zero.
drivers/char/tpm/tpm_spi_stm_st33.c:799 tpm_st33_spi_probe() warn:
unsigned 'err' is never less than zero.
drivers/char/tpm/tpm_spi_stm_st33.c:818 tpm_st33_spi_probe() warn:
variable dereferenced before check 'platform_data' (see line 717)

And again this patch is getting its last 2 lines clipped in tpm_spi_stm_st33.c.  Please look below for where to put the ChangeLog info.

Just as an FYI, I probably am not going to reply to repeat anything I've said above for future submissions.  We're really going around in circles here. :-/

Kent

> On Tue, May 07, 2013 at 12:16:05PM +0200, Matthias Leblanc wrote:
>> From: Mathias Leblanc <mathias.leblanc@st.com>
>>
>>  * STMicroelectronics version 1.2.0, Copyright (C) 2010
>>  * 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 SPI, in menuconfig or .config choose the tpm driver on device -->
>> tpm and activate the protocol of your choice before compiling the
>> kernel.
>> The driver will be accessible from within Linux.
>>
>> Tested on linux x86/x64 and beagleboard REV B & XM REV C
>>
>> Changelog:
>>
>> date: 07-05-2013
>> Remove unused variables
>> Remove tpm_st33_spi_exit function that cause compilation error
>>
>> Signed-off-by: Mathias Leblanc <mathias.leblanc@st.com>
>> ---
>
>   ChangeLog goes in here.
>
>>  drivers/char/tpm/Kconfig            |   10 +
>>  drivers/char/tpm/Makefile           |    1 +
>>  drivers/char/tpm/tpm_spi_stm_st33.c |  939 +++++++++++++++++++++++++++++++++++
>>  drivers/char/tpm/tpm_spi_stm_st33.h |   61 +++
>>  4 files changed, 1011 insertions(+), 0 deletions(-)  create mode
>> 100644 drivers/char/tpm/tpm_spi_stm_st33.c
>>  create mode 100644 drivers/char/tpm/tpm_spi_stm_st33.h
>>
>> diff --git a/drivers/char/tpm/Kconfig b/drivers/char/tpm/Kconfig
>> index dbfd564..2fc1254 100644
>> --- a/drivers/char/tpm/Kconfig
>> +++ b/drivers/char/tpm/Kconfig
>> @@ -91,4 +91,14 @@ config TCG_ST33_I2C
>>          To compile this driver as a module, choose M here; the module will be
>>          called tpm_stm_st33_i2c.
>>
>> +config TCG_ST33_SPI
>> +       tristate "STMicroelectronics ST33 SPI"
>> +       depends on SPI
>> +       depends on GPIOLIB
>> +       ---help---
>> +       If you have a TPM security chip from STMicroelectronics working with
>> +       an SPI bus say Yes and it will be accessible from within Linux.
>> +       To compile this driver as a module, choose M here; the module will be
>> +       called tpm_stm_st33_spi.
>> +
>>  endif # TCG_TPM
>> diff --git a/drivers/char/tpm/Makefile b/drivers/char/tpm/Makefile
>> index a3736c9..a1db3dd 100644
>> --- a/drivers/char/tpm/Makefile
>> +++ b/drivers/char/tpm/Makefile
>> @@ -18,3 +18,4 @@ obj-$(CONFIG_TCG_ATMEL) += tpm_atmel.o
>>  obj-$(CONFIG_TCG_INFINEON) += tpm_infineon.o
>>  obj-$(CONFIG_TCG_IBMVTPM) += tpm_ibmvtpm.o
>>  obj-$(CONFIG_TCG_ST33_I2C) += tpm_i2c_stm_st33.o
>> +obj-$(CONFIG_TCG_ST33_SPI) += tpm_spi_stm_st33.o
>> diff --git a/drivers/char/tpm/tpm_spi_stm_st33.c
>> b/drivers/char/tpm/tpm_spi_stm_st33.c
>> new file mode 100644
>> index 0000000..9279dc0
>> --- /dev/null
>> +++ b/drivers/char/tpm/tpm_spi_stm_st33.c
>> @@ -0,0 +1,939 @@
>> +/*
>> + * STMicroelectronics TPM SPI Linux driver for TPM ST33ZP24
>> + * Copyright (C) 2009, 2010  STMicroelectronics
>> + *
>> + * 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.,
>> + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
>> + *
>> + * STMicroelectronics version 1.2.0, Copyright (C) 2010
>> + * STMicroelectronics comes with ABSOLUTELY NO WARRANTY.
>> + * This is free software, and you are welcome to redistribute it
>> + * under certain conditions.
>> + *
>> + * @Author: Christophe RICARD tpmsupport@st.com
>> + *
>> + * @File: tpm_stm_st33_spi.c
>> + *
>> + * @Synopsis:
>> + *   09/15/2010:     First shot driver tpm_tis driver for lpc is
>> + *                   used as model.
>> + */
>> +
>> +#include <linux/kernel.h>
>> +#include <linux/init.h>
>> +#include <linux/spi/spi.h>
>> +#include <linux/wait.h>
>> +#include <linux/string.h>
>> +#include <linux/interrupt.h>
>> +#include <linux/spinlock.h>
>> +#include <linux/sysfs.h>
>> +#include <linux/gpio.h>
>> +#include <linux/uaccess.h>
>> +#include <linux/slab.h>
>> +#include <linux/sched.h>
>> +
>> +#include "tpm.h"
>> +
>> +#include "tpm_spi_stm_st33.h"
>> +
>> +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 */
>> +};
>> +
>> +
>> +static inline int spi_read_write(struct spi_device *spi,
>> +      struct spi_transfer xfer) {
>> +     struct spi_message msg;
>> +     spi_message_init(&msg);
>> +     spi_message_add_tail(&xfer, &msg);
>> +     return spi_sync(spi, &msg);
>> +}
>> +
>> +/*
>> + * spi_write8_reg
>> + * Send byte to the TIS register according to the ST33ZP24 SPI protocol.
>> + * @param: tpm, 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: should be zero if success else a negative error code.
>> + */
>> +static int spi_write8_reg(struct tpm_chip *tpm, u8 tpm_register,
>> +                   u8 *tpm_data, u16 tpm_size) {
>> +     u8 data;
>> +     int total_length = 0, nbr_dummy_bytes;
>> +     int value = 0;
>> +     struct spi_device *dev = (struct spi_device *)tpm->vendor.iobase;
>> +     struct st33zp24_platform_data *platform_data = dev->dev.platform_data;
>> +     u8 *data_buffer = platform_data->tpm_spi_buffer[0];
>> +     struct spi_transfer xfer = {
>> +             .tx_buf  = data_buffer,
>> +             .rx_buf  = platform_data->tpm_spi_buffer[1],
>> +     };
>> +
>> +     u8 latency = platform_data->latency + tpm_size + 1;
>> +
>> +     /* Pre-Header */
>> +     data = TPM_WRITE_DIRECTION | tpm->vendor.locality;
>> +     memcpy(data_buffer + total_length, &data, sizeof(data));
>> +     total_length++;
>> +     data = tpm_register;
>> +     memcpy(data_buffer + total_length, &data, sizeof(data));
>> +     total_length++;
>> +
>> +     if (tpm_size > 0 &&
>> +          (tpm_register == TPM_DATA_FIFO ||
>> +           tpm_register == TPM_HASH_DATA)) {
>> +             data = (tpm_size >> 8) & 0x00ff;
>> +             data_buffer[total_length++] = data;
>> +             data = tpm_size & 0x00ff;
>> +             data_buffer[total_length++] = data;
>> +             latency -= 2;
>> +     }
>> +
>> +     memcpy(&data_buffer[total_length], tpm_data, tpm_size);
>> +     total_length += tpm_size;
>> +
>> +     nbr_dummy_bytes = platform_data->latency + 1;
>> +     memset(&data_buffer[total_length], TPM_DUMMY_BYTE,
>> +                                      platform_data->latency + 1);
>> +
>> +     xfer.len = total_length;
>> +
>> +     value = spi_read_write(dev, xfer);
>> +
>> +     if (value == 0) {
>> +             nbr_dummy_bytes = total_length - 1 - nbr_dummy_bytes;
>> +             while (nbr_dummy_bytes < total_length &&
>> +                      ((u8 *)xfer.rx_buf)[nbr_dummy_bytes] == 0)
>> +                                     nbr_dummy_bytes++;
>> +
>> +             if (((u8 *)xfer.rx_buf)[nbr_dummy_bytes] != 0)
>> +                     value = ((u8 *)xfer.rx_buf)[nbr_dummy_bytes];
>> +     }
>> +
>> +     return value;
>> +} /* spi_write8_reg() */
>> +
>> +/*
>> + * spi_read8_reg
>> + * Recv byte from the TIS register according to the ST33ZP24 SPI 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: should be zero if success else a negative error code.
>> + */
>> +static u8 spi_read8_reg(struct tpm_chip *tpm, u8 tpm_register,
>> +                 u8 *tpm_data, u16 tpm_size) {
>> +     u8 data;
>> +     int total_length = 0, nbr_dummy_bytes;
>> +     int value = 0;
>> +     struct spi_device *dev = (struct spi_device *)tpm->vendor.iobase;
>> +     struct st33zp24_platform_data *platform_data = dev->dev.platform_data;
>> +     u8 *data_buffer = platform_data->tpm_spi_buffer[0];
>> +     struct spi_transfer xfer = {
>> +             .tx_buf  = data_buffer,
>> +             .rx_buf  = platform_data->tpm_spi_buffer[1],
>> +     };
>> +
>> +     /* Pre-Header */
>> +
>> +     data = tpm->vendor.locality;
>> +     memcpy(data_buffer + total_length, &data, sizeof(data));
>> +     total_length++;
>> +     data = tpm_register;
>> +     memcpy(data_buffer + total_length, &data, sizeof(data));
>> +     total_length++;
>> +
>> +     nbr_dummy_bytes = platform_data->latency + 1;
>> +     memset(&data_buffer[total_length], TPM_DUMMY_BYTE,
>> +                                      platform_data->latency + 1);
>> +
>> +     xfer.len = total_length;
>> +
>> +     /* header + status byte + size of the data + status byte */
>> +     value = spi_read_write(dev, xfer);
>> +
>> +     if (tpm_size > 0 && value == 0) {
>> +             nbr_dummy_bytes = 2;
>> +             for (; nbr_dummy_bytes < total_length &&
>> +                      ((u8 *)xfer.rx_buf)[nbr_dummy_bytes] == 0;
>> +                      nbr_dummy_bytes++)
>> +                     ;
>> +
>> +             if (nbr_dummy_bytes + 1 < total_length) {
>> +                     value = ((u8 *)xfer.rx_buf)[nbr_dummy_bytes];
>> +
>> +                     if (tpm_size > 0)
>> +                             memcpy(tpm_data, xfer.rx_buf +
>> +                                                      nbr_dummy_bytes + 1,
>> +                                                              tpm_size);
>> +             }
>> +     }
>> +     return value;
>> +} /* spi_read8_reg() */
>> +
>> +/*
>> + * clear_interruption
>> + * clear the TPM interrupt register.
>> + * @param: tpm, the chip description  */ static void
>> +clear_interruption(struct tpm_chip *tpm) {
>> +     u8 interrupt;
>> +     spi_read8_reg(tpm, TPM_INT_STATUS, &interrupt, 1);
>> +     /* Clear interrupts handled with TPM_EOI */
>> +     spi_write8_reg(tpm, TPM_INT_STATUS, &interrupt, 1);
>> +     spi_read8_reg(tpm, TPM_INT_STATUS, &interrupt, 1); } /*
>> +clear_interruption() */
>> +
>> +/*
>> + * _wait_for_serirq_timeout
>> + * @param: tpm, the chip description
>> + * @param: timeout, the timeout of the interrupt
>> + * @return: the status of the interruption.
>> + */
>> +unsigned long wait_for_serirq_timeout(struct tpm_chip *chip, bool condition,
>> +                              unsigned long timeout) {
>> +     unsigned long status;
>> +     struct spi_device *client;
>> +     struct st33zp24_platform_data *pin_infos;
>> +
>> +     client = (struct spi_device *)chip->vendor.iobase;
>> +     pin_infos = client->dev.platform_data;
>> +
>> +     status = wait_for_completion_interruptible_timeout(
>> +                             &pin_infos->irq_detection, timeout);
>> +     if (status > 0)
>> +             enable_irq(gpio_to_irq(pin_infos->io_serirq));
>> +     gpio_direction_input(pin_infos->io_serirq);
>> +
>> +     if (!status) {
>> +             status = -EBUSY;
>> +             goto wait_end;
>> +     }
>> +     clear_interruption(chip);
>> +     if (condition)
>> +             status = 1;
>> +
>> +wait_end:
>> +     return status;
>> +}
>> +
>> +
>> +/*
>> + * tpm_stm_spi_cancel, cancel is not implemented.
>> + * @param: chip, the tpm chip description as specified in
>> + * driver/char/tpm/tpm.h.
>> + */
>> +static void tpm_stm_spi_cancel(struct tpm_chip *chip) {
>> +     u8 data = TPM_STS_COMMAND_READY;
>> +
>> +     /* this causes the current command to be aborted */
>> +     spi_write8_reg(chip, TPM_STS, &data, 1); } /*
>> +tpm_stm_spi_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_spi_status(struct tpm_chip *chip) {
>> +     u8 data;
>> +
>> +     spi_read8_reg(chip, TPM_STS, &data, 1);
>> +     return data;
>> +} /* tpm_stm_spi_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 = spi_read8_reg(chip, TPM_ACCESS, &data, 1);
>> +     if (status && (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 stop;
>> +     long rc;
>> +     u8 data;
>> +
>> +     /* Check locality */
>> +     if (check_locality(chip) == chip->vendor.locality)
>> +             return chip->vendor.locality;
>> +
>> +     /* Request locality */
>> +     data = TPM_ACCESS_REQUEST_USE;
>> +     rc = spi_write8_reg(chip, TPM_ACCESS, &data, 1);
>> +     if (rc < 0)
>> +             goto end;
>> +
>> +     /* wait for burstcount */
>> +     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 {
>> +             stop = jiffies + chip->vendor.timeout_a;
>> +             do {
>> +                     if (check_locality(chip) >= 0)
>> +                             return chip->vendor.locality;
>> +                     msleep(TPM_TIMEOUT);
>> +             } while (time_before(jiffies, stop));
>> +     }
>> +end:
>> +     return -EACCES;
>> +} /* request_locality() */
>> +
>> +/*
>> + * release_locality release the active locality
>> + * @param: chip, the tpm chip description.
>> + */
>> +static void release_locality(struct tpm_chip *chip) {
>> +     u8 data;
>> +
>> +     data = TPM_ACCESS_ACTIVE_LOCALITY;
>> +     spi_write8_reg(chip, TPM_ACCESS, &data, 1); } /*
>> +release_locality()*/
>> +
>> +/*
>> + * 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 stop;
>> +     u32 burstcnt, status;
>> +     u8 tpm_reg, temp;
>> +
>> +     /* wait for burstcount */
>> +     /* which timeout value, spec has 2 answers (c & d) */
>> +     stop = jiffies + chip->vendor.timeout_d;
>> +     do {
>> +             tpm_reg = TPM_STS + 1;
>> +             status = spi_read8_reg(chip, tpm_reg, &temp, 1);
>> +             if (status < 0)
>> +                     goto end;
>> +
>> +             tpm_reg = tpm_reg + 1;
>> +             burstcnt = temp;
>> +             status = spi_read8_reg(chip, tpm_reg, &temp, 1);
>> +             if (status < 0)
>> +                     goto end;
>> +
>> +             burstcnt |= temp << 8;
>> +             if (burstcnt)
>> +                     return burstcnt;
>> +             msleep(TPM_TIMEOUT);
>> +     } while (time_before(jiffies, stop));
>> +
>> +end:
>> +     return -EBUSY;
>> +} /* get_burstcount() */
>> +
>> +/*
>> + * wait_for_stat wait for a TPM_STS value
>> + * @param: chip, the tpm chip description
>> + * @param: mask, the value mask to wait
>> + * @param: timeout, the timeout
>> + * @param: queue, the wait queue.
>> + * @return: the tpm status, 0 if success, -ETIME if timeout is reached.
>> + */
>> +static int wait_for_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout,
>> +                      wait_queue_head_t *queue) {
>> +     unsigned long stop;
>> +     long rc;
>> +     u8 status;
>> +
>> +     /* check current status */
>> +     status = tpm_stm_spi_status(chip);
>> +     if (!chip->vendor.irq && (status & mask) == mask)
>> +             return 0;
>> +
>> +     if (chip->vendor.irq) {
>> +             rc = wait_for_serirq_timeout(chip, ((tpm_stm_spi_status
>> +                                                 (chip) & mask) ==
>> +                                                  mask), timeout);
>> +             if (rc > 0)
>> +                     return 0;
>> +     } else {
>> +             stop = jiffies + timeout;
>> +             do {
>> +                     msleep(TPM_TIMEOUT);
>> +                     status = tpm_stm_spi_status(chip);
>> +                     if ((status & mask) == mask)
>> +                             return 0;
>> +             } while (time_before(jiffies, stop));
>> +     }
>> +     return -ETIME;
>> +} /* wait_for_stat() */
>> +
>> +/*
>> + * 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 tpm status, 0 if success, -ETIME if timeout is reached.
>> + */
>> +static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count) {
>> +     u32 size = 0, burstcnt, status, len;
>> +
>> +     while (size < count &&
>> +            wait_for_stat(chip,
>> +                          TPM_STS_DATA_AVAIL | TPM_STS_VALID,
>> +                          chip->vendor.timeout_c,
>> +                          &chip->vendor.read_queue)
>> +                                             == 0) {
>> +             burstcnt = get_burstcount(chip);
>> +             len = min_t(int, burstcnt, count - size);
>> +             status = spi_read8_reg(chip, TPM_DATA_FIFO, buf + size, len);
>> +             if (status < 0)
>> +                     return status;
>> +
>> +
>> +             size += len;
>> +     }
>> +     return size;
>> +}
>> +
>> +/*
>> + * tpm_ioserirq_handler the serirq irq handler
>> + * @param: irq, the tpm chip description
>> + * @param: dev_id, the description of the chip
>> + * @return: the status of the handler.
>> + */
>> +static irqreturn_t tpm_ioserirq_handler(int irq, void *dev_id) {
>> +     struct tpm_chip *chip = dev_id;
>> +     struct spi_device *client;
>> +     struct st33zp24_platform_data *pin_infos;
>> +
>> +     disable_irq_nosync(irq);
>> +
>> +     client = (struct spi_device *)chip->vendor.iobase;
>> +     pin_infos = client->dev.platform_data;
>> +
>> +     complete(&pin_infos->irq_detection);
>> +     return IRQ_HANDLED;
>> +} /* tpm_ioserirq_handler() */
>> +
>> +/*
>> + * tpm_stm_spi_send send TPM commands through the SPI bus.
>> + * @param: chip, the tpm chip description
>> + * @param: buf, the tpm command buffer
>> + * @param: len, the tpm command size
>> + * @return: should be zero if success else the negative error code.
>> + */
>> +static int tpm_stm_spi_send(struct tpm_chip *chip, unsigned char *buf,
>> +                         size_t len) {
>> +     u32 ret = 0, status, burstcnt = 0, i, size = 0;
>> +     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_spi_status(chip);
>> +     if ((status & TPM_STS_COMMAND_READY) == 0) {
>> +             tpm_stm_spi_cancel(chip);
>> +             if (wait_for_stat
>> +                 (chip, TPM_STS_COMMAND_READY, chip->vendor.timeout_b,
>> +                  &chip->vendor.int_queue) < 0) {
>> +                     ret = -ETIME;
>> +                     goto out_err;
>> +             }
>> +     }
>> +
>> +     for (i = 0; i < len - 1;) {
>> +             burstcnt = get_burstcount(chip);
>> +             size = min_t(int, len - i - 1, burstcnt);
>> +             ret = spi_write8_reg(chip, TPM_DATA_FIFO, buf, size);
>> +             if (ret < 0)
>> +                     goto out_err;
>> +             i += size;
>> +     }
>> +
>> +     status = tpm_stm_spi_status(chip);
>> +     if ((status & TPM_STS_DATA_EXPECT) == 0) {
>> +             ret = -EIO;
>> +             goto out_err;
>> +     }
>> +
>> +     /* write last byte */
>> +     spi_write8_reg(chip, TPM_DATA_FIFO, buf + len - 1, 1);
>> +
>> +     status = tpm_stm_spi_status(chip);
>> +     if ((status & TPM_STS_DATA_EXPECT) != 0) {
>> +             ret = -EIO;
>> +             goto out_err;
>> +     }
>> +
>> +     /* go and do it */
>> +     data = TPM_STS_GO;
>> +     ret = spi_write8_reg(chip, TPM_STS, &data, 1);
>> +     if (ret < 0)
>> +             goto out_err;
>> +
>> +     return len;
>> +out_err:
>> +     tpm_stm_spi_cancel(chip);
>> +     release_locality(chip);
>> +     return ret;
>> +}
>> +
>> +/*
>> + * tpm_stm_spi_recv received TPM response through the SPI bus.
>> + * @param: chip, the tpm chip description
>> + * @param: buf, the tpm command buffer
>> + * @param: len, the tpm command size
>> +* @return: should be zero if success else the negative error code.
>> + */
>> +static int tpm_stm_spi_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);
>> +
>> +     /* read first 10 bytes, including tag, paramsize, and result */
>> +     if (size < TPM_HEADER_SIZE) {
>> +             dev_err(chip->dev, "Unable to read header\n");
>> +             goto out;
>> +     }
>> +
>> +     expected = be32_to_cpu(*(__be32 *)(buf + 2));
>> +     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;
>> +}
>> +
>> +
>> +static const struct file_operations tpm_st33_spi_fops = {
>> +     .owner = THIS_MODULE,
>> +     .llseek = no_llseek,
>> +     .read = tpm_read,
>> +     .write = tpm_write,
>> +     .open = tpm_open,
>> +     .release = tpm_release,
>> +};
>> +
>> +static DEVICE_ATTR(pubek, S_IRUGO, tpm_show_pubek, NULL); static
>> +DEVICE_ATTR(pcrs, S_IRUGO, tpm_show_pcrs, NULL); static
>> +DEVICE_ATTR(enabled, S_IRUGO, tpm_show_enabled, NULL); static
>> +DEVICE_ATTR(active, S_IRUGO, tpm_show_active, NULL); static
>> +DEVICE_ATTR(owned, S_IRUGO, tpm_show_owned, NULL); static
>> +DEVICE_ATTR(temp_deactivated, S_IRUGO, tpm_show_temp_deactivated,
>> +NULL); static DEVICE_ATTR(caps, S_IRUGO, tpm_show_caps_1_2, NULL);
>> +static DEVICE_ATTR(cancel, S_IWUSR | S_IWGRP, NULL,
>> +tpm_store_cancel);
>> +
>> +static struct attribute *stm_tpm_attrs[] = {
>> +     &dev_attr_pubek.attr,
>> +     &dev_attr_pcrs.attr,
>> +     &dev_attr_enabled.attr,
>> +     &dev_attr_active.attr,
>> +     &dev_attr_owned.attr,
>> +     &dev_attr_temp_deactivated.attr,
>> +     &dev_attr_caps.attr,
>> +     &dev_attr_cancel.attr, NULL,
>> +};
>> +
>> +static struct attribute_group stm_tpm_attr_grp = {
>> +     .attrs = stm_tpm_attrs
>> +};
>> +
>> +static struct tpm_vendor_specific st_spi_tpm = {
>> +     .send = tpm_stm_spi_send,
>> +     .recv = tpm_stm_spi_recv,
>> +     .cancel = tpm_stm_spi_cancel,
>> +     .status = tpm_stm_spi_status,
>> +     .attr_group = &stm_tpm_attr_grp,
>> +     .miscdev = {.fops = &tpm_st33_spi_fops,}, };
>> +
>> +static int evaluate_latency(struct tpm_chip *chip) {
>> +     int latency = 0;
>> +     struct spi_device *dev = (struct spi_device *)chip->vendor.iobase;
>> +     struct st33zp24_platform_data *platform_data = dev->dev.platform_data;
>> +     int status = 0;
>> +     u8 data;
>> +
>> +     for (; status == 0x00 && latency < MAX_SPI_LATENCY; latency++) {
>> +             platform_data->latency = latency;
>> +             status = spi_read8_reg(chip, TPM_INTF_CAPABILITY, &data, 1);
>> +     }
>> +     return latency - 1;
>> +} /* evaluate_latency() */
>> +
>> +static int interrupts;
>> +module_param(interrupts, int, 0444); MODULE_PARM_DESC(interrupts,
>> +"Enable interrupts");
>> +
>> +static int power_mgt = 1;
>> +module_param(power_mgt, int, 0444);
>> +MODULE_PARM_DESC(power_mgt, "Power Management");
>> +
>> +/*
>> + * tpm_st33_spi_probe initialize the TPM device
>> + * @param: client, the spi_device drescription (TPM SPI description).
>> + * @param: id, the spi_device_id struct.
>> + * @return: 0 in case of success.
>> + *    -1 in other case.
>> + */
>> +static int
>> +tpm_st33_spi_probe(struct spi_device *dev) {
>> +     u32 err = 0;
>> +     u8 intmask;
>> +     struct tpm_chip *chip;
>> +     struct st33zp24_platform_data *platform_data;
>> +
>> +     /* Check SPI platform functionnalities */
>> +     if (dev == NULL) {
>> +             pr_info("dev is NULL. exiting.\n");
>> +             err = -ENODEV;
>> +             goto end;
>> +     }
>> +
>> +     chip = tpm_register_hardware(&dev->dev, &st_spi_tpm);
>> +     if (!chip) {
>> +             err = -ENODEV;
>> +             goto end;
>> +     }
>> +
>> +     /* Allocation of SPI buffers MISO and MOSI              */
>> +     /* Size is as follow:                                   */
>> +     /* Request burstcount value  = 0x800 = 2048             */
>> +     /* +                                                    */
>> +     /* Response burstcount value = 0x400 = 1024             */
>> +     /* +                                                    */
>> +     /* At least:                                            */
>> +     /* 1 byte for direction/locality                        */
>> +     /* 1 byte tpm tis register                              */
>> +     /* 2 bytes spi data length (for request only)           */
>> +     /* 2 latency bytes                                      */
>> +     /* 1 status byte                                        */
>> +     /* = 2048 + 1024 + 7                                    */
>> +     /* We reserved 2048 + 1024 + 20 in case latency byte    */
>> +     /* change                                               */
>> +     platform_data = dev->dev.platform_data;
>> +     platform_data->tpm_spi_buffer[0] =
>> +         kmalloc((TPM_BUFSIZE + (TPM_BUFSIZE / 2) +
>> +                              TPM_DIGEST_SIZE) * sizeof(u8), GFP_KERNEL);
>> +     if (platform_data->tpm_spi_buffer[0] == NULL) {
>> +             err = -ENOMEM;
>> +             goto _tpm_clean_answer;
>> +     }
>> +     platform_data->tpm_spi_buffer[1] =
>> +         kmalloc((TPM_BUFSIZE + (TPM_BUFSIZE / 2) +
>> +                              TPM_DIGEST_SIZE) * sizeof(u8) , GFP_KERNEL);
>> +     if (platform_data->tpm_spi_buffer[1] == NULL) {
>> +             err = -ENOMEM;
>> +             goto _tpm_clean_response;
>> +     }
>> +
>> +     chip->vendor.iobase = dev;
>> +
>> +     /* Default timeouts */
>> +     chip->vendor.timeout_a = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
>> +     chip->vendor.timeout_b = msecs_to_jiffies(TIS_LONG_TIMEOUT);
>> +     chip->vendor.timeout_c = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
>> +     chip->vendor.timeout_d = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
>> +     chip->vendor.locality = LOCALITY0;
>> +
>> +     if (power_mgt) {
>> +             err = gpio_request(platform_data->io_lpcpd, "TPM IO_LPCPD");
>> +             if (err)
>> +                     goto _gpio_init1;
>> +             gpio_set_value(platform_data->io_lpcpd, 1);
>> +     }
>> +
>> +     platform_data->latency = evaluate_latency(chip);
>> +     if (platform_data->latency <= 0x00) {
>> +             err = -ENODEV;
>> +             goto _gpio_init1;
>> +     }
>> +
>> +     /* Enable interrupt */
>> +     /* Register GPIO pin through generic Linux GPIO API */
>> +     if (interrupts) {
>> +             init_completion(&platform_data->irq_detection);
>> +             if (request_locality(chip) != LOCALITY0) {
>> +                     err = -ENODEV;
>> +                     goto _tpm_clean_response;
>> +             }
>> +             err = gpio_request(platform_data->io_serirq, "TPM IO_SERIRQ");
>> +             if (err)
>> +                     goto _gpio_init2;
>> +
>> +             /* Clear all existing */
>> +             clear_interruption(chip);
>> +             err = request_irq(gpio_to_irq(platform_data->io_serirq),
>> +                                                     &tpm_ioserirq_handler,
>> +                             IRQF_TRIGGER_HIGH | IRQF_SHARED,
>> +                             "TPM SERIRQ management", chip);
>> +             if (err < 0) {
>> +                     pr_info("TPM SERIRQ signals %d not available\n",
>> +                     gpio_to_irq(platform_data->io_serirq));
>> +                     goto _irq_set;
>> +             }
>> +             err = spi_read8_reg(chip, TPM_INT_ENABLE, &intmask, 1);
>> +             if (err < 0)
>> +                     goto _irq_set;
>> +
>> +             intmask |= TPM_INTF_CMD_READY_INT
>> +                     |  TPM_INTF_FIFO_AVALAIBLE_INT
>> +                     |  TPM_INTF_WAKE_UP_READY_INT
>> +                     |  TPM_INTF_LOC4SOFTRELEASE_INT
>> +                     |  TPM_INTF_LOCALITY_CHANGE_INT
>> +                     |  TPM_INTF_STS_VALID_INT
>> +                     |  TPM_INTF_DATA_AVAIL_INT;
>> +
>> +             err = spi_write8_reg(chip, TPM_INT_ENABLE, &intmask, 1);
>> +             if (err < 0)
>> +                     goto _irq_set;
>> +
>> +             intmask = TPM_GLOBAL_INT_ENABLE;
>> +             err = spi_write8_reg(chip, TPM_INT_ENABLE + 3, &intmask, 1);
>> +             if (err < 0)
>> +                     goto _irq_set;
>> +
>> +             err = spi_read8_reg(chip, TPM_INT_STATUS, &intmask, 1);
>> +             if (err < 0)
>> +                     goto _irq_set;
>> +
>> +             chip->vendor.irq = interrupts;
>> +
>> +             tpm_gen_interrupt(chip);
>> +     }
>> +
>> +     tpm_get_timeouts(chip);
>> +
>> +     /* attach chip datas to client */
>> +     spi_set_drvdata(dev, chip);
>> +     platform_data->bchipf = false;
>> +
>> +     pr_info("TPM SPI Initialized\n");
>> +     return 0;
>> +_irq_set:
>> +     free_irq(gpio_to_irq(platform_data->io_serirq), chip);
>> +_gpio_init2:
>> +     if (platform_data && interrupts)
>> +             gpio_free(platform_data->io_serirq);
>> +_gpio_init1:
>> +     if (platform_data && power_mgt)
>> +             gpio_free(platform_data->io_lpcpd);
>> +_tpm_clean_response:
>> +     tpm_remove_hardware(chip->dev);
>> +     if (platform_data->tpm_spi_buffer[1] != NULL) {
>> +             kfree(platform_data->tpm_spi_buffer[1]);
>> +             platform_data->tpm_spi_buffer[1] = NULL;
>> +     }
>> +_tpm_clean_answer:
>> +     if (platform_data->tpm_spi_buffer[0] != NULL) {
>> +             kfree(platform_data->tpm_spi_buffer[0]);
>> +             platform_data->tpm_spi_buffer[0] = NULL;
>> +     }
>> +
>> +     platform_data->bchipf = true;
>> +end:
>> +     pr_info("TPM SPI initialisation fail\n");
>> +     return err;
>> +}
>> +
>> +/*
>> + * tpm_st33_spi_remove remove the TPM device
>> + * @param: client, the spi_device drescription (TPM SPI description).
>> +             clear_bit(0, &chip->is_open);
>> + * @return: 0 in case of success.
>> + */
>> +static int tpm_st33_spi_remove(struct spi_device *client) {
>> +     struct tpm_chip *chip = (struct tpm_chip *)spi_get_drvdata(client);
>> +     struct st33zp24_platform_data *pin_infos =
>> +             ((struct spi_device
>> +*)chip->vendor.iobase)->dev.platform_data;
>> +
>> +     if (pin_infos != NULL) {
>> +             gpio_free(pin_infos->io_lpcpd);
>> +
>> +             /* Check if chip has been previously clean */
>> +             if (pin_infos->bchipf != true)
>> +                     tpm_remove_hardware(chip->dev);
>> +             if (pin_infos->tpm_spi_buffer[1] != NULL) {
>> +                     kfree(pin_infos->tpm_spi_buffer[1]);
>> +                     pin_infos->tpm_spi_buffer[1] = NULL;
>> +             }
>> +             if (pin_infos->tpm_spi_buffer[0] != NULL) {
>> +                     kfree(pin_infos->tpm_spi_buffer[0]);
>> +                     pin_infos->tpm_spi_buffer[0] = NULL;
>> +             }
>> +     }
>> +
>> +     return 0;
>> +}
>> +
>> +#ifdef CONFIG_PM_SLEEP
>> +/*
>> + * tpm_st33_spi_pm_suspend suspend the TPM device
>> + * Added: Work around when suspend and no tpm application is
>> +running, suspend
>> + * may fail because chip->data_buffer is not set (only set in
>> +tpm_open in Linux
>> + * TPM core)
>> + * @return: 0 in case of success.
>> + */
>> +static int tpm_st33_spi_pm_suspend(struct device *dev) {
>> +     struct tpm_chip *chip = dev_get_drvdata(dev);
>> +     struct st33zp24_platform_data *pin_infos = dev->platform_data;
>> +     int ret = 0;
>> +
>> +     if (power_mgt)
>> +             gpio_set_value(pin_infos->io_lpcpd, 0);
>> +     else {
>> +             if (chip->data_buffer == NULL)
>> +                     chip->data_buffer = pin_infos->tpm_spi_buffer[0];
>> +             ret = tpm_pm_suspend(dev);
>> +             }
>> +     return ret;
>> +}                            /* tpm_st33_spi_suspend() */
>> +
>> +/*
>> + * tpm_st33_spi_pm_resume resume the TPM device
>> + * @param: spi, the spi_device drescription (TPM SPI description).
>> + * @return: 0 in case of success.
>> + */
>> +static int tpm_st33_spi_pm_resume(struct device *dev) {
>> +     struct tpm_chip *chip = dev_get_drvdata(dev);
>> +     struct st33zp24_platform_data *pin_infos = dev->platform_data;
>> +
>> +     int ret = 0;
>> +     if (power_mgt) {
>> +             gpio_set_value(pin_infos->io_lpcpd, 1);
>> +             ret = wait_for_serirq_timeout(chip,
>> +                                      (chip->vendor.status(chip) &&
>> +                                       TPM_STS_VALID) == TPM_STS_VALID,
>> +                                       chip->vendor.timeout_b);
>> +     } else {
>> +             if (chip->data_buffer == NULL)
>> +                     chip->data_buffer = pin_infos->tpm_spi_buffer[0];
>> +             ret = tpm_pm_resume(dev);
>> +             if (!ret)
>> +                     tpm_do_selftest(chip);
>> +     }
>> +     return ret;
>> +} /* tpm_st33_spi_pm_resume() */
>> +#endif
>> +
>> +static SIMPLE_DEV_PM_OPS(tpm_st33_spi_ops, tpm_st33_spi_pm_suspend,
>> +      tpm_st33_spi_pm_resume);
>> +static struct spi_driver tpm_st33_spi_driver = {
>> +     .driver = {
>> +                .owner = THIS_MODULE,
>> +                .name = TPM_ST33_SPI,
>> +                .pm = &tpm_st33_spi_ops,
>> +                },
>> +     .probe = tpm_st33_spi_probe,
>> +     .remove = tpm_st33_spi_remove,
>> +};
>> +
>> +module_spi_driver(tpm_st33_spi_driver);
>> +
>> +MODULE_AUTHOR("Christophe Ricard (tpmsupport@st.com)");
>> +MODULE_DESCRIPTION("STM TPM SPI ST33 Driver");
>> +MODULE_VERSION("1.2.0"); MODULE_LICENSE("GPL");
>> diff --git a/drivers/char/tpm/tpm_spi_stm_st33.h
>> b/drivers/char/tpm/tpm_spi_stm_st33.h
>> new file mode 100644
>> index 0000000..4ae5ba6
>> --- /dev/null
>> +++ b/drivers/char/tpm/tpm_spi_stm_st33.h
>> @@ -0,0 +1,61 @@
>> +/*
>> + * STMicroelectronics TPM SPI Linux driver for TPM ST33NP18
>> + * Copyright (C) 2009, 2010  STMicroelectronics
>> + *
>> + * 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.,
>> + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
>> + *
>> + * STMicroelectronics version 1.2.0, Copyright (C) 2010
>> + * STMicroelectronics comes with ABSOLUTELY NO WARRANTY.
>> + * This is free software, and you are welcome to redistribute it
>> + * under certain conditions.
>> + *
>> + * @Author: Christophe RICARD tpmsupport@st.com
>> + *
>> + * @File: tpm_spi_stm_st33.h
>> + *
>> + * @Date: 09/15/2010
>> + */
>> +#ifndef __STM_ST33_TPM_SPI_MAIN_H__
>> +#define __STM_ST33_TPM_SPI_MAIN_H__
>> +
>> +#define TPM_ACCESS                   (0x0)
>> +#define TPM_STS                              (0x18)
>> +#define TPM_DATA_FIFO                        (0x24)
>> +#define TPM_HASH_DATA                        (0x24)
>> +#define TPM_INTF_CAPABILITY          (0x14)
>> +#define TPM_INT_STATUS                       (0x10)
>> +#define TPM_INT_ENABLE                       (0x08)
>> +
>> +#define TPM_DUMMY_BYTE                       0x00
>> +#define TPM_WRITE_DIRECTION          0x80
>> +#define TPM_HEADER_SIZE                      10
>> +#define TPM_BUFSIZE                  2048
>> +
>> +#define LOCALITY0            0
>> +
>> +#define MAX_SPI_LATENCY              15
>> +
>> +#define TPM_ST33_SPI         "st33zp24_spi"
>> +
>> +struct st33zp24_platform_data {
>> +     int io_serirq;
>> +     int io_lpcpd;
>> +     int latency;
>> +     bool bchipf;
>> +     u8 *tpm_spi_buffer[2]; /* 0 Request 1 Response */
>> +     struct completion irq_detection; };
>> +
>> +#endif /* __STM_ST33_TPM_SPI_MAIN_H__ */
>> --
>> 1.7.1
>>
>>
>> ---------------------------------------------------------------------
>> --------- Learn Graph Databases - Download FREE O'Reilly Book "Graph
>> Databases" is the definitive new guide to graph databases and their
>> applications. This 200-page book is written by three acclaimed
>> leaders in the field. The early access version is available now.
>> Download your free book today! http://p.sf.net/sfu/neotech_d2d_may
>> _______________________________________________
>> tpmdd-devel mailing list
>> tpmdd-devel@lists.sourceforge.net
>> https://lists.sourceforge.net/lists/listinfo/tpmdd-devel

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

* Re: [tpmdd-devel] [PATCH 1/1] TPM: STMicroelectronics st33 driver SPI
  2013-05-13 15:30     ` Mathias LEBLANC
@ 2013-05-13 15:56       ` Kent Yoder
  0 siblings, 0 replies; 21+ messages in thread
From: Kent Yoder @ 2013-05-13 15:56 UTC (permalink / raw)
  To: Mathias LEBLANC
  Cc: Kent Yoder, Rajiv Andrade, Marcel Selhorst, Sirrix AG,
	TPM Device Driver List, Jean-Luc BLANC, linux-kernel

Hi Mathias,

On Mon, May 13, 2013 at 10:30 AM, Mathias LEBLANC
<Mathias.LEBLANC@st.com> wrote:
> Hi Kent,
>
> I have tried different compilation command and I haven't these errors.
> What's the command you do to generate these warning?

  I used the smatch tool, which you can get from sourceforge, with
some instructions, here: http://smatch.sourceforge.net/.

Kent

> However, these warning will be corrected in the next patch and the others too.
>
> Regards,
>
> Mathias Leblanc
>
> -----Original Message-----
> From: Kent Yoder [mailto:shpedoikal@gmail.com]
> Sent: 10 May, 2013 17:07
> To: Mathias LEBLANC
> Cc: Kent Yoder; Rajiv Andrade; Marcel Selhorst; Sirrix AG; TPM Device Driver List; Jean-Luc BLANC; linux-kernel@vger.kernel.org
> Subject: Re: [tpmdd-devel] [PATCH 1/1] TPM: STMicroelectronics st33 driver SPI
>
> On Fri, May 10, 2013 at 8:38 AM, Kent Yoder <shpedoikal@gmail.com> wrote:
>> Hi Mathias,
>
> Sorry, this note got sent before I was done somehow...
>
> A few compile warnings left:
>
> drivers/char/tpm/tpm_spi_stm_st33.c: In function 'tpm_stm_spi_status':
> drivers/char/tpm/tpm_spi_stm_st33.c:289:2: error: 'data' is used uninitialized in this function [-Werror=uninitialized]
> drivers/char/tpm/tpm_spi_stm_st33.c: In function 'get_burstcount':
> drivers/char/tpm/tpm_spi_stm_st33.c:386:12: error: 'temp' is used uninitialized in this function [-Werror=uninitialized]
> drivers/char/tpm/tpm_spi_stm_st33.c: In function 'check_locality':
> drivers/char/tpm/tpm_spi_stm_st33.c:305:22: error: 'data' may be used uninitialized in this function [-Werror=uninitialized]
>
> And smatch warnings:
>
> drivers/char/tpm/tpm_spi_stm_st33.c:247 wait_for_serirq_timeout()
> error: 'wait_for_completion_interruptible_timeout()' returns negative and 'status' is unsigned
> drivers/char/tpm/tpm_spi_stm_st33.c:382 get_burstcount() warn:
> unsigned 'status' is never less than zero.
> drivers/char/tpm/tpm_spi_stm_st33.c:388 get_burstcount() warn:
> unsigned 'status' is never less than zero.
> drivers/char/tpm/tpm_spi_stm_st33.c:459 recv_data() warn: unsigned 'status' is never less than zero.
> drivers/char/tpm/tpm_spi_stm_st33.c:508 tpm_stm_spi_send() warn:
> unsigned 'ret' is never less than zero.
> drivers/char/tpm/tpm_spi_stm_st33.c:526 tpm_stm_spi_send() warn:
> unsigned 'ret' is never less than zero.
> drivers/char/tpm/tpm_spi_stm_st33.c:549 tpm_stm_spi_send() warn:
> unsigned 'ret' is never less than zero.
> drivers/char/tpm/tpm_spi_stm_st33.c:772 tpm_st33_spi_probe() warn:
> unsigned 'err' is never less than zero.
> drivers/char/tpm/tpm_spi_stm_st33.c:778 tpm_st33_spi_probe() warn:
> unsigned 'err' is never less than zero.
> drivers/char/tpm/tpm_spi_stm_st33.c:790 tpm_st33_spi_probe() warn:
> unsigned 'err' is never less than zero.
> drivers/char/tpm/tpm_spi_stm_st33.c:795 tpm_st33_spi_probe() warn:
> unsigned 'err' is never less than zero.
> drivers/char/tpm/tpm_spi_stm_st33.c:799 tpm_st33_spi_probe() warn:
> unsigned 'err' is never less than zero.
> drivers/char/tpm/tpm_spi_stm_st33.c:818 tpm_st33_spi_probe() warn:
> variable dereferenced before check 'platform_data' (see line 717)
>
> And again this patch is getting its last 2 lines clipped in tpm_spi_stm_st33.c.  Please look below for where to put the ChangeLog info.
>
> Just as an FYI, I probably am not going to reply to repeat anything I've said above for future submissions.  We're really going around in circles here. :-/
>
> Kent
>
>> On Tue, May 07, 2013 at 12:16:05PM +0200, Matthias Leblanc wrote:
>>> From: Mathias Leblanc <mathias.leblanc@st.com>
>>>
>>>  * STMicroelectronics version 1.2.0, Copyright (C) 2010
>>>  * 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 SPI, in menuconfig or .config choose the tpm driver on device -->
>>> tpm and activate the protocol of your choice before compiling the
>>> kernel.
>>> The driver will be accessible from within Linux.
>>>
>>> Tested on linux x86/x64 and beagleboard REV B & XM REV C
>>>
>>> Changelog:
>>>
>>> date: 07-05-2013
>>> Remove unused variables
>>> Remove tpm_st33_spi_exit function that cause compilation error
>>>
>>> Signed-off-by: Mathias Leblanc <mathias.leblanc@st.com>
>>> ---
>>
>>   ChangeLog goes in here.
>>
>>>  drivers/char/tpm/Kconfig            |   10 +
>>>  drivers/char/tpm/Makefile           |    1 +
>>>  drivers/char/tpm/tpm_spi_stm_st33.c |  939 +++++++++++++++++++++++++++++++++++
>>>  drivers/char/tpm/tpm_spi_stm_st33.h |   61 +++
>>>  4 files changed, 1011 insertions(+), 0 deletions(-)  create mode
>>> 100644 drivers/char/tpm/tpm_spi_stm_st33.c
>>>  create mode 100644 drivers/char/tpm/tpm_spi_stm_st33.h
>>>
>>> diff --git a/drivers/char/tpm/Kconfig b/drivers/char/tpm/Kconfig
>>> index dbfd564..2fc1254 100644
>>> --- a/drivers/char/tpm/Kconfig
>>> +++ b/drivers/char/tpm/Kconfig
>>> @@ -91,4 +91,14 @@ config TCG_ST33_I2C
>>>          To compile this driver as a module, choose M here; the module will be
>>>          called tpm_stm_st33_i2c.
>>>
>>> +config TCG_ST33_SPI
>>> +       tristate "STMicroelectronics ST33 SPI"
>>> +       depends on SPI
>>> +       depends on GPIOLIB
>>> +       ---help---
>>> +       If you have a TPM security chip from STMicroelectronics working with
>>> +       an SPI bus say Yes and it will be accessible from within Linux.
>>> +       To compile this driver as a module, choose M here; the module will be
>>> +       called tpm_stm_st33_spi.
>>> +
>>>  endif # TCG_TPM
>>> diff --git a/drivers/char/tpm/Makefile b/drivers/char/tpm/Makefile
>>> index a3736c9..a1db3dd 100644
>>> --- a/drivers/char/tpm/Makefile
>>> +++ b/drivers/char/tpm/Makefile
>>> @@ -18,3 +18,4 @@ obj-$(CONFIG_TCG_ATMEL) += tpm_atmel.o
>>>  obj-$(CONFIG_TCG_INFINEON) += tpm_infineon.o
>>>  obj-$(CONFIG_TCG_IBMVTPM) += tpm_ibmvtpm.o
>>>  obj-$(CONFIG_TCG_ST33_I2C) += tpm_i2c_stm_st33.o
>>> +obj-$(CONFIG_TCG_ST33_SPI) += tpm_spi_stm_st33.o
>>> diff --git a/drivers/char/tpm/tpm_spi_stm_st33.c
>>> b/drivers/char/tpm/tpm_spi_stm_st33.c
>>> new file mode 100644
>>> index 0000000..9279dc0
>>> --- /dev/null
>>> +++ b/drivers/char/tpm/tpm_spi_stm_st33.c
>>> @@ -0,0 +1,939 @@
>>> +/*
>>> + * STMicroelectronics TPM SPI Linux driver for TPM ST33ZP24
>>> + * Copyright (C) 2009, 2010  STMicroelectronics
>>> + *
>>> + * 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.,
>>> + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
>>> + *
>>> + * STMicroelectronics version 1.2.0, Copyright (C) 2010
>>> + * STMicroelectronics comes with ABSOLUTELY NO WARRANTY.
>>> + * This is free software, and you are welcome to redistribute it
>>> + * under certain conditions.
>>> + *
>>> + * @Author: Christophe RICARD tpmsupport@st.com
>>> + *
>>> + * @File: tpm_stm_st33_spi.c
>>> + *
>>> + * @Synopsis:
>>> + *   09/15/2010:     First shot driver tpm_tis driver for lpc is
>>> + *                   used as model.
>>> + */
>>> +
>>> +#include <linux/kernel.h>
>>> +#include <linux/init.h>
>>> +#include <linux/spi/spi.h>
>>> +#include <linux/wait.h>
>>> +#include <linux/string.h>
>>> +#include <linux/interrupt.h>
>>> +#include <linux/spinlock.h>
>>> +#include <linux/sysfs.h>
>>> +#include <linux/gpio.h>
>>> +#include <linux/uaccess.h>
>>> +#include <linux/slab.h>
>>> +#include <linux/sched.h>
>>> +
>>> +#include "tpm.h"
>>> +
>>> +#include "tpm_spi_stm_st33.h"
>>> +
>>> +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 */
>>> +};
>>> +
>>> +
>>> +static inline int spi_read_write(struct spi_device *spi,
>>> +      struct spi_transfer xfer) {
>>> +     struct spi_message msg;
>>> +     spi_message_init(&msg);
>>> +     spi_message_add_tail(&xfer, &msg);
>>> +     return spi_sync(spi, &msg);
>>> +}
>>> +
>>> +/*
>>> + * spi_write8_reg
>>> + * Send byte to the TIS register according to the ST33ZP24 SPI protocol.
>>> + * @param: tpm, 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: should be zero if success else a negative error code.
>>> + */
>>> +static int spi_write8_reg(struct tpm_chip *tpm, u8 tpm_register,
>>> +                   u8 *tpm_data, u16 tpm_size) {
>>> +     u8 data;
>>> +     int total_length = 0, nbr_dummy_bytes;
>>> +     int value = 0;
>>> +     struct spi_device *dev = (struct spi_device *)tpm->vendor.iobase;
>>> +     struct st33zp24_platform_data *platform_data = dev->dev.platform_data;
>>> +     u8 *data_buffer = platform_data->tpm_spi_buffer[0];
>>> +     struct spi_transfer xfer = {
>>> +             .tx_buf  = data_buffer,
>>> +             .rx_buf  = platform_data->tpm_spi_buffer[1],
>>> +     };
>>> +
>>> +     u8 latency = platform_data->latency + tpm_size + 1;
>>> +
>>> +     /* Pre-Header */
>>> +     data = TPM_WRITE_DIRECTION | tpm->vendor.locality;
>>> +     memcpy(data_buffer + total_length, &data, sizeof(data));
>>> +     total_length++;
>>> +     data = tpm_register;
>>> +     memcpy(data_buffer + total_length, &data, sizeof(data));
>>> +     total_length++;
>>> +
>>> +     if (tpm_size > 0 &&
>>> +          (tpm_register == TPM_DATA_FIFO ||
>>> +           tpm_register == TPM_HASH_DATA)) {
>>> +             data = (tpm_size >> 8) & 0x00ff;
>>> +             data_buffer[total_length++] = data;
>>> +             data = tpm_size & 0x00ff;
>>> +             data_buffer[total_length++] = data;
>>> +             latency -= 2;
>>> +     }
>>> +
>>> +     memcpy(&data_buffer[total_length], tpm_data, tpm_size);
>>> +     total_length += tpm_size;
>>> +
>>> +     nbr_dummy_bytes = platform_data->latency + 1;
>>> +     memset(&data_buffer[total_length], TPM_DUMMY_BYTE,
>>> +                                      platform_data->latency + 1);
>>> +
>>> +     xfer.len = total_length;
>>> +
>>> +     value = spi_read_write(dev, xfer);
>>> +
>>> +     if (value == 0) {
>>> +             nbr_dummy_bytes = total_length - 1 - nbr_dummy_bytes;
>>> +             while (nbr_dummy_bytes < total_length &&
>>> +                      ((u8 *)xfer.rx_buf)[nbr_dummy_bytes] == 0)
>>> +                                     nbr_dummy_bytes++;
>>> +
>>> +             if (((u8 *)xfer.rx_buf)[nbr_dummy_bytes] != 0)
>>> +                     value = ((u8 *)xfer.rx_buf)[nbr_dummy_bytes];
>>> +     }
>>> +
>>> +     return value;
>>> +} /* spi_write8_reg() */
>>> +
>>> +/*
>>> + * spi_read8_reg
>>> + * Recv byte from the TIS register according to the ST33ZP24 SPI 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: should be zero if success else a negative error code.
>>> + */
>>> +static u8 spi_read8_reg(struct tpm_chip *tpm, u8 tpm_register,
>>> +                 u8 *tpm_data, u16 tpm_size) {
>>> +     u8 data;
>>> +     int total_length = 0, nbr_dummy_bytes;
>>> +     int value = 0;
>>> +     struct spi_device *dev = (struct spi_device *)tpm->vendor.iobase;
>>> +     struct st33zp24_platform_data *platform_data = dev->dev.platform_data;
>>> +     u8 *data_buffer = platform_data->tpm_spi_buffer[0];
>>> +     struct spi_transfer xfer = {
>>> +             .tx_buf  = data_buffer,
>>> +             .rx_buf  = platform_data->tpm_spi_buffer[1],
>>> +     };
>>> +
>>> +     /* Pre-Header */
>>> +
>>> +     data = tpm->vendor.locality;
>>> +     memcpy(data_buffer + total_length, &data, sizeof(data));
>>> +     total_length++;
>>> +     data = tpm_register;
>>> +     memcpy(data_buffer + total_length, &data, sizeof(data));
>>> +     total_length++;
>>> +
>>> +     nbr_dummy_bytes = platform_data->latency + 1;
>>> +     memset(&data_buffer[total_length], TPM_DUMMY_BYTE,
>>> +                                      platform_data->latency + 1);
>>> +
>>> +     xfer.len = total_length;
>>> +
>>> +     /* header + status byte + size of the data + status byte */
>>> +     value = spi_read_write(dev, xfer);
>>> +
>>> +     if (tpm_size > 0 && value == 0) {
>>> +             nbr_dummy_bytes = 2;
>>> +             for (; nbr_dummy_bytes < total_length &&
>>> +                      ((u8 *)xfer.rx_buf)[nbr_dummy_bytes] == 0;
>>> +                      nbr_dummy_bytes++)
>>> +                     ;
>>> +
>>> +             if (nbr_dummy_bytes + 1 < total_length) {
>>> +                     value = ((u8 *)xfer.rx_buf)[nbr_dummy_bytes];
>>> +
>>> +                     if (tpm_size > 0)
>>> +                             memcpy(tpm_data, xfer.rx_buf +
>>> +                                                      nbr_dummy_bytes + 1,
>>> +                                                              tpm_size);
>>> +             }
>>> +     }
>>> +     return value;
>>> +} /* spi_read8_reg() */
>>> +
>>> +/*
>>> + * clear_interruption
>>> + * clear the TPM interrupt register.
>>> + * @param: tpm, the chip description  */ static void
>>> +clear_interruption(struct tpm_chip *tpm) {
>>> +     u8 interrupt;
>>> +     spi_read8_reg(tpm, TPM_INT_STATUS, &interrupt, 1);
>>> +     /* Clear interrupts handled with TPM_EOI */
>>> +     spi_write8_reg(tpm, TPM_INT_STATUS, &interrupt, 1);
>>> +     spi_read8_reg(tpm, TPM_INT_STATUS, &interrupt, 1); } /*
>>> +clear_interruption() */
>>> +
>>> +/*
>>> + * _wait_for_serirq_timeout
>>> + * @param: tpm, the chip description
>>> + * @param: timeout, the timeout of the interrupt
>>> + * @return: the status of the interruption.
>>> + */
>>> +unsigned long wait_for_serirq_timeout(struct tpm_chip *chip, bool condition,
>>> +                              unsigned long timeout) {
>>> +     unsigned long status;
>>> +     struct spi_device *client;
>>> +     struct st33zp24_platform_data *pin_infos;
>>> +
>>> +     client = (struct spi_device *)chip->vendor.iobase;
>>> +     pin_infos = client->dev.platform_data;
>>> +
>>> +     status = wait_for_completion_interruptible_timeout(
>>> +                             &pin_infos->irq_detection, timeout);
>>> +     if (status > 0)
>>> +             enable_irq(gpio_to_irq(pin_infos->io_serirq));
>>> +     gpio_direction_input(pin_infos->io_serirq);
>>> +
>>> +     if (!status) {
>>> +             status = -EBUSY;
>>> +             goto wait_end;
>>> +     }
>>> +     clear_interruption(chip);
>>> +     if (condition)
>>> +             status = 1;
>>> +
>>> +wait_end:
>>> +     return status;
>>> +}
>>> +
>>> +
>>> +/*
>>> + * tpm_stm_spi_cancel, cancel is not implemented.
>>> + * @param: chip, the tpm chip description as specified in
>>> + * driver/char/tpm/tpm.h.
>>> + */
>>> +static void tpm_stm_spi_cancel(struct tpm_chip *chip) {
>>> +     u8 data = TPM_STS_COMMAND_READY;
>>> +
>>> +     /* this causes the current command to be aborted */
>>> +     spi_write8_reg(chip, TPM_STS, &data, 1); } /*
>>> +tpm_stm_spi_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_spi_status(struct tpm_chip *chip) {
>>> +     u8 data;
>>> +
>>> +     spi_read8_reg(chip, TPM_STS, &data, 1);
>>> +     return data;
>>> +} /* tpm_stm_spi_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 = spi_read8_reg(chip, TPM_ACCESS, &data, 1);
>>> +     if (status && (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 stop;
>>> +     long rc;
>>> +     u8 data;
>>> +
>>> +     /* Check locality */
>>> +     if (check_locality(chip) == chip->vendor.locality)
>>> +             return chip->vendor.locality;
>>> +
>>> +     /* Request locality */
>>> +     data = TPM_ACCESS_REQUEST_USE;
>>> +     rc = spi_write8_reg(chip, TPM_ACCESS, &data, 1);
>>> +     if (rc < 0)
>>> +             goto end;
>>> +
>>> +     /* wait for burstcount */
>>> +     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 {
>>> +             stop = jiffies + chip->vendor.timeout_a;
>>> +             do {
>>> +                     if (check_locality(chip) >= 0)
>>> +                             return chip->vendor.locality;
>>> +                     msleep(TPM_TIMEOUT);
>>> +             } while (time_before(jiffies, stop));
>>> +     }
>>> +end:
>>> +     return -EACCES;
>>> +} /* request_locality() */
>>> +
>>> +/*
>>> + * release_locality release the active locality
>>> + * @param: chip, the tpm chip description.
>>> + */
>>> +static void release_locality(struct tpm_chip *chip) {
>>> +     u8 data;
>>> +
>>> +     data = TPM_ACCESS_ACTIVE_LOCALITY;
>>> +     spi_write8_reg(chip, TPM_ACCESS, &data, 1); } /*
>>> +release_locality()*/
>>> +
>>> +/*
>>> + * 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 stop;
>>> +     u32 burstcnt, status;
>>> +     u8 tpm_reg, temp;
>>> +
>>> +     /* wait for burstcount */
>>> +     /* which timeout value, spec has 2 answers (c & d) */
>>> +     stop = jiffies + chip->vendor.timeout_d;
>>> +     do {
>>> +             tpm_reg = TPM_STS + 1;
>>> +             status = spi_read8_reg(chip, tpm_reg, &temp, 1);
>>> +             if (status < 0)
>>> +                     goto end;
>>> +
>>> +             tpm_reg = tpm_reg + 1;
>>> +             burstcnt = temp;
>>> +             status = spi_read8_reg(chip, tpm_reg, &temp, 1);
>>> +             if (status < 0)
>>> +                     goto end;
>>> +
>>> +             burstcnt |= temp << 8;
>>> +             if (burstcnt)
>>> +                     return burstcnt;
>>> +             msleep(TPM_TIMEOUT);
>>> +     } while (time_before(jiffies, stop));
>>> +
>>> +end:
>>> +     return -EBUSY;
>>> +} /* get_burstcount() */
>>> +
>>> +/*
>>> + * wait_for_stat wait for a TPM_STS value
>>> + * @param: chip, the tpm chip description
>>> + * @param: mask, the value mask to wait
>>> + * @param: timeout, the timeout
>>> + * @param: queue, the wait queue.
>>> + * @return: the tpm status, 0 if success, -ETIME if timeout is reached.
>>> + */
>>> +static int wait_for_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout,
>>> +                      wait_queue_head_t *queue) {
>>> +     unsigned long stop;
>>> +     long rc;
>>> +     u8 status;
>>> +
>>> +     /* check current status */
>>> +     status = tpm_stm_spi_status(chip);
>>> +     if (!chip->vendor.irq && (status & mask) == mask)
>>> +             return 0;
>>> +
>>> +     if (chip->vendor.irq) {
>>> +             rc = wait_for_serirq_timeout(chip, ((tpm_stm_spi_status
>>> +                                                 (chip) & mask) ==
>>> +                                                  mask), timeout);
>>> +             if (rc > 0)
>>> +                     return 0;
>>> +     } else {
>>> +             stop = jiffies + timeout;
>>> +             do {
>>> +                     msleep(TPM_TIMEOUT);
>>> +                     status = tpm_stm_spi_status(chip);
>>> +                     if ((status & mask) == mask)
>>> +                             return 0;
>>> +             } while (time_before(jiffies, stop));
>>> +     }
>>> +     return -ETIME;
>>> +} /* wait_for_stat() */
>>> +
>>> +/*
>>> + * 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 tpm status, 0 if success, -ETIME if timeout is reached.
>>> + */
>>> +static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count) {
>>> +     u32 size = 0, burstcnt, status, len;
>>> +
>>> +     while (size < count &&
>>> +            wait_for_stat(chip,
>>> +                          TPM_STS_DATA_AVAIL | TPM_STS_VALID,
>>> +                          chip->vendor.timeout_c,
>>> +                          &chip->vendor.read_queue)
>>> +                                             == 0) {
>>> +             burstcnt = get_burstcount(chip);
>>> +             len = min_t(int, burstcnt, count - size);
>>> +             status = spi_read8_reg(chip, TPM_DATA_FIFO, buf + size, len);
>>> +             if (status < 0)
>>> +                     return status;
>>> +
>>> +
>>> +             size += len;
>>> +     }
>>> +     return size;
>>> +}
>>> +
>>> +/*
>>> + * tpm_ioserirq_handler the serirq irq handler
>>> + * @param: irq, the tpm chip description
>>> + * @param: dev_id, the description of the chip
>>> + * @return: the status of the handler.
>>> + */
>>> +static irqreturn_t tpm_ioserirq_handler(int irq, void *dev_id) {
>>> +     struct tpm_chip *chip = dev_id;
>>> +     struct spi_device *client;
>>> +     struct st33zp24_platform_data *pin_infos;
>>> +
>>> +     disable_irq_nosync(irq);
>>> +
>>> +     client = (struct spi_device *)chip->vendor.iobase;
>>> +     pin_infos = client->dev.platform_data;
>>> +
>>> +     complete(&pin_infos->irq_detection);
>>> +     return IRQ_HANDLED;
>>> +} /* tpm_ioserirq_handler() */
>>> +
>>> +/*
>>> + * tpm_stm_spi_send send TPM commands through the SPI bus.
>>> + * @param: chip, the tpm chip description
>>> + * @param: buf, the tpm command buffer
>>> + * @param: len, the tpm command size
>>> + * @return: should be zero if success else the negative error code.
>>> + */
>>> +static int tpm_stm_spi_send(struct tpm_chip *chip, unsigned char *buf,
>>> +                         size_t len) {
>>> +     u32 ret = 0, status, burstcnt = 0, i, size = 0;
>>> +     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_spi_status(chip);
>>> +     if ((status & TPM_STS_COMMAND_READY) == 0) {
>>> +             tpm_stm_spi_cancel(chip);
>>> +             if (wait_for_stat
>>> +                 (chip, TPM_STS_COMMAND_READY, chip->vendor.timeout_b,
>>> +                  &chip->vendor.int_queue) < 0) {
>>> +                     ret = -ETIME;
>>> +                     goto out_err;
>>> +             }
>>> +     }
>>> +
>>> +     for (i = 0; i < len - 1;) {
>>> +             burstcnt = get_burstcount(chip);
>>> +             size = min_t(int, len - i - 1, burstcnt);
>>> +             ret = spi_write8_reg(chip, TPM_DATA_FIFO, buf, size);
>>> +             if (ret < 0)
>>> +                     goto out_err;
>>> +             i += size;
>>> +     }
>>> +
>>> +     status = tpm_stm_spi_status(chip);
>>> +     if ((status & TPM_STS_DATA_EXPECT) == 0) {
>>> +             ret = -EIO;
>>> +             goto out_err;
>>> +     }
>>> +
>>> +     /* write last byte */
>>> +     spi_write8_reg(chip, TPM_DATA_FIFO, buf + len - 1, 1);
>>> +
>>> +     status = tpm_stm_spi_status(chip);
>>> +     if ((status & TPM_STS_DATA_EXPECT) != 0) {
>>> +             ret = -EIO;
>>> +             goto out_err;
>>> +     }
>>> +
>>> +     /* go and do it */
>>> +     data = TPM_STS_GO;
>>> +     ret = spi_write8_reg(chip, TPM_STS, &data, 1);
>>> +     if (ret < 0)
>>> +             goto out_err;
>>> +
>>> +     return len;
>>> +out_err:
>>> +     tpm_stm_spi_cancel(chip);
>>> +     release_locality(chip);
>>> +     return ret;
>>> +}
>>> +
>>> +/*
>>> + * tpm_stm_spi_recv received TPM response through the SPI bus.
>>> + * @param: chip, the tpm chip description
>>> + * @param: buf, the tpm command buffer
>>> + * @param: len, the tpm command size
>>> +* @return: should be zero if success else the negative error code.
>>> + */
>>> +static int tpm_stm_spi_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);
>>> +
>>> +     /* read first 10 bytes, including tag, paramsize, and result */
>>> +     if (size < TPM_HEADER_SIZE) {
>>> +             dev_err(chip->dev, "Unable to read header\n");
>>> +             goto out;
>>> +     }
>>> +
>>> +     expected = be32_to_cpu(*(__be32 *)(buf + 2));
>>> +     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;
>>> +}
>>> +
>>> +
>>> +static const struct file_operations tpm_st33_spi_fops = {
>>> +     .owner = THIS_MODULE,
>>> +     .llseek = no_llseek,
>>> +     .read = tpm_read,
>>> +     .write = tpm_write,
>>> +     .open = tpm_open,
>>> +     .release = tpm_release,
>>> +};
>>> +
>>> +static DEVICE_ATTR(pubek, S_IRUGO, tpm_show_pubek, NULL); static
>>> +DEVICE_ATTR(pcrs, S_IRUGO, tpm_show_pcrs, NULL); static
>>> +DEVICE_ATTR(enabled, S_IRUGO, tpm_show_enabled, NULL); static
>>> +DEVICE_ATTR(active, S_IRUGO, tpm_show_active, NULL); static
>>> +DEVICE_ATTR(owned, S_IRUGO, tpm_show_owned, NULL); static
>>> +DEVICE_ATTR(temp_deactivated, S_IRUGO, tpm_show_temp_deactivated,
>>> +NULL); static DEVICE_ATTR(caps, S_IRUGO, tpm_show_caps_1_2, NULL);
>>> +static DEVICE_ATTR(cancel, S_IWUSR | S_IWGRP, NULL,
>>> +tpm_store_cancel);
>>> +
>>> +static struct attribute *stm_tpm_attrs[] = {
>>> +     &dev_attr_pubek.attr,
>>> +     &dev_attr_pcrs.attr,
>>> +     &dev_attr_enabled.attr,
>>> +     &dev_attr_active.attr,
>>> +     &dev_attr_owned.attr,
>>> +     &dev_attr_temp_deactivated.attr,
>>> +     &dev_attr_caps.attr,
>>> +     &dev_attr_cancel.attr, NULL,
>>> +};
>>> +
>>> +static struct attribute_group stm_tpm_attr_grp = {
>>> +     .attrs = stm_tpm_attrs
>>> +};
>>> +
>>> +static struct tpm_vendor_specific st_spi_tpm = {
>>> +     .send = tpm_stm_spi_send,
>>> +     .recv = tpm_stm_spi_recv,
>>> +     .cancel = tpm_stm_spi_cancel,
>>> +     .status = tpm_stm_spi_status,
>>> +     .attr_group = &stm_tpm_attr_grp,
>>> +     .miscdev = {.fops = &tpm_st33_spi_fops,}, };
>>> +
>>> +static int evaluate_latency(struct tpm_chip *chip) {
>>> +     int latency = 0;
>>> +     struct spi_device *dev = (struct spi_device *)chip->vendor.iobase;
>>> +     struct st33zp24_platform_data *platform_data = dev->dev.platform_data;
>>> +     int status = 0;
>>> +     u8 data;
>>> +
>>> +     for (; status == 0x00 && latency < MAX_SPI_LATENCY; latency++) {
>>> +             platform_data->latency = latency;
>>> +             status = spi_read8_reg(chip, TPM_INTF_CAPABILITY, &data, 1);
>>> +     }
>>> +     return latency - 1;
>>> +} /* evaluate_latency() */
>>> +
>>> +static int interrupts;
>>> +module_param(interrupts, int, 0444); MODULE_PARM_DESC(interrupts,
>>> +"Enable interrupts");
>>> +
>>> +static int power_mgt = 1;
>>> +module_param(power_mgt, int, 0444);
>>> +MODULE_PARM_DESC(power_mgt, "Power Management");
>>> +
>>> +/*
>>> + * tpm_st33_spi_probe initialize the TPM device
>>> + * @param: client, the spi_device drescription (TPM SPI description).
>>> + * @param: id, the spi_device_id struct.
>>> + * @return: 0 in case of success.
>>> + *    -1 in other case.
>>> + */
>>> +static int
>>> +tpm_st33_spi_probe(struct spi_device *dev) {
>>> +     u32 err = 0;
>>> +     u8 intmask;
>>> +     struct tpm_chip *chip;
>>> +     struct st33zp24_platform_data *platform_data;
>>> +
>>> +     /* Check SPI platform functionnalities */
>>> +     if (dev == NULL) {
>>> +             pr_info("dev is NULL. exiting.\n");
>>> +             err = -ENODEV;
>>> +             goto end;
>>> +     }
>>> +
>>> +     chip = tpm_register_hardware(&dev->dev, &st_spi_tpm);
>>> +     if (!chip) {
>>> +             err = -ENODEV;
>>> +             goto end;
>>> +     }
>>> +
>>> +     /* Allocation of SPI buffers MISO and MOSI              */
>>> +     /* Size is as follow:                                   */
>>> +     /* Request burstcount value  = 0x800 = 2048             */
>>> +     /* +                                                    */
>>> +     /* Response burstcount value = 0x400 = 1024             */
>>> +     /* +                                                    */
>>> +     /* At least:                                            */
>>> +     /* 1 byte for direction/locality                        */
>>> +     /* 1 byte tpm tis register                              */
>>> +     /* 2 bytes spi data length (for request only)           */
>>> +     /* 2 latency bytes                                      */
>>> +     /* 1 status byte                                        */
>>> +     /* = 2048 + 1024 + 7                                    */
>>> +     /* We reserved 2048 + 1024 + 20 in case latency byte    */
>>> +     /* change                                               */
>>> +     platform_data = dev->dev.platform_data;
>>> +     platform_data->tpm_spi_buffer[0] =
>>> +         kmalloc((TPM_BUFSIZE + (TPM_BUFSIZE / 2) +
>>> +                              TPM_DIGEST_SIZE) * sizeof(u8), GFP_KERNEL);
>>> +     if (platform_data->tpm_spi_buffer[0] == NULL) {
>>> +             err = -ENOMEM;
>>> +             goto _tpm_clean_answer;
>>> +     }
>>> +     platform_data->tpm_spi_buffer[1] =
>>> +         kmalloc((TPM_BUFSIZE + (TPM_BUFSIZE / 2) +
>>> +                              TPM_DIGEST_SIZE) * sizeof(u8) , GFP_KERNEL);
>>> +     if (platform_data->tpm_spi_buffer[1] == NULL) {
>>> +             err = -ENOMEM;
>>> +             goto _tpm_clean_response;
>>> +     }
>>> +
>>> +     chip->vendor.iobase = dev;
>>> +
>>> +     /* Default timeouts */
>>> +     chip->vendor.timeout_a = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
>>> +     chip->vendor.timeout_b = msecs_to_jiffies(TIS_LONG_TIMEOUT);
>>> +     chip->vendor.timeout_c = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
>>> +     chip->vendor.timeout_d = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
>>> +     chip->vendor.locality = LOCALITY0;
>>> +
>>> +     if (power_mgt) {
>>> +             err = gpio_request(platform_data->io_lpcpd, "TPM IO_LPCPD");
>>> +             if (err)
>>> +                     goto _gpio_init1;
>>> +             gpio_set_value(platform_data->io_lpcpd, 1);
>>> +     }
>>> +
>>> +     platform_data->latency = evaluate_latency(chip);
>>> +     if (platform_data->latency <= 0x00) {
>>> +             err = -ENODEV;
>>> +             goto _gpio_init1;
>>> +     }
>>> +
>>> +     /* Enable interrupt */
>>> +     /* Register GPIO pin through generic Linux GPIO API */
>>> +     if (interrupts) {
>>> +             init_completion(&platform_data->irq_detection);
>>> +             if (request_locality(chip) != LOCALITY0) {
>>> +                     err = -ENODEV;
>>> +                     goto _tpm_clean_response;
>>> +             }
>>> +             err = gpio_request(platform_data->io_serirq, "TPM IO_SERIRQ");
>>> +             if (err)
>>> +                     goto _gpio_init2;
>>> +
>>> +             /* Clear all existing */
>>> +             clear_interruption(chip);
>>> +             err = request_irq(gpio_to_irq(platform_data->io_serirq),
>>> +                                                     &tpm_ioserirq_handler,
>>> +                             IRQF_TRIGGER_HIGH | IRQF_SHARED,
>>> +                             "TPM SERIRQ management", chip);
>>> +             if (err < 0) {
>>> +                     pr_info("TPM SERIRQ signals %d not available\n",
>>> +                     gpio_to_irq(platform_data->io_serirq));
>>> +                     goto _irq_set;
>>> +             }
>>> +             err = spi_read8_reg(chip, TPM_INT_ENABLE, &intmask, 1);
>>> +             if (err < 0)
>>> +                     goto _irq_set;
>>> +
>>> +             intmask |= TPM_INTF_CMD_READY_INT
>>> +                     |  TPM_INTF_FIFO_AVALAIBLE_INT
>>> +                     |  TPM_INTF_WAKE_UP_READY_INT
>>> +                     |  TPM_INTF_LOC4SOFTRELEASE_INT
>>> +                     |  TPM_INTF_LOCALITY_CHANGE_INT
>>> +                     |  TPM_INTF_STS_VALID_INT
>>> +                     |  TPM_INTF_DATA_AVAIL_INT;
>>> +
>>> +             err = spi_write8_reg(chip, TPM_INT_ENABLE, &intmask, 1);
>>> +             if (err < 0)
>>> +                     goto _irq_set;
>>> +
>>> +             intmask = TPM_GLOBAL_INT_ENABLE;
>>> +             err = spi_write8_reg(chip, TPM_INT_ENABLE + 3, &intmask, 1);
>>> +             if (err < 0)
>>> +                     goto _irq_set;
>>> +
>>> +             err = spi_read8_reg(chip, TPM_INT_STATUS, &intmask, 1);
>>> +             if (err < 0)
>>> +                     goto _irq_set;
>>> +
>>> +             chip->vendor.irq = interrupts;
>>> +
>>> +             tpm_gen_interrupt(chip);
>>> +     }
>>> +
>>> +     tpm_get_timeouts(chip);
>>> +
>>> +     /* attach chip datas to client */
>>> +     spi_set_drvdata(dev, chip);
>>> +     platform_data->bchipf = false;
>>> +
>>> +     pr_info("TPM SPI Initialized\n");
>>> +     return 0;
>>> +_irq_set:
>>> +     free_irq(gpio_to_irq(platform_data->io_serirq), chip);
>>> +_gpio_init2:
>>> +     if (platform_data && interrupts)
>>> +             gpio_free(platform_data->io_serirq);
>>> +_gpio_init1:
>>> +     if (platform_data && power_mgt)
>>> +             gpio_free(platform_data->io_lpcpd);
>>> +_tpm_clean_response:
>>> +     tpm_remove_hardware(chip->dev);
>>> +     if (platform_data->tpm_spi_buffer[1] != NULL) {
>>> +             kfree(platform_data->tpm_spi_buffer[1]);
>>> +             platform_data->tpm_spi_buffer[1] = NULL;
>>> +     }
>>> +_tpm_clean_answer:
>>> +     if (platform_data->tpm_spi_buffer[0] != NULL) {
>>> +             kfree(platform_data->tpm_spi_buffer[0]);
>>> +             platform_data->tpm_spi_buffer[0] = NULL;
>>> +     }
>>> +
>>> +     platform_data->bchipf = true;
>>> +end:
>>> +     pr_info("TPM SPI initialisation fail\n");
>>> +     return err;
>>> +}
>>> +
>>> +/*
>>> + * tpm_st33_spi_remove remove the TPM device
>>> + * @param: client, the spi_device drescription (TPM SPI description).
>>> +             clear_bit(0, &chip->is_open);
>>> + * @return: 0 in case of success.
>>> + */
>>> +static int tpm_st33_spi_remove(struct spi_device *client) {
>>> +     struct tpm_chip *chip = (struct tpm_chip *)spi_get_drvdata(client);
>>> +     struct st33zp24_platform_data *pin_infos =
>>> +             ((struct spi_device
>>> +*)chip->vendor.iobase)->dev.platform_data;
>>> +
>>> +     if (pin_infos != NULL) {
>>> +             gpio_free(pin_infos->io_lpcpd);
>>> +
>>> +             /* Check if chip has been previously clean */
>>> +             if (pin_infos->bchipf != true)
>>> +                     tpm_remove_hardware(chip->dev);
>>> +             if (pin_infos->tpm_spi_buffer[1] != NULL) {
>>> +                     kfree(pin_infos->tpm_spi_buffer[1]);
>>> +                     pin_infos->tpm_spi_buffer[1] = NULL;
>>> +             }
>>> +             if (pin_infos->tpm_spi_buffer[0] != NULL) {
>>> +                     kfree(pin_infos->tpm_spi_buffer[0]);
>>> +                     pin_infos->tpm_spi_buffer[0] = NULL;
>>> +             }
>>> +     }
>>> +
>>> +     return 0;
>>> +}
>>> +
>>> +#ifdef CONFIG_PM_SLEEP
>>> +/*
>>> + * tpm_st33_spi_pm_suspend suspend the TPM device
>>> + * Added: Work around when suspend and no tpm application is
>>> +running, suspend
>>> + * may fail because chip->data_buffer is not set (only set in
>>> +tpm_open in Linux
>>> + * TPM core)
>>> + * @return: 0 in case of success.
>>> + */
>>> +static int tpm_st33_spi_pm_suspend(struct device *dev) {
>>> +     struct tpm_chip *chip = dev_get_drvdata(dev);
>>> +     struct st33zp24_platform_data *pin_infos = dev->platform_data;
>>> +     int ret = 0;
>>> +
>>> +     if (power_mgt)
>>> +             gpio_set_value(pin_infos->io_lpcpd, 0);
>>> +     else {
>>> +             if (chip->data_buffer == NULL)
>>> +                     chip->data_buffer = pin_infos->tpm_spi_buffer[0];
>>> +             ret = tpm_pm_suspend(dev);
>>> +             }
>>> +     return ret;
>>> +}                            /* tpm_st33_spi_suspend() */
>>> +
>>> +/*
>>> + * tpm_st33_spi_pm_resume resume the TPM device
>>> + * @param: spi, the spi_device drescription (TPM SPI description).
>>> + * @return: 0 in case of success.
>>> + */
>>> +static int tpm_st33_spi_pm_resume(struct device *dev) {
>>> +     struct tpm_chip *chip = dev_get_drvdata(dev);
>>> +     struct st33zp24_platform_data *pin_infos = dev->platform_data;
>>> +
>>> +     int ret = 0;
>>> +     if (power_mgt) {
>>> +             gpio_set_value(pin_infos->io_lpcpd, 1);
>>> +             ret = wait_for_serirq_timeout(chip,
>>> +                                      (chip->vendor.status(chip) &&
>>> +                                       TPM_STS_VALID) == TPM_STS_VALID,
>>> +                                       chip->vendor.timeout_b);
>>> +     } else {
>>> +             if (chip->data_buffer == NULL)
>>> +                     chip->data_buffer = pin_infos->tpm_spi_buffer[0];
>>> +             ret = tpm_pm_resume(dev);
>>> +             if (!ret)
>>> +                     tpm_do_selftest(chip);
>>> +     }
>>> +     return ret;
>>> +} /* tpm_st33_spi_pm_resume() */
>>> +#endif
>>> +
>>> +static SIMPLE_DEV_PM_OPS(tpm_st33_spi_ops, tpm_st33_spi_pm_suspend,
>>> +      tpm_st33_spi_pm_resume);
>>> +static struct spi_driver tpm_st33_spi_driver = {
>>> +     .driver = {
>>> +                .owner = THIS_MODULE,
>>> +                .name = TPM_ST33_SPI,
>>> +                .pm = &tpm_st33_spi_ops,
>>> +                },
>>> +     .probe = tpm_st33_spi_probe,
>>> +     .remove = tpm_st33_spi_remove,
>>> +};
>>> +
>>> +module_spi_driver(tpm_st33_spi_driver);
>>> +
>>> +MODULE_AUTHOR("Christophe Ricard (tpmsupport@st.com)");
>>> +MODULE_DESCRIPTION("STM TPM SPI ST33 Driver");
>>> +MODULE_VERSION("1.2.0"); MODULE_LICENSE("GPL");
>>> diff --git a/drivers/char/tpm/tpm_spi_stm_st33.h
>>> b/drivers/char/tpm/tpm_spi_stm_st33.h
>>> new file mode 100644
>>> index 0000000..4ae5ba6
>>> --- /dev/null
>>> +++ b/drivers/char/tpm/tpm_spi_stm_st33.h
>>> @@ -0,0 +1,61 @@
>>> +/*
>>> + * STMicroelectronics TPM SPI Linux driver for TPM ST33NP18
>>> + * Copyright (C) 2009, 2010  STMicroelectronics
>>> + *
>>> + * 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.,
>>> + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
>>> + *
>>> + * STMicroelectronics version 1.2.0, Copyright (C) 2010
>>> + * STMicroelectronics comes with ABSOLUTELY NO WARRANTY.
>>> + * This is free software, and you are welcome to redistribute it
>>> + * under certain conditions.
>>> + *
>>> + * @Author: Christophe RICARD tpmsupport@st.com
>>> + *
>>> + * @File: tpm_spi_stm_st33.h
>>> + *
>>> + * @Date: 09/15/2010
>>> + */
>>> +#ifndef __STM_ST33_TPM_SPI_MAIN_H__
>>> +#define __STM_ST33_TPM_SPI_MAIN_H__
>>> +
>>> +#define TPM_ACCESS                   (0x0)
>>> +#define TPM_STS                              (0x18)
>>> +#define TPM_DATA_FIFO                        (0x24)
>>> +#define TPM_HASH_DATA                        (0x24)
>>> +#define TPM_INTF_CAPABILITY          (0x14)
>>> +#define TPM_INT_STATUS                       (0x10)
>>> +#define TPM_INT_ENABLE                       (0x08)
>>> +
>>> +#define TPM_DUMMY_BYTE                       0x00
>>> +#define TPM_WRITE_DIRECTION          0x80
>>> +#define TPM_HEADER_SIZE                      10
>>> +#define TPM_BUFSIZE                  2048
>>> +
>>> +#define LOCALITY0            0
>>> +
>>> +#define MAX_SPI_LATENCY              15
>>> +
>>> +#define TPM_ST33_SPI         "st33zp24_spi"
>>> +
>>> +struct st33zp24_platform_data {
>>> +     int io_serirq;
>>> +     int io_lpcpd;
>>> +     int latency;
>>> +     bool bchipf;
>>> +     u8 *tpm_spi_buffer[2]; /* 0 Request 1 Response */
>>> +     struct completion irq_detection; };
>>> +
>>> +#endif /* __STM_ST33_TPM_SPI_MAIN_H__ */
>>> --
>>> 1.7.1
>>>
>>>
>>> ---------------------------------------------------------------------
>>> --------- Learn Graph Databases - Download FREE O'Reilly Book "Graph
>>> Databases" is the definitive new guide to graph databases and their
>>> applications. This 200-page book is written by three acclaimed
>>> leaders in the field. The early access version is available now.
>>> Download your free book today! http://p.sf.net/sfu/neotech_d2d_may
>>> _______________________________________________
>>> tpmdd-devel mailing list
>>> tpmdd-devel@lists.sourceforge.net
>>> https://lists.sourceforge.net/lists/listinfo/tpmdd-devel

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

* [PATCH 1/1] TPM: STMicroelectronics st33 driver SPI
@ 2013-07-25 15:20 Mathias leblanc
  0 siblings, 0 replies; 21+ messages in thread
From: Mathias leblanc @ 2013-07-25 15:20 UTC (permalink / raw)
  To: Kent Yoder, Rajiv Andrade, Marcel Selhorst, Sirrix AG, tpmdd-devel
  Cc: linux-kernel, Mathias Leblanc

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 SPI, in menuconfig or .config choose the tpm driver on
device --> tpm and activate the protocol of your choice before compiling
the kernel.
The driver will be accessible from within Linux.

Tested on linux x86/x64 and beagleboard REV B & XM REV C

Signed-off-by: Mathias Leblanc <mathias.leblanc@st.com>
---
 drivers/char/tpm/Kconfig            |   10 +
 drivers/char/tpm/Makefile           |    1 +
 drivers/char/tpm/tpm_spi_stm_st33.c |  912 +++++++++++++++++++++++++++++++++++
 drivers/char/tpm/tpm_spi_stm_st33.h |   61 +++
 4 files changed, 984 insertions(+), 0 deletions(-)
 create mode 100644 drivers/char/tpm/tpm_spi_stm_st33.c
 create mode 100644 drivers/char/tpm/tpm_spi_stm_st33.h

diff --git a/drivers/char/tpm/Kconfig b/drivers/char/tpm/Kconfig
index dbfd564..2fc1254 100644
--- a/drivers/char/tpm/Kconfig
+++ b/drivers/char/tpm/Kconfig
@@ -91,4 +91,14 @@ config TCG_ST33_I2C
         To compile this driver as a module, choose M here; the module will be
         called tpm_stm_st33_i2c.
 
+config TCG_ST33_SPI
+       tristate "STMicroelectronics ST33 SPI"
+       depends on SPI
+       depends on GPIOLIB
+       ---help---
+       If you have a TPM security chip from STMicroelectronics working with
+       an SPI bus say Yes and it will be accessible from within Linux.
+       To compile this driver as a module, choose M here; the module will be
+       called tpm_stm_st33_spi.
+
 endif # TCG_TPM
diff --git a/drivers/char/tpm/Makefile b/drivers/char/tpm/Makefile
index a3736c9..a1db3dd 100644
--- a/drivers/char/tpm/Makefile
+++ b/drivers/char/tpm/Makefile
@@ -18,3 +18,4 @@ obj-$(CONFIG_TCG_ATMEL) += tpm_atmel.o
 obj-$(CONFIG_TCG_INFINEON) += tpm_infineon.o
 obj-$(CONFIG_TCG_IBMVTPM) += tpm_ibmvtpm.o
 obj-$(CONFIG_TCG_ST33_I2C) += tpm_i2c_stm_st33.o
+obj-$(CONFIG_TCG_ST33_SPI) += tpm_spi_stm_st33.o
diff --git a/drivers/char/tpm/tpm_spi_stm_st33.c b/drivers/char/tpm/tpm_spi_stm_st33.c
new file mode 100644
index 0000000..730424e
--- /dev/null
+++ b/drivers/char/tpm/tpm_spi_stm_st33.c
@@ -0,0 +1,912 @@
+/*
+ * STMicroelectronics TPM SPI Linux driver for TPM ST33ZP24
+ * Copyright (C) 2009, 2013  STMicroelectronics
+ *
+ * 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.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * STMicroelectronics version 1.2.0, Copyright (C) 2010
+ * STMicroelectronics comes with ABSOLUTELY NO WARRANTY.
+ * This is free software, and you are welcome to redistribute it
+ * under certain conditions.
+ *
+ * @Author: Christophe RICARD tpmsupport@st.com
+ *
+ * @File: tpm_stm_st33_spi.c
+ *
+ * @Synopsis:
+ *	09/15/2010: First shot driver tpm_tis driver for lpc is used as model.
+ */
+
+#include <linux/kernel.h>
+#include <linux/init.h>
+#include <linux/spi/spi.h>
+#include <linux/wait.h>
+#include <linux/string.h>
+#include <linux/interrupt.h>
+#include <linux/spinlock.h>
+#include <linux/sysfs.h>
+#include <linux/gpio.h>
+#include <linux/uaccess.h>
+#include <linux/slab.h>
+#include <linux/sched.h>
+#include "tpm.h"
+#include "tpm_spi_stm_st33.h"
+
+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 = 0x80,
+	TPM_INTF_FIFO_AVALAIBLE_INT = 0x40,
+	TPM_INTF_WAKE_UP_READY_INT = 0x20,
+	TPM_INTF_LOC4SOFTRELEASE_INT = 0x08,
+	TPM_INTF_LOCALITY_CHANGE_INT = 0x04,
+	TPM_INTF_STS_VALID_INT = 0x02,
+	TPM_INTF_DATA_AVAIL_INT = 0x01,
+};
+
+enum tis_defaults {
+	TIS_SHORT_TIMEOUT = 750,	/* ms */
+	TIS_LONG_TIMEOUT = 2000,	/* 2 sec */
+};
+
+
+static int interrupts;
+module_param(interrupts, int, 0444);
+MODULE_PARM_DESC(interrupts, "Enable interrupts");
+
+static int power_mgt = 1;
+module_param(power_mgt, int, 0444);
+MODULE_PARM_DESC(power_mgt, "Power Management");
+
+/*
+ * spi_write8_reg
+ * Send byte to the TIS register according to the ST33ZP24 SPI protocol.
+ * @param: tpm, 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: should be zero if success else a negative error code.
+ */
+static int spi_write8_reg(struct tpm_chip *tpm, u8 tpm_register,
+		      u8 *tpm_data, u16 tpm_size)
+{
+	u8 data = 0;
+	int total_length = 0, nbr_dummy_bytes = 0;
+	int value = 0;
+	struct spi_device *dev =
+		 (struct spi_device __force *)tpm->vendor.iobase;
+	struct st33zp24_platform_data *platform_data = dev->dev.platform_data;
+	u8 *data_buffer = platform_data->tpm_spi_buffer[0];
+	struct spi_transfer xfer = {
+		.tx_buf	 = data_buffer,
+		.rx_buf	 = platform_data->tpm_spi_buffer[1],
+	};
+
+	/* Pre-Header */
+	data = TPM_WRITE_DIRECTION | tpm->vendor.locality;
+	memcpy(data_buffer + total_length, &data, sizeof(data));
+	total_length++;
+	data = tpm_register;
+	memcpy(data_buffer + total_length, &data, sizeof(data));
+	total_length++;
+
+	if (tpm_size > 0 &&
+	 (tpm_register == TPM_DATA_FIFO || tpm_register == TPM_HASH_DATA)) {
+			data_buffer[total_length++] = tpm_size >> 8;
+			data_buffer[total_length++] = tpm_size;
+	}
+
+	memcpy(&data_buffer[total_length], tpm_data, tpm_size);
+	total_length += tpm_size;
+
+	nbr_dummy_bytes = platform_data->latency + 1;
+	memset(&data_buffer[total_length], TPM_DUMMY_BYTE,
+					 platform_data->latency + 1);
+
+	xfer.len = total_length;
+
+	value = spi_sync_transfer(dev, &xfer, 1);
+
+	if (value == 0) {
+		nbr_dummy_bytes = total_length - 1 - nbr_dummy_bytes;
+		while (nbr_dummy_bytes < total_length &&
+			 ((u8 *)xfer.rx_buf)[nbr_dummy_bytes] == 0)
+					nbr_dummy_bytes++;
+
+		if (((u8 *)xfer.rx_buf)[nbr_dummy_bytes] != 0)
+			value = ((u8 *)xfer.rx_buf)[nbr_dummy_bytes];
+	}
+
+	return value;
+} /* spi_write8_reg() */
+
+/*
+ * spi_read8_reg
+ * Recv byte from the TIS register according to the ST33ZP24 SPI 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: should be zero if success else a negative error code.
+ */
+static u8 spi_read8_reg(struct tpm_chip *tpm, u8 tpm_register,
+		    u8 *tpm_data, u16 tpm_size)
+{
+	u8 data = 0;
+	int total_length = 0, nbr_dummy_bytes;
+	int value = 0;
+	struct spi_device *dev =
+		 (struct spi_device __force *)tpm->vendor.iobase;
+	struct st33zp24_platform_data *platform_data = dev->dev.platform_data;
+	u8 *data_buffer = platform_data->tpm_spi_buffer[0];
+	struct spi_transfer xfer = {
+		.tx_buf	 = data_buffer,
+		.rx_buf	 = platform_data->tpm_spi_buffer[1],
+	};
+
+	/* Pre-Header */
+	data = tpm->vendor.locality;
+	memcpy(data_buffer + total_length, &data, sizeof(data));
+	total_length++;
+	data = tpm_register;
+	memcpy(data_buffer + total_length, &data, sizeof(data));
+	total_length++;
+
+	nbr_dummy_bytes = platform_data->latency + 1;
+	memset(&data_buffer[total_length], TPM_DUMMY_BYTE,
+		 platform_data->latency + 1);
+
+	xfer.len = total_length;
+
+	/* header + status byte + size of the data + status byte */
+	value = spi_sync_transfer(dev, &xfer, 1);
+
+	if (tpm_size > 0 && value == 0) {
+		nbr_dummy_bytes = 2;
+		while (nbr_dummy_bytes < total_length &&
+			 ((u8 *)xfer.rx_buf)[nbr_dummy_bytes] == 0)
+				nbr_dummy_bytes++;
+
+		if (nbr_dummy_bytes + 1 < total_length) {
+			value = ((u8 *)xfer.rx_buf)[nbr_dummy_bytes];
+
+			if (tpm_size > 0)
+				memcpy(tpm_data, xfer.rx_buf +
+							 nbr_dummy_bytes + 1,
+								 tpm_size);
+		}
+	}
+	return value;
+} /* spi_read8_reg() */
+
+/*
+ * clear_interruption
+ * clear the TPM interrupt register.
+ * @param: tpm, the chip description
+ */
+static void clear_interruption(struct tpm_chip *tpm)
+{
+	u8 interrupt;
+	spi_read8_reg(tpm, TPM_INT_STATUS, &interrupt, 1);
+	/* Clear interrupts handled with TPM_EOI */
+	spi_write8_reg(tpm, TPM_INT_STATUS, &interrupt, 1);
+	spi_read8_reg(tpm, TPM_INT_STATUS, &interrupt, 1);
+} /* clear_interruption() */
+
+/*
+ * _wait_for_serirq_timeout
+ * @param: tpm, the chip description
+ * @param: timeout, the timeout of the interrupt
+ * @return: the status of the interruption.
+ */
+static unsigned long wait_for_serirq_timeout(struct tpm_chip *chip,
+	 bool condition, unsigned long timeout)
+{
+	long status = 0;
+	struct spi_device *client;
+	struct st33zp24_platform_data *pin_infos;
+
+	client = (struct spi_device __force *)chip->vendor.iobase;
+	pin_infos = client->dev.platform_data;
+
+	status = wait_for_completion_interruptible_timeout(
+				&pin_infos->irq_detection, timeout);
+	if (status > 0)
+		enable_irq(gpio_to_irq(pin_infos->io_serirq));
+	gpio_direction_input(pin_infos->io_serirq);
+
+	if (!status)
+		return -EBUSY;
+
+	clear_interruption(chip);
+	if (condition)
+		status = 1;
+return status;
+}
+
+
+/*
+ * tpm_stm_spi_cancel, cancel is not implemented.
+ * @param: chip, the tpm chip description as specified in
+ * driver/char/tpm/tpm.h.
+ */
+static void tpm_stm_spi_cancel(struct tpm_chip *chip)
+{
+	u8 data = TPM_STS_COMMAND_READY;
+
+	/* this causes the current command to be aborted */
+	spi_write8_reg(chip, TPM_STS, &data, 1);
+} /* tpm_stm_spi_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_spi_status(struct tpm_chip *chip)
+{
+	u8 data = 0;
+
+	spi_read8_reg(chip, TPM_STS, &data, 1);
+	return data;
+} /* tpm_stm_spi_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 = 0;
+	u8 status;
+
+	status = spi_read8_reg(chip, TPM_ACCESS, &data, 1);
+	if (status && (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 stop;
+	long rc;
+	u8 data = 0;
+
+	/* Check locality */
+	if (check_locality(chip) == chip->vendor.locality)
+		return chip->vendor.locality;
+
+	/* Request locality */
+	data = TPM_ACCESS_REQUEST_USE;
+	rc = spi_write8_reg(chip, TPM_ACCESS, &data, 1);
+	if (rc < 0)
+		return -EACCES;
+
+	/* wait for burstcount */
+	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 {
+		stop = jiffies + chip->vendor.timeout_a;
+		do {
+			if (check_locality(chip) >= 0)
+				return chip->vendor.locality;
+			msleep(TPM_TIMEOUT);
+		} while (time_before(jiffies, stop));
+	}
+return -EACCES;
+} /* request_locality() */
+
+/*
+ * release_locality release the active locality
+ * @param: chip, the tpm chip description.
+ */
+static void release_locality(struct tpm_chip *chip)
+{
+	u8 data = 0;
+
+	data = TPM_ACCESS_ACTIVE_LOCALITY;
+	spi_write8_reg(chip, TPM_ACCESS, &data, 1);
+} /* release_locality()*/
+
+/*
+ * 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 stop;
+	u32 burstcnt;
+	u8 tpm_reg, temp = 0;
+	long status = 0;
+
+	/* wait for burstcount */
+	/* which timeout value, spec has 2 answers (c & d) */
+	stop = jiffies + chip->vendor.timeout_d;
+	do {
+		tpm_reg = TPM_STS + 1;
+		status = spi_read8_reg(chip, tpm_reg, &temp, 1);
+		if (status < 0)
+			return -EBUSY;
+
+		burstcnt = temp;
+		status = spi_read8_reg(chip, ++tpm_reg, &temp, 1);
+		if (status < 0)
+			return -EBUSY;
+
+		burstcnt |= temp << 8;
+		if (burstcnt)
+			return burstcnt;
+		msleep(TPM_TIMEOUT);
+	} while (time_before(jiffies, stop));
+return -EBUSY;
+} /* get_burstcount() */
+
+/*
+ * wait_for_stat wait for a TPM_STS value
+ * @param: chip, the tpm chip description
+ * @param: mask, the value mask to wait
+ * @param: timeout, the timeout
+ * @param: queue, the wait queue.
+ * @return: the tpm status, 0 if success, -ETIME if timeout is reached.
+ */
+static int wait_for_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout,
+			 wait_queue_head_t *queue)
+{
+	unsigned long stop;
+	long rc;
+	u8 status;
+
+	/* check current status */
+	status = tpm_stm_spi_status(chip);
+	if (!chip->vendor.irq && (status & mask) == mask)
+		return 0;
+
+	if (chip->vendor.irq) {
+		rc = wait_for_serirq_timeout(chip, ((tpm_stm_spi_status
+						    (chip) & mask) ==
+						     mask), timeout);
+		if (rc > 0)
+			return 0;
+	} else {
+		stop = jiffies + timeout;
+		do {
+			msleep(TPM_TIMEOUT);
+			status = tpm_stm_spi_status(chip);
+			if ((status & mask) == mask)
+				return 0;
+		} while (time_before(jiffies, stop));
+	}
+return -ETIME;
+} /* wait_for_stat() */
+
+/*
+ * 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 tpm status, 0 if success, -ETIME if timeout is reached.
+ */
+static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count)
+{
+	u32 size = 0, burstcnt, len;
+	long status = 0;
+
+	while (size < count &&
+	       wait_for_stat(chip,
+			     TPM_STS_DATA_AVAIL | TPM_STS_VALID,
+			     chip->vendor.timeout_c,
+			     &chip->vendor.read_queue)
+						== 0) {
+		burstcnt = get_burstcount(chip);
+		len = min_t(int, burstcnt, count - size);
+		status = spi_read8_reg(chip, TPM_DATA_FIFO, buf + size, len);
+		if (status < 0)
+			return status;
+
+
+		size += len;
+	}
+return size;
+}
+
+/*
+ * tpm_ioserirq_handler the serirq irq handler
+ * @param: irq, the tpm chip description
+ * @param: dev_id, the description of the chip
+ * @return: the status of the handler.
+ */
+static irqreturn_t tpm_ioserirq_handler(int irq, void *dev_id)
+{
+	struct tpm_chip *chip = dev_id;
+	struct spi_device *client;
+	struct st33zp24_platform_data *pin_infos;
+
+	disable_irq_nosync(irq);
+
+	client = (struct spi_device __force *)chip->vendor.iobase;
+	pin_infos = client->dev.platform_data;
+
+	complete(&pin_infos->irq_detection);
+	return IRQ_HANDLED;
+} /* tpm_ioserirq_handler() */
+
+/*
+ * tpm_stm_spi_send send TPM commands through the SPI bus.
+ * @param: chip, the tpm chip description
+ * @param: buf, the tpm command buffer
+ * @param: len, the tpm command size
+ * @return: should be zero if success else the negative error code.
+ */
+static int tpm_stm_spi_send(struct tpm_chip *chip, unsigned char *buf,
+			    size_t len)
+{
+	u32 burstcnt = 0, i, size = 0;
+	u8 data = 0;
+	long status = 0, ret = 0;
+
+	if (chip == NULL)
+		return -EINVAL;
+	if (len < TPM_HEADER_SIZE)
+		return -EINVAL;
+
+	ret = request_locality(chip);
+	if (ret < 0)
+		return ret;
+
+	status = tpm_stm_spi_status(chip);
+	if ((status & TPM_STS_COMMAND_READY) == 0) {
+		tpm_stm_spi_cancel(chip);
+		if (wait_for_stat
+		    (chip, TPM_STS_COMMAND_READY, chip->vendor.timeout_b,
+		     &chip->vendor.int_queue) < 0) {
+			ret = -ETIME;
+			goto out_err;
+		}
+	}
+
+	for (i = 0; i < len - 1;) {
+		burstcnt = get_burstcount(chip);
+		size = min_t(int, len - i - 1, burstcnt);
+		ret = spi_write8_reg(chip, TPM_DATA_FIFO, buf, size);
+		if (ret < 0)
+			goto out_err;
+		i += size;
+	}
+
+	status = tpm_stm_spi_status(chip);
+	if ((status & TPM_STS_DATA_EXPECT) == 0) {
+		ret = -EIO;
+		goto out_err;
+	}
+
+	/* write last byte */
+	spi_write8_reg(chip, TPM_DATA_FIFO, buf + len - 1, 1);
+
+	status = tpm_stm_spi_status(chip);
+	if ((status & TPM_STS_DATA_EXPECT) != 0) {
+		ret = -EIO;
+		goto out_err;
+	}
+
+	/* go and do it */
+	data = TPM_STS_GO;
+	ret = spi_write8_reg(chip, TPM_STS, &data, 1);
+	if (ret < 0)
+		goto out_err;
+
+	return len;
+out_err:
+	tpm_stm_spi_cancel(chip);
+	release_locality(chip);
+	return ret;
+}
+
+/*
+ * tpm_stm_spi_recv received TPM response through the SPI bus.
+ * @param: chip, the tpm chip description
+ * @param: buf, the tpm command buffer
+ * @param: len, the tpm command size
+* @return: should be zero if success else the negative error code.
+ */
+static int tpm_stm_spi_recv(struct tpm_chip *chip, unsigned char *buf,
+			    size_t count)
+{
+	int size = 0;
+	int expected;
+
+	if (chip == NULL)
+		return -EINVAL;
+	if (count < TPM_HEADER_SIZE) {
+		size = -EIO;
+		goto out;
+	}
+
+	size = recv_data(chip, buf, TPM_HEADER_SIZE);
+
+	/* read first 10 bytes, including tag, paramsize, and result */
+	if (size < TPM_HEADER_SIZE) {
+		dev_err(chip->dev, "Unable to read header\n");
+		goto out;
+	}
+
+	expected = be32_to_cpu(*(__be32 *)(buf + 2));
+	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;
+}
+
+static const struct file_operations tpm_st33_spi_fops = {
+	.owner = THIS_MODULE,
+	.llseek = no_llseek,
+	.read = tpm_read,
+	.write = tpm_write,
+	.open = tpm_open,
+	.release = tpm_release,
+};
+
+static DEVICE_ATTR(pubek, S_IRUGO, tpm_show_pubek, NULL);
+static DEVICE_ATTR(pcrs, S_IRUGO, tpm_show_pcrs, NULL);
+static DEVICE_ATTR(enabled, S_IRUGO, tpm_show_enabled, NULL);
+static DEVICE_ATTR(active, S_IRUGO, tpm_show_active, NULL);
+static DEVICE_ATTR(owned, S_IRUGO, tpm_show_owned, NULL);
+static DEVICE_ATTR(temp_deactivated, S_IRUGO, tpm_show_temp_deactivated, NULL);
+static DEVICE_ATTR(caps, S_IRUGO, tpm_show_caps_1_2, NULL);
+static DEVICE_ATTR(cancel, S_IWUSR | S_IWGRP, NULL, tpm_store_cancel);
+
+static struct attribute *stm_tpm_attrs[] = {
+	&dev_attr_pubek.attr,
+	&dev_attr_pcrs.attr,
+	&dev_attr_enabled.attr,
+	&dev_attr_active.attr,
+	&dev_attr_owned.attr,
+	&dev_attr_temp_deactivated.attr,
+	&dev_attr_caps.attr,
+	&dev_attr_cancel.attr, NULL,
+};
+
+static struct attribute_group stm_tpm_attr_grp = {
+	.attrs = stm_tpm_attrs
+};
+
+static struct tpm_vendor_specific st_spi_tpm = {
+	.send = tpm_stm_spi_send,
+	.recv = tpm_stm_spi_recv,
+	.cancel = tpm_stm_spi_cancel,
+	.status = tpm_stm_spi_status,
+	.attr_group = &stm_tpm_attr_grp,
+	.miscdev = {.fops = &tpm_st33_spi_fops,},
+};
+
+static int evaluate_latency(struct tpm_chip *chip)
+{
+	int latency = 0;
+	struct spi_device *dev =
+		 (struct spi_device __force *)chip->vendor.iobase;
+	struct st33zp24_platform_data *platform_data = dev->dev.platform_data;
+	int status = 0;
+	u8 data = 0;
+
+	while (status == 0x00 && latency < MAX_SPI_LATENCY) {
+		platform_data->latency = latency;
+		status = spi_read8_reg(chip, TPM_INTF_CAPABILITY, &data, 1);
+		latency++;
+	}
+	return latency - 1;
+} /* evaluate_latency() */
+
+/*
+ * tpm_st33_spi_probe initialize the TPM device
+ * @param: client, the spi_device drescription (TPM SPI description).
+ * @param: id, the spi_device_id struct.
+ * @return: 0 in case of success.
+ *	 -1 in other case.
+ */
+static int
+tpm_st33_spi_probe(struct spi_device *dev)
+{
+	long err = 0;
+	u8 intmask;
+	struct tpm_chip *chip;
+	struct st33zp24_platform_data *platform_data;
+
+	/* Check SPI platform functionnalities */
+	if (dev == NULL) {
+		pr_info("dev is NULL. exiting.\n");
+		err = -ENODEV;
+		goto end;
+	}
+
+	chip = tpm_register_hardware(&dev->dev, &st_spi_tpm);
+	if (!chip) {
+		err = -ENODEV;
+		goto end;
+	}
+
+	platform_data = dev->dev.platform_data;
+
+	if (!platform_data)
+		return -ENODEV;
+
+	platform_data->tpm_spi_buffer[0] =
+	kmalloc((TPM_BUFSIZE + (TPM_BUFSIZE / 2) +
+		 TPM_DIGEST_SIZE) * sizeof(u8), GFP_KERNEL);
+
+	if (platform_data->tpm_spi_buffer[0] == NULL) {
+		err = -ENOMEM;
+		goto _tpm_clean_answer;
+	}
+
+	platform_data->tpm_spi_buffer[1] =
+	    kmalloc((TPM_BUFSIZE + (TPM_BUFSIZE / 2) +
+				 TPM_DIGEST_SIZE) * sizeof(u8) , GFP_KERNEL);
+
+	if (platform_data->tpm_spi_buffer[1] == NULL) {
+		err = -ENOMEM;
+		goto _tpm_clean_response;
+	}
+
+	chip->vendor.iobase = (void __iomem *)dev;
+
+	/* Default timeouts */
+	chip->vendor.timeout_a = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
+	chip->vendor.timeout_b = msecs_to_jiffies(TIS_LONG_TIMEOUT);
+	chip->vendor.timeout_c = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
+	chip->vendor.timeout_d = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
+	chip->vendor.locality = LOCALITY0;
+
+	if (power_mgt) {
+		err = gpio_request(platform_data->io_lpcpd, "TPM IO_LPCPD");
+		if (err)
+			goto _gpio_init1;
+		gpio_set_value(platform_data->io_lpcpd, 1);
+	}
+
+	platform_data->latency = evaluate_latency(chip);
+	if (platform_data->latency <= 0x00) {
+		err = -ENODEV;
+		goto _gpio_init1;
+	}
+
+	/* Enable interrupt */
+	/* Register GPIO pin through generic Linux GPIO API */
+	if (interrupts) {
+		init_completion(&platform_data->irq_detection);
+		if (request_locality(chip) != LOCALITY0) {
+			err = -ENODEV;
+			goto _tpm_clean_response;
+		}
+		err = gpio_request(platform_data->io_serirq, "TPM IO_SERIRQ");
+		if (err)
+			goto _gpio_init2;
+
+		/* Clear all existing */
+		clear_interruption(chip);
+		err = request_irq(gpio_to_irq(platform_data->io_serirq),
+							&tpm_ioserirq_handler,
+				IRQF_TRIGGER_HIGH | IRQF_SHARED,
+				"TPM SERIRQ management", chip);
+		if (err < 0) {
+			pr_info("TPM SERIRQ signals %d not available\n",
+				gpio_to_irq(platform_data->io_serirq));
+			goto _irq_set;
+		}
+		err = spi_read8_reg(chip, TPM_INT_ENABLE, &intmask, 1);
+		if (err < 0)
+			goto _irq_set;
+
+		intmask |= TPM_INTF_CMD_READY_INT
+			|  TPM_INTF_FIFO_AVALAIBLE_INT
+			|  TPM_INTF_WAKE_UP_READY_INT
+			|  TPM_INTF_LOC4SOFTRELEASE_INT
+			|  TPM_INTF_LOCALITY_CHANGE_INT
+			|  TPM_INTF_STS_VALID_INT
+			|  TPM_INTF_DATA_AVAIL_INT;
+
+		err = spi_write8_reg(chip, TPM_INT_ENABLE, &intmask, 1);
+		if (err < 0)
+			goto _irq_set;
+
+		intmask = TPM_GLOBAL_INT_ENABLE;
+		err = spi_write8_reg(chip, TPM_INT_ENABLE + 3, &intmask, 1);
+		if (err < 0)
+			goto _irq_set;
+
+		err = spi_read8_reg(chip, TPM_INT_STATUS, &intmask, 1);
+		if (err < 0)
+			goto _irq_set;
+
+		chip->vendor.irq = interrupts;
+
+		tpm_gen_interrupt(chip);
+	}
+
+	tpm_get_timeouts(chip);
+
+	/* attach chip datas to client */
+	spi_set_drvdata(dev, chip);
+	platform_data->bchipf = false;
+
+	pr_info("TPM SPI Initialized\n");
+	return 0;
+_irq_set:
+	free_irq(gpio_to_irq(platform_data->io_serirq), chip);
+_gpio_init2:
+	if (platform_data && interrupts)
+		gpio_free(platform_data->io_serirq);
+_gpio_init1:
+	if (platform_data && power_mgt)
+		gpio_free(platform_data->io_lpcpd);
+_tpm_clean_response:
+	tpm_remove_hardware(chip->dev);
+	if (platform_data->tpm_spi_buffer[1] != NULL) {
+		kfree(platform_data->tpm_spi_buffer[1]);
+		platform_data->tpm_spi_buffer[1] = NULL;
+	}
+_tpm_clean_answer:
+	if (platform_data->tpm_spi_buffer[0] != NULL) {
+		kfree(platform_data->tpm_spi_buffer[0]);
+		platform_data->tpm_spi_buffer[0] = NULL;
+	}
+
+	platform_data->bchipf = true;
+end:
+	pr_info("TPM SPI initialisation fail\n");
+	return err;
+}
+
+/*
+ * tpm_st33_spi_remove remove the TPM device
+ * @param: client, the spi_device drescription (TPM SPI description).
+		clear_bit(0, &chip->is_open);
+ * @return: 0 in case of success.
+ */
+static int tpm_st33_spi_remove(struct spi_device *client)
+{
+	struct tpm_chip *chip = (struct tpm_chip *)spi_get_drvdata(client);
+	struct st33zp24_platform_data *pin_infos =
+	 ((struct spi_device __force *)chip->vendor.iobase)->dev.platform_data;
+
+	if (pin_infos != NULL) {
+		gpio_free(pin_infos->io_lpcpd);
+
+		/* Check if chip has been previously clean */
+		if (pin_infos->bchipf != true)
+			tpm_remove_hardware(chip->dev);
+		if (pin_infos->tpm_spi_buffer[1] != NULL) {
+			kfree(pin_infos->tpm_spi_buffer[1]);
+			pin_infos->tpm_spi_buffer[1] = NULL;
+		}
+		if (pin_infos->tpm_spi_buffer[0] != NULL) {
+			kfree(pin_infos->tpm_spi_buffer[0]);
+			pin_infos->tpm_spi_buffer[0] = NULL;
+		}
+	}
+
+	return 0;
+}
+
+#ifdef CONFIG_PM_SLEEP
+/*
+ * tpm_st33_spi_pm_suspend suspend the TPM device
+ * Added: Work around when suspend and no tpm application is running, suspend
+ * may fail because chip->data_buffer is not set (only set in tpm_open in Linux
+ * TPM core)
+ * @return: 0 in case of success.
+ */
+static int tpm_st33_spi_pm_suspend(struct device *dev)
+{
+	struct tpm_chip *chip = dev_get_drvdata(dev);
+	struct st33zp24_platform_data *pin_infos = dev->platform_data;
+	int ret = 0;
+
+	if (power_mgt)
+		gpio_set_value(pin_infos->io_lpcpd, 0);
+	else {
+		if (chip->data_buffer == NULL)
+			chip->data_buffer = pin_infos->tpm_spi_buffer[0];
+		ret = tpm_pm_suspend(dev);
+		}
+	return ret;
+}				/* tpm_st33_spi_suspend() */
+
+/*
+ * tpm_st33_spi_pm_resume resume the TPM device
+ * @param: spi, the spi_device drescription (TPM SPI description).
+ * @return: 0 in case of success.
+ */
+static int tpm_st33_spi_pm_resume(struct device *dev)
+{
+	struct tpm_chip *chip = dev_get_drvdata(dev);
+	struct st33zp24_platform_data *pin_infos = dev->platform_data;
+
+	int ret = 0;
+	if (power_mgt) {
+		gpio_set_value(pin_infos->io_lpcpd, 1);
+		ret = wait_for_serirq_timeout(chip,
+					 (chip->vendor.status(chip) &
+					  TPM_STS_VALID) == TPM_STS_VALID,
+					  chip->vendor.timeout_b);
+	} else {
+		if (chip->data_buffer == NULL)
+			chip->data_buffer = pin_infos->tpm_spi_buffer[0];
+		ret = tpm_pm_resume(dev);
+		if (!ret)
+			tpm_do_selftest(chip);
+	}
+	return ret;
+} /* tpm_st33_spi_pm_resume() */
+#endif
+
+static SIMPLE_DEV_PM_OPS(tpm_st33_spi_ops, tpm_st33_spi_pm_suspend,
+tpm_st33_spi_pm_resume);
+static struct spi_driver tpm_st33_spi_driver = {
+	.driver = {
+		   .owner = THIS_MODULE,
+		   .name = TPM_ST33_SPI,
+		   .pm = &tpm_st33_spi_ops,
+		   },
+	.probe = tpm_st33_spi_probe,
+	.remove = tpm_st33_spi_remove,
+};
+
+module_spi_driver(tpm_st33_spi_driver);
+
+MODULE_AUTHOR("Christophe Ricard (tpmsupport@st.com)");
+MODULE_DESCRIPTION("STM TPM SPI ST33 Driver");
+MODULE_VERSION("1.2.0");
diff --git a/drivers/char/tpm/tpm_spi_stm_st33.h b/drivers/char/tpm/tpm_spi_stm_st33.h
new file mode 100644
index 0000000..127f2ca
--- /dev/null
+++ b/drivers/char/tpm/tpm_spi_stm_st33.h
@@ -0,0 +1,61 @@
+/*
+ * STMicroelectronics TPM SPI Linux driver for TPM ST33NP18
+ * Copyright (C) 2009, 2013  STMicroelectronics
+ *
+ * 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.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * STMicroelectronics version 1.2.0, Copyright (C) 2010
+ * STMicroelectronics comes with ABSOLUTELY NO WARRANTY.
+ * This is free software, and you are welcome to redistribute it
+ * under certain conditions.
+ *
+ * @Author: Christophe RICARD tpmsupport@st.com
+ *
+ * @File: tpm_spi_stm_st33.h
+ *
+ * @Date: 09/15/2010
+ */
+#ifndef __STM_ST33_TPM_SPI_MAIN_H__
+#define __STM_ST33_TPM_SPI_MAIN_H__
+
+#define TPM_ACCESS			(0x0)
+#define TPM_STS				(0x18)
+#define TPM_DATA_FIFO			(0x24)
+#define TPM_HASH_DATA			(0x24)
+#define TPM_INTF_CAPABILITY		(0x14)
+#define TPM_INT_STATUS			(0x10)
+#define TPM_INT_ENABLE			(0x08)
+
+#define TPM_DUMMY_BYTE			0x00
+#define TPM_WRITE_DIRECTION		0x80
+#define TPM_HEADER_SIZE			10
+#define TPM_BUFSIZE			2048
+
+#define LOCALITY0		0
+
+#define MAX_SPI_LATENCY		15
+
+#define TPM_ST33_SPI		"st33zp24_spi"
+
+struct st33zp24_platform_data {
+	int io_serirq;
+	int io_lpcpd;
+	int latency;
+	bool bchipf;
+	u8 *tpm_spi_buffer[2]; /* 0 Request 1 Response */
+	struct completion irq_detection;
+};
+
+#endif /* __STM_ST33_TPM_SPI_MAIN_H__ */
-- 
1.7.10.4


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

* Re: [PATCH 1/1] TPM: STMicroelectronics st33 driver SPI
  2013-05-23  7:43   ` Mathias LEBLANC
@ 2013-05-23  8:05     ` Andy Shevchenko
  0 siblings, 0 replies; 21+ messages in thread
From: Andy Shevchenko @ 2013-05-23  8:05 UTC (permalink / raw)
  To: Mathias LEBLANC
  Cc: Kent Yoder, Rajiv Andrade, Marcel Selhorst, Sirrix AG,
	tpmdd-devel, linux-kernel, Jean-Luc BLANC, Mark Brown, linux-spi

On Thu, May 23, 2013 at 10:43 AM, Mathias LEBLANC
<Mathias.LEBLANC@st.com> wrote:

> Thanks for your support, I will fix these code style problem.

I left below the comments I think should be addressed besides style.
Please, comment what you think about them.

> However in a first time, can we publish this SPI driver?

It's up to SPI subsystem maintainer, though I couldn't consider the
quality of the driver is enough to include to upstream. You may try to
ask Greg to go to staging if you have real demand of this.

> I think that it will be preferable to submit it and apply some patch if it's only coding style error.

I don't support the way of submitting patch on top of submiting
something that must be just fixed.

> I have fix errors in this patch that has been discovered in the I2C patch, so I don't know what's stop this submission.
> I think that's driver is more criticized than the I2C driver although it's the same base.

I hope you understand it's not an exuce.

> I know that's important to have good code in the kernel source and I'm agree about that,

Good.

> I propose it be published as a first release and I fix coding style problem in a second time.

See above.

>> +++ b/drivers/char/tpm/tpm_spi_stm_st33.c

>> +enum stm33zp24_int_flags {
>> +       TPM_GLOBAL_INT_ENABLE = 0x80,
>> +       TPM_INTF_CMD_READY_INT = 0x080,
>
> What the difference? It looks like first constant is not belong to this enum.
>
>> +static int spi_write8_reg(struct tpm_chip *tpm, u8 tpm_register,
>> +                     u8 *tpm_data, u16 tpm_size) {
>> +       u8 data = 0;
>> +       int total_length = 0, nbr_dummy_bytes;
>> +       int value = 0;
>> +       struct spi_device *dev =
>> +                (struct spi_device __force *)tpm->vendor.iobase;
>> +       struct st33zp24_platform_data *platform_data = dev->dev.platform_data;
>> +       u8 *data_buffer = platform_data->tpm_spi_buffer[0];
>
> It seems a bad idea to have buffers in platform_data. I bet the buffers should be part of other struct. What did I miss?
>
>> +       struct spi_transfer xfer = {
>> +               .tx_buf  = data_buffer,
>> +               .rx_buf  = platform_data->tpm_spi_buffer[1],
>> +       };
>
> ... even this entire structure.
> Can you consider to use spi_message API ?

>> +static unsigned long wait_for_serirq_timeout(struct tpm_chip *chip,
>> +        bool condition, unsigned long timeout) {
>> +       long status = 0;
>> +       struct spi_device *client;
>> +       struct st33zp24_platform_data *pin_infos;
>> +
>> +       client = (struct spi_device __force *)chip->vendor.iobase;
>
> Is there any better storage for this pointer? It seems an abuse of iobase member.

>> +       chip->vendor.iobase = (void __iomem *)dev;
>
> Don't like this one. Try to find better way to drag pointer.


--
With Best Regards,
Andy Shevchenko

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

* RE: [PATCH 1/1] TPM: STMicroelectronics st33 driver SPI
  2013-05-19 10:36 ` Andy Shevchenko
@ 2013-05-23  7:43   ` Mathias LEBLANC
  2013-05-23  8:05     ` Andy Shevchenko
  0 siblings, 1 reply; 21+ messages in thread
From: Mathias LEBLANC @ 2013-05-23  7:43 UTC (permalink / raw)
  To: Andy Shevchenko
  Cc: Kent Yoder, Rajiv Andrade, Marcel Selhorst, Sirrix AG,
	tpmdd-devel, linux-kernel, Jean-Luc BLANC

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain; charset="utf-8", Size: 11174 bytes --]

Hello Andy,

Thanks for your support, I will fix these code style problem.

However in a first time, can we publish this SPI driver?
I think that it will be preferable to submit it and apply some patch if it's only coding style error.
I have fix errors in this patch that has been discovered in the I2C patch, so I don't know what's stop this submission.
I think that's driver is more criticized than the I2C driver although it's the same base.
I know that's important to have good code in the kernel source and I'm agree about that,
I propose it be published as a first release and I fix coding style problem in a second time.

Thanks,

Mathias Leblanc

-----Original Message-----
From: Andy Shevchenko [mailto:andy.shevchenko@gmail.com] 
Sent: 19 May, 2013 12:36
To: Mathias LEBLANC
Cc: Kent Yoder; Rajiv Andrade; Marcel Selhorst; Sirrix AG; tpmdd-devel@lists.sourceforge.net; linux-kernel@vger.kernel.org; Jean-Luc BLANC
Subject: Re: [PATCH 1/1] TPM: STMicroelectronics st33 driver SPI

On Fri, May 17, 2013 at 4:10 PM, Matthias Leblanc <mathias.leblanc@st.com> wrote:

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

Which name is correct? You have not to have this From: line if submitter and author is the same person.

> +++ b/drivers/char/tpm/tpm_spi_stm_st33.c
> @@ -0,0 +1,943 @@
> +/*
> + * STMicroelectronics TPM SPI Linux driver for TPM ST33ZP24
> + * Copyright (C) 2009, 2010  STMicroelectronics

2013 as well?

> + *     09/15/2010:     First shot driver tpm_tis driver for lpc is
> + *                     used as model.

I beleive it could fit one line.

> +#include "tpm.h"
> +

Seems redundant empty line.

> +#include "tpm_spi_stm_st33.h"

> +enum stm33zp24_int_flags {
> +       TPM_GLOBAL_INT_ENABLE = 0x80,
> +       TPM_INTF_CMD_READY_INT = 0x080,

What the difference? It looks like first constant is not belong to this enum.

> +static int spi_write8_reg(struct tpm_chip *tpm, u8 tpm_register,
> +                     u8 *tpm_data, u16 tpm_size) {
> +       u8 data = 0;
> +       int total_length = 0, nbr_dummy_bytes;
> +       int value = 0;
> +       struct spi_device *dev =
> +                (struct spi_device __force *)tpm->vendor.iobase;
> +       struct st33zp24_platform_data *platform_data = dev->dev.platform_data;
> +       u8 *data_buffer = platform_data->tpm_spi_buffer[0];

It seems a bad idea to have buffers in platform_data. I bet the buffers should be part of other struct. What did I miss?

> +       struct spi_transfer xfer = {
> +               .tx_buf  = data_buffer,
> +               .rx_buf  = platform_data->tpm_spi_buffer[1],
> +       };

... even this entire structure.
Can you consider to use spi_message API ?

> +

Redundant empty line.

> +                       data = (tpm_size >> 8) & 0x00ff;

No need to do & 0xff. You have u8 type anyway.

> +                       data_buffer[total_length++] = data;
> +                       data = tpm_size & 0x00ff;

Ditto.

> +static unsigned long wait_for_serirq_timeout(struct tpm_chip *chip,
> +        bool condition, unsigned long timeout) {
> +       long status = 0;
> +       struct spi_device *client;
> +       struct st33zp24_platform_data *pin_infos;
> +
> +       client = (struct spi_device __force *)chip->vendor.iobase;

Is there any better storage for this pointer? It seems an abuse of iobase member.

> +       pin_infos = client->dev.platform_data;
> +
> +       status = wait_for_completion_interruptible_timeout(
> +                               &pin_infos->irq_detection, timeout);
> +       if (status > 0)
> +               enable_irq(gpio_to_irq(pin_infos->io_serirq));
> +       gpio_direction_input(pin_infos->io_serirq);
> +
> +       if (!status) {
> +               status = -EBUSY;
> +               goto wait_end;
> +       }
> +       clear_interruption(chip);
> +       if (condition)
> +               status = 1;
> +
> +wait_end:

Redundant label. Use direct return wherever it applies.

> +       return status;

> +/*
> + * tpm_stm_spi_cancel, cancel is not implemented.
> + * @param: chip, the tpm chip description as specified in
> + * driver/char/tpm/tpm.h.

Just mention the member and struct names here, no need to refer to entire file.

> + */
> +static void tpm_stm_spi_cancel(struct tpm_chip *chip) {
> +       u8 data = TPM_STS_COMMAND_READY;
> +
> +       /* this causes the current command to be aborted */
> +       spi_write8_reg(chip, TPM_STS, &data, 1); } /* 
> +tpm_stm_spi_cancel() */

This comment is redundant.

> +} /* tpm_stm_spi_status() */

Ditto.
Here and anywhere in the file.

> +
> +
> +

Couple of redundant empty lines.

> +static int request_locality(struct tpm_chip *chip) {

> +       unsigned long stop;
> +       long rc;
> +       u8 data = 0;

Redundant assignment. Please, check entire file for such assignments.

> +end:

Redundant label.

> +       return -EACCES;

> +static int get_burstcount(struct tpm_chip *chip) {

> +               tpm_reg = tpm_reg + 1;

tpm_reg++;

> +end:

Redundant label. Please, clean up entire file from such useless labels.

> +       return -EBUSY;

> +static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count) {

> +               burstcnt = get_burstcount(chip);
> +               len = min_t(int, burstcnt, count - size);
> +               status = spi_read8_reg(chip, TPM_DATA_FIFO, buf + size, len);
> +               if (status < 0)
> +                       return status;
> +
> +

Useless empty line(s).

> +static int tpm_stm_spi_send(struct tpm_chip *chip, unsigned char *buf,
> +                           size_t len) {
> +       u32 burstcnt = 0, i, size = 0;
> +       u8 data = 0;
> +       long status = 0, ret = 0;
> +
> +       if (chip == NULL)
> +               return -EBUSY;

-EINVAL, btw.

> +       if (len < TPM_HEADER_SIZE)
> +               return -EBUSY;

Same.

> +static int tpm_stm_spi_recv(struct tpm_chip *chip, unsigned char *buf,
> +                           size_t count) {
> +       int size = 0;
> +       int expected;
> +
> +       if (chip == NULL)
> +               return -EBUSY;

-EINVAL. Check entire code.

> +       if (count < TPM_HEADER_SIZE) {
> +               size = -EIO;
> +               goto out;
> +       }

You will perform asymmetric actions here. At least it requires some explanations in the header of fuction.

> +static int interrupts;
> +module_param(interrupts, int, 0444);
> +MODULE_PARM_DESC(interrupts, "Enable interrupts");
> +
> +static int power_mgt = 1;
> +module_param(power_mgt, int, 0444);
> +MODULE_PARM_DESC(power_mgt, "Power Management");

Move this section to the top/bottom of the file.

> +static int
> +tpm_st33_spi_probe(struct spi_device *dev) {
> +       long err = 0;
> +       u8 intmask;
> +       struct tpm_chip *chip;
> +       struct st33zp24_platform_data *platform_data;
> +
> +       /* Check SPI platform functionnalities */
> +       if (dev == NULL) {
> +               pr_info("dev is NULL. exiting.\n");

Looks like debug print. Should be remove

> +               err = -ENODEV;
> +               goto end;

return -ENODEV;

> +       chip = tpm_register_hardware(&dev->dev, &st_spi_tpm);
> +       if (!chip) {
> +               err = -ENODEV;
> +               goto end;

Ditto.

> +       /* Allocation of SPI buffers MISO and MOSI              */
> +       /* Size is as follow:                                   */
> +       /* Request burstcount value  = 0x800 = 2048             */
> +       /* +                                                    */
> +       /* Response burstcount value = 0x400 = 1024             */
> +       /* +                                                    */
> +       /* At least:                                            */
> +       /* 1 byte for direction/locality                        */
> +       /* 1 byte tpm tis register                              */
> +       /* 2 bytes spi data length (for request only)           */
> +       /* 2 latency bytes                                      */
> +       /* 1 status byte                                        */
> +       /* = 2048 + 1024 + 7                                    */
> +       /* We reserved 2048 + 1024 + 20 in case latency byte    */
> +       /* change                                               */

Looks like a candidate to *.h file in the struct description.

> +       platform_data = dev->dev.platform_data;

And as I said already, it's not a platform_data.

> +
> +       if (platform_data)

if (!platform_data)
 return -ENODEV;

> +               platform_data->tpm_spi_buffer[0] =
> +               kmalloc((TPM_BUFSIZE + (TPM_BUFSIZE / 2) +
> +                                TPM_DIGEST_SIZE) * sizeof(u8), 
> + GFP_KERNEL);

This magic calc may gone when you will use dedicated constant with mentioned explanations.

> +       else
> +               goto end;

Remove those two.

> +       chip->vendor.iobase = (void __iomem *)dev;

Don't like this one. Try to find better way to drag pointer.

> +       pr_info("TPM SPI Initialized\n");

Something like
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt at the very top of file could be helpful.

> +       if (platform_data->tpm_spi_buffer[1] != NULL) {

Redundant check.

> +               kfree(platform_data->tpm_spi_buffer[1]);

> +       if (platform_data->tpm_spi_buffer[0] != NULL) {

Ditto.

> +/*
> + * tpm_st33_spi_remove remove the TPM device
> + * @param: client, the spi_device drescription (TPM SPI description).

> +               clear_bit(0, &chip->is_open);

Leftover?

> + * @return: 0 in case of success.
> + */
> +static int tpm_st33_spi_remove(struct spi_device *client) {
> +       struct tpm_chip *chip = (struct tpm_chip *)spi_get_drvdata(client);
> +       struct st33zp24_platform_data *pin_infos =
> +        ((struct spi_device __force 
> +*)chip->vendor.iobase)->dev.platform_data;
> +
> +       if (pin_infos != NULL) {
> +               gpio_free(pin_infos->io_lpcpd);
> +
> +               /* Check if chip has been previously clean */
> +               if (pin_infos->bchipf != true)
> +                       tpm_remove_hardware(chip->dev);
> +               if (pin_infos->tpm_spi_buffer[1] != NULL) {

Redundant check.

> +                       kfree(pin_infos->tpm_spi_buffer[1]);
> +                       pin_infos->tpm_spi_buffer[1] = NULL;
> +               }
> +               if (pin_infos->tpm_spi_buffer[0] != NULL) {

Ditto.

> +++ b/drivers/char/tpm/tpm_spi_stm_st33.h
> @@ -0,0 +1,61 @@
> +/*
> + * STMicroelectronics TPM SPI Linux driver for TPM ST33NP18
> + * Copyright (C) 2009, 2010  STMicroelectronics

2013 as well?

> +#define TPM_ACCESS                     (0x0)
> +#define TPM_STS                                (0x18)
> +#define TPM_DATA_FIFO                  (0x24)
> +#define TPM_HASH_DATA                  (0x24)
> +#define TPM_INTF_CAPABILITY            (0x14)
> +#define TPM_INT_STATUS                 (0x10)
> +#define TPM_INT_ENABLE                 (0x08)

What the point to embrace those constants?

--
With Best Regards,
Andy Shevchenko
ÿôèº{.nÇ+‰·Ÿ®‰­†+%ŠËÿ±éݶ\x17¥Šwÿº{.nÇ+‰·¥Š{±þG«éÿŠ{ayº\x1dʇڙë,j\a­¢f£¢·hšïêÿ‘êçz_è®\x03(­éšŽŠÝ¢j"ú\x1a¶^[m§ÿÿ¾\a«þG«éÿ¢¸?™¨è­Ú&£ø§~á¶iO•æ¬z·švØ^\x14\x04\x1a¶^[m§ÿÿÃ\fÿ¶ìÿ¢¸?–I¥

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

* Re: [PATCH 1/1] TPM: STMicroelectronics st33 driver SPI
  2013-05-17 13:10 Matthias Leblanc
@ 2013-05-19 10:36 ` Andy Shevchenko
  2013-05-23  7:43   ` Mathias LEBLANC
  0 siblings, 1 reply; 21+ messages in thread
From: Andy Shevchenko @ 2013-05-19 10:36 UTC (permalink / raw)
  To: Matthias Leblanc
  Cc: Kent Yoder, Rajiv Andrade, Marcel Selhorst, Sirrix AG,
	tpmdd-devel, linux-kernel, Jean-Luc Blanc

On Fri, May 17, 2013 at 4:10 PM, Matthias Leblanc
<mathias.leblanc@st.com> wrote:

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

Which name is correct? You have not to have this From: line if
submitter and author is the same person.

> +++ b/drivers/char/tpm/tpm_spi_stm_st33.c
> @@ -0,0 +1,943 @@
> +/*
> + * STMicroelectronics TPM SPI Linux driver for TPM ST33ZP24
> + * Copyright (C) 2009, 2010  STMicroelectronics

2013 as well?

> + *     09/15/2010:     First shot driver tpm_tis driver for lpc is
> + *                     used as model.

I beleive it could fit one line.

> +#include "tpm.h"
> +

Seems redundant empty line.

> +#include "tpm_spi_stm_st33.h"

> +enum stm33zp24_int_flags {
> +       TPM_GLOBAL_INT_ENABLE = 0x80,
> +       TPM_INTF_CMD_READY_INT = 0x080,

What the difference? It looks like first constant is not belong to this enum.

> +static int spi_write8_reg(struct tpm_chip *tpm, u8 tpm_register,
> +                     u8 *tpm_data, u16 tpm_size)
> +{
> +       u8 data = 0;
> +       int total_length = 0, nbr_dummy_bytes;
> +       int value = 0;
> +       struct spi_device *dev =
> +                (struct spi_device __force *)tpm->vendor.iobase;
> +       struct st33zp24_platform_data *platform_data = dev->dev.platform_data;
> +       u8 *data_buffer = platform_data->tpm_spi_buffer[0];

It seems a bad idea to have buffers in platform_data. I bet the
buffers should be part of other struct. What did I miss?

> +       struct spi_transfer xfer = {
> +               .tx_buf  = data_buffer,
> +               .rx_buf  = platform_data->tpm_spi_buffer[1],
> +       };

... even this entire structure.
Can you consider to use spi_message API ?

> +

Redundant empty line.

> +                       data = (tpm_size >> 8) & 0x00ff;

No need to do & 0xff. You have u8 type anyway.

> +                       data_buffer[total_length++] = data;
> +                       data = tpm_size & 0x00ff;

Ditto.

> +static unsigned long wait_for_serirq_timeout(struct tpm_chip *chip,
> +        bool condition, unsigned long timeout)
> +{
> +       long status = 0;
> +       struct spi_device *client;
> +       struct st33zp24_platform_data *pin_infos;
> +
> +       client = (struct spi_device __force *)chip->vendor.iobase;

Is there any better storage for this pointer? It seems an abuse of
iobase member.

> +       pin_infos = client->dev.platform_data;
> +
> +       status = wait_for_completion_interruptible_timeout(
> +                               &pin_infos->irq_detection, timeout);
> +       if (status > 0)
> +               enable_irq(gpio_to_irq(pin_infos->io_serirq));
> +       gpio_direction_input(pin_infos->io_serirq);
> +
> +       if (!status) {
> +               status = -EBUSY;
> +               goto wait_end;
> +       }
> +       clear_interruption(chip);
> +       if (condition)
> +               status = 1;
> +
> +wait_end:

Redundant label. Use direct return wherever it applies.

> +       return status;

> +/*
> + * tpm_stm_spi_cancel, cancel is not implemented.
> + * @param: chip, the tpm chip description as specified in
> + * driver/char/tpm/tpm.h.

Just mention the member and struct names here, no need to refer to entire file.

> + */
> +static void tpm_stm_spi_cancel(struct tpm_chip *chip)
> +{
> +       u8 data = TPM_STS_COMMAND_READY;
> +
> +       /* this causes the current command to be aborted */
> +       spi_write8_reg(chip, TPM_STS, &data, 1);
> +} /* tpm_stm_spi_cancel() */

This comment is redundant.

> +} /* tpm_stm_spi_status() */

Ditto.
Here and anywhere in the file.

> +
> +
> +

Couple of redundant empty lines.

> +static int request_locality(struct tpm_chip *chip)
> +{

> +       unsigned long stop;
> +       long rc;
> +       u8 data = 0;

Redundant assignment. Please, check entire file for such assignments.

> +end:

Redundant label.

> +       return -EACCES;

> +static int get_burstcount(struct tpm_chip *chip)
> +{

> +               tpm_reg = tpm_reg + 1;

tpm_reg++;

> +end:

Redundant label. Please, clean up entire file from such useless labels.

> +       return -EBUSY;

> +static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count)
> +{

> +               burstcnt = get_burstcount(chip);
> +               len = min_t(int, burstcnt, count - size);
> +               status = spi_read8_reg(chip, TPM_DATA_FIFO, buf + size, len);
> +               if (status < 0)
> +                       return status;
> +
> +

Useless empty line(s).

> +static int tpm_stm_spi_send(struct tpm_chip *chip, unsigned char *buf,
> +                           size_t len)
> +{
> +       u32 burstcnt = 0, i, size = 0;
> +       u8 data = 0;
> +       long status = 0, ret = 0;
> +
> +       if (chip == NULL)
> +               return -EBUSY;

-EINVAL, btw.

> +       if (len < TPM_HEADER_SIZE)
> +               return -EBUSY;

Same.

> +static int tpm_stm_spi_recv(struct tpm_chip *chip, unsigned char *buf,
> +                           size_t count)
> +{
> +       int size = 0;
> +       int expected;
> +
> +       if (chip == NULL)
> +               return -EBUSY;

-EINVAL. Check entire code.

> +       if (count < TPM_HEADER_SIZE) {
> +               size = -EIO;
> +               goto out;
> +       }

You will perform asymmetric actions here. At least it requires some
explanations in the header of fuction.

> +static int interrupts;
> +module_param(interrupts, int, 0444);
> +MODULE_PARM_DESC(interrupts, "Enable interrupts");
> +
> +static int power_mgt = 1;
> +module_param(power_mgt, int, 0444);
> +MODULE_PARM_DESC(power_mgt, "Power Management");

Move this section to the top/bottom of the file.

> +static int
> +tpm_st33_spi_probe(struct spi_device *dev)
> +{
> +       long err = 0;
> +       u8 intmask;
> +       struct tpm_chip *chip;
> +       struct st33zp24_platform_data *platform_data;
> +
> +       /* Check SPI platform functionnalities */
> +       if (dev == NULL) {
> +               pr_info("dev is NULL. exiting.\n");

Looks like debug print. Should be remove

> +               err = -ENODEV;
> +               goto end;

return -ENODEV;

> +       chip = tpm_register_hardware(&dev->dev, &st_spi_tpm);
> +       if (!chip) {
> +               err = -ENODEV;
> +               goto end;

Ditto.

> +       /* Allocation of SPI buffers MISO and MOSI              */
> +       /* Size is as follow:                                   */
> +       /* Request burstcount value  = 0x800 = 2048             */
> +       /* +                                                    */
> +       /* Response burstcount value = 0x400 = 1024             */
> +       /* +                                                    */
> +       /* At least:                                            */
> +       /* 1 byte for direction/locality                        */
> +       /* 1 byte tpm tis register                              */
> +       /* 2 bytes spi data length (for request only)           */
> +       /* 2 latency bytes                                      */
> +       /* 1 status byte                                        */
> +       /* = 2048 + 1024 + 7                                    */
> +       /* We reserved 2048 + 1024 + 20 in case latency byte    */
> +       /* change                                               */

Looks like a candidate to *.h file in the struct description.

> +       platform_data = dev->dev.platform_data;

And as I said already, it's not a platform_data.

> +
> +       if (platform_data)

if (!platform_data)
 return -ENODEV;

> +               platform_data->tpm_spi_buffer[0] =
> +               kmalloc((TPM_BUFSIZE + (TPM_BUFSIZE / 2) +
> +                                TPM_DIGEST_SIZE) * sizeof(u8), GFP_KERNEL);

This magic calc may gone when you will use dedicated constant with
mentioned explanations.

> +       else
> +               goto end;

Remove those two.

> +       chip->vendor.iobase = (void __iomem *)dev;

Don't like this one. Try to find better way to drag pointer.

> +       pr_info("TPM SPI Initialized\n");

Something like
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt at the very top of file
could be helpful.

> +       if (platform_data->tpm_spi_buffer[1] != NULL) {

Redundant check.

> +               kfree(platform_data->tpm_spi_buffer[1]);

> +       if (platform_data->tpm_spi_buffer[0] != NULL) {

Ditto.

> +/*
> + * tpm_st33_spi_remove remove the TPM device
> + * @param: client, the spi_device drescription (TPM SPI description).

> +               clear_bit(0, &chip->is_open);

Leftover?

> + * @return: 0 in case of success.
> + */
> +static int tpm_st33_spi_remove(struct spi_device *client)
> +{
> +       struct tpm_chip *chip = (struct tpm_chip *)spi_get_drvdata(client);
> +       struct st33zp24_platform_data *pin_infos =
> +        ((struct spi_device __force *)chip->vendor.iobase)->dev.platform_data;
> +
> +       if (pin_infos != NULL) {
> +               gpio_free(pin_infos->io_lpcpd);
> +
> +               /* Check if chip has been previously clean */
> +               if (pin_infos->bchipf != true)
> +                       tpm_remove_hardware(chip->dev);
> +               if (pin_infos->tpm_spi_buffer[1] != NULL) {

Redundant check.

> +                       kfree(pin_infos->tpm_spi_buffer[1]);
> +                       pin_infos->tpm_spi_buffer[1] = NULL;
> +               }
> +               if (pin_infos->tpm_spi_buffer[0] != NULL) {

Ditto.

> +++ b/drivers/char/tpm/tpm_spi_stm_st33.h
> @@ -0,0 +1,61 @@
> +/*
> + * STMicroelectronics TPM SPI Linux driver for TPM ST33NP18
> + * Copyright (C) 2009, 2010  STMicroelectronics

2013 as well?

> +#define TPM_ACCESS                     (0x0)
> +#define TPM_STS                                (0x18)
> +#define TPM_DATA_FIFO                  (0x24)
> +#define TPM_HASH_DATA                  (0x24)
> +#define TPM_INTF_CAPABILITY            (0x14)
> +#define TPM_INT_STATUS                 (0x10)
> +#define TPM_INT_ENABLE                 (0x08)

What the point to embrace those constants?

--
With Best Regards,
Andy Shevchenko

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

* [PATCH 1/1] TPM: STMicroelectronics st33 driver SPI
@ 2013-05-17 13:10 Matthias Leblanc
  2013-05-19 10:36 ` Andy Shevchenko
  0 siblings, 1 reply; 21+ messages in thread
From: Matthias Leblanc @ 2013-05-17 13:10 UTC (permalink / raw)
  To: Kent Yoder, Rajiv Andrade, Marcel Selhorst, Sirrix AG, tpmdd-devel
  Cc: linux-kernel, Jean-Luc Blanc, Mathias Leblanc

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

 * STMicroelectronics version 1.2.0, Copyright (C) 2010
 * 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 SPI, in menuconfig or .config choose the tpm driver on
device --> tpm and activate the protocol of your choice before compiling
the kernel.
The driver will be accessible from within Linux.

Tested on linux x86/x64 and beagleboard REV B & XM REV C

Signed-off-by: Mathias Leblanc <mathias.leblanc@st.com>
---
Remove unused variables
Remove tpm_st33_spi_exit function causing compilation errors
Initalise data and temp variable
Change some variable type
Fix warnings and errors from smatch tools 
Change for loop to while loop

 drivers/char/tpm/Kconfig            |   10 +
 drivers/char/tpm/Makefile           |    1 +
 drivers/char/tpm/tpm_spi_stm_st33.c |  943 +++++++++++++++++++++++++++++++++++
 drivers/char/tpm/tpm_spi_stm_st33.h |   61 +++
 4 files changed, 1015 insertions(+), 0 deletions(-)
 create mode 100644 drivers/char/tpm/tpm_spi_stm_st33.c
 create mode 100644 drivers/char/tpm/tpm_spi_stm_st33.h

diff --git a/drivers/char/tpm/Kconfig b/drivers/char/tpm/Kconfig
index dbfd564..2fc1254 100644
--- a/drivers/char/tpm/Kconfig
+++ b/drivers/char/tpm/Kconfig
@@ -91,4 +91,14 @@ config TCG_ST33_I2C
         To compile this driver as a module, choose M here; the module will be
         called tpm_stm_st33_i2c.
 
+config TCG_ST33_SPI
+       tristate "STMicroelectronics ST33 SPI"
+       depends on SPI
+       depends on GPIOLIB
+       ---help---
+       If you have a TPM security chip from STMicroelectronics working with
+       an SPI bus say Yes and it will be accessible from within Linux.
+       To compile this driver as a module, choose M here; the module will be
+       called tpm_stm_st33_spi.
+
 endif # TCG_TPM
diff --git a/drivers/char/tpm/Makefile b/drivers/char/tpm/Makefile
index a3736c9..a1db3dd 100644
--- a/drivers/char/tpm/Makefile
+++ b/drivers/char/tpm/Makefile
@@ -18,3 +18,4 @@ obj-$(CONFIG_TCG_ATMEL) += tpm_atmel.o
 obj-$(CONFIG_TCG_INFINEON) += tpm_infineon.o
 obj-$(CONFIG_TCG_IBMVTPM) += tpm_ibmvtpm.o
 obj-$(CONFIG_TCG_ST33_I2C) += tpm_i2c_stm_st33.o
+obj-$(CONFIG_TCG_ST33_SPI) += tpm_spi_stm_st33.o
diff --git a/drivers/char/tpm/tpm_spi_stm_st33.c b/drivers/char/tpm/tpm_spi_stm_st33.c
new file mode 100644
index 0000000..6ed23a7
--- /dev/null
+++ b/drivers/char/tpm/tpm_spi_stm_st33.c
@@ -0,0 +1,943 @@
+/*
+ * STMicroelectronics TPM SPI Linux driver for TPM ST33ZP24
+ * Copyright (C) 2009, 2010  STMicroelectronics
+ *
+ * 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.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * STMicroelectronics version 1.2.0, Copyright (C) 2010
+ * STMicroelectronics comes with ABSOLUTELY NO WARRANTY.
+ * This is free software, and you are welcome to redistribute it
+ * under certain conditions.
+ *
+ * @Author: Christophe RICARD tpmsupport@st.com
+ *
+ * @File: tpm_stm_st33_spi.c
+ *
+ * @Synopsis:
+ *	09/15/2010:	First shot driver tpm_tis driver for lpc is
+ *			used as model.
+ */
+
+#include <linux/kernel.h>
+#include <linux/init.h>
+#include <linux/spi/spi.h>
+#include <linux/wait.h>
+#include <linux/string.h>
+#include <linux/interrupt.h>
+#include <linux/spinlock.h>
+#include <linux/sysfs.h>
+#include <linux/gpio.h>
+#include <linux/uaccess.h>
+#include <linux/slab.h>
+#include <linux/sched.h>
+
+#include "tpm.h"
+
+#include "tpm_spi_stm_st33.h"
+
+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 */
+};
+
+/*
+ * spi_write8_reg
+ * Send byte to the TIS register according to the ST33ZP24 SPI protocol.
+ * @param: tpm, 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: should be zero if success else a negative error code.
+ */
+static int spi_write8_reg(struct tpm_chip *tpm, u8 tpm_register,
+		      u8 *tpm_data, u16 tpm_size)
+{
+	u8 data = 0;
+	int total_length = 0, nbr_dummy_bytes;
+	int value = 0;
+	struct spi_device *dev =
+		 (struct spi_device __force *)tpm->vendor.iobase;
+	struct st33zp24_platform_data *platform_data = dev->dev.platform_data;
+	u8 *data_buffer = platform_data->tpm_spi_buffer[0];
+	struct spi_transfer xfer = {
+		.tx_buf	 = data_buffer,
+		.rx_buf	 = platform_data->tpm_spi_buffer[1],
+	};
+
+	u8 latency = platform_data->latency + tpm_size + 1;
+
+	/* Pre-Header */
+	data = TPM_WRITE_DIRECTION | tpm->vendor.locality;
+	memcpy(data_buffer + total_length, &data, sizeof(data));
+	total_length++;
+	data = tpm_register;
+	memcpy(data_buffer + total_length, &data, sizeof(data));
+	total_length++;
+
+	if (tpm_size > 0 &&
+	 (tpm_register == TPM_DATA_FIFO || tpm_register == TPM_HASH_DATA)) {
+			data = (tpm_size >> 8) & 0x00ff;
+			data_buffer[total_length++] = data;
+			data = tpm_size & 0x00ff;
+			data_buffer[total_length++] = data;
+			latency -= 2;
+	}
+
+	memcpy(&data_buffer[total_length], tpm_data, tpm_size);
+	total_length += tpm_size;
+
+	nbr_dummy_bytes = platform_data->latency + 1;
+	memset(&data_buffer[total_length], TPM_DUMMY_BYTE,
+					 platform_data->latency + 1);
+
+	xfer.len = total_length;
+
+	value = spi_sync_transfer(dev, &xfer, xfer.len);
+
+	if (value == 0) {
+		nbr_dummy_bytes = total_length - 1 - nbr_dummy_bytes;
+		while (nbr_dummy_bytes < total_length &&
+			 ((u8 *)xfer.rx_buf)[nbr_dummy_bytes] == 0)
+					nbr_dummy_bytes++;
+
+		if (((u8 *)xfer.rx_buf)[nbr_dummy_bytes] != 0)
+			value = ((u8 *)xfer.rx_buf)[nbr_dummy_bytes];
+	}
+
+	return value;
+} /* spi_write8_reg() */
+
+/*
+ * spi_read8_reg
+ * Recv byte from the TIS register according to the ST33ZP24 SPI 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: should be zero if success else a negative error code.
+ */
+static u8 spi_read8_reg(struct tpm_chip *tpm, u8 tpm_register,
+		    u8 *tpm_data, u16 tpm_size)
+{
+	u8 data = 0;
+	int total_length = 0, nbr_dummy_bytes;
+	int value = 0;
+	struct spi_device *dev =
+		 (struct spi_device __force *)tpm->vendor.iobase;
+	struct st33zp24_platform_data *platform_data = dev->dev.platform_data;
+	u8 *data_buffer = platform_data->tpm_spi_buffer[0];
+	struct spi_transfer xfer = {
+		.tx_buf	 = data_buffer,
+		.rx_buf	 = platform_data->tpm_spi_buffer[1],
+	};
+
+	/* Pre-Header */
+
+	data = tpm->vendor.locality;
+	memcpy(data_buffer + total_length, &data, sizeof(data));
+	total_length++;
+	data = tpm_register;
+	memcpy(data_buffer + total_length, &data, sizeof(data));
+	total_length++;
+
+	nbr_dummy_bytes = platform_data->latency + 1;
+	memset(&data_buffer[total_length], TPM_DUMMY_BYTE,
+		 platform_data->latency + 1);
+
+	xfer.len = total_length;
+
+	/* header + status byte + size of the data + status byte */
+	value = spi_sync_transfer(dev, &xfer, xfer.len);
+
+	if (tpm_size > 0 && value == 0) {
+		nbr_dummy_bytes = 2;
+		while (nbr_dummy_bytes < total_length &&
+			 ((u8 *)xfer.rx_buf)[nbr_dummy_bytes] == 0)
+				nbr_dummy_bytes++;
+
+		if (nbr_dummy_bytes + 1 < total_length) {
+			value = ((u8 *)xfer.rx_buf)[nbr_dummy_bytes];
+
+			if (tpm_size > 0)
+				memcpy(tpm_data, xfer.rx_buf +
+							 nbr_dummy_bytes + 1,
+								 tpm_size);
+		}
+	}
+	return value;
+} /* spi_read8_reg() */
+
+/*
+ * clear_interruption
+ * clear the TPM interrupt register.
+ * @param: tpm, the chip description
+ */
+static void clear_interruption(struct tpm_chip *tpm)
+{
+	u8 interrupt;
+	spi_read8_reg(tpm, TPM_INT_STATUS, &interrupt, 1);
+	/* Clear interrupts handled with TPM_EOI */
+	spi_write8_reg(tpm, TPM_INT_STATUS, &interrupt, 1);
+	spi_read8_reg(tpm, TPM_INT_STATUS, &interrupt, 1);
+} /* clear_interruption() */
+
+/*
+ * _wait_for_serirq_timeout
+ * @param: tpm, the chip description
+ * @param: timeout, the timeout of the interrupt
+ * @return: the status of the interruption.
+ */
+static unsigned long wait_for_serirq_timeout(struct tpm_chip *chip,
+	 bool condition, unsigned long timeout)
+{
+	long status = 0;
+	struct spi_device *client;
+	struct st33zp24_platform_data *pin_infos;
+
+	client = (struct spi_device __force *)chip->vendor.iobase;
+	pin_infos = client->dev.platform_data;
+
+	status = wait_for_completion_interruptible_timeout(
+				&pin_infos->irq_detection, timeout);
+	if (status > 0)
+		enable_irq(gpio_to_irq(pin_infos->io_serirq));
+	gpio_direction_input(pin_infos->io_serirq);
+
+	if (!status) {
+		status = -EBUSY;
+		goto wait_end;
+	}
+	clear_interruption(chip);
+	if (condition)
+		status = 1;
+
+wait_end:
+	return status;
+}
+
+
+/*
+ * tpm_stm_spi_cancel, cancel is not implemented.
+ * @param: chip, the tpm chip description as specified in
+ * driver/char/tpm/tpm.h.
+ */
+static void tpm_stm_spi_cancel(struct tpm_chip *chip)
+{
+	u8 data = TPM_STS_COMMAND_READY;
+
+	/* this causes the current command to be aborted */
+	spi_write8_reg(chip, TPM_STS, &data, 1);
+} /* tpm_stm_spi_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_spi_status(struct tpm_chip *chip)
+{
+	u8 data = 0;
+
+	spi_read8_reg(chip, TPM_STS, &data, 1);
+	return data;
+} /* tpm_stm_spi_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 = 0;
+	u8 status;
+
+	status = spi_read8_reg(chip, TPM_ACCESS, &data, 1);
+	if (status && (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 stop;
+	long rc;
+	u8 data = 0;
+
+	/* Check locality */
+	if (check_locality(chip) == chip->vendor.locality)
+		return chip->vendor.locality;
+
+	/* Request locality */
+	data = TPM_ACCESS_REQUEST_USE;
+	rc = spi_write8_reg(chip, TPM_ACCESS, &data, 1);
+	if (rc < 0)
+		goto end;
+
+	/* wait for burstcount */
+	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 {
+		stop = jiffies + chip->vendor.timeout_a;
+		do {
+			if (check_locality(chip) >= 0)
+				return chip->vendor.locality;
+			msleep(TPM_TIMEOUT);
+		} while (time_before(jiffies, stop));
+	}
+end:
+	return -EACCES;
+} /* request_locality() */
+
+/*
+ * release_locality release the active locality
+ * @param: chip, the tpm chip description.
+ */
+static void release_locality(struct tpm_chip *chip)
+{
+	u8 data = 0;
+
+	data = TPM_ACCESS_ACTIVE_LOCALITY;
+	spi_write8_reg(chip, TPM_ACCESS, &data, 1);
+} /* release_locality()*/
+
+/*
+ * 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 stop;
+	u32 burstcnt;
+	u8 tpm_reg, temp = 0;
+	long status = 0;
+
+	/* wait for burstcount */
+	/* which timeout value, spec has 2 answers (c & d) */
+	stop = jiffies + chip->vendor.timeout_d;
+	do {
+		tpm_reg = TPM_STS + 1;
+		status = spi_read8_reg(chip, tpm_reg, &temp, 1);
+		if (status < 0)
+			goto end;
+
+		tpm_reg = tpm_reg + 1;
+		burstcnt = temp;
+		status = spi_read8_reg(chip, tpm_reg, &temp, 1);
+		if (status < 0)
+			goto end;
+
+		burstcnt |= temp << 8;
+		if (burstcnt)
+			return burstcnt;
+		msleep(TPM_TIMEOUT);
+	} while (time_before(jiffies, stop));
+
+end:
+	return -EBUSY;
+} /* get_burstcount() */
+
+/*
+ * wait_for_stat wait for a TPM_STS value
+ * @param: chip, the tpm chip description
+ * @param: mask, the value mask to wait
+ * @param: timeout, the timeout
+ * @param: queue, the wait queue.
+ * @return: the tpm status, 0 if success, -ETIME if timeout is reached.
+ */
+static int wait_for_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout,
+			 wait_queue_head_t *queue)
+{
+	unsigned long stop;
+	long rc;
+	u8 status;
+
+	/* check current status */
+	status = tpm_stm_spi_status(chip);
+	if (!chip->vendor.irq && (status & mask) == mask)
+		return 0;
+
+	if (chip->vendor.irq) {
+		rc = wait_for_serirq_timeout(chip, ((tpm_stm_spi_status
+						    (chip) & mask) ==
+						     mask), timeout);
+		if (rc > 0)
+			return 0;
+	} else {
+		stop = jiffies + timeout;
+		do {
+			msleep(TPM_TIMEOUT);
+			status = tpm_stm_spi_status(chip);
+			if ((status & mask) == mask)
+				return 0;
+		} while (time_before(jiffies, stop));
+	}
+	return -ETIME;
+} /* wait_for_stat() */
+
+/*
+ * 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 tpm status, 0 if success, -ETIME if timeout is reached.
+ */
+static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count)
+{
+	u32 size = 0, burstcnt, len;
+	long status = 0;
+
+	while (size < count &&
+	       wait_for_stat(chip,
+			     TPM_STS_DATA_AVAIL | TPM_STS_VALID,
+			     chip->vendor.timeout_c,
+			     &chip->vendor.read_queue)
+						== 0) {
+		burstcnt = get_burstcount(chip);
+		len = min_t(int, burstcnt, count - size);
+		status = spi_read8_reg(chip, TPM_DATA_FIFO, buf + size, len);
+		if (status < 0)
+			return status;
+
+
+		size += len;
+	}
+	return size;
+}
+
+/*
+ * tpm_ioserirq_handler the serirq irq handler
+ * @param: irq, the tpm chip description
+ * @param: dev_id, the description of the chip
+ * @return: the status of the handler.
+ */
+static irqreturn_t tpm_ioserirq_handler(int irq, void *dev_id)
+{
+	struct tpm_chip *chip = dev_id;
+	struct spi_device *client;
+	struct st33zp24_platform_data *pin_infos;
+
+	disable_irq_nosync(irq);
+
+	client = (struct spi_device __force *)chip->vendor.iobase;
+	pin_infos = client->dev.platform_data;
+
+	complete(&pin_infos->irq_detection);
+	return IRQ_HANDLED;
+} /* tpm_ioserirq_handler() */
+
+/*
+ * tpm_stm_spi_send send TPM commands through the SPI bus.
+ * @param: chip, the tpm chip description
+ * @param: buf, the tpm command buffer
+ * @param: len, the tpm command size
+ * @return: should be zero if success else the negative error code.
+ */
+static int tpm_stm_spi_send(struct tpm_chip *chip, unsigned char *buf,
+			    size_t len)
+{
+	u32 burstcnt = 0, i, size = 0;
+	u8 data = 0;
+	long status = 0, ret = 0;
+
+	if (chip == NULL)
+		return -EBUSY;
+	if (len < TPM_HEADER_SIZE)
+		return -EBUSY;
+
+	ret = request_locality(chip);
+	if (ret < 0)
+		return ret;
+
+	status = tpm_stm_spi_status(chip);
+	if ((status & TPM_STS_COMMAND_READY) == 0) {
+		tpm_stm_spi_cancel(chip);
+		if (wait_for_stat
+		    (chip, TPM_STS_COMMAND_READY, chip->vendor.timeout_b,
+		     &chip->vendor.int_queue) < 0) {
+			ret = -ETIME;
+			goto out_err;
+		}
+	}
+
+	for (i = 0; i < len - 1;) {
+		burstcnt = get_burstcount(chip);
+		size = min_t(int, len - i - 1, burstcnt);
+		ret = spi_write8_reg(chip, TPM_DATA_FIFO, buf, size);
+		if (ret < 0)
+			goto out_err;
+		i += size;
+	}
+
+	status = tpm_stm_spi_status(chip);
+	if ((status & TPM_STS_DATA_EXPECT) == 0) {
+		ret = -EIO;
+		goto out_err;
+	}
+
+	/* write last byte */
+	spi_write8_reg(chip, TPM_DATA_FIFO, buf + len - 1, 1);
+
+	status = tpm_stm_spi_status(chip);
+	if ((status & TPM_STS_DATA_EXPECT) != 0) {
+		ret = -EIO;
+		goto out_err;
+	}
+
+	/* go and do it */
+	data = TPM_STS_GO;
+	ret = spi_write8_reg(chip, TPM_STS, &data, 1);
+	if (ret < 0)
+		goto out_err;
+
+	return len;
+out_err:
+	tpm_stm_spi_cancel(chip);
+	release_locality(chip);
+	return ret;
+}
+
+/*
+ * tpm_stm_spi_recv received TPM response through the SPI bus.
+ * @param: chip, the tpm chip description
+ * @param: buf, the tpm command buffer
+ * @param: len, the tpm command size
+* @return: should be zero if success else the negative error code.
+ */
+static int tpm_stm_spi_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);
+
+	/* read first 10 bytes, including tag, paramsize, and result */
+	if (size < TPM_HEADER_SIZE) {
+		dev_err(chip->dev, "Unable to read header\n");
+		goto out;
+	}
+
+	expected = be32_to_cpu(*(__be32 *)(buf + 2));
+	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;
+}
+
+
+static const struct file_operations tpm_st33_spi_fops = {
+	.owner = THIS_MODULE,
+	.llseek = no_llseek,
+	.read = tpm_read,
+	.write = tpm_write,
+	.open = tpm_open,
+	.release = tpm_release,
+};
+
+static DEVICE_ATTR(pubek, S_IRUGO, tpm_show_pubek, NULL);
+static DEVICE_ATTR(pcrs, S_IRUGO, tpm_show_pcrs, NULL);
+static DEVICE_ATTR(enabled, S_IRUGO, tpm_show_enabled, NULL);
+static DEVICE_ATTR(active, S_IRUGO, tpm_show_active, NULL);
+static DEVICE_ATTR(owned, S_IRUGO, tpm_show_owned, NULL);
+static DEVICE_ATTR(temp_deactivated, S_IRUGO, tpm_show_temp_deactivated, NULL);
+static DEVICE_ATTR(caps, S_IRUGO, tpm_show_caps_1_2, NULL);
+static DEVICE_ATTR(cancel, S_IWUSR | S_IWGRP, NULL, tpm_store_cancel);
+
+static struct attribute *stm_tpm_attrs[] = {
+	&dev_attr_pubek.attr,
+	&dev_attr_pcrs.attr,
+	&dev_attr_enabled.attr,
+	&dev_attr_active.attr,
+	&dev_attr_owned.attr,
+	&dev_attr_temp_deactivated.attr,
+	&dev_attr_caps.attr,
+	&dev_attr_cancel.attr, NULL,
+};
+
+static struct attribute_group stm_tpm_attr_grp = {
+	.attrs = stm_tpm_attrs
+};
+
+static struct tpm_vendor_specific st_spi_tpm = {
+	.send = tpm_stm_spi_send,
+	.recv = tpm_stm_spi_recv,
+	.cancel = tpm_stm_spi_cancel,
+	.status = tpm_stm_spi_status,
+	.attr_group = &stm_tpm_attr_grp,
+	.miscdev = {.fops = &tpm_st33_spi_fops,},
+};
+
+static int evaluate_latency(struct tpm_chip *chip)
+{
+	int latency = 0;
+	struct spi_device *dev =
+		 (struct spi_device __force *)chip->vendor.iobase;
+	struct st33zp24_platform_data *platform_data = dev->dev.platform_data;
+	int status = 0;
+	u8 data = 0;
+
+	while (status == 0x00 && latency < MAX_SPI_LATENCY) {
+		platform_data->latency = latency;
+		status = spi_read8_reg(chip, TPM_INTF_CAPABILITY, &data, 1);
+		latency++;
+	}
+	return latency - 1;
+} /* evaluate_latency() */
+
+static int interrupts;
+module_param(interrupts, int, 0444);
+MODULE_PARM_DESC(interrupts, "Enable interrupts");
+
+static int power_mgt = 1;
+module_param(power_mgt, int, 0444);
+MODULE_PARM_DESC(power_mgt, "Power Management");
+
+/*
+ * tpm_st33_spi_probe initialize the TPM device
+ * @param: client, the spi_device drescription (TPM SPI description).
+ * @param: id, the spi_device_id struct.
+ * @return: 0 in case of success.
+ *	 -1 in other case.
+ */
+static int
+tpm_st33_spi_probe(struct spi_device *dev)
+{
+	long err = 0;
+	u8 intmask;
+	struct tpm_chip *chip;
+	struct st33zp24_platform_data *platform_data;
+
+	/* Check SPI platform functionnalities */
+	if (dev == NULL) {
+		pr_info("dev is NULL. exiting.\n");
+		err = -ENODEV;
+		goto end;
+	}
+
+	chip = tpm_register_hardware(&dev->dev, &st_spi_tpm);
+	if (!chip) {
+		err = -ENODEV;
+		goto end;
+	}
+
+	/* Allocation of SPI buffers MISO and MOSI		*/
+	/* Size is as follow:					*/
+	/* Request burstcount value  = 0x800 = 2048		*/
+	/* +							*/
+	/* Response burstcount value = 0x400 = 1024		*/
+	/* +							*/
+	/* At least:						*/
+	/* 1 byte for direction/locality			*/
+	/* 1 byte tpm tis register				*/
+	/* 2 bytes spi data length (for request only)		*/
+	/* 2 latency bytes					*/
+	/* 1 status byte					*/
+	/* = 2048 + 1024 + 7					*/
+	/* We reserved 2048 + 1024 + 20 in case latency byte	*/
+	/* change						*/
+	platform_data = dev->dev.platform_data;
+
+	if (platform_data)
+		platform_data->tpm_spi_buffer[0] =
+		kmalloc((TPM_BUFSIZE + (TPM_BUFSIZE / 2) +
+				 TPM_DIGEST_SIZE) * sizeof(u8), GFP_KERNEL);
+	else
+		goto end;
+
+	if (platform_data->tpm_spi_buffer[0] == NULL) {
+		err = -ENOMEM;
+		goto _tpm_clean_answer;
+	}
+
+	platform_data->tpm_spi_buffer[1] =
+	    kmalloc((TPM_BUFSIZE + (TPM_BUFSIZE / 2) +
+				 TPM_DIGEST_SIZE) * sizeof(u8) , GFP_KERNEL);
+
+	if (platform_data->tpm_spi_buffer[1] == NULL) {
+		err = -ENOMEM;
+		goto _tpm_clean_response;
+	}
+
+	chip->vendor.iobase = (void __iomem *)dev;
+
+	/* Default timeouts */
+	chip->vendor.timeout_a = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
+	chip->vendor.timeout_b = msecs_to_jiffies(TIS_LONG_TIMEOUT);
+	chip->vendor.timeout_c = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
+	chip->vendor.timeout_d = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
+	chip->vendor.locality = LOCALITY0;
+
+	if (power_mgt) {
+		err = gpio_request(platform_data->io_lpcpd, "TPM IO_LPCPD");
+		if (err)
+			goto _gpio_init1;
+		gpio_set_value(platform_data->io_lpcpd, 1);
+	}
+
+	platform_data->latency = evaluate_latency(chip);
+	if (platform_data->latency <= 0x00) {
+		err = -ENODEV;
+		goto _gpio_init1;
+	}
+
+	/* Enable interrupt */
+	/* Register GPIO pin through generic Linux GPIO API */
+	if (interrupts) {
+		init_completion(&platform_data->irq_detection);
+		if (request_locality(chip) != LOCALITY0) {
+			err = -ENODEV;
+			goto _tpm_clean_response;
+		}
+		err = gpio_request(platform_data->io_serirq, "TPM IO_SERIRQ");
+		if (err)
+			goto _gpio_init2;
+
+		/* Clear all existing */
+		clear_interruption(chip);
+		err = request_irq(gpio_to_irq(platform_data->io_serirq),
+							&tpm_ioserirq_handler,
+				IRQF_TRIGGER_HIGH | IRQF_SHARED,
+				"TPM SERIRQ management", chip);
+		if (err < 0) {
+			pr_info("TPM SERIRQ signals %d not available\n",
+				gpio_to_irq(platform_data->io_serirq));
+			goto _irq_set;
+		}
+		err = spi_read8_reg(chip, TPM_INT_ENABLE, &intmask, 1);
+		if (err < 0)
+			goto _irq_set;
+
+		intmask |= TPM_INTF_CMD_READY_INT
+			|  TPM_INTF_FIFO_AVALAIBLE_INT
+			|  TPM_INTF_WAKE_UP_READY_INT
+			|  TPM_INTF_LOC4SOFTRELEASE_INT
+			|  TPM_INTF_LOCALITY_CHANGE_INT
+			|  TPM_INTF_STS_VALID_INT
+			|  TPM_INTF_DATA_AVAIL_INT;
+
+		err = spi_write8_reg(chip, TPM_INT_ENABLE, &intmask, 1);
+		if (err < 0)
+			goto _irq_set;
+
+		intmask = TPM_GLOBAL_INT_ENABLE;
+		err = spi_write8_reg(chip, TPM_INT_ENABLE + 3, &intmask, 1);
+		if (err < 0)
+			goto _irq_set;
+
+		err = spi_read8_reg(chip, TPM_INT_STATUS, &intmask, 1);
+		if (err < 0)
+			goto _irq_set;
+
+		chip->vendor.irq = interrupts;
+
+		tpm_gen_interrupt(chip);
+	}
+
+	tpm_get_timeouts(chip);
+
+	/* attach chip datas to client */
+	spi_set_drvdata(dev, chip);
+	platform_data->bchipf = false;
+
+	pr_info("TPM SPI Initialized\n");
+	return 0;
+_irq_set:
+	free_irq(gpio_to_irq(platform_data->io_serirq), chip);
+_gpio_init2:
+	if (platform_data && interrupts)
+		gpio_free(platform_data->io_serirq);
+_gpio_init1:
+	if (platform_data && power_mgt)
+		gpio_free(platform_data->io_lpcpd);
+_tpm_clean_response:
+	tpm_remove_hardware(chip->dev);
+	if (platform_data->tpm_spi_buffer[1] != NULL) {
+		kfree(platform_data->tpm_spi_buffer[1]);
+		platform_data->tpm_spi_buffer[1] = NULL;
+	}
+_tpm_clean_answer:
+	if (platform_data->tpm_spi_buffer[0] != NULL) {
+		kfree(platform_data->tpm_spi_buffer[0]);
+		platform_data->tpm_spi_buffer[0] = NULL;
+	}
+
+	platform_data->bchipf = true;
+end:
+	pr_info("TPM SPI initialisation fail\n");
+	return err;
+}
+
+/*
+ * tpm_st33_spi_remove remove the TPM device
+ * @param: client, the spi_device drescription (TPM SPI description).
+		clear_bit(0, &chip->is_open);
+ * @return: 0 in case of success.
+ */
+static int tpm_st33_spi_remove(struct spi_device *client)
+{
+	struct tpm_chip *chip = (struct tpm_chip *)spi_get_drvdata(client);
+	struct st33zp24_platform_data *pin_infos =
+	 ((struct spi_device __force *)chip->vendor.iobase)->dev.platform_data;
+
+	if (pin_infos != NULL) {
+		gpio_free(pin_infos->io_lpcpd);
+
+		/* Check if chip has been previously clean */
+		if (pin_infos->bchipf != true)
+			tpm_remove_hardware(chip->dev);
+		if (pin_infos->tpm_spi_buffer[1] != NULL) {
+			kfree(pin_infos->tpm_spi_buffer[1]);
+			pin_infos->tpm_spi_buffer[1] = NULL;
+		}
+		if (pin_infos->tpm_spi_buffer[0] != NULL) {
+			kfree(pin_infos->tpm_spi_buffer[0]);
+			pin_infos->tpm_spi_buffer[0] = NULL;
+		}
+	}
+
+	return 0;
+}
+
+#ifdef CONFIG_PM_SLEEP
+/*
+ * tpm_st33_spi_pm_suspend suspend the TPM device
+ * Added: Work around when suspend and no tpm application is running, suspend
+ * may fail because chip->data_buffer is not set (only set in tpm_open in Linux
+ * TPM core)
+ * @return: 0 in case of success.
+ */
+static int tpm_st33_spi_pm_suspend(struct device *dev)
+{
+	struct tpm_chip *chip = dev_get_drvdata(dev);
+	struct st33zp24_platform_data *pin_infos = dev->platform_data;
+	int ret = 0;
+
+	if (power_mgt) {
+		gpio_set_value(pin_infos->io_lpcpd, 0);
+	} else {
+		if (chip->data_buffer == NULL)
+			chip->data_buffer = pin_infos->tpm_spi_buffer[0];
+		ret = tpm_pm_suspend(dev);
+		}
+	return ret;
+}				/* tpm_st33_spi_suspend() */
+
+/*
+ * tpm_st33_spi_pm_resume resume the TPM device
+ * @param: spi, the spi_device drescription (TPM SPI description).
+ * @return: 0 in case of success.
+ */
+static int tpm_st33_spi_pm_resume(struct device *dev)
+{
+	struct tpm_chip *chip = dev_get_drvdata(dev);
+	struct st33zp24_platform_data *pin_infos = dev->platform_data;
+
+	int ret = 0;
+	if (power_mgt) {
+		gpio_set_value(pin_infos->io_lpcpd, 1);
+		ret = wait_for_serirq_timeout(chip,
+					 (chip->vendor.status(chip) &
+					  TPM_STS_VALID) == TPM_STS_VALID,
+					  chip->vendor.timeout_b);
+	} else {
+		if (chip->data_buffer == NULL)
+			chip->data_buffer = pin_infos->tpm_spi_buffer[0];
+		ret = tpm_pm_resume(dev);
+		if (!ret)
+			tpm_do_selftest(chip);
+	}
+	return ret;
+} /* tpm_st33_spi_pm_resume() */
+#endif
+
+static SIMPLE_DEV_PM_OPS(tpm_st33_spi_ops, tpm_st33_spi_pm_suspend,
+	 tpm_st33_spi_pm_resume);
+static struct spi_driver tpm_st33_spi_driver = {
+	.driver = {
+		   .owner = THIS_MODULE,
+		   .name = TPM_ST33_SPI,
+		   .pm = &tpm_st33_spi_ops,
+		   },
+	.probe = tpm_st33_spi_probe,
+	.remove = tpm_st33_spi_remove,
+};
+
+module_spi_driver(tpm_st33_spi_driver);
+
+MODULE_AUTHOR("Christophe Ricard (tpmsupport@st.com)");
+MODULE_DESCRIPTION("STM TPM SPI ST33 Driver");
+MODULE_VERSION("1.2.0");
diff --git a/drivers/char/tpm/tpm_spi_stm_st33.h b/drivers/char/tpm/tpm_spi_stm_st33.h
new file mode 100644
index 0000000..4ae5ba6
--- /dev/null
+++ b/drivers/char/tpm/tpm_spi_stm_st33.h
@@ -0,0 +1,61 @@
+/*
+ * STMicroelectronics TPM SPI Linux driver for TPM ST33NP18
+ * Copyright (C) 2009, 2010  STMicroelectronics
+ *
+ * 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.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * STMicroelectronics version 1.2.0, Copyright (C) 2010
+ * STMicroelectronics comes with ABSOLUTELY NO WARRANTY.
+ * This is free software, and you are welcome to redistribute it
+ * under certain conditions.
+ *
+ * @Author: Christophe RICARD tpmsupport@st.com
+ *
+ * @File: tpm_spi_stm_st33.h
+ *
+ * @Date: 09/15/2010
+ */
+#ifndef __STM_ST33_TPM_SPI_MAIN_H__
+#define __STM_ST33_TPM_SPI_MAIN_H__
+
+#define TPM_ACCESS			(0x0)
+#define TPM_STS				(0x18)
+#define TPM_DATA_FIFO			(0x24)
+#define TPM_HASH_DATA			(0x24)
+#define TPM_INTF_CAPABILITY		(0x14)
+#define TPM_INT_STATUS			(0x10)
+#define TPM_INT_ENABLE			(0x08)
+
+#define TPM_DUMMY_BYTE			0x00
+#define TPM_WRITE_DIRECTION		0x80
+#define TPM_HEADER_SIZE			10
+#define TPM_BUFSIZE			2048
+
+#define LOCALITY0		0
+
+#define MAX_SPI_LATENCY		15
+
+#define TPM_ST33_SPI		"st33zp24_spi"
+
+struct st33zp24_platform_data {
+	int io_serirq;
+	int io_lpcpd;
+	int latency;
+	bool bchipf;
+	u8 *tpm_spi_buffer[2]; /* 0 Request 1 Response */
+	struct completion irq_detection;
+};
+
+#endif /* __STM_ST33_TPM_SPI_MAIN_H__ */
-- 
1.7.10.4


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

* [PATCH 1/1] TPM: STMicroelectronics st33 driver SPI
@ 2013-05-15 13:53 Matthias Leblanc
  0 siblings, 0 replies; 21+ messages in thread
From: Matthias Leblanc @ 2013-05-15 13:53 UTC (permalink / raw)
  To: Kent Yoder, Rajiv Andrade, Marcel Selhorst, Sirrix AG, tpmdd-devel
  Cc: linux-kernel, Jean-Luc Blanc, Mathias Leblanc

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

 * STMicroelectronics version 1.2.0, Copyright (C) 2010
 * 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 SPI, in menuconfig or .config choose the tpm driver on
device --> tpm and activate the protocol of your choice before compiling
the kernel.
The driver will be accessible from within Linux.

Tested on linux x86/x64 and beagleboard REV B & XM REV C

Signed-off-by: Mathias Leblanc <mathias.leblanc@st.com>
---
Remove unused variables
Remove tpm_st33_spi_exit function that cause compilation error
Initalise data and temp variable
Change some variable type
Correction of warnings and errors from smatch tools 

 drivers/char/tpm/Kconfig            |   10 +
 drivers/char/tpm/Makefile           |    1 +
 drivers/char/tpm/tpm_spi_stm_st33.c |  948 +++++++++++++++++++++++++++++++++++
 drivers/char/tpm/tpm_spi_stm_st33.h |   61 +++
 4 files changed, 1020 insertions(+), 0 deletions(-)
 create mode 100644 drivers/char/tpm/tpm_spi_stm_st33.c
 create mode 100644 drivers/char/tpm/tpm_spi_stm_st33.h

diff --git a/drivers/char/tpm/Kconfig b/drivers/char/tpm/Kconfig
index dbfd564..2fc1254 100644
--- a/drivers/char/tpm/Kconfig
+++ b/drivers/char/tpm/Kconfig
@@ -91,4 +91,14 @@ config TCG_ST33_I2C
         To compile this driver as a module, choose M here; the module will be
         called tpm_stm_st33_i2c.
 
+config TCG_ST33_SPI
+       tristate "STMicroelectronics ST33 SPI"
+       depends on SPI
+       depends on GPIOLIB
+       ---help---
+       If you have a TPM security chip from STMicroelectronics working with
+       an SPI bus say Yes and it will be accessible from within Linux.
+       To compile this driver as a module, choose M here; the module will be
+       called tpm_spi_stm_st33.
+
 endif # TCG_TPM
diff --git a/drivers/char/tpm/Makefile b/drivers/char/tpm/Makefile
index a3736c9..a1db3dd 100644
--- a/drivers/char/tpm/Makefile
+++ b/drivers/char/tpm/Makefile
@@ -18,3 +18,4 @@ obj-$(CONFIG_TCG_ATMEL) += tpm_atmel.o
 obj-$(CONFIG_TCG_INFINEON) += tpm_infineon.o
 obj-$(CONFIG_TCG_IBMVTPM) += tpm_ibmvtpm.o
 obj-$(CONFIG_TCG_ST33_I2C) += tpm_i2c_stm_st33.o
+obj-$(CONFIG_TCG_ST33_SPI) += tpm_spi_stm_st33.o
diff --git a/drivers/char/tpm/tpm_spi_stm_st33.c b/drivers/char/tpm/tpm_spi_stm_st33.c
new file mode 100644
index 0000000..192ac1c
--- /dev/null
+++ b/drivers/char/tpm/tpm_spi_stm_st33.c
@@ -0,0 +1,948 @@
+/*
+ * STMicroelectronics TPM SPI Linux driver for TPM ST33ZP24
+ * Copyright (C) 2009, 2010  STMicroelectronics
+ *
+ * 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.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * STMicroelectronics version 1.2.0, Copyright (C) 2010
+ * STMicroelectronics comes with ABSOLUTELY NO WARRANTY.
+ * This is free software, and you are welcome to redistribute it
+ * under certain conditions.
+ *
+ * @Author: Christophe RICARD tpmsupport@st.com
+ *
+ * @File: tpm_stm_st33_spi.c
+ *
+ * @Synopsis:
+ *	09/15/2010:	First shot driver tpm_tis driver for lpc is
+ *			used as model.
+ */
+
+#include <linux/kernel.h>
+#include <linux/init.h>
+#include <linux/spi/spi.h>
+#include <linux/wait.h>
+#include <linux/string.h>
+#include <linux/interrupt.h>
+#include <linux/spinlock.h>
+#include <linux/sysfs.h>
+#include <linux/gpio.h>
+#include <linux/uaccess.h>
+#include <linux/slab.h>
+#include <linux/sched.h>
+
+#include "tpm.h"
+
+#include "tpm_spi_stm_st33.h"
+
+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 */
+};
+
+static inline int spi_read_write(struct spi_device *spi,
+	 struct spi_transfer xfer) {
+	struct spi_message msg;
+	spi_message_init(&msg);
+	spi_message_add_tail(&xfer, &msg);
+	return spi_sync(spi, &msg);
+}
+
+/*
+ * spi_write8_reg
+ * Send byte to the TIS register according to the ST33ZP24 SPI protocol.
+ * @param: tpm, 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: should be zero if success else a negative error code.
+ */
+static int spi_write8_reg(struct tpm_chip *tpm, u8 tpm_register,
+		      u8 *tpm_data, u16 tpm_size)
+{
+	u8 data = 0;
+	int total_length = 0, nbr_dummy_bytes;
+	int value = 0;
+	struct spi_device *dev = (struct spi_device *)tpm->vendor.iobase;
+	struct st33zp24_platform_data *platform_data = dev->dev.platform_data;
+	u8 *data_buffer = platform_data->tpm_spi_buffer[0];
+	struct spi_transfer xfer = {
+		.tx_buf	 = data_buffer,
+		.rx_buf	 = platform_data->tpm_spi_buffer[1],
+	};
+
+	u8 latency = platform_data->latency + tpm_size + 1;
+
+	/* Pre-Header */
+	data = TPM_WRITE_DIRECTION | tpm->vendor.locality;
+	memcpy(data_buffer + total_length, &data, sizeof(data));
+	total_length++;
+	data = tpm_register;
+	memcpy(data_buffer + total_length, &data, sizeof(data));
+	total_length++;
+
+	if (tpm_size > 0 &&
+	 (tpm_register == TPM_DATA_FIFO || tpm_register == TPM_HASH_DATA)) {
+			data = (tpm_size >> 8) & 0x00ff;
+			data_buffer[total_length++] = data;
+			data = tpm_size & 0x00ff;
+			data_buffer[total_length++] = data;
+			latency -= 2;
+	}
+
+	memcpy(&data_buffer[total_length], tpm_data, tpm_size);
+	total_length += tpm_size;
+
+	nbr_dummy_bytes = platform_data->latency + 1;
+	memset(&data_buffer[total_length], TPM_DUMMY_BYTE,
+					 platform_data->latency + 1);
+
+	xfer.len = total_length;
+
+	value = spi_read_write(dev, xfer);
+
+	if (value == 0) {
+		nbr_dummy_bytes = total_length - 1 - nbr_dummy_bytes;
+		while (nbr_dummy_bytes < total_length &&
+			 ((u8 *)xfer.rx_buf)[nbr_dummy_bytes] == 0)
+					nbr_dummy_bytes++;
+
+		if (((u8 *)xfer.rx_buf)[nbr_dummy_bytes] != 0)
+			value = ((u8 *)xfer.rx_buf)[nbr_dummy_bytes];
+	}
+
+	return value;
+} /* spi_write8_reg() */
+
+/*
+ * spi_read8_reg
+ * Recv byte from the TIS register according to the ST33ZP24 SPI 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: should be zero if success else a negative error code.
+ */
+static u8 spi_read8_reg(struct tpm_chip *tpm, u8 tpm_register,
+		    u8 *tpm_data, u16 tpm_size)
+{
+	u8 data = 0;
+	int total_length = 0, nbr_dummy_bytes;
+	int value = 0;
+	struct spi_device *dev = (struct spi_device *)tpm->vendor.iobase;
+	struct st33zp24_platform_data *platform_data = dev->dev.platform_data;
+	u8 *data_buffer = platform_data->tpm_spi_buffer[0];
+	struct spi_transfer xfer = {
+		.tx_buf	 = data_buffer,
+		.rx_buf	 = platform_data->tpm_spi_buffer[1],
+	};
+
+	/* Pre-Header */
+
+	data = tpm->vendor.locality;
+	memcpy(data_buffer + total_length, &data, sizeof(data));
+	total_length++;
+	data = tpm_register;
+	memcpy(data_buffer + total_length, &data, sizeof(data));
+	total_length++;
+
+	nbr_dummy_bytes = platform_data->latency + 1;
+	memset(&data_buffer[total_length], TPM_DUMMY_BYTE,
+		 platform_data->latency + 1);
+
+	xfer.len = total_length;
+
+	/* header + status byte + size of the data + status byte */
+	value = spi_read_write(dev, xfer);
+
+	if (tpm_size > 0 && value == 0) {
+		nbr_dummy_bytes = 2;
+		for (; nbr_dummy_bytes < total_length &&
+			 ((u8 *)xfer.rx_buf)[nbr_dummy_bytes] == 0;
+			 nbr_dummy_bytes++)
+			;
+
+		if (nbr_dummy_bytes + 1 < total_length) {
+			value = ((u8 *)xfer.rx_buf)[nbr_dummy_bytes];
+
+			if (tpm_size > 0)
+				memcpy(tpm_data, xfer.rx_buf +
+							 nbr_dummy_bytes + 1,
+								 tpm_size);
+		}
+	}
+	return value;
+} /* spi_read8_reg() */
+
+/*
+ * clear_interruption
+ * clear the TPM interrupt register.
+ * @param: tpm, the chip description
+ */
+static void clear_interruption(struct tpm_chip *tpm)
+{
+	u8 interrupt;
+	spi_read8_reg(tpm, TPM_INT_STATUS, &interrupt, 1);
+	/* Clear interrupts handled with TPM_EOI */
+	spi_write8_reg(tpm, TPM_INT_STATUS, &interrupt, 1);
+	spi_read8_reg(tpm, TPM_INT_STATUS, &interrupt, 1);
+} /* clear_interruption() */
+
+/*
+ * _wait_for_serirq_timeout
+ * @param: tpm, the chip description
+ * @param: timeout, the timeout of the interrupt
+ * @return: the status of the interruption.
+ */
+unsigned long wait_for_serirq_timeout(struct tpm_chip *chip, bool condition,
+				 unsigned long timeout)
+{
+	long status = 0;
+	struct spi_device *client;
+	struct st33zp24_platform_data *pin_infos;
+
+	client = (struct spi_device *)chip->vendor.iobase;
+	pin_infos = client->dev.platform_data;
+
+	status = wait_for_completion_interruptible_timeout(
+				&pin_infos->irq_detection, timeout);
+	if (status > 0)
+		enable_irq(gpio_to_irq(pin_infos->io_serirq));
+	gpio_direction_input(pin_infos->io_serirq);
+
+	if (!status) {
+		status = -EBUSY;
+		goto wait_end;
+	}
+	clear_interruption(chip);
+	if (condition)
+		status = 1;
+
+wait_end:
+	return status;
+}
+
+
+/*
+ * tpm_stm_spi_cancel, cancel is not implemented.
+ * @param: chip, the tpm chip description as specified in
+ * driver/char/tpm/tpm.h.
+ */
+static void tpm_stm_spi_cancel(struct tpm_chip *chip)
+{
+	u8 data = TPM_STS_COMMAND_READY;
+
+	/* this causes the current command to be aborted */
+	spi_write8_reg(chip, TPM_STS, &data, 1);
+} /* tpm_stm_spi_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_spi_status(struct tpm_chip *chip)
+{
+	u8 data = 0;
+
+	spi_read8_reg(chip, TPM_STS, &data, 1);
+	return data;
+} /* tpm_stm_spi_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 = 0;
+	u8 status;
+
+	status = spi_read8_reg(chip, TPM_ACCESS, &data, 1);
+	if (status && (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 stop;
+	long rc;
+	u8 data = 0;
+
+	/* Check locality */
+	if (check_locality(chip) == chip->vendor.locality)
+		return chip->vendor.locality;
+
+	/* Request locality */
+	data = TPM_ACCESS_REQUEST_USE;
+	rc = spi_write8_reg(chip, TPM_ACCESS, &data, 1);
+	if (rc < 0)
+		goto end;
+
+	/* wait for burstcount */
+	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 {
+		stop = jiffies + chip->vendor.timeout_a;
+		do {
+			if (check_locality(chip) >= 0)
+				return chip->vendor.locality;
+			msleep(TPM_TIMEOUT);
+		} while (time_before(jiffies, stop));
+	}
+end:
+	return -EACCES;
+} /* request_locality() */
+
+/*
+ * release_locality release the active locality
+ * @param: chip, the tpm chip description.
+ */
+static void release_locality(struct tpm_chip *chip)
+{
+	u8 data = 0;
+
+	data = TPM_ACCESS_ACTIVE_LOCALITY;
+	spi_write8_reg(chip, TPM_ACCESS, &data, 1);
+} /* release_locality()*/
+
+/*
+ * 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 stop;
+	u32 burstcnt;
+	u8 tpm_reg, temp = 0;
+	long status = 0;
+
+	/* wait for burstcount */
+	/* which timeout value, spec has 2 answers (c & d) */
+	stop = jiffies + chip->vendor.timeout_d;
+	do {
+		tpm_reg = TPM_STS + 1;
+		status = spi_read8_reg(chip, tpm_reg, &temp, 1);
+		if (status < 0)
+			goto end;
+
+		tpm_reg = tpm_reg + 1;
+		burstcnt = temp;
+		status = spi_read8_reg(chip, tpm_reg, &temp, 1);
+		if (status < 0)
+			goto end;
+
+		burstcnt |= temp << 8;
+		if (burstcnt)
+			return burstcnt;
+		msleep(TPM_TIMEOUT);
+	} while (time_before(jiffies, stop));
+
+end:
+	return -EBUSY;
+} /* get_burstcount() */
+
+/*
+ * wait_for_stat wait for a TPM_STS value
+ * @param: chip, the tpm chip description
+ * @param: mask, the value mask to wait
+ * @param: timeout, the timeout
+ * @param: queue, the wait queue.
+ * @return: the tpm status, 0 if success, -ETIME if timeout is reached.
+ */
+static int wait_for_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout,
+			 wait_queue_head_t *queue)
+{
+	unsigned long stop;
+	long rc;
+	u8 status;
+
+	/* check current status */
+	status = tpm_stm_spi_status(chip);
+	if (!chip->vendor.irq && (status & mask) == mask)
+		return 0;
+
+	if (chip->vendor.irq) {
+		rc = wait_for_serirq_timeout(chip, ((tpm_stm_spi_status
+						    (chip) & mask) ==
+						     mask), timeout);
+		if (rc > 0)
+			return 0;
+	} else {
+		stop = jiffies + timeout;
+		do {
+			msleep(TPM_TIMEOUT);
+			status = tpm_stm_spi_status(chip);
+			if ((status & mask) == mask)
+				return 0;
+		} while (time_before(jiffies, stop));
+	}
+	return -ETIME;
+} /* wait_for_stat() */
+
+/*
+ * 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 tpm status, 0 if success, -ETIME if timeout is reached.
+ */
+static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count)
+{
+	u32 size = 0, burstcnt, len;
+	long status = 0;
+
+	while (size < count &&
+	       wait_for_stat(chip,
+			     TPM_STS_DATA_AVAIL | TPM_STS_VALID,
+			     chip->vendor.timeout_c,
+			     &chip->vendor.read_queue)
+						== 0) {
+		burstcnt = get_burstcount(chip);
+		len = min_t(int, burstcnt, count - size);
+		status = spi_read8_reg(chip, TPM_DATA_FIFO, buf + size, len);
+		if (status < 0)
+			return status;
+
+
+		size += len;
+	}
+	return size;
+}
+
+/*
+ * tpm_ioserirq_handler the serirq irq handler
+ * @param: irq, the tpm chip description
+ * @param: dev_id, the description of the chip
+ * @return: the status of the handler.
+ */
+static irqreturn_t tpm_ioserirq_handler(int irq, void *dev_id)
+{
+	struct tpm_chip *chip = dev_id;
+	struct spi_device *client;
+	struct st33zp24_platform_data *pin_infos;
+
+	disable_irq_nosync(irq);
+
+	client = (struct spi_device *)chip->vendor.iobase;
+	pin_infos = client->dev.platform_data;
+
+	complete(&pin_infos->irq_detection);
+	return IRQ_HANDLED;
+} /* tpm_ioserirq_handler() */
+
+/*
+ * tpm_stm_spi_send send TPM commands through the SPI bus.
+ * @param: chip, the tpm chip description
+ * @param: buf, the tpm command buffer
+ * @param: len, the tpm command size
+ * @return: should be zero if success else the negative error code.
+ */
+static int tpm_stm_spi_send(struct tpm_chip *chip, unsigned char *buf,
+			    size_t len)
+{
+	u32 burstcnt = 0, i, size = 0;
+	u8 data = 0;
+	long status = 0, ret = 0;
+
+	if (chip == NULL)
+		return -EBUSY;
+	if (len < TPM_HEADER_SIZE)
+		return -EBUSY;
+
+	ret = request_locality(chip);
+	if (ret < 0)
+		return ret;
+
+	status = tpm_stm_spi_status(chip);
+	if ((status & TPM_STS_COMMAND_READY) == 0) {
+		tpm_stm_spi_cancel(chip);
+		if (wait_for_stat
+		    (chip, TPM_STS_COMMAND_READY, chip->vendor.timeout_b,
+		     &chip->vendor.int_queue) < 0) {
+			ret = -ETIME;
+			goto out_err;
+		}
+	}
+
+	for (i = 0; i < len - 1;) {
+		burstcnt = get_burstcount(chip);
+		size = min_t(int, len - i - 1, burstcnt);
+		ret = spi_write8_reg(chip, TPM_DATA_FIFO, buf, size);
+		if (ret < 0)
+			goto out_err;
+		i += size;
+	}
+
+	status = tpm_stm_spi_status(chip);
+	if ((status & TPM_STS_DATA_EXPECT) == 0) {
+		ret = -EIO;
+		goto out_err;
+	}
+
+	/* write last byte */
+	spi_write8_reg(chip, TPM_DATA_FIFO, buf + len - 1, 1);
+
+	status = tpm_stm_spi_status(chip);
+	if ((status & TPM_STS_DATA_EXPECT) != 0) {
+		ret = -EIO;
+		goto out_err;
+	}
+
+	/* go and do it */
+	data = TPM_STS_GO;
+	ret = spi_write8_reg(chip, TPM_STS, &data, 1);
+	if (ret < 0)
+		goto out_err;
+
+	return len;
+out_err:
+	tpm_stm_spi_cancel(chip);
+	release_locality(chip);
+	return ret;
+}
+
+/*
+ * tpm_stm_spi_recv received TPM response through the SPI bus.
+ * @param: chip, the tpm chip description
+ * @param: buf, the tpm command buffer
+ * @param: len, the tpm command size
+* @return: should be zero if success else the negative error code.
+ */
+static int tpm_stm_spi_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);
+
+	/* read first 10 bytes, including tag, paramsize, and result */
+	if (size < TPM_HEADER_SIZE) {
+		dev_err(chip->dev, "Unable to read header\n");
+		goto out;
+	}
+
+	expected = be32_to_cpu(*(__be32 *)(buf + 2));
+	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;
+}
+
+
+static const struct file_operations tpm_st33_spi_fops = {
+	.owner = THIS_MODULE,
+	.llseek = no_llseek,
+	.read = tpm_read,
+	.write = tpm_write,
+	.open = tpm_open,
+	.release = tpm_release,
+};
+
+static DEVICE_ATTR(pubek, S_IRUGO, tpm_show_pubek, NULL);
+static DEVICE_ATTR(pcrs, S_IRUGO, tpm_show_pcrs, NULL);
+static DEVICE_ATTR(enabled, S_IRUGO, tpm_show_enabled, NULL);
+static DEVICE_ATTR(active, S_IRUGO, tpm_show_active, NULL);
+static DEVICE_ATTR(owned, S_IRUGO, tpm_show_owned, NULL);
+static DEVICE_ATTR(temp_deactivated, S_IRUGO, tpm_show_temp_deactivated, NULL);
+static DEVICE_ATTR(caps, S_IRUGO, tpm_show_caps_1_2, NULL);
+static DEVICE_ATTR(cancel, S_IWUSR | S_IWGRP, NULL, tpm_store_cancel);
+
+static struct attribute *stm_tpm_attrs[] = {
+	&dev_attr_pubek.attr,
+	&dev_attr_pcrs.attr,
+	&dev_attr_enabled.attr,
+	&dev_attr_active.attr,
+	&dev_attr_owned.attr,
+	&dev_attr_temp_deactivated.attr,
+	&dev_attr_caps.attr,
+	&dev_attr_cancel.attr, NULL,
+};
+
+static struct attribute_group stm_tpm_attr_grp = {
+	.attrs = stm_tpm_attrs
+};
+
+static struct tpm_vendor_specific st_spi_tpm = {
+	.send = tpm_stm_spi_send,
+	.recv = tpm_stm_spi_recv,
+	.cancel = tpm_stm_spi_cancel,
+	.status = tpm_stm_spi_status,
+	.attr_group = &stm_tpm_attr_grp,
+	.miscdev = {.fops = &tpm_st33_spi_fops,},
+};
+
+static int evaluate_latency(struct tpm_chip *chip)
+{
+	int latency = 0;
+	struct spi_device *dev = (struct spi_device *)chip->vendor.iobase;
+	struct st33zp24_platform_data *platform_data = dev->dev.platform_data;
+	int status = 0;
+	u8 data = 0;
+
+	for (; status == 0x00 && latency < MAX_SPI_LATENCY; latency++) {
+		platform_data->latency = latency;
+		status = spi_read8_reg(chip, TPM_INTF_CAPABILITY, &data, 1);
+	}
+	return latency - 1;
+} /* evaluate_latency() */
+
+static int interrupts;
+module_param(interrupts, int, 0444);
+MODULE_PARM_DESC(interrupts, "Enable interrupts");
+
+static int power_mgt = 1;
+module_param(power_mgt, int, 0444);
+MODULE_PARM_DESC(power_mgt, "Power Management");
+
+/*
+ * tpm_st33_spi_probe initialize the TPM device
+ * @param: client, the spi_device drescription (TPM SPI description).
+ * @param: id, the spi_device_id struct.
+ * @return: 0 in case of success.
+ *	 -1 in other case.
+ */
+static int
+tpm_st33_spi_probe(struct spi_device *dev)
+{
+	long err = 0;
+	u8 intmask;
+	struct tpm_chip *chip;
+	struct st33zp24_platform_data *platform_data;
+
+	/* Check SPI platform functionnalities */
+	if (dev == NULL) {
+		pr_info("dev is NULL. exiting.\n");
+		err = -ENODEV;
+		goto end;
+	}
+
+	chip = tpm_register_hardware(&dev->dev, &st_spi_tpm);
+	if (!chip) {
+		err = -ENODEV;
+		goto end;
+	}
+
+	/* Allocation of SPI buffers MISO and MOSI		*/
+	/* Size is as follow:					*/
+	/* Request burstcount value  = 0x800 = 2048		*/
+	/* +							*/
+	/* Response burstcount value = 0x400 = 1024		*/
+	/* +							*/
+	/* At least:						*/
+	/* 1 byte for direction/locality			*/
+	/* 1 byte tpm tis register				*/
+	/* 2 bytes spi data length (for request only)		*/
+	/* 2 latency bytes					*/
+	/* 1 status byte					*/
+	/* = 2048 + 1024 + 7					*/
+	/* We reserved 2048 + 1024 + 20 in case latency byte	*/
+	/* change						*/
+	platform_data = dev->dev.platform_data;
+
+	if (platform_data)
+		platform_data->tpm_spi_buffer[0] =
+		kmalloc((TPM_BUFSIZE + (TPM_BUFSIZE / 2) +
+				 TPM_DIGEST_SIZE) * sizeof(u8), GFP_KERNEL);
+	else
+		goto end;
+
+	if (platform_data->tpm_spi_buffer[0] == NULL) {
+		err = -ENOMEM;
+		goto _tpm_clean_answer;
+	}
+
+	platform_data->tpm_spi_buffer[1] =
+	    kmalloc((TPM_BUFSIZE + (TPM_BUFSIZE / 2) +
+				 TPM_DIGEST_SIZE) * sizeof(u8) , GFP_KERNEL);
+
+	if (platform_data->tpm_spi_buffer[1] == NULL) {
+		err = -ENOMEM;
+		goto _tpm_clean_response;
+	}
+
+	chip->vendor.iobase = dev;
+
+	/* Default timeouts */
+	chip->vendor.timeout_a = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
+	chip->vendor.timeout_b = msecs_to_jiffies(TIS_LONG_TIMEOUT);
+	chip->vendor.timeout_c = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
+	chip->vendor.timeout_d = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
+	chip->vendor.locality = LOCALITY0;
+
+	if (power_mgt) {
+		err = gpio_request(platform_data->io_lpcpd, "TPM IO_LPCPD");
+		if (err)
+			goto _gpio_init1;
+		gpio_set_value(platform_data->io_lpcpd, 1);
+	}
+
+	platform_data->latency = evaluate_latency(chip);
+	if (platform_data->latency <= 0x00) {
+		err = -ENODEV;
+		goto _gpio_init1;
+	}
+
+	/* Enable interrupt */
+	/* Register GPIO pin through generic Linux GPIO API */
+	if (interrupts) {
+		init_completion(&platform_data->irq_detection);
+		if (request_locality(chip) != LOCALITY0) {
+			err = -ENODEV;
+			goto _tpm_clean_response;
+		}
+		err = gpio_request(platform_data->io_serirq, "TPM IO_SERIRQ");
+		if (err)
+			goto _gpio_init2;
+
+		/* Clear all existing */
+		clear_interruption(chip);
+		err = request_irq(gpio_to_irq(platform_data->io_serirq),
+							&tpm_ioserirq_handler,
+				IRQF_TRIGGER_HIGH | IRQF_SHARED,
+				"TPM SERIRQ management", chip);
+		if (err < 0) {
+			pr_info("TPM SERIRQ signals %d not available\n",
+				gpio_to_irq(platform_data->io_serirq));
+			goto _irq_set;
+		}
+		err = spi_read8_reg(chip, TPM_INT_ENABLE, &intmask, 1);
+		if (err < 0)
+			goto _irq_set;
+
+		intmask |= TPM_INTF_CMD_READY_INT
+			|  TPM_INTF_FIFO_AVALAIBLE_INT
+			|  TPM_INTF_WAKE_UP_READY_INT
+			|  TPM_INTF_LOC4SOFTRELEASE_INT
+			|  TPM_INTF_LOCALITY_CHANGE_INT
+			|  TPM_INTF_STS_VALID_INT
+			|  TPM_INTF_DATA_AVAIL_INT;
+
+		err = spi_write8_reg(chip, TPM_INT_ENABLE, &intmask, 1);
+		if (err < 0)
+			goto _irq_set;
+
+		intmask = TPM_GLOBAL_INT_ENABLE;
+		err = spi_write8_reg(chip, TPM_INT_ENABLE + 3, &intmask, 1);
+		if (err < 0)
+			goto _irq_set;
+
+		err = spi_read8_reg(chip, TPM_INT_STATUS, &intmask, 1);
+		if (err < 0)
+			goto _irq_set;
+
+		chip->vendor.irq = interrupts;
+
+		tpm_gen_interrupt(chip);
+	}
+
+	tpm_get_timeouts(chip);
+
+	/* attach chip datas to client */
+	spi_set_drvdata(dev, chip);
+	platform_data->bchipf = false;
+
+	pr_info("TPM SPI Initialized\n");
+	return 0;
+_irq_set:
+	free_irq(gpio_to_irq(platform_data->io_serirq), chip);
+_gpio_init2:
+	if (platform_data && interrupts)
+		gpio_free(platform_data->io_serirq);
+_gpio_init1:
+	if (platform_data && power_mgt)
+		gpio_free(platform_data->io_lpcpd);
+_tpm_clean_response:
+	tpm_remove_hardware(chip->dev);
+	if (platform_data->tpm_spi_buffer[1] != NULL) {
+		kfree(platform_data->tpm_spi_buffer[1]);
+		platform_data->tpm_spi_buffer[1] = NULL;
+	}
+_tpm_clean_answer:
+	if (platform_data->tpm_spi_buffer[0] != NULL) {
+		kfree(platform_data->tpm_spi_buffer[0]);
+		platform_data->tpm_spi_buffer[0] = NULL;
+	}
+
+	platform_data->bchipf = true;
+end:
+	pr_info("TPM SPI initialisation fail\n");
+	return err;
+}
+
+/*
+ * tpm_st33_spi_remove remove the TPM device
+ * @param: client, the spi_device drescription (TPM SPI description).
+		clear_bit(0, &chip->is_open);
+ * @return: 0 in case of success.
+ */
+static int tpm_st33_spi_remove(struct spi_device *client)
+{
+	struct tpm_chip *chip = (struct tpm_chip *)spi_get_drvdata(client);
+	struct st33zp24_platform_data *pin_infos =
+		((struct spi_device *)chip->vendor.iobase)->dev.platform_data;
+
+	if (pin_infos != NULL) {
+		gpio_free(pin_infos->io_lpcpd);
+
+		/* Check if chip has been previously clean */
+		if (pin_infos->bchipf != true)
+			tpm_remove_hardware(chip->dev);
+		if (pin_infos->tpm_spi_buffer[1] != NULL) {
+			kfree(pin_infos->tpm_spi_buffer[1]);
+			pin_infos->tpm_spi_buffer[1] = NULL;
+		}
+		if (pin_infos->tpm_spi_buffer[0] != NULL) {
+			kfree(pin_infos->tpm_spi_buffer[0]);
+			pin_infos->tpm_spi_buffer[0] = NULL;
+		}
+	}
+
+	return 0;
+}
+
+#ifdef CONFIG_PM_SLEEP
+/*
+ * tpm_st33_spi_pm_suspend suspend the TPM device
+ * Added: Work around when suspend and no tpm application is running, suspend
+ * may fail because chip->data_buffer is not set (only set in tpm_open in Linux
+ * TPM core)
+ * @return: 0 in case of success.
+ */
+static int tpm_st33_spi_pm_suspend(struct device *dev)
+{
+	struct tpm_chip *chip = dev_get_drvdata(dev);
+	struct st33zp24_platform_data *pin_infos = dev->platform_data;
+	int ret = 0;
+
+	if (power_mgt) {
+		gpio_set_value(pin_infos->io_lpcpd, 0);
+	} else {
+		if (chip->data_buffer == NULL)
+			chip->data_buffer = pin_infos->tpm_spi_buffer[0];
+		ret = tpm_pm_suspend(dev);
+		}
+	return ret;
+}				/* tpm_st33_spi_suspend() */
+
+/*
+ * tpm_st33_spi_pm_resume resume the TPM device
+ * @param: spi, the spi_device drescription (TPM SPI description).
+ * @return: 0 in case of success.
+ */
+static int tpm_st33_spi_pm_resume(struct device *dev)
+{
+	struct tpm_chip *chip = dev_get_drvdata(dev);
+	struct st33zp24_platform_data *pin_infos = dev->platform_data;
+
+	int ret = 0;
+	if (power_mgt) {
+		gpio_set_value(pin_infos->io_lpcpd, 1);
+		ret = wait_for_serirq_timeout(chip,
+					 (chip->vendor.status(chip) &
+					  TPM_STS_VALID) == TPM_STS_VALID,
+					  chip->vendor.timeout_b);
+	} else {
+		if (chip->data_buffer == NULL)
+			chip->data_buffer = pin_infos->tpm_spi_buffer[0];
+		ret = tpm_pm_resume(dev);
+		if (!ret)
+			tpm_do_selftest(chip);
+	}
+	return ret;
+} /* tpm_st33_spi_pm_resume() */
+#endif
+
+static SIMPLE_DEV_PM_OPS(tpm_st33_spi_ops, tpm_st33_spi_pm_suspend,
+	 tpm_st33_spi_pm_resume);
+static struct spi_driver tpm_st33_spi_driver = {
+	.driver = {
+		   .owner = THIS_MODULE,
+		   .name = TPM_ST33_SPI,
+		   .pm = &tpm_st33_spi_ops,
+		   },
+	.probe = tpm_st33_spi_probe,
+	.remove = tpm_st33_spi_remove,
+};
+
+module_spi_driver(tpm_st33_spi_driver);
+
+MODULE_AUTHOR("Christophe Ricard (tpmsupport@st.com)");
+MODULE_DESCRIPTION("STM TPM SPI ST33 Driver");
+MODULE_VERSION("1.2.0");
+MODULE_LICENSE("GPL");
diff --git a/drivers/char/tpm/tpm_spi_stm_st33.h b/drivers/char/tpm/tpm_spi_stm_st33.h
new file mode 100644
index 0000000..4ae5ba6
--- /dev/null
+++ b/drivers/char/tpm/tpm_spi_stm_st33.h
@@ -0,0 +1,61 @@
+/*
+ * STMicroelectronics TPM SPI Linux driver for TPM ST33NP18
+ * Copyright (C) 2009, 2010  STMicroelectronics
+ *
+ * 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.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * STMicroelectronics version 1.2.0, Copyright (C) 2010
+ * STMicroelectronics comes with ABSOLUTELY NO WARRANTY.
+ * This is free software, and you are welcome to redistribute it
+ * under certain conditions.
+ *
+ * @Author: Christophe RICARD tpmsupport@st.com
+ *
+ * @File: tpm_spi_stm_st33.h
+ *
+ * @Date: 09/15/2010
+ */
+#ifndef __STM_ST33_TPM_SPI_MAIN_H__
+#define __STM_ST33_TPM_SPI_MAIN_H__
+
+#define TPM_ACCESS			(0x0)
+#define TPM_STS				(0x18)
+#define TPM_DATA_FIFO			(0x24)
+#define TPM_HASH_DATA			(0x24)
+#define TPM_INTF_CAPABILITY		(0x14)
+#define TPM_INT_STATUS			(0x10)
+#define TPM_INT_ENABLE			(0x08)
+
+#define TPM_DUMMY_BYTE			0x00
+#define TPM_WRITE_DIRECTION		0x80
+#define TPM_HEADER_SIZE			10
+#define TPM_BUFSIZE			2048
+
+#define LOCALITY0		0
+
+#define MAX_SPI_LATENCY		15
+
+#define TPM_ST33_SPI		"st33zp24_spi"
+
+struct st33zp24_platform_data {
+	int io_serirq;
+	int io_lpcpd;
+	int latency;
+	bool bchipf;
+	u8 *tpm_spi_buffer[2]; /* 0 Request 1 Response */
+	struct completion irq_detection;
+};
+
+#endif /* __STM_ST33_TPM_SPI_MAIN_H__ */
-- 
1.7.9.5


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

* Re: [PATCH 1/1] TPM: STMicroelectronics st33 driver SPI
  2013-05-06 11:14 Matthias Leblanc
@ 2013-05-06 19:12 ` Kent Yoder
  0 siblings, 0 replies; 21+ messages in thread
From: Kent Yoder @ 2013-05-06 19:12 UTC (permalink / raw)
  To: Matthias Leblanc
  Cc: Rajiv Andrade, Marcel Selhorst, Sirrix AG, tpmdd-devel,
	linux-kernel, Jean-Luc Blanc

Hi Mathias,

On Mon, May 06, 2013 at 01:14:44PM +0200, Matthias Leblanc wrote:
> From: Mathias Leblanc <mathias.leblanc@st.com>
> 
>  * STMicroelectronics version 1.2.0, Copyright (C) 2010
>  * 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 SPI, in menuconfig or .config choose the tpm driver on
> device --> tpm and activate the protocol of your choice before compiling
> the kernel.
> The driver will be accessible from within Linux.
> 
> Tested on linux x86/x64 and beagleboard REV B & XM REV C
> 
> Signed-off-by: Mathias Leblanc <mathias.leblanc@st.com>
> ---

  As Peter mentioned, please provide a changelog, it makes the
maintainer very happy.

>  drivers/char/tpm/Kconfig            |   10 +
>  drivers/char/tpm/Makefile           |    1 +
>  drivers/char/tpm/tpm_spi_stm_st33.c |  956 +++++++++++++++++++++++++++++++++++
>  drivers/char/tpm/tpm_spi_stm_st33.h |   86 ++++
>  4 files changed, 1053 insertions(+), 0 deletions(-)
>  create mode 100644 drivers/char/tpm/tpm_spi_stm_st33.c
>  create mode 100644 drivers/char/tpm/tpm_spi_stm_st33.h
> 
> diff --git a/drivers/char/tpm/Kconfig b/drivers/char/tpm/Kconfig
> index dbfd564..2fc1254 100644
> --- a/drivers/char/tpm/Kconfig
> +++ b/drivers/char/tpm/Kconfig
> @@ -91,4 +91,14 @@ config TCG_ST33_I2C
>          To compile this driver as a module, choose M here; the module will be
>          called tpm_stm_st33_i2c.
> 
> +config TCG_ST33_SPI
> +       tristate "STMicroelectronics ST33 SPI"
> +       depends on SPI
> +       depends on GPIOLIB
> +       ---help---
> +       If you have a TPM security chip from STMicroelectronics working with
> +       an SPI bus say Yes and it will be accessible from within Linux.
> +       To compile this driver as a module, choose M here; the module will be
> +       called tpm_stm_st33_spi.
> +
>  endif # TCG_TPM
> diff --git a/drivers/char/tpm/Makefile b/drivers/char/tpm/Makefile
> index a3736c9..a1db3dd 100644
> --- a/drivers/char/tpm/Makefile
> +++ b/drivers/char/tpm/Makefile
> @@ -18,3 +18,4 @@ obj-$(CONFIG_TCG_ATMEL) += tpm_atmel.o
>  obj-$(CONFIG_TCG_INFINEON) += tpm_infineon.o
>  obj-$(CONFIG_TCG_IBMVTPM) += tpm_ibmvtpm.o
>  obj-$(CONFIG_TCG_ST33_I2C) += tpm_i2c_stm_st33.o
> +obj-$(CONFIG_TCG_ST33_SPI) += tpm_spi_stm_st33.o
> diff --git a/drivers/char/tpm/tpm_spi_stm_st33.c b/drivers/char/tpm/tpm_spi_stm_st33.c
> new file mode 100644
> index 0000000..f1bac19
> --- /dev/null
> +++ b/drivers/char/tpm/tpm_spi_stm_st33.c
> @@ -0,0 +1,954 @@

  Something is weird with this patch -- if you count down 954 lines,
that chops the last 2 lines of this patch hunk (last 2 lines of
tpm_spi_stm_st33.c).  The only reason I noticed this was that the
compile complained that there was no MODULE_LICENSE() in the file.

> +/*
> + * STMicroelectronics TPM SPI Linux driver for TPM ST33ZP24
> + * Copyright (C) 2009, 2010  STMicroelectronics
> + *
> + * 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.,
> + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
> + *
> + * STMicroelectronics version 1.2.0, Copyright (C) 2010
> + * STMicroelectronics comes with ABSOLUTELY NO WARRANTY.
> + * This is free software, and you are welcome to redistribute it
> + * under certain conditions.
> + *
> + * @Author: Christophe RICARD tpmsupport@st.com
> + *
> + * @File: tpm_stm_st33_spi.c
> + *
> + * @Synopsis:
> + *	09/15/2010:	First shot driver tpm_tis driver for lpc is
> + *			used as model.
> + */
> +
> +#define pr_fmt(fmt)	KBUILD_MODNAME ": " fmt

 Still here, still unused.

> +
> +#include <linux/module.h>
> +#include <linux/kernel.h>
> +#include <linux/delay.h>
> +#include <linux/init.h>
> +#include <linux/spi/spi.h>
> +#include <linux/wait.h>
> +#include <linux/string.h>
> +#include <linux/interrupt.h>
> +#include <linux/spinlock.h>
> +#include <linux/sysfs.h>
> +#include <linux/gpio.h>
> +#include <linux/sched.h>
> +#include <linux/uaccess.h>
> +#include <linux/io.h>
> +#include <linux/slab.h>
> +#include <linux/sched.h>
> +
> +#include "tpm.h"
> +
> +#include "tpm_spi_stm_st33.h"
> +
> +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 */
> +};
> +
> +
> +static inline int spi_read_write(struct spi_device *spi,
> +	 struct spi_transfer xfer) {
> +	struct spi_message msg;
> +	spi_message_init(&msg);
> +	spi_message_add_tail(&xfer, &msg);
> +	return spi_sync(spi, &msg);
> +}
> +
> +/*
> + * spi_write8_reg
> + * Send byte to the TIS register according to the ST33ZP24 SPI protocol.
> + * @param: tpm, 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: should be zero if success else a negative error code.
> + */
> +static int spi_write8_reg(struct tpm_chip *tpm, u8 tpm_register,
> +		      u8 *tpm_data, u16 tpm_size)
> +{
> +	u8 data;
> +	int total_length = 0, nbr_dummy_bytes;
> +	int value = 0;
> +	struct spi_device *dev = (struct spi_device *)tpm->vendor.iobase;
> +	struct st33zp24_platform_data *platform_data = dev->dev.platform_data;
> +	u8 *data_buffer = platform_data->tpm_spi_buffer[0];
> +	struct spi_transfer xfer = {
> +		.tx_buf	 = data_buffer,
> +		.rx_buf	 = platform_data->tpm_spi_buffer[1],
> +	};
> +
> +	u8 latency = platform_data->latency + tpm_size + 1;
> +
> +	/* Pre-Header */
> +	data = TPM_WRITE_DIRECTION | tpm->vendor.locality;
> +	memcpy(data_buffer + total_length, &data, sizeof(data));
> +	total_length++;
> +	data = tpm_register;
> +	memcpy(data_buffer + total_length, &data, sizeof(data));
> +	total_length++;
> +
> +	if (tpm_size > 0 &&
> +	     (tpm_register == TPM_DATA_FIFO ||
> +		tpm_register == TPM_HASH_DATA)) {
> +		data = (tpm_size >> 8) & 0x00ff;
> +		data_buffer[total_length++] = data;
> +		data = tpm_size & 0x00ff;
> +		data_buffer[total_length++] = data;
> +		latency -= 2;
> +	}
> +
> +	memcpy(&data_buffer[total_length], tpm_data, tpm_size);
> +	total_length += tpm_size;
> +
> +	nbr_dummy_bytes = platform_data->latency + 1;
> +	memset(&data_buffer[total_length], TPM_DUMMY_BYTE,
> +			platform_data->latency + 1);
> +
> +	xfer.len = total_length;
> +
> +	value = spi_read_write(dev, xfer);
> +
> +	if (value == 0) {
> +		nbr_dummy_bytes = total_length - 1 - nbr_dummy_bytes;
> +		while (nbr_dummy_bytes < total_length &&
> +			 ((u8 *)xfer.rx_buf)[nbr_dummy_bytes] == 0)
> +					nbr_dummy_bytes++;
> +
> +		if (((u8 *)xfer.rx_buf)[nbr_dummy_bytes] != 0)
> +			value = ((u8 *)xfer.rx_buf)[nbr_dummy_bytes];
> +	}
> +
> +	return value;
> +} /* spi_write8_reg() */
> +
> +/*
> + * spi_read8_reg
> + * Recv byte from the TIS register according to the ST33ZP24 SPI 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: should be zero if success else a negative error code.
> + */
> +static u8 spi_read8_reg(struct tpm_chip *tpm, u8 tpm_register,
> +		    u8 *tpm_data, u16 tpm_size)
> +{
> +	u8 data;
> +	int total_length = 0, nbr_dummy_bytes;
> +	int value = 0;
> +	struct spi_device *dev = (struct spi_device *)tpm->vendor.iobase;
> +	struct st33zp24_platform_data *platform_data = dev->dev.platform_data;
> +	u8 *data_buffer = platform_data->tpm_spi_buffer[0];
> +	struct spi_transfer xfer = {
> +		.tx_buf	 = data_buffer,
> +		.rx_buf	 = platform_data->tpm_spi_buffer[1],
> +	};
> +
> +	/* Pre-Header */
> +
> +	data = tpm->vendor.locality;
> +	memcpy(data_buffer + total_length, &data, sizeof(data));
> +	total_length++;
> +	data = tpm_register;
> +	memcpy(data_buffer + total_length, &data, sizeof(data));
> +	total_length++;
> +
> +	nbr_dummy_bytes = platform_data->latency + 1;
> +	memset(&data_buffer[total_length], TPM_DUMMY_BYTE,
> +					 platform_data->latency + 1);
> +
> +	xfer.len = total_length;
> +
> +	/* header + status byte + size of the data + status byte */
> +	value = spi_read_write(dev, xfer);
> +
> +	if (tpm_size > 0 && value == 0) {
> +		nbr_dummy_bytes = 2;
> +		for (; nbr_dummy_bytes < total_length &&
> +			 ((u8 *)xfer.rx_buf)[nbr_dummy_bytes] == 0;
> +			 nbr_dummy_bytes++)
> +			;
> +
> +		if (nbr_dummy_bytes + 1 < total_length) {
> +			value = ((u8 *)xfer.rx_buf)[nbr_dummy_bytes];
> +
> +			if (tpm_size > 0)
> +				memcpy(tpm_data, xfer.rx_buf +
> +							 nbr_dummy_bytes + 1,
> +								 tpm_size);
> +		}
> +	}
> +	return value;
> +} /* spi_read8_reg() */
> +
> +/*
> + * clear_interruption
> + * clear the TPM interrupt register.
> + * @param: tpm, the chip description
> + */
> +static void clear_interruption(struct tpm_chip *tpm)
> +{
> +	u8 interrupt;
> +	spi_read8_reg(tpm, TPM_INT_STATUS, &interrupt, 1);
> +	/* Clear interrupts handled with TPM_EOI */
> +	spi_write8_reg(tpm, TPM_INT_STATUS, &interrupt, 1);
> +	spi_read8_reg(tpm, TPM_INT_STATUS, &interrupt, 1);
> +} /* clear_interruption() */
> +
> +/*
> + * _wait_for_serirq_timeout
> + * @param: tpm, the chip description
> + * @param: timeout, the timeout of the interrupt
> + * @return: the status of the interruption.
> + */
> +unsigned long wait_for_serirq_timeout(struct tpm_chip *chip, bool condition,
> +				 unsigned long timeout)
> +{
> +	unsigned long status;
> +	struct spi_device *client;
> +	struct st33zp24_platform_data *pin_infos;
> +
> +	client = (struct spi_device *)chip->vendor.iobase;
> +	pin_infos = client->dev.platform_data;
> +
> +	status = wait_for_completion_interruptible_timeout(
> +				&pin_infos->irq_detection, timeout);
> +	if (status > 0)
> +		enable_irq(gpio_to_irq(pin_infos->io_serirq));
> +	gpio_direction_input(pin_infos->io_serirq);
> +
> +	if (!status) {
> +		status = -EBUSY;
> +		goto wait_end;
> +	}
> +	clear_interruption(chip);
> +	if (condition)
> +		status = 1;
> +
> +wait_end:
> +	return status;
> +}
> +
> +
> +/*
> + * tpm_stm_spi_cancel, cancel is not implemented.
> + * @param: chip, the tpm chip description as specified in
> + * driver/char/tpm/tpm.h.
> + */
> +static void tpm_stm_spi_cancel(struct tpm_chip *chip)
> +{
> +	u8 data = TPM_STS_COMMAND_READY;
> +
> +	/* this causes the current command to be aborted */
> +	spi_write8_reg(chip, TPM_STS, &data, 1);
> +} /* tpm_stm_spi_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_spi_status(struct tpm_chip *chip)
> +{
> +	u8 data;
> +
> +	spi_read8_reg(chip, TPM_STS, &data, 1);
> +	return data;
> +} /* tpm_stm_spi_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 = spi_read8_reg(chip, TPM_ACCESS, &data, 1);
> +	if (status && (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 stop;
> +	long rc;
> +	u8 data;
> +
> +	/* Check locality */
> +	if (check_locality(chip) == chip->vendor.locality)
> +		return chip->vendor.locality;
> +
> +	/* Request locality */
> +	data = TPM_ACCESS_REQUEST_USE;
> +	rc = spi_write8_reg(chip, TPM_ACCESS, &data, 1);
> +	if (rc < 0)
> +		goto end;
> +
> +	/* wait for burstcount */
> +	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 {
> +		stop = jiffies + chip->vendor.timeout_a;
> +		do {
> +			if (check_locality(chip) >= 0)
> +				return chip->vendor.locality;
> +			msleep(TPM_TIMEOUT);
> +		} while (time_before(jiffies, stop));
> +	}
> +end:
> +	return -EACCES;
> +} /* request_locality() */
> +
> +/*
> + * release_locality release the active locality
> + * @param: chip, the tpm chip description.
> + */
> +static void release_locality(struct tpm_chip *chip)
> +{
> +	u8 data;
> +
> +	data = TPM_ACCESS_ACTIVE_LOCALITY;
> +	spi_write8_reg(chip, TPM_ACCESS, &data, 1);
> +} /* release_locality()*/
> +
> +/*
> + * 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 stop;
> +	u32 burstcnt, status;
> +	u8 tpm_reg, temp;
> +
> +	/* wait for burstcount */
> +	/* which timeout value, spec has 2 answers (c & d) */
> +	stop = jiffies + chip->vendor.timeout_d;
> +	do {
> +		tpm_reg = TPM_STS + 1;
> +		status = spi_read8_reg(chip, tpm_reg, &temp, 1);
> +		if (status < 0)
> +			goto end;
> +
> +		tpm_reg = tpm_reg + 1;
> +		burstcnt = temp;
> +		status = spi_read8_reg(chip, tpm_reg, &temp, 1);
> +		if (status < 0)
> +			goto end;
> +
> +		burstcnt |= temp << 8;
> +		if (burstcnt)
> +			return burstcnt;
> +		msleep(TPM_TIMEOUT);
> +	} while (time_before(jiffies, stop));
> +
> +end:
> +	return -EBUSY;
> +} /* get_burstcount() */
> +
> +/*
> + * wait_for_stat wait for a TPM_STS value
> + * @param: chip, the tpm chip description
> + * @param: mask, the value mask to wait
> + * @param: timeout, the timeout
> + * @param: queue, the wait queue.
> + * @return: the tpm status, 0 if success, -ETIME if timeout is reached.
> + */
> +static int wait_for_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout,
> +			 wait_queue_head_t *queue)
> +{
> +	unsigned long stop;
> +	long rc;
> +	u8 status;
> +
> +	/* check current status */
> +	status = tpm_stm_spi_status(chip);
> +	if (!chip->vendor.irq && (status & mask) == mask)
> +		return 0;
> +
> +	if (chip->vendor.irq) {
> +		rc = wait_for_serirq_timeout(chip, ((tpm_stm_spi_status
> +						    (chip) & mask) ==
> +						     mask), timeout);
> +		if (rc > 0)
> +			return 0;
> +	} else {
> +		stop = jiffies + timeout;
> +		do {
> +			msleep(TPM_TIMEOUT);
> +			status = tpm_stm_spi_status(chip);
> +			if ((status & mask) == mask)
> +				return 0;
> +		} while (time_before(jiffies, stop));
> +	}
> +	return -ETIME;
> +} /* wait_for_stat() */
> +
> +/*
> + * 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 tpm status, 0 if success, -ETIME if timeout is reached.
> + */
> +static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count)
> +{
> +	u32 size = 0, burstcnt, status, len;
> +
> +	while (size < count &&
> +	       wait_for_stat(chip,
> +			     TPM_STS_DATA_AVAIL | TPM_STS_VALID,
> +			     chip->vendor.timeout_c,
> +			     &chip->vendor.read_queue)
> +						== 0) {
> +		burstcnt = get_burstcount(chip);
> +		len = min_t(int, burstcnt, count - size);
> +		status = spi_read8_reg(chip, TPM_DATA_FIFO, buf + size, len);
> +		if (status < 0)
> +			return status;
> +
> +
> +		size += len;
> +	}
> +	return size;
> +}
> +
> +/*
> + * tpm_ioserirq_handler the serirq irq handler
> + * @param: irq, the tpm chip description
> + * @param: dev_id, the description of the chip
> + * @return: the status of the handler.
> + */
> +static irqreturn_t tpm_ioserirq_handler(int irq, void *dev_id)
> +{
> +	struct tpm_chip *chip = dev_id;
> +	struct spi_device *client;
> +	struct st33zp24_platform_data *pin_infos;
> +
> +	disable_irq_nosync(irq);
> +
> +	client = (struct spi_device *)chip->vendor.iobase;
> +	pin_infos = client->dev.platform_data;
> +
> +	complete(&pin_infos->irq_detection);
> +	return IRQ_HANDLED;
> +} /* tpm_ioserirq_handler() */
> +
> +/*
> + * tpm_stm_spi_send send TPM commands through the SPI bus.
> + * @param: chip, the tpm chip description
> + * @param: buf, the tpm command buffer
> + * @param: len, the tpm command size
> + * @return: should be zero if success else the negative error code.
> + */
> +static int tpm_stm_spi_send(struct tpm_chip *chip, unsigned char *buf,
> +			    size_t len)
> +{
> +	u32 ret = 0, status, burstcnt = 0, i, size = 0;
> +	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_spi_status(chip);
> +	if ((status & TPM_STS_COMMAND_READY) == 0) {
> +		tpm_stm_spi_cancel(chip);
> +		if (wait_for_stat
> +		    (chip, TPM_STS_COMMAND_READY, chip->vendor.timeout_b,
> +		     &chip->vendor.int_queue) < 0) {
> +			ret = -ETIME;
> +			goto out_err;
> +		}
> +	}
> +
> +	for (i = 0; i < len - 1;) {
> +		burstcnt = get_burstcount(chip);
> +		size = min_t(int, len - i - 1, burstcnt);
> +		ret = spi_write8_reg(chip, TPM_DATA_FIFO, buf, size);
> +		if (ret < 0)
> +			goto out_err;
> +		i += size;
> +	}
> +
> +	status = tpm_stm_spi_status(chip);
> +	if ((status & TPM_STS_DATA_EXPECT) == 0) {
> +		ret = -EIO;
> +		goto out_err;
> +	}
> +
> +	/* write last byte */
> +	spi_write8_reg(chip, TPM_DATA_FIFO, buf + len - 1, 1);
> +
> +	status = tpm_stm_spi_status(chip);
> +	if ((status & TPM_STS_DATA_EXPECT) != 0) {
> +		ret = -EIO;
> +		goto out_err;
> +	}
> +
> +	/* go and do it */
> +	data = TPM_STS_GO;
> +	ret = spi_write8_reg(chip, TPM_STS, &data, 1);
> +	if (ret < 0)
> +		goto out_err;
> +
> +	return len;
> +out_err:
> +	tpm_stm_spi_cancel(chip);
> +	release_locality(chip);
> +	return ret;
> +}
> +
> +/*
> + * tpm_stm_spi_recv received TPM response through the SPI bus.
> + * @param: chip, the tpm chip description
> + * @param: buf, the tpm command buffer
> + * @param: len, the tpm command size
> +* @return: should be zero if success else the negative error code.
> + */
> +static int tpm_stm_spi_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);
> +
> +	/* read first 10 bytes, including tag, paramsize, and result */
> +	if (size < TPM_HEADER_SIZE) {
> +		dev_err(chip->dev, "Unable to read header\n");
> +		goto out;
> +	}
> +
> +	expected = be32_to_cpu(*(__be32 *)(buf + 2));
> +	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;
> +}
> +
> +
> +static const struct file_operations tpm_st33_spi_fops = {
> +	.owner = THIS_MODULE,
> +	.llseek = no_llseek,
> +	.read = tpm_read,
> +	.write = tpm_write,
> +	.open = tpm_open,
> +	.release = tpm_release,
> +};
> +
> +static DEVICE_ATTR(pubek, S_IRUGO, tpm_show_pubek, NULL);
> +static DEVICE_ATTR(pcrs, S_IRUGO, tpm_show_pcrs, NULL);
> +static DEVICE_ATTR(enabled, S_IRUGO, tpm_show_enabled, NULL);
> +static DEVICE_ATTR(active, S_IRUGO, tpm_show_active, NULL);
> +static DEVICE_ATTR(owned, S_IRUGO, tpm_show_owned, NULL);
> +static DEVICE_ATTR(temp_deactivated, S_IRUGO, tpm_show_temp_deactivated, NULL);
> +static DEVICE_ATTR(caps, S_IRUGO, tpm_show_caps_1_2, NULL);
> +static DEVICE_ATTR(cancel, S_IWUSR | S_IWGRP, NULL, tpm_store_cancel);
> +
> +static struct attribute *stm_tpm_attrs[] = {
> +	&dev_attr_pubek.attr,
> +	&dev_attr_pcrs.attr,
> +	&dev_attr_enabled.attr,
> +	&dev_attr_active.attr,
> +	&dev_attr_owned.attr,
> +	&dev_attr_temp_deactivated.attr,
> +	&dev_attr_caps.attr,
> +	&dev_attr_cancel.attr, NULL,
> +};
> +
> +static struct attribute_group stm_tpm_attr_grp = {
> +	.attrs = stm_tpm_attrs
> +};
> +
> +static struct tpm_vendor_specific st_spi_tpm = {
> +	.send = tpm_stm_spi_send,
> +	.recv = tpm_stm_spi_recv,
> +	.cancel = tpm_stm_spi_cancel,
> +	.status = tpm_stm_spi_status,
> +	.attr_group = &stm_tpm_attr_grp,
> +	.miscdev = {.fops = &tpm_st33_spi_fops,},
> +};
> +
> +static int evaluate_latency(struct tpm_chip *chip)
> +{
> +	int latency = 0;
> +	struct spi_device *dev = (struct spi_device *)chip->vendor.iobase;
> +	struct st33zp24_platform_data *platform_data = dev->dev.platform_data;
> +	int status = 0;
> +	u8 data;
> +
> +	for (; status == 0x00 && latency < MAX_SPI_LATENCY; latency++) {
> +		platform_data->latency = latency;
> +		status = spi_read8_reg(chip, TPM_INTF_CAPABILITY, &data, 1);
> +	}
> +	return latency - 1;
> +} /* evaluate_latency() */
> +
> +static int interrupts;
> +module_param(interrupts, int, 0444);
> +MODULE_PARM_DESC(interrupts, "Enable interrupts");
> +
> +static int power_mgt = 1;
> +module_param(power_mgt, int, 0444);
> +MODULE_PARM_DESC(power_mgt, "Power Management");
> +
> +/*
> + * tpm_st33_spi_probe initialize the TPM device
> + * @param: client, the spi_device drescription (TPM SPI description).
> + * @param: id, the spi_device_id struct.
> + * @return: 0 in case of success.
> + *	 -1 in other case.
> + */
> +static int
> +tpm_st33_spi_probe(struct spi_device *dev)
> +{
> +	u32 err = 0;
> +	u8 intmask;
> +	struct tpm_chip *chip;
> +	struct st33zp24_platform_data *platform_data;
> +
> +	/* Check SPI platform functionnalities */
> +	if (dev == NULL) {
> +		pr_info("dev is NULL. exiting.\n");
> +		err = -ENODEV;
> +		goto end;
> +	}
> +
> +	chip = tpm_register_hardware(&dev->dev, &st_spi_tpm);
> +	if (!chip) {
> +		err = -ENODEV;
> +		goto end;
> +	}
> +
> +	/* Allocation of SPI buffers MISO and MOSI		*/
> +	/* Size is as follow:					*/
> +	/* Request burstcount value  = 0x800 = 2048		*/
> +	/* +							*/
> +	/* Response burstcount value = 0x400 = 1024		*/
> +	/* +							*/
> +	/* At least:						*/
> +	/* 1 byte for direction/locality			*/
> +	/* 1 byte tpm tis register				*/
> +	/* 2 bytes spi data length (for request only)		*/
> +	/* 2 latency bytes					*/
> +	/* 1 status byte					*/
> +	/* = 2048 + 1024 + 7					*/
> +	/* We reserved 2048 + 1024 + 20 in case latency byte	*/
> +	/* change						*/
> +	platform_data = dev->dev.platform_data;
> +	platform_data->tpm_spi_buffer[0] =
> +	    kmalloc((TPM_BUFSIZE + (TPM_BUFSIZE / 2) +
> +				 TPM_DIGEST_SIZE) * sizeof(u8), GFP_KERNEL);
> +	if (platform_data->tpm_spi_buffer[0] == NULL) {
> +		err = -ENOMEM;
> +		goto _tpm_clean_answer;
> +	}
> +	platform_data->tpm_spi_buffer[1] =
> +	    kmalloc((TPM_BUFSIZE + (TPM_BUFSIZE / 2) +
> +				 TPM_DIGEST_SIZE) * sizeof(u8) , GFP_KERNEL);
> +	if (platform_data->tpm_spi_buffer[1] == NULL) {
> +		err = -ENOMEM;
> +		goto _tpm_clean_response;
> +	}
> +
> +	chip->vendor.iobase = dev;
> +
> +	/* Default timeouts */
> +	chip->vendor.timeout_a = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
> +	chip->vendor.timeout_b = msecs_to_jiffies(TIS_LONG_TIMEOUT);
> +	chip->vendor.timeout_c = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
> +	chip->vendor.timeout_d = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
> +	chip->vendor.locality = LOCALITY0;
> +
> +	if (power_mgt) {
> +		err = gpio_request(platform_data->io_lpcpd, "TPM IO_LPCPD");
> +		if (err)
> +			goto _gpio_init1;
> +		gpio_set_value(platform_data->io_lpcpd, 1);
> +	}
> +
> +	platform_data->latency = evaluate_latency(chip);
> +	if (platform_data->latency <= 0x00) {
> +		err = -ENODEV;
> +		goto _gpio_init1;
> +	}
> +
> +	/* Enable interrupt */
> +	/* Register GPIO pin through generic Linux GPIO API */
> +	if (interrupts) {
> +		init_completion(&platform_data->irq_detection);
> +		if (request_locality(chip) != LOCALITY0) {
> +			err = -ENODEV;
> +			goto _tpm_clean_response;
> +		}
> +		err = gpio_request(platform_data->io_serirq, "TPM IO_SERIRQ");
> +		if (err)
> +			goto _gpio_init2;
> +
> +		/* Clear all existing */
> +		clear_interruption(chip);
> +		err = request_irq(gpio_to_irq(platform_data->io_serirq),
> +							&tpm_ioserirq_handler,
> +				IRQF_TRIGGER_HIGH | IRQF_SHARED,
> +				"TPM SERIRQ management", chip);
> +		if (err < 0) {
> +			pr_info("TPM SERIRQ signals %d not available\n",
> +				gpio_to_irq(platform_data->io_serirq));
> +				goto _irq_set;
> +		}
> +		err = spi_read8_reg(chip, TPM_INT_ENABLE, &intmask, 1);
> +		if (err < 0)
> +			goto _irq_set;
> +
> +		intmask |= TPM_INTF_CMD_READY_INT
> +			|  TPM_INTF_FIFO_AVALAIBLE_INT
> +			|  TPM_INTF_WAKE_UP_READY_INT
> +			|  TPM_INTF_LOC4SOFTRELEASE_INT
> +			|  TPM_INTF_LOCALITY_CHANGE_INT
> +			|  TPM_INTF_STS_VALID_INT
> +			|  TPM_INTF_DATA_AVAIL_INT;
> +
> +		err = spi_write8_reg(chip, TPM_INT_ENABLE, &intmask, 1);
> +		if (err < 0)
> +			goto _irq_set;
> +
> +		intmask = TPM_GLOBAL_INT_ENABLE;
> +		err = spi_write8_reg(chip, TPM_INT_ENABLE + 3, &intmask, 1);
> +		if (err < 0)
> +			goto _irq_set;
> +
> +		err = spi_read8_reg(chip, TPM_INT_STATUS, &intmask, 1);
> +		if (err < 0)
> +			goto _irq_set;
> +
> +		chip->vendor.irq = interrupts;
> +
> +		tpm_gen_interrupt(chip);
> +	}
> +
> +	tpm_get_timeouts(chip);
> +
> +	/* attach chip datas to client */
> +	spi_set_drvdata(dev, chip);
> +	platform_data->bchipf = false;
> +
> +	pr_info("TPM SPI Initialized\n");
> +	return 0;
> +_irq_set:
> +	free_irq(gpio_to_irq(platform_data->io_serirq), chip);
> +_gpio_init2:
> +	if (platform_data && interrupts)
> +		gpio_free(platform_data->io_serirq);
> +_gpio_init1:
> +	if (platform_data && power_mgt)
> +		gpio_free(platform_data->io_lpcpd);
> +_tpm_clean_response:
> +	tpm_remove_hardware(chip->dev);
> +	if (platform_data->tpm_spi_buffer[1] != NULL) {
> +		kfree(platform_data->tpm_spi_buffer[1]);
> +		platform_data->tpm_spi_buffer[1] = NULL;
> +	}
> +_tpm_clean_answer:
> +	if (platform_data->tpm_spi_buffer[0] != NULL) {
> +		kfree(platform_data->tpm_spi_buffer[0]);
> +		platform_data->tpm_spi_buffer[0] = NULL;
> +	}
> +
> +	platform_data->bchipf = true;
> +end:
> +	pr_info("TPM SPI initialisation fail\n");
> +	return err;
> +}
> +
> +/*
> + * tpm_st33_spi_remove remove the TPM device
> + * @param: client, the spi_device drescription (TPM SPI description).
> +		clear_bit(0, &chip->is_open);
> + * @return: 0 in case of success.
> + */
> +static int tpm_st33_spi_remove(struct spi_device *client)
> +{
> +	struct tpm_chip *chip = (struct tpm_chip *)spi_get_drvdata(client);
> +	struct st33zp24_platform_data *pin_infos =
> +		((struct spi_device *)chip->vendor.iobase)->dev.platform_data;
> +
> +	if (pin_infos != NULL) {
> +		gpio_free(pin_infos->io_lpcpd);
> +
> +		/* Check if chip has been previously clean */
> +		if (pin_infos->bchipf != true)
> +			tpm_remove_hardware(chip->dev);
> +		if (pin_infos->tpm_spi_buffer[1] != NULL) {
> +			kfree(pin_infos->tpm_spi_buffer[1]);
> +			pin_infos->tpm_spi_buffer[1] = NULL;
> +		}
> +		if (pin_infos->tpm_spi_buffer[0] != NULL) {
> +			kfree(pin_infos->tpm_spi_buffer[0]);
> +			pin_infos->tpm_spi_buffer[0] = NULL;
> +		}
> +	}
> +
> +	return 0;
> +}
> +
> +#ifdef CONFIG_PM_SLEEP
> +/*
> + * tpm_st33_spi_pm_suspend suspend the TPM device
> + * Added: Work around when suspend and no tpm application is running, suspend
> + * may fail because chip->data_buffer is not set (only set in tpm_open in Linux
> + * TPM core)
> + * @return: 0 in case of success.
> + */
> +static int tpm_st33_spi_pm_suspend(struct device *dev)
> +{
> +	struct tpm_chip *chip = dev_get_drvdata(dev);
> +	struct st33zp24_platform_data *pin_infos = dev->platform_data;
> +	int ret = 0;
> +
> +	if (power_mgt) {
> +		gpio_set_value(pin_infos->io_lpcpd, 0);
> +	} else {
> +		if (chip->data_buffer == NULL)
> +			chip->data_buffer = pin_infos->tpm_spi_buffer[0];
> +		ret = tpm_pm_suspend(dev);
> +		}
> +	return ret;
> +}				/* tpm_st33_spi_suspend() */
> +
> +/*
> + * tpm_st33_spi_pm_resume resume the TPM device
> + * @param: spi, the spi_device drescription (TPM SPI description).
> + * @return: 0 in case of success.
> + */
> +static int tpm_st33_spi_pm_resume(struct device *dev)
> +{
> +	struct tpm_chip *chip = dev_get_drvdata(dev);
> +	struct st33zp24_platform_data *pin_infos = dev->platform_data;
> +
> +	int ret = 0;
> +	if (power_mgt) {
> +		gpio_set_value(pin_infos->io_lpcpd, 1);
> +		ret = wait_for_serirq_timeout(chip,
> +					 (chip->vendor.status(chip) &&
> +					  TPM_STS_VALID) == TPM_STS_VALID,
> +					  chip->vendor.timeout_b);
> +	} else {
> +		if (chip->data_buffer == NULL)
> +			chip->data_buffer = pin_infos->tpm_spi_buffer[0];
> +		ret = tpm_pm_resume(dev);
> +		if (!ret)
> +			tpm_do_selftest(chip);
> +	}
> +	return ret;
> +} /* tpm_st33_spi_pm_resume() */
> +#endif
> +
> +static SIMPLE_DEV_PM_OPS(tpm_st33_spi_ops, tpm_st33_spi_pm_suspend,
> +	 tpm_st33_spi_pm_resume);
> +static struct spi_driver tpm_st33_spi_driver = {
> +	.driver = {
> +		   .owner = THIS_MODULE,
> +		   .name = TPM_ST33_SPI,
> +		   .pm = &tpm_st33_spi_ops,
> +		   },
> +	.probe = tpm_st33_spi_probe,
> +	.remove = tpm_st33_spi_remove,
> +};
> +
> +/*
> + * tpm_st33_spi_exit The kernel calls this function during unloading the
> + * module or during shut down process
> + */
> +static void __exit tpm_st33_spi_exit(void)
> +{
> +	spi_unregister_driver(&tpm_st33_spi_driver);
> +}

  CC [M]  drivers/char/tpm/tpm_spi_stm_st33.o
  drivers/char/tpm/tpm_spi_stm_st33.c:946:20: error: ‘tpm_st33_spi_exit’
  defined but not used [-Werror=unused-function]

Kent

> +
> +module_spi_driver(tpm_st33_spi_driver);
> +
> +MODULE_AUTHOR("Christophe Ricard (tpmsupport@st.com)");
> +MODULE_DESCRIPTION("STM TPM SPI ST33 Driver");
> +MODULE_VERSION("1.2.0");
> +MODULE_LICENSE("GPL");
> diff --git a/drivers/char/tpm/tpm_spi_stm_st33.h b/drivers/char/tpm/tpm_spi_stm_st33.h
> new file mode 100644
> index 0000000..752158e
> --- /dev/null
> +++ b/drivers/char/tpm/tpm_spi_stm_st33.h
> @@ -0,0 +1,86 @@
> +/*
> + * STMicroelectronics TPM SPI Linux driver for TPM ST33NP18
> + * Copyright (C) 2009, 2010  STMicroelectronics
> + *
> + * 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.,
> + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
> + *
> + * STMicroelectronics version 1.2.0, Copyright (C) 2010
> + * STMicroelectronics comes with ABSOLUTELY NO WARRANTY.
> + * This is free software, and you are welcome to redistribute it
> + * under certain conditions.
> + *
> + * @Author: Christophe RICARD tpmsupport@st.com
> + *
> + * @File: tpm_spi_stm_st33.h
> + *
> + * @Date: 09/15/2010
> + */
> +#ifndef __STM_ST33_TPM_SPI_MAIN_H__
> +#define __STM_ST33_TPM_SPI_MAIN_H__
> +
> +#include <linux/pci.h>
> +#include <linux/module.h>
> +#include <linux/platform_device.h>
> +#include <linux/fs.h>
> +#include <linux/miscdevice.h>
> +
> +#define MINOR_NUM_SPI		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			0x00
> +#define TPM_WRITE_DIRECTION		0x80
> +#define TPM_HEADER_SIZE			10
> +#define TPM_BUFSIZE			2048
> +
> +/* ioctl commands */
> +#define TPMIOC_CANCEL		_IO('T', 0x00)
> +#define TPMIOC_TRANSMIT		_IO('T', 0x01)
> +#define TPMIOC_HASH		_IO('T', 0x02)
> +#define TPMIOC_CHANGELOCALITY	_IO('T', 0x03)
> +
> +#define LOCALITY0		0
> +#define LOCALITY4		4
> +
> +#define TPM_ST_SPI_OK		0x5A
> +
> +
> +#define MAX_SPI_LATENCY		15
> +
> +#define TPM_ST33_SPI		"st33zp24_spi"
> +
> +struct st33zp24_platform_data {
> +	int io_serirq;
> +	int io_lpcpd;
> +	int latency;
> +	bool bchipf;
> +	u8 *tpm_spi_buffer[2]; /* 0 Request 1 Response */
> +	struct completion irq_detection;
> +};
> +
> +struct st_tpm_hash_t {
> +	int size;
> +	u8 *data;
> +};

 Please remove everything that's unused here. The ioctl stuff,
 st_tpm_hash_t, LOCALITY4, etc.

Thanks,
Kent

> +
> +#endif /* __STM_ST33_TPM_SPI_MAIN_H__ */
> -- 
> 1.7.1
> 


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

* [PATCH 1/1] TPM: STMicroelectronics st33 driver SPI
@ 2013-05-06 11:14 Matthias Leblanc
  2013-05-06 19:12 ` Kent Yoder
  0 siblings, 1 reply; 21+ messages in thread
From: Matthias Leblanc @ 2013-05-06 11:14 UTC (permalink / raw)
  To: Kent Yoder, Rajiv Andrade, Marcel Selhorst, Sirrix AG, tpmdd-devel
  Cc: linux-kernel, Jean-Luc Blanc, Mathias Leblanc

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

 * STMicroelectronics version 1.2.0, Copyright (C) 2010
 * 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 SPI, in menuconfig or .config choose the tpm driver on
device --> tpm and activate the protocol of your choice before compiling
the kernel.
The driver will be accessible from within Linux.

Tested on linux x86/x64 and beagleboard REV B & XM REV C

Signed-off-by: Mathias Leblanc <mathias.leblanc@st.com>
---
 drivers/char/tpm/Kconfig            |   10 +
 drivers/char/tpm/Makefile           |    1 +
 drivers/char/tpm/tpm_spi_stm_st33.c |  956 +++++++++++++++++++++++++++++++++++
 drivers/char/tpm/tpm_spi_stm_st33.h |   86 ++++
 4 files changed, 1053 insertions(+), 0 deletions(-)
 create mode 100644 drivers/char/tpm/tpm_spi_stm_st33.c
 create mode 100644 drivers/char/tpm/tpm_spi_stm_st33.h

diff --git a/drivers/char/tpm/Kconfig b/drivers/char/tpm/Kconfig
index dbfd564..2fc1254 100644
--- a/drivers/char/tpm/Kconfig
+++ b/drivers/char/tpm/Kconfig
@@ -91,4 +91,14 @@ config TCG_ST33_I2C
         To compile this driver as a module, choose M here; the module will be
         called tpm_stm_st33_i2c.
 
+config TCG_ST33_SPI
+       tristate "STMicroelectronics ST33 SPI"
+       depends on SPI
+       depends on GPIOLIB
+       ---help---
+       If you have a TPM security chip from STMicroelectronics working with
+       an SPI bus say Yes and it will be accessible from within Linux.
+       To compile this driver as a module, choose M here; the module will be
+       called tpm_stm_st33_spi.
+
 endif # TCG_TPM
diff --git a/drivers/char/tpm/Makefile b/drivers/char/tpm/Makefile
index a3736c9..a1db3dd 100644
--- a/drivers/char/tpm/Makefile
+++ b/drivers/char/tpm/Makefile
@@ -18,3 +18,4 @@ obj-$(CONFIG_TCG_ATMEL) += tpm_atmel.o
 obj-$(CONFIG_TCG_INFINEON) += tpm_infineon.o
 obj-$(CONFIG_TCG_IBMVTPM) += tpm_ibmvtpm.o
 obj-$(CONFIG_TCG_ST33_I2C) += tpm_i2c_stm_st33.o
+obj-$(CONFIG_TCG_ST33_SPI) += tpm_spi_stm_st33.o
diff --git a/drivers/char/tpm/tpm_spi_stm_st33.c b/drivers/char/tpm/tpm_spi_stm_st33.c
new file mode 100644
index 0000000..f1bac19
--- /dev/null
+++ b/drivers/char/tpm/tpm_spi_stm_st33.c
@@ -0,0 +1,954 @@
+/*
+ * STMicroelectronics TPM SPI Linux driver for TPM ST33ZP24
+ * Copyright (C) 2009, 2010  STMicroelectronics
+ *
+ * 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.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * STMicroelectronics version 1.2.0, Copyright (C) 2010
+ * STMicroelectronics comes with ABSOLUTELY NO WARRANTY.
+ * This is free software, and you are welcome to redistribute it
+ * under certain conditions.
+ *
+ * @Author: Christophe RICARD tpmsupport@st.com
+ *
+ * @File: tpm_stm_st33_spi.c
+ *
+ * @Synopsis:
+ *	09/15/2010:	First shot driver tpm_tis driver for lpc is
+ *			used as model.
+ */
+
+#define pr_fmt(fmt)	KBUILD_MODNAME ": " fmt
+
+#include <linux/module.h>
+#include <linux/kernel.h>
+#include <linux/delay.h>
+#include <linux/init.h>
+#include <linux/spi/spi.h>
+#include <linux/wait.h>
+#include <linux/string.h>
+#include <linux/interrupt.h>
+#include <linux/spinlock.h>
+#include <linux/sysfs.h>
+#include <linux/gpio.h>
+#include <linux/sched.h>
+#include <linux/uaccess.h>
+#include <linux/io.h>
+#include <linux/slab.h>
+#include <linux/sched.h>
+
+#include "tpm.h"
+
+#include "tpm_spi_stm_st33.h"
+
+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 */
+};
+
+
+static inline int spi_read_write(struct spi_device *spi,
+	 struct spi_transfer xfer) {
+	struct spi_message msg;
+	spi_message_init(&msg);
+	spi_message_add_tail(&xfer, &msg);
+	return spi_sync(spi, &msg);
+}
+
+/*
+ * spi_write8_reg
+ * Send byte to the TIS register according to the ST33ZP24 SPI protocol.
+ * @param: tpm, 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: should be zero if success else a negative error code.
+ */
+static int spi_write8_reg(struct tpm_chip *tpm, u8 tpm_register,
+		      u8 *tpm_data, u16 tpm_size)
+{
+	u8 data;
+	int total_length = 0, nbr_dummy_bytes;
+	int value = 0;
+	struct spi_device *dev = (struct spi_device *)tpm->vendor.iobase;
+	struct st33zp24_platform_data *platform_data = dev->dev.platform_data;
+	u8 *data_buffer = platform_data->tpm_spi_buffer[0];
+	struct spi_transfer xfer = {
+		.tx_buf	 = data_buffer,
+		.rx_buf	 = platform_data->tpm_spi_buffer[1],
+	};
+
+	u8 latency = platform_data->latency + tpm_size + 1;
+
+	/* Pre-Header */
+	data = TPM_WRITE_DIRECTION | tpm->vendor.locality;
+	memcpy(data_buffer + total_length, &data, sizeof(data));
+	total_length++;
+	data = tpm_register;
+	memcpy(data_buffer + total_length, &data, sizeof(data));
+	total_length++;
+
+	if (tpm_size > 0 &&
+	     (tpm_register == TPM_DATA_FIFO ||
+		tpm_register == TPM_HASH_DATA)) {
+		data = (tpm_size >> 8) & 0x00ff;
+		data_buffer[total_length++] = data;
+		data = tpm_size & 0x00ff;
+		data_buffer[total_length++] = data;
+		latency -= 2;
+	}
+
+	memcpy(&data_buffer[total_length], tpm_data, tpm_size);
+	total_length += tpm_size;
+
+	nbr_dummy_bytes = platform_data->latency + 1;
+	memset(&data_buffer[total_length], TPM_DUMMY_BYTE,
+			platform_data->latency + 1);
+
+	xfer.len = total_length;
+
+	value = spi_read_write(dev, xfer);
+
+	if (value == 0) {
+		nbr_dummy_bytes = total_length - 1 - nbr_dummy_bytes;
+		while (nbr_dummy_bytes < total_length &&
+			 ((u8 *)xfer.rx_buf)[nbr_dummy_bytes] == 0)
+					nbr_dummy_bytes++;
+
+		if (((u8 *)xfer.rx_buf)[nbr_dummy_bytes] != 0)
+			value = ((u8 *)xfer.rx_buf)[nbr_dummy_bytes];
+	}
+
+	return value;
+} /* spi_write8_reg() */
+
+/*
+ * spi_read8_reg
+ * Recv byte from the TIS register according to the ST33ZP24 SPI 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: should be zero if success else a negative error code.
+ */
+static u8 spi_read8_reg(struct tpm_chip *tpm, u8 tpm_register,
+		    u8 *tpm_data, u16 tpm_size)
+{
+	u8 data;
+	int total_length = 0, nbr_dummy_bytes;
+	int value = 0;
+	struct spi_device *dev = (struct spi_device *)tpm->vendor.iobase;
+	struct st33zp24_platform_data *platform_data = dev->dev.platform_data;
+	u8 *data_buffer = platform_data->tpm_spi_buffer[0];
+	struct spi_transfer xfer = {
+		.tx_buf	 = data_buffer,
+		.rx_buf	 = platform_data->tpm_spi_buffer[1],
+	};
+
+	/* Pre-Header */
+
+	data = tpm->vendor.locality;
+	memcpy(data_buffer + total_length, &data, sizeof(data));
+	total_length++;
+	data = tpm_register;
+	memcpy(data_buffer + total_length, &data, sizeof(data));
+	total_length++;
+
+	nbr_dummy_bytes = platform_data->latency + 1;
+	memset(&data_buffer[total_length], TPM_DUMMY_BYTE,
+					 platform_data->latency + 1);
+
+	xfer.len = total_length;
+
+	/* header + status byte + size of the data + status byte */
+	value = spi_read_write(dev, xfer);
+
+	if (tpm_size > 0 && value == 0) {
+		nbr_dummy_bytes = 2;
+		for (; nbr_dummy_bytes < total_length &&
+			 ((u8 *)xfer.rx_buf)[nbr_dummy_bytes] == 0;
+			 nbr_dummy_bytes++)
+			;
+
+		if (nbr_dummy_bytes + 1 < total_length) {
+			value = ((u8 *)xfer.rx_buf)[nbr_dummy_bytes];
+
+			if (tpm_size > 0)
+				memcpy(tpm_data, xfer.rx_buf +
+							 nbr_dummy_bytes + 1,
+								 tpm_size);
+		}
+	}
+	return value;
+} /* spi_read8_reg() */
+
+/*
+ * clear_interruption
+ * clear the TPM interrupt register.
+ * @param: tpm, the chip description
+ */
+static void clear_interruption(struct tpm_chip *tpm)
+{
+	u8 interrupt;
+	spi_read8_reg(tpm, TPM_INT_STATUS, &interrupt, 1);
+	/* Clear interrupts handled with TPM_EOI */
+	spi_write8_reg(tpm, TPM_INT_STATUS, &interrupt, 1);
+	spi_read8_reg(tpm, TPM_INT_STATUS, &interrupt, 1);
+} /* clear_interruption() */
+
+/*
+ * _wait_for_serirq_timeout
+ * @param: tpm, the chip description
+ * @param: timeout, the timeout of the interrupt
+ * @return: the status of the interruption.
+ */
+unsigned long wait_for_serirq_timeout(struct tpm_chip *chip, bool condition,
+				 unsigned long timeout)
+{
+	unsigned long status;
+	struct spi_device *client;
+	struct st33zp24_platform_data *pin_infos;
+
+	client = (struct spi_device *)chip->vendor.iobase;
+	pin_infos = client->dev.platform_data;
+
+	status = wait_for_completion_interruptible_timeout(
+				&pin_infos->irq_detection, timeout);
+	if (status > 0)
+		enable_irq(gpio_to_irq(pin_infos->io_serirq));
+	gpio_direction_input(pin_infos->io_serirq);
+
+	if (!status) {
+		status = -EBUSY;
+		goto wait_end;
+	}
+	clear_interruption(chip);
+	if (condition)
+		status = 1;
+
+wait_end:
+	return status;
+}
+
+
+/*
+ * tpm_stm_spi_cancel, cancel is not implemented.
+ * @param: chip, the tpm chip description as specified in
+ * driver/char/tpm/tpm.h.
+ */
+static void tpm_stm_spi_cancel(struct tpm_chip *chip)
+{
+	u8 data = TPM_STS_COMMAND_READY;
+
+	/* this causes the current command to be aborted */
+	spi_write8_reg(chip, TPM_STS, &data, 1);
+} /* tpm_stm_spi_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_spi_status(struct tpm_chip *chip)
+{
+	u8 data;
+
+	spi_read8_reg(chip, TPM_STS, &data, 1);
+	return data;
+} /* tpm_stm_spi_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 = spi_read8_reg(chip, TPM_ACCESS, &data, 1);
+	if (status && (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 stop;
+	long rc;
+	u8 data;
+
+	/* Check locality */
+	if (check_locality(chip) == chip->vendor.locality)
+		return chip->vendor.locality;
+
+	/* Request locality */
+	data = TPM_ACCESS_REQUEST_USE;
+	rc = spi_write8_reg(chip, TPM_ACCESS, &data, 1);
+	if (rc < 0)
+		goto end;
+
+	/* wait for burstcount */
+	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 {
+		stop = jiffies + chip->vendor.timeout_a;
+		do {
+			if (check_locality(chip) >= 0)
+				return chip->vendor.locality;
+			msleep(TPM_TIMEOUT);
+		} while (time_before(jiffies, stop));
+	}
+end:
+	return -EACCES;
+} /* request_locality() */
+
+/*
+ * release_locality release the active locality
+ * @param: chip, the tpm chip description.
+ */
+static void release_locality(struct tpm_chip *chip)
+{
+	u8 data;
+
+	data = TPM_ACCESS_ACTIVE_LOCALITY;
+	spi_write8_reg(chip, TPM_ACCESS, &data, 1);
+} /* release_locality()*/
+
+/*
+ * 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 stop;
+	u32 burstcnt, status;
+	u8 tpm_reg, temp;
+
+	/* wait for burstcount */
+	/* which timeout value, spec has 2 answers (c & d) */
+	stop = jiffies + chip->vendor.timeout_d;
+	do {
+		tpm_reg = TPM_STS + 1;
+		status = spi_read8_reg(chip, tpm_reg, &temp, 1);
+		if (status < 0)
+			goto end;
+
+		tpm_reg = tpm_reg + 1;
+		burstcnt = temp;
+		status = spi_read8_reg(chip, tpm_reg, &temp, 1);
+		if (status < 0)
+			goto end;
+
+		burstcnt |= temp << 8;
+		if (burstcnt)
+			return burstcnt;
+		msleep(TPM_TIMEOUT);
+	} while (time_before(jiffies, stop));
+
+end:
+	return -EBUSY;
+} /* get_burstcount() */
+
+/*
+ * wait_for_stat wait for a TPM_STS value
+ * @param: chip, the tpm chip description
+ * @param: mask, the value mask to wait
+ * @param: timeout, the timeout
+ * @param: queue, the wait queue.
+ * @return: the tpm status, 0 if success, -ETIME if timeout is reached.
+ */
+static int wait_for_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout,
+			 wait_queue_head_t *queue)
+{
+	unsigned long stop;
+	long rc;
+	u8 status;
+
+	/* check current status */
+	status = tpm_stm_spi_status(chip);
+	if (!chip->vendor.irq && (status & mask) == mask)
+		return 0;
+
+	if (chip->vendor.irq) {
+		rc = wait_for_serirq_timeout(chip, ((tpm_stm_spi_status
+						    (chip) & mask) ==
+						     mask), timeout);
+		if (rc > 0)
+			return 0;
+	} else {
+		stop = jiffies + timeout;
+		do {
+			msleep(TPM_TIMEOUT);
+			status = tpm_stm_spi_status(chip);
+			if ((status & mask) == mask)
+				return 0;
+		} while (time_before(jiffies, stop));
+	}
+	return -ETIME;
+} /* wait_for_stat() */
+
+/*
+ * 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 tpm status, 0 if success, -ETIME if timeout is reached.
+ */
+static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count)
+{
+	u32 size = 0, burstcnt, status, len;
+
+	while (size < count &&
+	       wait_for_stat(chip,
+			     TPM_STS_DATA_AVAIL | TPM_STS_VALID,
+			     chip->vendor.timeout_c,
+			     &chip->vendor.read_queue)
+						== 0) {
+		burstcnt = get_burstcount(chip);
+		len = min_t(int, burstcnt, count - size);
+		status = spi_read8_reg(chip, TPM_DATA_FIFO, buf + size, len);
+		if (status < 0)
+			return status;
+
+
+		size += len;
+	}
+	return size;
+}
+
+/*
+ * tpm_ioserirq_handler the serirq irq handler
+ * @param: irq, the tpm chip description
+ * @param: dev_id, the description of the chip
+ * @return: the status of the handler.
+ */
+static irqreturn_t tpm_ioserirq_handler(int irq, void *dev_id)
+{
+	struct tpm_chip *chip = dev_id;
+	struct spi_device *client;
+	struct st33zp24_platform_data *pin_infos;
+
+	disable_irq_nosync(irq);
+
+	client = (struct spi_device *)chip->vendor.iobase;
+	pin_infos = client->dev.platform_data;
+
+	complete(&pin_infos->irq_detection);
+	return IRQ_HANDLED;
+} /* tpm_ioserirq_handler() */
+
+/*
+ * tpm_stm_spi_send send TPM commands through the SPI bus.
+ * @param: chip, the tpm chip description
+ * @param: buf, the tpm command buffer
+ * @param: len, the tpm command size
+ * @return: should be zero if success else the negative error code.
+ */
+static int tpm_stm_spi_send(struct tpm_chip *chip, unsigned char *buf,
+			    size_t len)
+{
+	u32 ret = 0, status, burstcnt = 0, i, size = 0;
+	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_spi_status(chip);
+	if ((status & TPM_STS_COMMAND_READY) == 0) {
+		tpm_stm_spi_cancel(chip);
+		if (wait_for_stat
+		    (chip, TPM_STS_COMMAND_READY, chip->vendor.timeout_b,
+		     &chip->vendor.int_queue) < 0) {
+			ret = -ETIME;
+			goto out_err;
+		}
+	}
+
+	for (i = 0; i < len - 1;) {
+		burstcnt = get_burstcount(chip);
+		size = min_t(int, len - i - 1, burstcnt);
+		ret = spi_write8_reg(chip, TPM_DATA_FIFO, buf, size);
+		if (ret < 0)
+			goto out_err;
+		i += size;
+	}
+
+	status = tpm_stm_spi_status(chip);
+	if ((status & TPM_STS_DATA_EXPECT) == 0) {
+		ret = -EIO;
+		goto out_err;
+	}
+
+	/* write last byte */
+	spi_write8_reg(chip, TPM_DATA_FIFO, buf + len - 1, 1);
+
+	status = tpm_stm_spi_status(chip);
+	if ((status & TPM_STS_DATA_EXPECT) != 0) {
+		ret = -EIO;
+		goto out_err;
+	}
+
+	/* go and do it */
+	data = TPM_STS_GO;
+	ret = spi_write8_reg(chip, TPM_STS, &data, 1);
+	if (ret < 0)
+		goto out_err;
+
+	return len;
+out_err:
+	tpm_stm_spi_cancel(chip);
+	release_locality(chip);
+	return ret;
+}
+
+/*
+ * tpm_stm_spi_recv received TPM response through the SPI bus.
+ * @param: chip, the tpm chip description
+ * @param: buf, the tpm command buffer
+ * @param: len, the tpm command size
+* @return: should be zero if success else the negative error code.
+ */
+static int tpm_stm_spi_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);
+
+	/* read first 10 bytes, including tag, paramsize, and result */
+	if (size < TPM_HEADER_SIZE) {
+		dev_err(chip->dev, "Unable to read header\n");
+		goto out;
+	}
+
+	expected = be32_to_cpu(*(__be32 *)(buf + 2));
+	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;
+}
+
+
+static const struct file_operations tpm_st33_spi_fops = {
+	.owner = THIS_MODULE,
+	.llseek = no_llseek,
+	.read = tpm_read,
+	.write = tpm_write,
+	.open = tpm_open,
+	.release = tpm_release,
+};
+
+static DEVICE_ATTR(pubek, S_IRUGO, tpm_show_pubek, NULL);
+static DEVICE_ATTR(pcrs, S_IRUGO, tpm_show_pcrs, NULL);
+static DEVICE_ATTR(enabled, S_IRUGO, tpm_show_enabled, NULL);
+static DEVICE_ATTR(active, S_IRUGO, tpm_show_active, NULL);
+static DEVICE_ATTR(owned, S_IRUGO, tpm_show_owned, NULL);
+static DEVICE_ATTR(temp_deactivated, S_IRUGO, tpm_show_temp_deactivated, NULL);
+static DEVICE_ATTR(caps, S_IRUGO, tpm_show_caps_1_2, NULL);
+static DEVICE_ATTR(cancel, S_IWUSR | S_IWGRP, NULL, tpm_store_cancel);
+
+static struct attribute *stm_tpm_attrs[] = {
+	&dev_attr_pubek.attr,
+	&dev_attr_pcrs.attr,
+	&dev_attr_enabled.attr,
+	&dev_attr_active.attr,
+	&dev_attr_owned.attr,
+	&dev_attr_temp_deactivated.attr,
+	&dev_attr_caps.attr,
+	&dev_attr_cancel.attr, NULL,
+};
+
+static struct attribute_group stm_tpm_attr_grp = {
+	.attrs = stm_tpm_attrs
+};
+
+static struct tpm_vendor_specific st_spi_tpm = {
+	.send = tpm_stm_spi_send,
+	.recv = tpm_stm_spi_recv,
+	.cancel = tpm_stm_spi_cancel,
+	.status = tpm_stm_spi_status,
+	.attr_group = &stm_tpm_attr_grp,
+	.miscdev = {.fops = &tpm_st33_spi_fops,},
+};
+
+static int evaluate_latency(struct tpm_chip *chip)
+{
+	int latency = 0;
+	struct spi_device *dev = (struct spi_device *)chip->vendor.iobase;
+	struct st33zp24_platform_data *platform_data = dev->dev.platform_data;
+	int status = 0;
+	u8 data;
+
+	for (; status == 0x00 && latency < MAX_SPI_LATENCY; latency++) {
+		platform_data->latency = latency;
+		status = spi_read8_reg(chip, TPM_INTF_CAPABILITY, &data, 1);
+	}
+	return latency - 1;
+} /* evaluate_latency() */
+
+static int interrupts;
+module_param(interrupts, int, 0444);
+MODULE_PARM_DESC(interrupts, "Enable interrupts");
+
+static int power_mgt = 1;
+module_param(power_mgt, int, 0444);
+MODULE_PARM_DESC(power_mgt, "Power Management");
+
+/*
+ * tpm_st33_spi_probe initialize the TPM device
+ * @param: client, the spi_device drescription (TPM SPI description).
+ * @param: id, the spi_device_id struct.
+ * @return: 0 in case of success.
+ *	 -1 in other case.
+ */
+static int
+tpm_st33_spi_probe(struct spi_device *dev)
+{
+	u32 err = 0;
+	u8 intmask;
+	struct tpm_chip *chip;
+	struct st33zp24_platform_data *platform_data;
+
+	/* Check SPI platform functionnalities */
+	if (dev == NULL) {
+		pr_info("dev is NULL. exiting.\n");
+		err = -ENODEV;
+		goto end;
+	}
+
+	chip = tpm_register_hardware(&dev->dev, &st_spi_tpm);
+	if (!chip) {
+		err = -ENODEV;
+		goto end;
+	}
+
+	/* Allocation of SPI buffers MISO and MOSI		*/
+	/* Size is as follow:					*/
+	/* Request burstcount value  = 0x800 = 2048		*/
+	/* +							*/
+	/* Response burstcount value = 0x400 = 1024		*/
+	/* +							*/
+	/* At least:						*/
+	/* 1 byte for direction/locality			*/
+	/* 1 byte tpm tis register				*/
+	/* 2 bytes spi data length (for request only)		*/
+	/* 2 latency bytes					*/
+	/* 1 status byte					*/
+	/* = 2048 + 1024 + 7					*/
+	/* We reserved 2048 + 1024 + 20 in case latency byte	*/
+	/* change						*/
+	platform_data = dev->dev.platform_data;
+	platform_data->tpm_spi_buffer[0] =
+	    kmalloc((TPM_BUFSIZE + (TPM_BUFSIZE / 2) +
+				 TPM_DIGEST_SIZE) * sizeof(u8), GFP_KERNEL);
+	if (platform_data->tpm_spi_buffer[0] == NULL) {
+		err = -ENOMEM;
+		goto _tpm_clean_answer;
+	}
+	platform_data->tpm_spi_buffer[1] =
+	    kmalloc((TPM_BUFSIZE + (TPM_BUFSIZE / 2) +
+				 TPM_DIGEST_SIZE) * sizeof(u8) , GFP_KERNEL);
+	if (platform_data->tpm_spi_buffer[1] == NULL) {
+		err = -ENOMEM;
+		goto _tpm_clean_response;
+	}
+
+	chip->vendor.iobase = dev;
+
+	/* Default timeouts */
+	chip->vendor.timeout_a = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
+	chip->vendor.timeout_b = msecs_to_jiffies(TIS_LONG_TIMEOUT);
+	chip->vendor.timeout_c = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
+	chip->vendor.timeout_d = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
+	chip->vendor.locality = LOCALITY0;
+
+	if (power_mgt) {
+		err = gpio_request(platform_data->io_lpcpd, "TPM IO_LPCPD");
+		if (err)
+			goto _gpio_init1;
+		gpio_set_value(platform_data->io_lpcpd, 1);
+	}
+
+	platform_data->latency = evaluate_latency(chip);
+	if (platform_data->latency <= 0x00) {
+		err = -ENODEV;
+		goto _gpio_init1;
+	}
+
+	/* Enable interrupt */
+	/* Register GPIO pin through generic Linux GPIO API */
+	if (interrupts) {
+		init_completion(&platform_data->irq_detection);
+		if (request_locality(chip) != LOCALITY0) {
+			err = -ENODEV;
+			goto _tpm_clean_response;
+		}
+		err = gpio_request(platform_data->io_serirq, "TPM IO_SERIRQ");
+		if (err)
+			goto _gpio_init2;
+
+		/* Clear all existing */
+		clear_interruption(chip);
+		err = request_irq(gpio_to_irq(platform_data->io_serirq),
+							&tpm_ioserirq_handler,
+				IRQF_TRIGGER_HIGH | IRQF_SHARED,
+				"TPM SERIRQ management", chip);
+		if (err < 0) {
+			pr_info("TPM SERIRQ signals %d not available\n",
+				gpio_to_irq(platform_data->io_serirq));
+				goto _irq_set;
+		}
+		err = spi_read8_reg(chip, TPM_INT_ENABLE, &intmask, 1);
+		if (err < 0)
+			goto _irq_set;
+
+		intmask |= TPM_INTF_CMD_READY_INT
+			|  TPM_INTF_FIFO_AVALAIBLE_INT
+			|  TPM_INTF_WAKE_UP_READY_INT
+			|  TPM_INTF_LOC4SOFTRELEASE_INT
+			|  TPM_INTF_LOCALITY_CHANGE_INT
+			|  TPM_INTF_STS_VALID_INT
+			|  TPM_INTF_DATA_AVAIL_INT;
+
+		err = spi_write8_reg(chip, TPM_INT_ENABLE, &intmask, 1);
+		if (err < 0)
+			goto _irq_set;
+
+		intmask = TPM_GLOBAL_INT_ENABLE;
+		err = spi_write8_reg(chip, TPM_INT_ENABLE + 3, &intmask, 1);
+		if (err < 0)
+			goto _irq_set;
+
+		err = spi_read8_reg(chip, TPM_INT_STATUS, &intmask, 1);
+		if (err < 0)
+			goto _irq_set;
+
+		chip->vendor.irq = interrupts;
+
+		tpm_gen_interrupt(chip);
+	}
+
+	tpm_get_timeouts(chip);
+
+	/* attach chip datas to client */
+	spi_set_drvdata(dev, chip);
+	platform_data->bchipf = false;
+
+	pr_info("TPM SPI Initialized\n");
+	return 0;
+_irq_set:
+	free_irq(gpio_to_irq(platform_data->io_serirq), chip);
+_gpio_init2:
+	if (platform_data && interrupts)
+		gpio_free(platform_data->io_serirq);
+_gpio_init1:
+	if (platform_data && power_mgt)
+		gpio_free(platform_data->io_lpcpd);
+_tpm_clean_response:
+	tpm_remove_hardware(chip->dev);
+	if (platform_data->tpm_spi_buffer[1] != NULL) {
+		kfree(platform_data->tpm_spi_buffer[1]);
+		platform_data->tpm_spi_buffer[1] = NULL;
+	}
+_tpm_clean_answer:
+	if (platform_data->tpm_spi_buffer[0] != NULL) {
+		kfree(platform_data->tpm_spi_buffer[0]);
+		platform_data->tpm_spi_buffer[0] = NULL;
+	}
+
+	platform_data->bchipf = true;
+end:
+	pr_info("TPM SPI initialisation fail\n");
+	return err;
+}
+
+/*
+ * tpm_st33_spi_remove remove the TPM device
+ * @param: client, the spi_device drescription (TPM SPI description).
+		clear_bit(0, &chip->is_open);
+ * @return: 0 in case of success.
+ */
+static int tpm_st33_spi_remove(struct spi_device *client)
+{
+	struct tpm_chip *chip = (struct tpm_chip *)spi_get_drvdata(client);
+	struct st33zp24_platform_data *pin_infos =
+		((struct spi_device *)chip->vendor.iobase)->dev.platform_data;
+
+	if (pin_infos != NULL) {
+		gpio_free(pin_infos->io_lpcpd);
+
+		/* Check if chip has been previously clean */
+		if (pin_infos->bchipf != true)
+			tpm_remove_hardware(chip->dev);
+		if (pin_infos->tpm_spi_buffer[1] != NULL) {
+			kfree(pin_infos->tpm_spi_buffer[1]);
+			pin_infos->tpm_spi_buffer[1] = NULL;
+		}
+		if (pin_infos->tpm_spi_buffer[0] != NULL) {
+			kfree(pin_infos->tpm_spi_buffer[0]);
+			pin_infos->tpm_spi_buffer[0] = NULL;
+		}
+	}
+
+	return 0;
+}
+
+#ifdef CONFIG_PM_SLEEP
+/*
+ * tpm_st33_spi_pm_suspend suspend the TPM device
+ * Added: Work around when suspend and no tpm application is running, suspend
+ * may fail because chip->data_buffer is not set (only set in tpm_open in Linux
+ * TPM core)
+ * @return: 0 in case of success.
+ */
+static int tpm_st33_spi_pm_suspend(struct device *dev)
+{
+	struct tpm_chip *chip = dev_get_drvdata(dev);
+	struct st33zp24_platform_data *pin_infos = dev->platform_data;
+	int ret = 0;
+
+	if (power_mgt) {
+		gpio_set_value(pin_infos->io_lpcpd, 0);
+	} else {
+		if (chip->data_buffer == NULL)
+			chip->data_buffer = pin_infos->tpm_spi_buffer[0];
+		ret = tpm_pm_suspend(dev);
+		}
+	return ret;
+}				/* tpm_st33_spi_suspend() */
+
+/*
+ * tpm_st33_spi_pm_resume resume the TPM device
+ * @param: spi, the spi_device drescription (TPM SPI description).
+ * @return: 0 in case of success.
+ */
+static int tpm_st33_spi_pm_resume(struct device *dev)
+{
+	struct tpm_chip *chip = dev_get_drvdata(dev);
+	struct st33zp24_platform_data *pin_infos = dev->platform_data;
+
+	int ret = 0;
+	if (power_mgt) {
+		gpio_set_value(pin_infos->io_lpcpd, 1);
+		ret = wait_for_serirq_timeout(chip,
+					 (chip->vendor.status(chip) &&
+					  TPM_STS_VALID) == TPM_STS_VALID,
+					  chip->vendor.timeout_b);
+	} else {
+		if (chip->data_buffer == NULL)
+			chip->data_buffer = pin_infos->tpm_spi_buffer[0];
+		ret = tpm_pm_resume(dev);
+		if (!ret)
+			tpm_do_selftest(chip);
+	}
+	return ret;
+} /* tpm_st33_spi_pm_resume() */
+#endif
+
+static SIMPLE_DEV_PM_OPS(tpm_st33_spi_ops, tpm_st33_spi_pm_suspend,
+	 tpm_st33_spi_pm_resume);
+static struct spi_driver tpm_st33_spi_driver = {
+	.driver = {
+		   .owner = THIS_MODULE,
+		   .name = TPM_ST33_SPI,
+		   .pm = &tpm_st33_spi_ops,
+		   },
+	.probe = tpm_st33_spi_probe,
+	.remove = tpm_st33_spi_remove,
+};
+
+/*
+ * tpm_st33_spi_exit The kernel calls this function during unloading the
+ * module or during shut down process
+ */
+static void __exit tpm_st33_spi_exit(void)
+{
+	spi_unregister_driver(&tpm_st33_spi_driver);
+}
+
+module_spi_driver(tpm_st33_spi_driver);
+
+MODULE_AUTHOR("Christophe Ricard (tpmsupport@st.com)");
+MODULE_DESCRIPTION("STM TPM SPI ST33 Driver");
+MODULE_VERSION("1.2.0");
+MODULE_LICENSE("GPL");
diff --git a/drivers/char/tpm/tpm_spi_stm_st33.h b/drivers/char/tpm/tpm_spi_stm_st33.h
new file mode 100644
index 0000000..752158e
--- /dev/null
+++ b/drivers/char/tpm/tpm_spi_stm_st33.h
@@ -0,0 +1,86 @@
+/*
+ * STMicroelectronics TPM SPI Linux driver for TPM ST33NP18
+ * Copyright (C) 2009, 2010  STMicroelectronics
+ *
+ * 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.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * STMicroelectronics version 1.2.0, Copyright (C) 2010
+ * STMicroelectronics comes with ABSOLUTELY NO WARRANTY.
+ * This is free software, and you are welcome to redistribute it
+ * under certain conditions.
+ *
+ * @Author: Christophe RICARD tpmsupport@st.com
+ *
+ * @File: tpm_spi_stm_st33.h
+ *
+ * @Date: 09/15/2010
+ */
+#ifndef __STM_ST33_TPM_SPI_MAIN_H__
+#define __STM_ST33_TPM_SPI_MAIN_H__
+
+#include <linux/pci.h>
+#include <linux/module.h>
+#include <linux/platform_device.h>
+#include <linux/fs.h>
+#include <linux/miscdevice.h>
+
+#define MINOR_NUM_SPI		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			0x00
+#define TPM_WRITE_DIRECTION		0x80
+#define TPM_HEADER_SIZE			10
+#define TPM_BUFSIZE			2048
+
+/* ioctl commands */
+#define TPMIOC_CANCEL		_IO('T', 0x00)
+#define TPMIOC_TRANSMIT		_IO('T', 0x01)
+#define TPMIOC_HASH		_IO('T', 0x02)
+#define TPMIOC_CHANGELOCALITY	_IO('T', 0x03)
+
+#define LOCALITY0		0
+#define LOCALITY4		4
+
+#define TPM_ST_SPI_OK		0x5A
+
+
+#define MAX_SPI_LATENCY		15
+
+#define TPM_ST33_SPI		"st33zp24_spi"
+
+struct st33zp24_platform_data {
+	int io_serirq;
+	int io_lpcpd;
+	int latency;
+	bool bchipf;
+	u8 *tpm_spi_buffer[2]; /* 0 Request 1 Response */
+	struct completion irq_detection;
+};
+
+struct st_tpm_hash_t {
+	int size;
+	u8 *data;
+};
+
+#endif /* __STM_ST33_TPM_SPI_MAIN_H__ */
-- 
1.7.1


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

* [PATCH 1/1] TPM: STMicroelectronics st33 driver SPI
@ 2013-04-29 13:39 Matthias Leblanc
  0 siblings, 0 replies; 21+ messages in thread
From: Matthias Leblanc @ 2013-04-29 13:39 UTC (permalink / raw)
  To: Kent Yoder, Rajiv Andrade, Marcel Selhorst, Sirrix AG, tpmdd-devel
  Cc: linux-kernel, Jean-Luc Blanc, Mathias Leblanc

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

 * STMicroelectronics version 1.2.0, Copyright (C) 2010
 * 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 SPI, in menuconfig or .config choose the tpm driver on
device --> tpm and activate the protocol of your choice before compiling
the kernel.
The driver will be accessible from within Linux.

Tested on linux x86/x64 and beagleboard REV B & XM REV C

Signed-off-by: Mathias Leblanc <mathias.leblanc@st.com>
---
 drivers/char/tpm/Kconfig            |   10 +
 drivers/char/tpm/Makefile           |    1 +
 drivers/char/tpm/tpm_spi_stm_st33.c | 1027 +++++++++++++++++++++++++++++++++++
 drivers/char/tpm/tpm_spi_stm_st33.h |   86 +++
 4 files changed, 1124 insertions(+), 0 deletions(-)
 create mode 100644 drivers/char/tpm/tpm_spi_stm_st33.c
 create mode 100644 drivers/char/tpm/tpm_spi_stm_st33.h

diff --git a/drivers/char/tpm/Kconfig b/drivers/char/tpm/Kconfig
index dbfd564..2fc1254 100644
--- a/drivers/char/tpm/Kconfig
+++ b/drivers/char/tpm/Kconfig
@@ -91,4 +91,14 @@ config TCG_ST33_I2C
         To compile this driver as a module, choose M here; the module will be
         called tpm_stm_st33_i2c.
 
+config TCG_ST33_SPI
+       tristate "STMicroelectronics ST33 SPI"
+       depends on SPI
+       depends on GPIOLIB
+       ---help---
+       If you have a TPM security chip from STMicroelectronics working with
+       an SPI bus say Yes and it will be accessible from within Linux.
+       To compile this driver as a module, choose M here; the module will be
+       called tpm_stm_st33_spi.
+
 endif # TCG_TPM
diff --git a/drivers/char/tpm/Makefile b/drivers/char/tpm/Makefile
index a3736c9..a1db3dd 100644
--- a/drivers/char/tpm/Makefile
+++ b/drivers/char/tpm/Makefile
@@ -18,3 +18,4 @@ obj-$(CONFIG_TCG_ATMEL) += tpm_atmel.o
 obj-$(CONFIG_TCG_INFINEON) += tpm_infineon.o
 obj-$(CONFIG_TCG_IBMVTPM) += tpm_ibmvtpm.o
 obj-$(CONFIG_TCG_ST33_I2C) += tpm_i2c_stm_st33.o
+obj-$(CONFIG_TCG_ST33_SPI) += tpm_spi_stm_st33.o
diff --git a/drivers/char/tpm/tpm_spi_stm_st33.c b/drivers/char/tpm/tpm_spi_stm_st33.c
new file mode 100644
index 0000000..70828dc
--- /dev/null
+++ b/drivers/char/tpm/tpm_spi_stm_st33.c
@@ -0,0 +1,1028 @@
+/*
+ * STMicroelectronics TPM SPI Linux driver for TPM ST33ZP24
+ * Copyright (C) 2009, 2010  STMicroelectronics
+ *
+ * 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.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * STMicroelectronics version 1.2.0, Copyright (C) 2010
+ * STMicroelectronics comes with ABSOLUTELY NO WARRANTY.
+ * This is free software, and you are welcome to redistribute it
+ * under certain conditions.
+ *
+ * @Author: Christophe RICARD tpmsupport@st.com
+ *
+ * @File: tpm_stm_st33_spi.c
+ *
+ * @Synopsis:
+ *	09/15/2010:	First shot driver tpm_tis driver for lpc is
+ *			used as model.
+ */
+
+#define pr_fmt(fmt)	KBUILD_MODNAME ": " fmt
+
+#include <linux/module.h>
+#include <linux/kernel.h>
+#include <linux/delay.h>
+#include <linux/init.h>
+#include <linux/spi/spi.h>
+#include <linux/wait.h>
+#include <linux/string.h>
+#include <linux/interrupt.h>
+#include <linux/spinlock.h>
+#include <linux/sysfs.h>
+#include <linux/gpio.h>
+#include <linux/sched.h>
+#include <linux/uaccess.h>
+#include <linux/io.h>
+#include <linux/slab.h>
+#include <linux/sched.h>
+
+#include "tpm.h"
+
+#include "tpm_spi_stm_st33.h"
+
+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 */
+};
+
+/*
+ * write8_reg
+ * Send byte to the TIS register according to the ST33ZP24 SPI protocol.
+ * @param: tpm, 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: should be zero if success else a negative error code.
+ */
+static int write8_reg(struct tpm_chip *tpm, u8 tpm_register,
+		      u8 *tpm_data, u16 tpm_size)
+{
+	u8 data;
+	int total_length = 0, nbr_dummy_bytes;
+	int value = 0;
+	struct spi_device *dev = (struct spi_device *)tpm->vendor.iobase;
+	struct st33zp24_platform_data *platform_data = dev->dev.platform_data;
+	u8 *data_buffer = platform_data->tpm_spi_buffer[0];
+	struct spi_transfer xfer = {
+		.tx_buf	 = data_buffer,
+		.rx_buf	 = platform_data->tpm_spi_buffer[1],
+	};
+	struct spi_message msg;
+	u8 latency = platform_data->latency + tpm_size + 1;
+
+	/* Pre-Header */
+	data = TPM_WRITE_DIRECTION | tpm->vendor.locality;
+	memcpy(data_buffer + total_length, &data, sizeof(data));
+	total_length++;
+	data = tpm_register;
+	memcpy(data_buffer + total_length, &data, sizeof(data));
+	total_length++;
+
+	if (tpm_size > 0 &&
+	     (tpm_register == TPM_DATA_FIFO ||
+	      tpm_register == TPM_HASH_DATA)) {
+		data = (tpm_size >> 8) & 0x00ff;
+		data_buffer[total_length++] = data;
+		data = tpm_size & 0x00ff;
+		data_buffer[total_length++] = data;
+		latency -= 2;
+	}
+
+	memcpy(&data_buffer[total_length], tpm_data, tpm_size);
+	total_length += tpm_size;
+
+	nbr_dummy_bytes = platform_data->latency + 1;
+	memset(&data_buffer[total_length], TPM_DUMMY_BYTE,
+					 platform_data->latency + 1);
+
+	xfer.len = total_length;
+	spi_message_init(&msg);
+	spi_message_add_tail(&xfer, &msg);
+	value = spi_sync(dev, &msg);
+
+	if (value == 0) {
+		nbr_dummy_bytes = total_length - 1 - nbr_dummy_bytes;
+		for (; nbr_dummy_bytes < total_length &&
+			 ((u8 *)xfer.rx_buf)[nbr_dummy_bytes] == 0;
+				 nbr_dummy_bytes++)
+			;
+
+		if (((u8 *)xfer.rx_buf)[nbr_dummy_bytes] != 0)
+			value = ((u8 *)xfer.rx_buf)[nbr_dummy_bytes];
+	}
+
+	return value;
+} /* write8_reg() */
+
+/*
+ * read8_reg
+ * Recv byte from the TIS register according to the ST33ZP24 SPI 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: should be zero if success else a negative error code.
+ */
+static u8 read8_reg(struct tpm_chip *tpm, u8 tpm_register,
+		    u8 *tpm_data, u16 tpm_size)
+{
+	u8 data;
+	int total_length = 0, nbr_dummy_bytes;
+	int value = 0;
+	struct spi_device *dev = (struct spi_device *)tpm->vendor.iobase;
+	struct st33zp24_platform_data *platform_data = dev->dev.platform_data;
+	u8 *data_buffer = platform_data->tpm_spi_buffer[0];
+	struct spi_transfer xfer = {
+		.tx_buf	 = data_buffer,
+		.rx_buf	 = platform_data->tpm_spi_buffer[1],
+	};
+	struct spi_message msg;
+
+	u8 latency = platform_data->latency + tpm_size + 1;
+
+	/* Pre-Header */
+
+	data = tpm->vendor.locality;
+	memcpy(data_buffer + total_length, &data, sizeof(data));
+	total_length++;
+	data = tpm_register;
+	memcpy(data_buffer + total_length, &data, sizeof(data));
+	total_length++;
+
+	nbr_dummy_bytes = platform_data->latency + 1;
+	memset(&data_buffer[total_length], TPM_DUMMY_BYTE,
+					 platform_data->latency + 1);
+
+	xfer.len = total_length;
+
+	/* header + status byte + size of the data + status byte */
+	spi_message_init(&msg);
+	spi_message_add_tail(&xfer, &msg);
+	value = spi_sync(dev, &msg);
+
+	if (tpm_size > 0 && value == 0) {
+		nbr_dummy_bytes = 2;
+		for (; nbr_dummy_bytes < total_length &&
+			 ((u8 *)xfer.rx_buf)[nbr_dummy_bytes] == 0;
+			 nbr_dummy_bytes++)
+			;
+
+		if (nbr_dummy_bytes + 1 < total_length) {
+			value = ((u8 *)xfer.rx_buf)[nbr_dummy_bytes];
+
+			if (tpm_size > 0)
+				memcpy(tpm_data, xfer.rx_buf +
+							 nbr_dummy_bytes + 1,
+								 tpm_size);
+		}
+	}
+	return value;
+} /* read8_reg() */
+
+/*
+ * spi_write_data()
+ * Send byte to the TIS register according to the ST33ZP24 SPI protocol.
+ * @param: tpm, 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: should be zero if success else the negative error code.
+ */
+static u32 spi_write_data(struct tpm_chip *tpm, u8 tpm_register,
+			  u8 *tpm_data, u16 tpm_size)
+{
+	u8 value = 0;
+	value = write8_reg(tpm, tpm_register, tpm_data, tpm_size);
+
+	switch (value) {
+	case TPM_ST_SPI_OK:
+		return TPM_ST_SPI_OK;
+	case 0:
+		return 0;
+	default:
+		return -EBUSY;
+	}
+} /* spi_write_data() */
+
+/*
+ * spi_read_data()
+ * Recv byte from the TIS register according to the ST33ZP24 SPI 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: should be zero if success else the negative error code.
+ */
+static u32 spi_read_data(struct tpm_chip *tpm, u8 tpm_register,
+			u8 *tpm_data, u16 tpm_size)
+{
+	u8 value = 0;
+
+	value = read8_reg(tpm, tpm_register, tpm_data, tpm_size);
+
+	switch (value) {
+	case TPM_ST_SPI_OK:
+		return TPM_ST_SPI_OK;
+	case 0:
+		return 0;
+	default:
+		return -EBUSY;
+	}
+} /* spi_read_data() */
+
+/*
+ * clear_interruption
+ * clear the TPM interrupt register.
+ * @param: tpm, the chip description
+ */
+static void clear_interruption(struct tpm_chip *tpm)
+{
+	u8 interrupt;
+	spi_read_data(tpm, TPM_INT_STATUS, &interrupt, 1);
+	/* Clear interrupts handled with TPM_EOI */
+	spi_write_data(tpm, TPM_INT_STATUS, &interrupt, 1);
+	spi_read_data(tpm, TPM_INT_STATUS, &interrupt, 1);
+} /* clear_interruption() */
+
+/*
+ * _wait_for_interrupt_serirq_timeout
+ * @param: tpm, the chip description
+ * @param: timeout, the timeout of the interrupt
+ * @return: the status of the interruption.
+ */
+static int _wait_for_interrupt_serirq_timeout(struct tpm_chip *chip,
+					unsigned long timeout)
+{
+	unsigned long status;
+	struct spi_device *client;
+	struct st33zp24_platform_data *pin_infos;
+
+	client = (struct spi_device *)chip->vendor.iobase;
+	pin_infos = client->dev.platform_data;
+
+	status = wait_for_completion_interruptible_timeout(
+				&pin_infos->irq_detection, timeout);
+	if (status > 0)
+		enable_irq(gpio_to_irq(pin_infos->io_serirq));
+	gpio_direction_input(pin_infos->io_serirq);
+
+	return status;
+} /* wait_for_interrupt_serirq_timeout() */
+
+unsigned long wait_for_serirq_timeout(struct tpm_chip *chip, bool condition,
+				 unsigned long timeout)
+{
+	unsigned long status = 0;
+
+	status = _wait_for_interrupt_serirq_timeout(chip, timeout);
+	if (!status) {
+		status = -EBUSY;
+		goto wait_end;
+	}
+	clear_interruption(chip);
+	if (condition)
+		status = 1;
+
+wait_end:
+	return status;
+}
+
+
+/*
+ * tpm_stm_spi_cancel, cancel is not implemented.
+ * @param: chip, the tpm chip description as specified in
+ * driver/char/tpm/tpm.h.
+ */
+static void tpm_stm_spi_cancel(struct tpm_chip *chip)
+{
+	u8 data = TPM_STS_COMMAND_READY;
+
+	/* this causes the current command to be aborted */
+	spi_write_data(chip, TPM_STS, &data, 1);
+} /* tpm_stm_spi_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_spi_status(struct tpm_chip *chip)
+{
+	u8 data;
+
+	spi_read_data(chip, TPM_STS, &data, 1);
+	return data;
+} /* tpm_stm_spi_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 = spi_read_data(chip, TPM_ACCESS, &data, 1);
+	if (status && (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 stop;
+	long rc;
+	u8 data;
+
+	/* Check locality */
+	if (check_locality(chip) == chip->vendor.locality)
+		return chip->vendor.locality;
+
+	/* Request locality */
+	data = TPM_ACCESS_REQUEST_USE;
+	rc = spi_write_data(chip, TPM_ACCESS, &data, 1);
+	if (rc < 0)
+		goto end;
+
+	/* wait for burstcount */
+	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 {
+		stop = jiffies + chip->vendor.timeout_a;
+		do {
+			if (check_locality(chip) >= 0)
+				return chip->vendor.locality;
+			msleep(TPM_TIMEOUT);
+		} while (time_before(jiffies, stop));
+	}
+end:
+	return -EACCES;
+} /* request_locality() */
+
+/*
+ * release_locality release the active locality
+ * @param: chip, the tpm chip description.
+ */
+static void release_locality(struct tpm_chip *chip)
+{
+	u8 data;
+
+	data = TPM_ACCESS_ACTIVE_LOCALITY;
+	spi_write_data(chip, TPM_ACCESS, &data, 1);
+} /* release_locality()*/
+
+/*
+ * 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 stop;
+	u32 burstcnt, status;
+	u8 tpm_reg, temp;
+
+	/* wait for burstcount */
+	/* which timeout value, spec has 2 answers (c & d) */
+	stop = jiffies + chip->vendor.timeout_d;
+	do {
+		tpm_reg = TPM_STS + 1;
+		status = spi_read_data(chip, tpm_reg, &temp, 1);
+		if (status < 0)
+			goto end;
+
+		tpm_reg = tpm_reg + 1;
+		burstcnt = temp;
+		status = spi_read_data(chip, tpm_reg, &temp, 1);
+		if (status < 0)
+			goto end;
+
+		burstcnt |= temp << 8;
+		if (burstcnt)
+			return burstcnt;
+		msleep(TPM_TIMEOUT);
+	} while (time_before(jiffies, stop));
+
+end:
+	return -EBUSY;
+} /* get_burstcount() */
+
+/*
+ * wait_for_stat wait for a TPM_STS value
+ * @param: chip, the tpm chip description
+ * @param: mask, the value mask to wait
+ * @param: timeout, the timeout
+ * @param: queue, the wait queue.
+ * @return: the tpm status, 0 if success, -ETIME if timeout is reached.
+ */
+static int wait_for_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout,
+			 wait_queue_head_t *queue)
+{
+	unsigned long stop;
+	long rc;
+	u8 status;
+
+	/* check current status */
+	status = tpm_stm_spi_status(chip);
+	if (!chip->vendor.irq && (status & mask) == mask)
+		return 0;
+
+	if (chip->vendor.irq) {
+		rc = wait_for_serirq_timeout(chip, ((tpm_stm_spi_status
+						    (chip) & mask) ==
+						     mask), timeout);
+		if (rc > 0)
+			return 0;
+	} else {
+		stop = jiffies + timeout;
+		do {
+			msleep(TPM_TIMEOUT);
+			status = tpm_stm_spi_status(chip);
+			if ((status & mask) == mask)
+				return 0;
+		} while (time_before(jiffies, stop));
+	}
+	return -ETIME;
+} /* wait_for_stat() */
+
+/*
+ * 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 tpm status, 0 if success, -ETIME if timeout is reached.
+ */
+static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count)
+{
+	u32 size = 0, burstcnt, status, len;
+
+	while (size < count &&
+	       wait_for_stat(chip,
+			     TPM_STS_DATA_AVAIL | TPM_STS_VALID,
+			     chip->vendor.timeout_c,
+			     &chip->vendor.read_queue)
+						== 0) {
+		burstcnt = get_burstcount(chip);
+		len = min_t(int, burstcnt, count - size);
+		status = spi_read_data(chip, TPM_DATA_FIFO, buf + size, len);
+		if (status < 0)
+			return status;
+
+
+		size += len;
+	}
+	return size;
+}
+
+/*
+ * tpm_ioserirq_handler the serirq irq handler
+ * @param: irq, the tpm chip description
+ * @param: dev_id, the description of the chip
+ * @return: the status of the handler.
+ */
+static irqreturn_t tpm_ioserirq_handler(int irq, void *dev_id)
+{
+	struct tpm_chip *chip = dev_id;
+	struct spi_device *client;
+	struct st33zp24_platform_data *pin_infos;
+
+	disable_irq_nosync(irq);
+
+	client = (struct spi_device *)chip->vendor.iobase;
+	pin_infos = client->dev.platform_data;
+
+	complete(&pin_infos->irq_detection);
+	return IRQ_HANDLED;
+} /* tpm_ioserirq_handler() */
+
+/*
+ * tpm_stm_spi_send send TPM commands through the SPI bus.
+ * @param: chip, the tpm chip description
+ * @param: buf, the tpm command buffer
+ * @param: len, the tpm command size
+ * @return: should be zero if success else the negative error code.
+ */
+static int tpm_stm_spi_send(struct tpm_chip *chip, unsigned char *buf,
+			    size_t len)
+{
+	u32 ret = 0, status, burstcnt = 0, i, size = 0;
+	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_spi_status(chip);
+	if ((status & TPM_STS_COMMAND_READY) == 0) {
+		tpm_stm_spi_cancel(chip);
+		if (wait_for_stat
+		    (chip, TPM_STS_COMMAND_READY, chip->vendor.timeout_b,
+		     &chip->vendor.int_queue) < 0) {
+			ret = -ETIME;
+			goto out_err;
+		}
+	}
+
+	for (i = 0; i < len - 1;) {
+		burstcnt = get_burstcount(chip);
+		size = min_t(int, len - i - 1, burstcnt);
+		ret = spi_write_data(chip, TPM_DATA_FIFO, buf, size);
+		if (ret < 0)
+			goto out_err;
+		i += size;
+	}
+
+	status = tpm_stm_spi_status(chip);
+	if ((status & TPM_STS_DATA_EXPECT) == 0) {
+		ret = -EIO;
+		goto out_err;
+	}
+
+	/* write last byte */
+	spi_write_data(chip, TPM_DATA_FIFO, buf + len - 1, 1);
+
+	status = tpm_stm_spi_status(chip);
+	if ((status & TPM_STS_DATA_EXPECT) != 0) {
+		ret = -EIO;
+		goto out_err;
+	}
+
+	/* go and do it */
+	data = TPM_STS_GO;
+	ret = spi_write_data(chip, TPM_STS, &data, 1);
+	if (ret < 0)
+		goto out_err;
+
+	return len;
+out_err:
+	tpm_stm_spi_cancel(chip);
+	release_locality(chip);
+	return ret;
+}
+
+/*
+ * tpm_stm_spi_recv received TPM response through the SPI bus.
+ * @param: chip, the tpm chip description
+ * @param: buf, the tpm command buffer
+ * @param: len, the tpm command size
+* @return: should be zero if success else the negative error code.
+ */
+static int tpm_stm_spi_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);
+
+	/* read first 10 bytes, including tag, paramsize, and result */
+	if (size < TPM_HEADER_SIZE) {
+		dev_err(chip->dev, "Unable to read header\n");
+		goto out;
+	}
+
+	expected = be32_to_cpu(*(__be32 *)(buf + 2));
+	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;
+}
+
+
+static const struct file_operations tpm_st33_spi_fops = {
+	.owner = THIS_MODULE,
+	.llseek = no_llseek,
+	.read = tpm_read,
+	.write = tpm_write,
+	.open = tpm_open,
+	.release = tpm_release,
+};
+
+static DEVICE_ATTR(pubek, S_IRUGO, tpm_show_pubek, NULL);
+static DEVICE_ATTR(pcrs, S_IRUGO, tpm_show_pcrs, NULL);
+static DEVICE_ATTR(enabled, S_IRUGO, tpm_show_enabled, NULL);
+static DEVICE_ATTR(active, S_IRUGO, tpm_show_active, NULL);
+static DEVICE_ATTR(owned, S_IRUGO, tpm_show_owned, NULL);
+static DEVICE_ATTR(temp_deactivated, S_IRUGO, tpm_show_temp_deactivated, NULL);
+static DEVICE_ATTR(caps, S_IRUGO, tpm_show_caps_1_2, NULL);
+static DEVICE_ATTR(cancel, S_IWUSR | S_IWGRP, NULL, tpm_store_cancel);
+
+static struct attribute *stm_tpm_attrs[] = {
+	&dev_attr_pubek.attr,
+	&dev_attr_pcrs.attr,
+	&dev_attr_enabled.attr,
+	&dev_attr_active.attr,
+	&dev_attr_owned.attr,
+	&dev_attr_temp_deactivated.attr,
+	&dev_attr_caps.attr,
+	&dev_attr_cancel.attr, NULL,
+};
+
+static struct attribute_group stm_tpm_attr_grp = {
+	.attrs = stm_tpm_attrs
+};
+
+static struct tpm_vendor_specific st_spi_tpm = {
+	.send = tpm_stm_spi_send,
+	.recv = tpm_stm_spi_recv,
+	.cancel = tpm_stm_spi_cancel,
+	.status = tpm_stm_spi_status,
+	.attr_group = &stm_tpm_attr_grp,
+	.miscdev = {.fops = &tpm_st33_spi_fops,},
+};
+
+static int evaluate_latency(struct tpm_chip *chip)
+{
+	int latency = 0;
+	struct spi_device *dev = (struct spi_device *)chip->vendor.iobase;
+	struct st33zp24_platform_data *platform_data = dev->dev.platform_data;
+	int status = 0;
+	u8 data;
+
+	for (; status == 0x00 && latency < MAX_SPI_LATENCY; latency++) {
+		platform_data->latency = latency;
+		status = spi_read_data(chip, TPM_INTF_CAPABILITY, &data, 1);
+	}
+	return latency - 1;
+} /* evaluate_latency() */
+
+static int interrupts;
+module_param(interrupts, int, 0444);
+MODULE_PARM_DESC(interrupts, "Enable interrupts");
+
+static int power_mgt = 1;
+module_param(power_mgt, int, 0444);
+MODULE_PARM_DESC(power_mgt, "Power Management");
+
+/*
+ * tpm_st33_spi_probe initialize the TPM device
+ * @param: client, the spi_device drescription (TPM SPI description).
+ * @param: id, the spi_device_id struct.
+ * @return: 0 in case of success.
+ *	 -1 in other case.
+ */
+static int
+tpm_st33_spi_probe(struct spi_device *dev)
+{
+	u32 err = 0;
+	u8 intmask;
+	struct tpm_chip *chip;
+	struct st33zp24_platform_data *platform_data;
+
+	/* Check SPI platform functionnalities */
+	if (dev == NULL) {
+		pr_info("dev is NULL. exiting.\n");
+		err = -ENODEV;
+		goto end;
+	}
+
+	chip = tpm_register_hardware(&dev->dev, &st_spi_tpm);
+	if (!chip) {
+		err = -ENODEV;
+		goto end;
+	}
+
+	/* Allocation of SPI buffers MISO and MOSI		*/
+	/* Size is as follow:					*/
+	/* Request burstcount value  = 0x800 = 2048		*/
+	/* +							*/
+	/* Response burstcount value = 0x400 = 1024		*/
+	/* +							*/
+	/* At least:						*/
+	/* 1 byte for direction/locality			*/
+	/* 1 byte tpm tis register				*/
+	/* 2 bytes spi data length (for request only)		*/
+	/* 2 latency bytes					*/
+	/* 1 status byte					*/
+	/* = 2048 + 1024 + 7					*/
+	/* We reserved 2048 + 1024 + 20 in case latency byte	*/
+	/* change						*/
+	platform_data = dev->dev.platform_data;
+	platform_data->tpm_spi_buffer[0] =
+	    kmalloc((TPM_BUFSIZE + (TPM_BUFSIZE / 2) +
+				 TPM_DIGEST_SIZE) * sizeof(u8), GFP_KERNEL);
+	if (platform_data->tpm_spi_buffer[0] == NULL) {
+		err = -ENOMEM;
+		goto _tpm_clean_answer;
+	}
+	platform_data->tpm_spi_buffer[1] =
+	    kmalloc((TPM_BUFSIZE + (TPM_BUFSIZE / 2) +
+				 TPM_DIGEST_SIZE) * sizeof(u8) , GFP_KERNEL);
+	if (platform_data->tpm_spi_buffer[1] == NULL) {
+		err = -ENOMEM;
+		goto _tpm_clean_response;
+	}
+
+	chip->vendor.iobase = dev;
+
+	/* Default timeouts */
+	chip->vendor.timeout_a = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
+	chip->vendor.timeout_b = msecs_to_jiffies(TIS_LONG_TIMEOUT);
+	chip->vendor.timeout_c = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
+	chip->vendor.timeout_d = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
+	chip->vendor.locality = LOCALITY0;
+
+	if (power_mgt) {
+		err = gpio_request(platform_data->io_lpcpd, "TPM IO_LPCPD");
+		if (err)
+			goto _gpio_init1;
+		gpio_set_value(platform_data->io_lpcpd, 1);
+	}
+
+	platform_data->latency = evaluate_latency(chip);
+	if (platform_data->latency <= 0x00) {
+		err = -ENODEV;
+		goto _gpio_init1;
+	}
+
+	/* Enable interrupt */
+	/* Register GPIO pin through generic Linux GPIO API */
+	if (interrupts) {
+		init_completion(&platform_data->irq_detection);
+		if (request_locality(chip) != LOCALITY0) {
+			err = -ENODEV;
+			goto _tpm_clean_response;
+		}
+		err = gpio_request(platform_data->io_serirq, "TPM IO_SERIRQ");
+		if (err)
+			goto _gpio_init2;
+
+		/* Clear all existing */
+		clear_interruption(chip);
+		err = request_irq(gpio_to_irq(platform_data->io_serirq),
+							&tpm_ioserirq_handler,
+				IRQF_TRIGGER_HIGH | IRQF_SHARED,
+				"TPM SERIRQ management", chip);
+		if (err < 0) {
+			pr_info("TPM SERIRQ signals %d not available\n",
+			gpio_to_irq(platform_data->io_serirq));
+			goto _irq_set;
+		}
+		err = spi_read_data(chip, TPM_INT_ENABLE, &intmask, 1);
+		if (err < 0)
+			goto _irq_set;
+
+		intmask |= TPM_INTF_CMD_READY_INT
+			|  TPM_INTF_FIFO_AVALAIBLE_INT
+			|  TPM_INTF_WAKE_UP_READY_INT
+			|  TPM_INTF_LOC4SOFTRELEASE_INT
+			|  TPM_INTF_LOCALITY_CHANGE_INT
+			|  TPM_INTF_STS_VALID_INT
+			|  TPM_INTF_DATA_AVAIL_INT;
+
+		err = spi_write_data(chip, TPM_INT_ENABLE, &intmask, 1);
+		if (err < 0)
+			goto _irq_set;
+
+		intmask = TPM_GLOBAL_INT_ENABLE;
+		err = spi_write_data(chip, TPM_INT_ENABLE + 3, &intmask, 1);
+		if (err < 0)
+			goto _irq_set;
+
+		err = spi_read_data(chip, TPM_INT_STATUS, &intmask, 1);
+		if (err < 0)
+			goto _irq_set;
+
+		chip->vendor.irq = interrupts;
+
+		tpm_gen_interrupt(chip);
+	}
+
+	tpm_get_timeouts(chip);
+
+	/* attach chip datas to client */
+	spi_set_drvdata(dev, chip);
+	platform_data->bchipf = false;
+
+	pr_info("TPM SPI Initialized\n");
+	return 0;
+_irq_set:
+	free_irq(gpio_to_irq(platform_data->io_serirq), chip);
+_gpio_init2:
+	if (platform_data && interrupts)
+		gpio_free(platform_data->io_serirq);
+_gpio_init1:
+	if (platform_data && power_mgt)
+		gpio_free(platform_data->io_lpcpd);
+_tpm_clean_response:
+	tpm_remove_hardware(chip->dev);
+	if (platform_data->tpm_spi_buffer[1] != NULL) {
+		kfree(platform_data->tpm_spi_buffer[1]);
+		platform_data->tpm_spi_buffer[1] = NULL;
+	}
+_tpm_clean_answer:
+	if (platform_data->tpm_spi_buffer[0] != NULL) {
+		kfree(platform_data->tpm_spi_buffer[0]);
+		platform_data->tpm_spi_buffer[0] = NULL;
+	}
+
+	platform_data->bchipf = true;
+end:
+	pr_info("TPM SPI initialisation fail\n");
+	return err;
+}
+
+/*
+ * tpm_st33_spi_remove remove the TPM device
+ * @param: client, the spi_device drescription (TPM SPI description).
+		clear_bit(0, &chip->is_open);
+ * @return: 0 in case of success.
+ */
+static int tpm_st33_spi_remove(struct spi_device *client)
+{
+	struct tpm_chip *chip = (struct tpm_chip *)spi_get_drvdata(client);
+	struct st33zp24_platform_data *pin_infos =
+		((struct spi_device *)chip->vendor.iobase)->dev.platform_data;
+
+	if (pin_infos != NULL) {
+		gpio_free(pin_infos->io_lpcpd);
+
+		/* Check if chip has been previously clean */
+		if (pin_infos->bchipf != true)
+			tpm_remove_hardware(chip->dev);
+		if (pin_infos->tpm_spi_buffer[1] != NULL) {
+			kfree(pin_infos->tpm_spi_buffer[1]);
+			pin_infos->tpm_spi_buffer[1] = NULL;
+		}
+		if (pin_infos->tpm_spi_buffer[0] != NULL) {
+			kfree(pin_infos->tpm_spi_buffer[0]);
+			pin_infos->tpm_spi_buffer[0] = NULL;
+		}
+	}
+
+	return 0;
+}
+
+/*
+ * tpm_st33_spi_pm_suspend suspend the TPM device
+ * Added: Work around when suspend and no tpm application is running, suspend
+ * may fail because chip->data_buffer is not set (only set in tpm_open in Linux
+ * TPM core)
+ * @param: client, the spi_device drescription (TPM SPI description).
+ * @param: mesg, the power management message.
+ * @return: 0 in case of success.
+ */
+static int tpm_st33_spi_pm_suspend(struct spi_device *client,
+							 pm_message_t mesg)
+{
+	struct tpm_chip *chip =
+	    (struct tpm_chip *)spi_get_drvdata(client);
+	struct st33zp24_platform_data *pin_infos =
+		((struct spi_device *)chip->vendor.iobase)->dev.platform_data;
+	int ret = 0;
+
+	if (power_mgt)
+		gpio_set_value(pin_infos->io_lpcpd, 0);
+	else {
+		if (chip->data_buffer == NULL)
+			chip->data_buffer = pin_infos->tpm_spi_buffer[0];
+		ret = tpm_pm_suspend(&client->dev);
+		}
+	return ret;
+}				/* tpm_st33_spi_suspend() */
+
+/*
+ * tpm_st33_spi_pm_resume resume the TPM device
+ * @param: spi, the spi_device drescription (TPM SPI description).
+ * @return: 0 in case of success.
+ */
+static int tpm_st33_spi_pm_resume(struct spi_device *spi)
+{
+	struct tpm_chip *chip =
+	    (struct tpm_chip *)spi_get_drvdata(spi);
+	struct st33zp24_platform_data *pin_infos =
+		((struct spi_device *)chip->vendor.iobase)->dev.platform_data;
+
+	int ret = 0;
+	if (power_mgt) {
+		gpio_set_value(pin_infos->io_lpcpd, 1);
+		ret = wait_for_serirq_timeout(chip,
+					 (chip->vendor.status(chip) &&
+					  TPM_STS_VALID) == TPM_STS_VALID,
+					  chip->vendor.timeout_b);
+	} else {
+		if (chip->data_buffer == NULL)
+			chip->data_buffer = pin_infos->tpm_spi_buffer[0];
+		ret = tpm_pm_resume(&spi->dev);
+		if (!ret)
+			tpm_do_selftest(chip);
+	}
+	return ret;
+}				/* tpm_st33_spi_pm_resume() */
+
+static struct spi_driver tpm_st33_spi_driver = {
+	.driver = {
+		   .owner = THIS_MODULE,
+		   .name = TPM_ST33_SPI,
+		   },
+	.probe = tpm_st33_spi_probe,
+	.remove = tpm_st33_spi_remove,
+	.resume = tpm_st33_spi_pm_resume,
+	.suspend = tpm_st33_spi_pm_suspend,
+};
+
+/*
+ * tpm_st33_spi_init initialize driver
+ * @return: 0 if successful, else non zero value.
+ */
+static int __init tpm_st33_spi_init(void)
+{
+	return spi_register_driver(&tpm_st33_spi_driver);
+}
+
+/*
+ * tpm_st33_spi_exit The kernel calls this function during unloading the
+ * module or during shut down process
+ */
+static void __exit tpm_st33_spi_exit(void)
+{
+	spi_unregister_driver(&tpm_st33_spi_driver);
+}
+
+module_init(tpm_st33_spi_init);
+module_exit(tpm_st33_spi_exit);
+
+MODULE_AUTHOR("Christophe Ricard (tpmsupport@st.com)");
+MODULE_DESCRIPTION("STM TPM SPI ST33 Driver");
+MODULE_VERSION("1.2.0");
+MODULE_LICENSE("GPL");
diff --git a/drivers/char/tpm/tpm_spi_stm_st33.h b/drivers/char/tpm/tpm_spi_stm_st33.h
new file mode 100644
index 0000000..752158e
--- /dev/null
+++ b/drivers/char/tpm/tpm_spi_stm_st33.h
@@ -0,0 +1,86 @@
+/*
+ * STMicroelectronics TPM SPI Linux driver for TPM ST33NP18
+ * Copyright (C) 2009, 2010  STMicroelectronics
+ *
+ * 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.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * STMicroelectronics version 1.2.0, Copyright (C) 2010
+ * STMicroelectronics comes with ABSOLUTELY NO WARRANTY.
+ * This is free software, and you are welcome to redistribute it
+ * under certain conditions.
+ *
+ * @Author: Christophe RICARD tpmsupport@st.com
+ *
+ * @File: tpm_spi_stm_st33.h
+ *
+ * @Date: 09/15/2010
+ */
+#ifndef __STM_ST33_TPM_SPI_MAIN_H__
+#define __STM_ST33_TPM_SPI_MAIN_H__
+
+#include <linux/pci.h>
+#include <linux/module.h>
+#include <linux/platform_device.h>
+#include <linux/fs.h>
+#include <linux/miscdevice.h>
+
+#define MINOR_NUM_SPI		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			0x00
+#define TPM_WRITE_DIRECTION		0x80
+#define TPM_HEADER_SIZE			10
+#define TPM_BUFSIZE			2048
+
+/* ioctl commands */
+#define TPMIOC_CANCEL		_IO('T', 0x00)
+#define TPMIOC_TRANSMIT		_IO('T', 0x01)
+#define TPMIOC_HASH		_IO('T', 0x02)
+#define TPMIOC_CHANGELOCALITY	_IO('T', 0x03)
+
+#define LOCALITY0		0
+#define LOCALITY4		4
+
+#define TPM_ST_SPI_OK		0x5A
+
+
+#define MAX_SPI_LATENCY		15
+
+#define TPM_ST33_SPI		"st33zp24_spi"
+
+struct st33zp24_platform_data {
+	int io_serirq;
+	int io_lpcpd;
+	int latency;
+	bool bchipf;
+	u8 *tpm_spi_buffer[2]; /* 0 Request 1 Response */
+	struct completion irq_detection;
+};
+
+struct st_tpm_hash_t {
+	int size;
+	u8 *data;
+};
+
+#endif /* __STM_ST33_TPM_SPI_MAIN_H__ */
-- 
1.7.1


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

* Re: [PATCH 1/1] TPM: STMicroelectronics st33 driver SPI
  2013-04-22 16:20 Matthias Leblanc
@ 2013-04-22 18:26 ` Kent Yoder
  0 siblings, 0 replies; 21+ messages in thread
From: Kent Yoder @ 2013-04-22 18:26 UTC (permalink / raw)
  To: Matthias Leblanc
  Cc: Kent Yoder, Rajiv Andrade, Marcel Selhorst, Sirrix AG,
	tpmdd-devel, linux-kernel, Jean-Luc Blanc

On Mon, Apr 22, 2013 at 06:20:17PM +0200, Matthias Leblanc wrote:
> From: Mathias Leblanc <mathias.leblanc@st.com>
> 
>  * STMicroelectronics version 1.2.0, Copyright (C) 2010
>  * 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 SPI, in menuconfig or .config choose the tpm driver on
> device --> tpm and activate the protocol of your choice before compiling
> the kernel.
> The driver will be accessible from within Linux.
> 
> Tested on linux x86/x64 and beagleboard REV B & XM REV C
> 
> Signed-off-by: Mathias Leblanc <mathias.leblanc@st.com>
> ---
>  drivers/char/tpm/Kconfig            |   10 +
>  drivers/char/tpm/Makefile           |    1 +
>  drivers/char/tpm/tpm_spi_stm_st33.c | 1027 +++++++++++++++++++++++++++++++++++
>  drivers/char/tpm/tpm_spi_stm_st33.h |   86 +++
>  4 files changed, 1124 insertions(+), 0 deletions(-)
>  create mode 100644 drivers/char/tpm/tpm_spi_stm_st33.c
>  create mode 100644 drivers/char/tpm/tpm_spi_stm_st33.h
> 
> diff --git a/drivers/char/tpm/Kconfig b/drivers/char/tpm/Kconfig
> index dbfd564..2fc1254 100644
> --- a/drivers/char/tpm/Kconfig
> +++ b/drivers/char/tpm/Kconfig
> @@ -91,4 +91,14 @@ config TCG_ST33_I2C
>          To compile this driver as a module, choose M here; the module will be
>          called tpm_stm_st33_i2c.
>  
> +config TCG_ST33_SPI
> +       tristate "STMicroelectronics ST33 SPI"
> +       depends on SPI
> +       depends on GPIOLIB
> +       ---help---
> +       If you have a TPM security chip from STMicroelectronics working with
> +       an SPI bus say Yes and it will be accessible from within Linux.
> +       To compile this driver as a module, choose M here; the module will be
> +       called tpm_stm_st33_spi.
> +
>  endif # TCG_TPM
> diff --git a/drivers/char/tpm/Makefile b/drivers/char/tpm/Makefile
> index a3736c9..a1db3dd 100644
> --- a/drivers/char/tpm/Makefile
> +++ b/drivers/char/tpm/Makefile
> @@ -18,3 +18,4 @@ obj-$(CONFIG_TCG_ATMEL) += tpm_atmel.o
>  obj-$(CONFIG_TCG_INFINEON) += tpm_infineon.o
>  obj-$(CONFIG_TCG_IBMVTPM) += tpm_ibmvtpm.o
>  obj-$(CONFIG_TCG_ST33_I2C) += tpm_i2c_stm_st33.o
> +obj-$(CONFIG_TCG_ST33_SPI) += tpm_spi_stm_st33.o
> diff --git a/drivers/char/tpm/tpm_spi_stm_st33.c b/drivers/char/tpm/tpm_spi_stm_st33.c
> new file mode 100644
> index 0000000..70828dc
> --- /dev/null
> +++ b/drivers/char/tpm/tpm_spi_stm_st33.c
> @@ -0,0 +1,1028 @@
> +/*
> + * STMicroelectronics TPM SPI Linux driver for TPM ST33ZP24
> + * Copyright (C) 2009, 2010  STMicroelectronics
> + *
> + * 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.,
> + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
> + *
> + * STMicroelectronics version 1.2.0, Copyright (C) 2010
> + * STMicroelectronics comes with ABSOLUTELY NO WARRANTY.
> + * This is free software, and you are welcome to redistribute it
> + * under certain conditions.
> + *
> + * @Author: Christophe RICARD tpmsupport@st.com
> + *
> + * @File: tpm_stm_st33_spi.c
> + *
> + * @Synopsis:
> + *	09/15/2010:	First shot driver tpm_tis driver for lpc is
> + *			used as model.
> + */
> +
> +#define pr_fmt(fmt)	KBUILD_MODNAME ": " fmt
> +
> +#include <linux/module.h>
> +#include <linux/kernel.h>
> +#include <linux/delay.h>
> +#include <linux/init.h>
> +#include <linux/spi/spi.h>
> +#include <linux/wait.h>
> +#include <linux/string.h>
> +#include <linux/interrupt.h>
> +#include <linux/spinlock.h>
> +#include <linux/sysfs.h>
> +#include <linux/gpio.h>
> +#include <linux/sched.h>
> +#include <linux/uaccess.h>
> +#include <linux/io.h>
> +#include <linux/slab.h>
> +#include <linux/sched.h>
> +
> +#include "tpm.h"
> +
> +#include "tpm_spi_stm_st33.h"
> +
> +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 */
> +};
> +
> +/*
> + * write8_reg
> + * Send byte to the TIS register according to the ST33ZP24 SPI protocol.
> + * @param: tpm, 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: should be zero if success else a negative error code.
> + */
> +static int write8_reg(struct tpm_chip *tpm, u8 tpm_register,
> +		      u8 *tpm_data, u16 tpm_size)
> +{
> +	u8 data;
> +	int total_length = 0, nbr_dummy_bytes;
> +	int value = 0;
> +	struct spi_device *dev = (struct spi_device *)tpm->vendor.iobase;
> +	struct st33zp24_platform_data *platform_data = dev->dev.platform_data;
> +	u8 *data_buffer = platform_data->tpm_spi_buffer[0];
> +	struct spi_transfer xfer = {
> +		.tx_buf	 = data_buffer,
> +		.rx_buf	 = platform_data->tpm_spi_buffer[1],
> +	};
> +	struct spi_message msg;
> +	u8 latency = platform_data->latency + tpm_size + 1;
> +
> +	/* Pre-Header */
> +	data = TPM_WRITE_DIRECTION | tpm->vendor.locality;
> +	memcpy(data_buffer + total_length, &data, sizeof(data));
> +	total_length++;
> +	data = tpm_register;
> +	memcpy(data_buffer + total_length, &data, sizeof(data));
> +	total_length++;
> +
> +	if (tpm_size > 0 &&
> +	     (tpm_register == TPM_DATA_FIFO ||
> +	      tpm_register == TPM_HASH_DATA)) {
> +		data = (tpm_size >> 8) & 0x00ff;
> +		data_buffer[total_length++] = data;
> +		data = tpm_size & 0x00ff;
> +		data_buffer[total_length++] = data;
> +		latency -= 2;
> +	}
> +
> +	memcpy(&data_buffer[total_length], tpm_data, tpm_size);
> +	total_length += tpm_size;
> +
> +	nbr_dummy_bytes = platform_data->latency + 1;
> +	memset(&data_buffer[total_length], TPM_DUMMY_BYTE,
> +					 platform_data->latency + 1);
> +
> +	xfer.len = total_length;
> +	spi_message_init(&msg);
> +	spi_message_add_tail(&xfer, &msg);
> +	value = spi_sync(dev, &msg);
> +
> +	if (value == 0) {
> +		nbr_dummy_bytes = total_length - 1 - nbr_dummy_bytes;
> +		for (; nbr_dummy_bytes < total_length &&
> +			 ((u8 *)xfer.rx_buf)[nbr_dummy_bytes] == 0;
> +				 nbr_dummy_bytes++)
> +			;
> +
> +		if (((u8 *)xfer.rx_buf)[nbr_dummy_bytes] != 0)
> +			value = ((u8 *)xfer.rx_buf)[nbr_dummy_bytes];
> +	}
> +
> +	return value;
> +} /* write8_reg() */
> +
> +/*
> + * read8_reg
> + * Recv byte from the TIS register according to the ST33ZP24 SPI 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: should be zero if success else a negative error code.
> + */
> +static u8 read8_reg(struct tpm_chip *tpm, u8 tpm_register,
> +		    u8 *tpm_data, u16 tpm_size)
> +{
> +	u8 data;
> +	int total_length = 0, nbr_dummy_bytes;
> +	int value = 0;
> +	struct spi_device *dev = (struct spi_device *)tpm->vendor.iobase;
> +	struct st33zp24_platform_data *platform_data = dev->dev.platform_data;
> +	u8 *data_buffer = platform_data->tpm_spi_buffer[0];
> +	struct spi_transfer xfer = {
> +		.tx_buf	 = data_buffer,
> +		.rx_buf	 = platform_data->tpm_spi_buffer[1],
> +	};
> +	struct spi_message msg;
> +
> +	u8 latency = platform_data->latency + tpm_size + 1;
> +
> +	/* Pre-Header */
> +
> +	data = tpm->vendor.locality;
> +	memcpy(data_buffer + total_length, &data, sizeof(data));
> +	total_length++;
> +	data = tpm_register;
> +	memcpy(data_buffer + total_length, &data, sizeof(data));
> +	total_length++;
> +
> +	nbr_dummy_bytes = platform_data->latency + 1;
> +	memset(&data_buffer[total_length], TPM_DUMMY_BYTE,
> +					 platform_data->latency + 1);
> +
> +	xfer.len = total_length;
> +
> +	/* header + status byte + size of the data + status byte */
> +	spi_message_init(&msg);
> +	spi_message_add_tail(&xfer, &msg);
> +	value = spi_sync(dev, &msg);
> +
> +	if (tpm_size > 0 && value == 0) {
> +		nbr_dummy_bytes = 2;
> +		for (; nbr_dummy_bytes < total_length &&
> +			 ((u8 *)xfer.rx_buf)[nbr_dummy_bytes] == 0;
> +			 nbr_dummy_bytes++)
> +			;
> +
> +		if (nbr_dummy_bytes + 1 < total_length) {
> +			value = ((u8 *)xfer.rx_buf)[nbr_dummy_bytes];
> +
> +			if (tpm_size > 0)
> +				memcpy(tpm_data, xfer.rx_buf +
> +							 nbr_dummy_bytes + 1,
> +								 tpm_size);
> +		}
> +	}
> +	return value;
> +} /* read8_reg() */
> +
> +/*
> + * spi_write_data()
> + * Send byte to the TIS register according to the ST33ZP24 SPI protocol.
> + * @param: tpm, 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: should be zero if success else the negative error code.
> + */
> +static u32 SPI_WRITE_DATA(struct tpm_chip *tpm, u8 tpm_register,
> +			  u8 *tpm_data, u16 tpm_size)
> +{
> +	u8 value = 0;
> +	value = write8_reg(tpm, tpm_register, tpm_data, tpm_size);
> +
> +	switch (value) {
> +	case TPM_ST_SPI_OK:
> +		return TPM_ST_SPI_OK;
> +	case 0:
> +		return 0;
> +	default:
> +		return -EBUSY;
> +	}
> +} /* spi_write_data() */
> +
> +/*
> + * spi_read_data()

  Umm...

> + * Recv byte from the TIS register according to the ST33ZP24 SPI 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: should be zero if success else the negative error code.
> + */
> +static u32 SPI_READ_DATA(struct tpm_chip *tpm, u8 tpm_register,
> +			u8 *tpm_data, u16 tpm_size)

  Please take another drink of coffee and think about this one again. I
don't always suggest trivial updates to a driver, but when I do, I try
to make sure they matter. :-)

> +{
> +	u8 value = 0;
> +
> +	value = read8_reg(tpm, tpm_register, tpm_data, tpm_size);
> +
> +	switch (value) {
> +	case TPM_ST_SPI_OK:
> +		return TPM_ST_SPI_OK;
> +	case 0:
> +		return 0;
> +	default:
> +		return -EBUSY;
> +	}
> +} /* spi_read_data() */
> +
> +/*
> + * clear_interruption
> + * clear the TPM interrupt register.
> + * @param: tpm, the chip description
> + */
> +static void clear_interruption(struct tpm_chip *tpm)
> +{
> +	u8 interrupt;
> +	SPI_READ_DATA(tpm, TPM_INT_STATUS, &interrupt, 1);
> +	/* Clear interrupts handled with TPM_EOI */
> +	SPI_WRITE_DATA(tpm, TPM_INT_STATUS, &interrupt, 1);
> +	SPI_READ_DATA(tpm, TPM_INT_STATUS, &interrupt, 1);
> +} /* clear_interruption() */
> +
> +/*
> + * _wait_for_interrupt_serirq_timeout
> + * @param: tpm, the chip description
> + * @param: timeout, the timeout of the interrupt
> + * @return: the status of the interruption.
> + */
> +static int _wait_for_interrupt_serirq_timeout(struct tpm_chip *chip,
> +					unsigned long timeout)
> +{
> +	unsigned long status;
> +	struct spi_device *client;
> +	struct st33zp24_platform_data *pin_infos;
> +
> +	client = (struct spi_device *)chip->vendor.iobase;
> +	pin_infos = client->dev.platform_data;
> +
> +	status = wait_for_completion_interruptible_timeout(
> +				&pin_infos->irq_detection, timeout);
> +	if (status > 0)
> +		enable_irq(gpio_to_irq(pin_infos->io_serirq));
> +	gpio_direction_input(pin_infos->io_serirq);
> +
> +	return status;
> +} /* wait_for_interrupt_serirq_timeout() */
> +
> +unsigned long wait_for_serirq_timeout(struct tpm_chip *chip, bool condition,
> +				 unsigned long timeout)
> +{
> +	unsigned long status = 0;
> +
> +	status = _wait_for_interrupt_serirq_timeout(chip, timeout);
> +	if (!status) {
> +		status = -EBUSY;
> +		goto wait_end;
> +	}
> +	clear_interruption(chip);
> +	if (condition)
> +		status = 1;
> +
> +wait_end:
> +	return status;
> +}
> +
> +
> +/*
> + * tpm_stm_spi_cancel, cancel is not implemented.
> + * @param: chip, the tpm chip description as specified in
> + * driver/char/tpm/tpm.h.
> + */
> +static void tpm_stm_spi_cancel(struct tpm_chip *chip)
> +{
> +	u8 data = TPM_STS_COMMAND_READY;
> +
> +	/* this causes the current command to be aborted */
> +	SPI_WRITE_DATA(chip, TPM_STS, &data, 1);
> +} /* tpm_stm_spi_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_spi_status(struct tpm_chip *chip)
> +{
> +	u8 data;
> +
> +	SPI_READ_DATA(chip, TPM_STS, &data, 1);
> +	return data;
> +} /* tpm_stm_spi_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 = SPI_READ_DATA(chip, TPM_ACCESS, &data, 1);
> +	if (status && (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 stop;
> +	long rc;
> +	u8 data;
> +
> +	/* Check locality */
> +	if (check_locality(chip) == chip->vendor.locality)
> +		return chip->vendor.locality;
> +
> +	/* Request locality */
> +	data = TPM_ACCESS_REQUEST_USE;
> +	rc = SPI_WRITE_DATA(chip, TPM_ACCESS, &data, 1);
> +	if (rc < 0)
> +		goto end;
> +
> +	/* wait for burstcount */
> +	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 {
> +		stop = jiffies + chip->vendor.timeout_a;
> +		do {
> +			if (check_locality(chip) >= 0)
> +				return chip->vendor.locality;
> +			msleep(TPM_TIMEOUT);
> +		} while (time_before(jiffies, stop));
> +	}
> +end:
> +	return -EACCES;
> +} /* request_locality() */
> +
> +/*
> + * release_locality release the active locality
> + * @param: chip, the tpm chip description.
> + */
> +static void release_locality(struct tpm_chip *chip)
> +{
> +	u8 data;
> +
> +	data = TPM_ACCESS_ACTIVE_LOCALITY;
> +	SPI_WRITE_DATA(chip, TPM_ACCESS, &data, 1);
> +} /* release_locality()*/
> +
> +/*
> + * 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 stop;
> +	u32 burstcnt, status;
> +	u8 tpm_reg, temp;
> +
> +	/* wait for burstcount */
> +	/* which timeout value, spec has 2 answers (c & d) */
> +	stop = jiffies + chip->vendor.timeout_d;
> +	do {
> +		tpm_reg = TPM_STS + 1;
> +		status = SPI_READ_DATA(chip, tpm_reg, &temp, 1);
> +		if (status < 0)
> +			goto end;
> +
> +		tpm_reg = tpm_reg + 1;
> +		burstcnt = temp;
> +		status = SPI_READ_DATA(chip, tpm_reg, &temp, 1);
> +		if (status < 0)
> +			goto end;
> +
> +		burstcnt |= temp << 8;
> +		if (burstcnt)
> +			return burstcnt;
> +		msleep(TPM_TIMEOUT);
> +	} while (time_before(jiffies, stop));
> +
> +end:
> +	return -EBUSY;
> +} /* get_burstcount() */
> +
> +/*
> + * wait_for_stat wait for a TPM_STS value
> + * @param: chip, the tpm chip description
> + * @param: mask, the value mask to wait
> + * @param: timeout, the timeout
> + * @param: queue, the wait queue.
> + * @return: the tpm status, 0 if success, -ETIME if timeout is reached.
> + */
> +static int wait_for_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout,
> +			 wait_queue_head_t *queue)
> +{
> +	unsigned long stop;
> +	long rc;
> +	u8 status;
> +
> +	/* check current status */
> +	status = tpm_stm_spi_status(chip);
> +	if (!chip->vendor.irq && (status & mask) == mask)
> +		return 0;
> +
> +	if (chip->vendor.irq) {
> +		rc = wait_for_serirq_timeout(chip, ((tpm_stm_spi_status
> +						    (chip) & mask) ==
> +						     mask), timeout);
> +		if (rc > 0)
> +			return 0;
> +	} else {
> +		stop = jiffies + timeout;
> +		do {
> +			msleep(TPM_TIMEOUT);
> +			status = tpm_stm_spi_status(chip);
> +			if ((status & mask) == mask)
> +				return 0;
> +		} while (time_before(jiffies, stop));
> +	}
> +	return -ETIME;
> +} /* wait_for_stat() */
> +
> +/*
> + * 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 tpm status, 0 if success, -ETIME if timeout is reached.
> + */
> +static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count)
> +{
> +	u32 size = 0, burstcnt, status, len;
> +
> +	while (size < count &&
> +	       wait_for_stat(chip,
> +			     TPM_STS_DATA_AVAIL | TPM_STS_VALID,
> +			     chip->vendor.timeout_c,
> +			     &chip->vendor.read_queue)
> +						== 0) {
> +		burstcnt = get_burstcount(chip);
> +		len = min_t(int, burstcnt, count - size);
> +		status = SPI_READ_DATA(chip, TPM_DATA_FIFO, buf + size, len);
> +		if (status < 0)
> +			return status;
> +
> +
> +		size += len;
> +	}
> +	return size;
> +}
> +
> +/*
> + * tpm_ioserirq_handler the serirq irq handler
> + * @param: irq, the tpm chip description
> + * @param: dev_id, the description of the chip
> + * @return: the status of the handler.
> + */
> +static irqreturn_t tpm_ioserirq_handler(int irq, void *dev_id)
> +{
> +	struct tpm_chip *chip = dev_id;
> +	struct spi_device *client;
> +	struct st33zp24_platform_data *pin_infos;
> +
> +	disable_irq_nosync(irq);
> +
> +	client = (struct spi_device *)chip->vendor.iobase;
> +	pin_infos = client->dev.platform_data;
> +
> +	complete(&pin_infos->irq_detection);
> +	return IRQ_HANDLED;
> +} /* tpm_ioserirq_handler() */
> +
> +/*
> + * tpm_stm_spi_send send TPM commands through the SPI bus.
> + * @param: chip, the tpm chip description
> + * @param: buf, the tpm command buffer
> + * @param: len, the tpm command size
> + * @return: should be zero if success else the negative error code.
> + */
> +static int tpm_stm_spi_send(struct tpm_chip *chip, unsigned char *buf,
> +			    size_t len)
> +{
> +	u32 ret = 0, status, burstcnt = 0, i, size = 0;
> +	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_spi_status(chip);
> +	if ((status & TPM_STS_COMMAND_READY) == 0) {
> +		tpm_stm_spi_cancel(chip);
> +		if (wait_for_stat
> +		    (chip, TPM_STS_COMMAND_READY, chip->vendor.timeout_b,
> +		     &chip->vendor.int_queue) < 0) {
> +			ret = -ETIME;
> +			goto out_err;
> +		}
> +	}
> +
> +	for (i = 0; i < len - 1;) {
> +		burstcnt = get_burstcount(chip);
> +		size = min_t(int, len - i - 1, burstcnt);
> +		ret = SPI_WRITE_DATA(chip, TPM_DATA_FIFO, buf, size);
> +		if (ret < 0)
> +			goto out_err;
> +		i += size;
> +	}
> +
> +	status = tpm_stm_spi_status(chip);
> +	if ((status & TPM_STS_DATA_EXPECT) == 0) {
> +		ret = -EIO;
> +		goto out_err;
> +	}
> +
> +	/* write last byte */
> +	SPI_WRITE_DATA(chip, TPM_DATA_FIFO, buf + len - 1, 1);
> +
> +	status = tpm_stm_spi_status(chip);
> +	if ((status & TPM_STS_DATA_EXPECT) != 0) {
> +		ret = -EIO;
> +		goto out_err;
> +	}
> +
> +	/* go and do it */
> +	data = TPM_STS_GO;
> +	ret = SPI_WRITE_DATA(chip, TPM_STS, &data, 1);
> +	if (ret < 0)
> +		goto out_err;
> +
> +	return len;
> +out_err:
> +	tpm_stm_spi_cancel(chip);
> +	release_locality(chip);
> +	return ret;
> +}
> +
> +/*
> + * tpm_stm_spi_recv received TPM response through the SPI bus.
> + * @param: chip, the tpm chip description
> + * @param: buf, the tpm command buffer
> + * @param: len, the tpm command size
> +* @return: should be zero if success else the negative error code.
> + */
> +static int tpm_stm_spi_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);
> +
> +	/* read first 10 bytes, including tag, paramsize, and result */
> +	if (size < TPM_HEADER_SIZE) {
> +		dev_err(chip->dev, "Unable to read header\n");
> +		goto out;
> +	}
> +
> +	expected = be32_to_cpu(*(__be32 *)(buf + 2));
> +	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;
> +}
> +
> +
> +static const struct file_operations tpm_st33_spi_fops = {
> +	.owner = THIS_MODULE,
> +	.llseek = no_llseek,
> +	.read = tpm_read,
> +	.write = tpm_write,
> +	.open = tpm_open,
> +	.release = tpm_release,
> +};
> +
> +static DEVICE_ATTR(pubek, S_IRUGO, tpm_show_pubek, NULL);
> +static DEVICE_ATTR(pcrs, S_IRUGO, tpm_show_pcrs, NULL);
> +static DEVICE_ATTR(enabled, S_IRUGO, tpm_show_enabled, NULL);
> +static DEVICE_ATTR(active, S_IRUGO, tpm_show_active, NULL);
> +static DEVICE_ATTR(owned, S_IRUGO, tpm_show_owned, NULL);
> +static DEVICE_ATTR(temp_deactivated, S_IRUGO, tpm_show_temp_deactivated, NULL);
> +static DEVICE_ATTR(caps, S_IRUGO, tpm_show_caps_1_2, NULL);
> +static DEVICE_ATTR(cancel, S_IWUSR | S_IWGRP, NULL, tpm_store_cancel);
> +
> +static struct attribute *stm_tpm_attrs[] = {
> +	&dev_attr_pubek.attr,
> +	&dev_attr_pcrs.attr,
> +	&dev_attr_enabled.attr,
> +	&dev_attr_active.attr,
> +	&dev_attr_owned.attr,
> +	&dev_attr_temp_deactivated.attr,
> +	&dev_attr_caps.attr,
> +	&dev_attr_cancel.attr, NULL,
> +};
> +
> +static struct attribute_group stm_tpm_attr_grp = {
> +	.attrs = stm_tpm_attrs
> +};
> +
> +static struct tpm_vendor_specific st_spi_tpm = {
> +	.send = tpm_stm_spi_send,
> +	.recv = tpm_stm_spi_recv,
> +	.cancel = tpm_stm_spi_cancel,
> +	.status = tpm_stm_spi_status,
> +	.attr_group = &stm_tpm_attr_grp,
> +	.miscdev = {.fops = &tpm_st33_spi_fops,},
> +};
> +
> +static int evaluate_latency(struct tpm_chip *chip)
> +{
> +	int latency = 0;
> +	struct spi_device *dev = (struct spi_device *)chip->vendor.iobase;
> +	struct st33zp24_platform_data *platform_data = dev->dev.platform_data;
> +	int status = 0;
> +	u8 data;
> +
> +	for (; status == 0x00 && latency < MAX_SPI_LATENCY; latency++) {
> +		platform_data->latency = latency;
> +		status = SPI_READ_DATA(chip, TPM_INTF_CAPABILITY, &data, 1);
> +	}
> +	return latency - 1;
> +} /* evaluate_latency() */
> +
> +static int interrupts;
> +module_param(interrupts, int, 0444);
> +MODULE_PARM_DESC(interrupts, "Enable interrupts");
> +
> +static int power_mgt = 1;
> +module_param(power_mgt, int, 0444);
> +MODULE_PARM_DESC(power_mgt, "Power Management");
> +
> +/*
> + * tpm_st33_spi_probe initialize the TPM device
> + * @param: client, the spi_device drescription (TPM SPI description).
> + * @param: id, the spi_device_id struct.
> + * @return: 0 in case of success.
> + *	 -1 in other case.
> + */
> +static int
> +tpm_st33_spi_probe(struct spi_device *dev)
> +{
> +	u32 err = 0;
> +	u8 intmask;
> +	struct tpm_chip *chip;
> +	struct st33zp24_platform_data *platform_data;
> +
> +	/* Check SPI platform functionnalities */
> +	if (dev == NULL) {
> +		pr_info("dev is NULL. exiting.\n");
> +		err = -ENODEV;
> +		goto end;
> +	}
> +
> +	chip = tpm_register_hardware(&dev->dev, &st_spi_tpm);
> +	if (!chip) {
> +		err = -ENODEV;
> +		goto end;
> +	}
> +
> +	/* Allocation of SPI buffers MISO and MOSI		*/
> +	/* Size is as follow:					*/
> +	/* Request burstcount value  = 0x800 = 2048		*/
> +	/* +							*/
> +	/* Response burstcount value = 0x400 = 1024		*/
> +	/* +							*/
> +	/* At least:						*/
> +	/* 1 byte for direction/locality			*/
> +	/* 1 byte tpm tis register				*/
> +	/* 2 bytes spi data length (for request only)		*/
> +	/* 2 latency bytes					*/
> +	/* 1 status byte					*/
> +	/* = 2048 + 1024 + 7					*/
> +	/* We reserved 2048 + 1024 + 20 in case latency byte	*/
> +	/* change						*/
> +	platform_data = dev->dev.platform_data;
> +	platform_data->tpm_spi_buffer[0] =
> +	    kmalloc((TPM_BUFSIZE + (TPM_BUFSIZE / 2) +
> +				 TPM_DIGEST_SIZE) * sizeof(u8), GFP_KERNEL);
> +	if (platform_data->tpm_spi_buffer[0] == NULL) {
> +		err = -ENOMEM;
> +		goto _tpm_clean_answer;
> +	}
> +	platform_data->tpm_spi_buffer[1] =
> +	    kmalloc((TPM_BUFSIZE + (TPM_BUFSIZE / 2) +
> +				 TPM_DIGEST_SIZE) * sizeof(u8) , GFP_KERNEL);
> +	if (platform_data->tpm_spi_buffer[1] == NULL) {
> +		err = -ENOMEM;
> +		goto _tpm_clean_response;
> +	}
> +
> +	chip->vendor.iobase = dev;
> +
> +	/* Default timeouts */
> +	chip->vendor.timeout_a = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
> +	chip->vendor.timeout_b = msecs_to_jiffies(TIS_LONG_TIMEOUT);
> +	chip->vendor.timeout_c = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
> +	chip->vendor.timeout_d = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
> +	chip->vendor.locality = LOCALITY0;
> +
> +	if (power_mgt) {
> +		err = gpio_request(platform_data->io_lpcpd, "TPM IO_LPCPD");
> +		if (err)
> +			goto _gpio_init1;
> +		gpio_set_value(platform_data->io_lpcpd, 1);
> +	}
> +
> +	platform_data->latency = evaluate_latency(chip);
> +	if (platform_data->latency <= 0x00) {
> +		err = -ENODEV;
> +		goto _gpio_init1;
> +	}
> +
> +	/* Enable interrupt */
> +	/* Register GPIO pin through generic Linux GPIO API */
> +	if (interrupts) {
> +		init_completion(&platform_data->irq_detection);
> +		if (request_locality(chip) != LOCALITY0) {
> +			err = -ENODEV;
> +			goto _tpm_clean_response;
> +		}
> +		err = gpio_request(platform_data->io_serirq, "TPM IO_SERIRQ");
> +		if (err)
> +			goto _gpio_init2;
> +
> +		/* Clear all existing */
> +		clear_interruption(chip);
> +		err = request_irq(gpio_to_irq(platform_data->io_serirq),
> +							&tpm_ioserirq_handler,
> +				IRQF_TRIGGER_HIGH | IRQF_SHARED,
> +				"TPM SERIRQ management", chip);
> +		if (err < 0) {
> +			pr_info("TPM SERIRQ signals %d not available\n",
> +			gpio_to_irq(platform_data->io_serirq));
> +			goto _irq_set;
> +		}
> +		err = SPI_READ_DATA(chip, TPM_INT_ENABLE, &intmask, 1);
> +		if (err < 0)
> +			goto _irq_set;
> +
> +		intmask |= TPM_INTF_CMD_READY_INT
> +			|  TPM_INTF_FIFO_AVALAIBLE_INT
> +			|  TPM_INTF_WAKE_UP_READY_INT
> +			|  TPM_INTF_LOC4SOFTRELEASE_INT
> +			|  TPM_INTF_LOCALITY_CHANGE_INT
> +			|  TPM_INTF_STS_VALID_INT
> +			|  TPM_INTF_DATA_AVAIL_INT;
> +
> +		err = SPI_WRITE_DATA(chip, TPM_INT_ENABLE, &intmask, 1);
> +		if (err < 0)
> +			goto _irq_set;
> +
> +		intmask = TPM_GLOBAL_INT_ENABLE;
> +		err = SPI_WRITE_DATA(chip, TPM_INT_ENABLE + 3, &intmask, 1);
> +		if (err < 0)
> +			goto _irq_set;
> +
> +		err = SPI_READ_DATA(chip, TPM_INT_STATUS, &intmask, 1);
> +		if (err < 0)
> +			goto _irq_set;
> +
> +		chip->vendor.irq = interrupts;
> +
> +		tpm_gen_interrupt(chip);
> +	}
> +
> +	tpm_get_timeouts(chip);
> +
> +	/* attach chip datas to client */
> +	spi_set_drvdata(dev, chip);
> +	platform_data->bchipf = false;

  This is still here too...

Kent

> +
> +	pr_info("TPM SPI Initialized\n");
> +	return 0;
> +_irq_set:
> +	free_irq(gpio_to_irq(platform_data->io_serirq), chip);
> +_gpio_init2:
> +	if (platform_data && interrupts)
> +		gpio_free(platform_data->io_serirq);
> +_gpio_init1:
> +	if (platform_data && power_mgt)
> +		gpio_free(platform_data->io_lpcpd);
> +_tpm_clean_response:
> +	tpm_remove_hardware(chip->dev);
> +	if (platform_data->tpm_spi_buffer[1] != NULL) {
> +		kfree(platform_data->tpm_spi_buffer[1]);
> +		platform_data->tpm_spi_buffer[1] = NULL;
> +	}
> +_tpm_clean_answer:
> +	if (platform_data->tpm_spi_buffer[0] != NULL) {
> +		kfree(platform_data->tpm_spi_buffer[0]);
> +		platform_data->tpm_spi_buffer[0] = NULL;
> +	}
> +
> +	platform_data->bchipf = true;
> +end:
> +	pr_info("TPM SPI initialisation fail\n");
> +	return err;
> +}
> +
> +/*
> + * tpm_st33_spi_remove remove the TPM device
> + * @param: client, the spi_device drescription (TPM SPI description).
> +		clear_bit(0, &chip->is_open);
> + * @return: 0 in case of success.
> + */
> +static int tpm_st33_spi_remove(struct spi_device *client)
> +{
> +	struct tpm_chip *chip = (struct tpm_chip *)spi_get_drvdata(client);
> +	struct st33zp24_platform_data *pin_infos =
> +		((struct spi_device *)chip->vendor.iobase)->dev.platform_data;
> +
> +	if (pin_infos != NULL) {
> +		gpio_free(pin_infos->io_lpcpd);
> +
> +		/* Check if chip has been previously clean */
> +		if (pin_infos->bchipf != true)
> +			tpm_remove_hardware(chip->dev);
> +		if (pin_infos->tpm_spi_buffer[1] != NULL) {
> +			kfree(pin_infos->tpm_spi_buffer[1]);
> +			pin_infos->tpm_spi_buffer[1] = NULL;
> +		}
> +		if (pin_infos->tpm_spi_buffer[0] != NULL) {
> +			kfree(pin_infos->tpm_spi_buffer[0]);
> +			pin_infos->tpm_spi_buffer[0] = NULL;
> +		}
> +	}
> +
> +	return 0;
> +}
> +
> +/*
> + * tpm_st33_spi_pm_suspend suspend the TPM device
> + * Added: Work around when suspend and no tpm application is running, suspend
> + * may fail because chip->data_buffer is not set (only set in tpm_open in Linux
> + * TPM core)
> + * @param: client, the spi_device drescription (TPM SPI description).
> + * @param: mesg, the power management message.
> + * @return: 0 in case of success.
> + */
> +static int tpm_st33_spi_pm_suspend(struct spi_device *client,
> +							 pm_message_t mesg)
> +{
> +	struct tpm_chip *chip =
> +	    (struct tpm_chip *)spi_get_drvdata(client);
> +	struct st33zp24_platform_data *pin_infos =
> +		((struct spi_device *)chip->vendor.iobase)->dev.platform_data;
> +	int ret = 0;
> +
> +	if (power_mgt)
> +		gpio_set_value(pin_infos->io_lpcpd, 0);
> +	else {
> +		if (chip->data_buffer == NULL)
> +			chip->data_buffer = pin_infos->tpm_spi_buffer[0];
> +		ret = tpm_pm_suspend(&client->dev);
> +		}
> +	return ret;
> +}				/* tpm_st33_spi_suspend() */
> +
> +/*
> + * tpm_st33_spi_pm_resume resume the TPM device
> + * @param: spi, the spi_device drescription (TPM SPI description).
> + * @return: 0 in case of success.
> + */
> +static int tpm_st33_spi_pm_resume(struct spi_device *spi)
> +{
> +	struct tpm_chip *chip =
> +	    (struct tpm_chip *)spi_get_drvdata(spi);
> +	struct st33zp24_platform_data *pin_infos =
> +		((struct spi_device *)chip->vendor.iobase)->dev.platform_data;
> +
> +	int ret = 0;
> +	if (power_mgt) {
> +		gpio_set_value(pin_infos->io_lpcpd, 1);
> +		ret = wait_for_serirq_timeout(chip,
> +					 (chip->vendor.status(chip) &&
> +					  TPM_STS_VALID) == TPM_STS_VALID,
> +					  chip->vendor.timeout_b);
> +	} else {
> +		if (chip->data_buffer == NULL)
> +			chip->data_buffer = pin_infos->tpm_spi_buffer[0];
> +		ret = tpm_pm_resume(&spi->dev);
> +		if (!ret)
> +			tpm_do_selftest(chip);
> +	}
> +	return ret;
> +}				/* tpm_st33_spi_pm_resume() */
> +
> +static struct spi_driver tpm_st33_spi_driver = {
> +	.driver = {
> +		   .owner = THIS_MODULE,
> +		   .name = TPM_ST33_SPI,
> +		   },
> +	.probe = tpm_st33_spi_probe,
> +	.remove = tpm_st33_spi_remove,
> +	.resume = tpm_st33_spi_pm_resume,
> +	.suspend = tpm_st33_spi_pm_suspend,
> +};
> +
> +/*
> + * tpm_st33_spi_init initialize driver
> + * @return: 0 if successful, else non zero value.
> + */
> +static int __init tpm_st33_spi_init(void)
> +{
> +	return spi_register_driver(&tpm_st33_spi_driver);
> +}
> +
> +/*
> + * tpm_st33_spi_exit The kernel calls this function during unloading the
> + * module or during shut down process
> + */
> +static void __exit tpm_st33_spi_exit(void)
> +{
> +	spi_unregister_driver(&tpm_st33_spi_driver);
> +}
> +
> +module_init(tpm_st33_spi_init);
> +module_exit(tpm_st33_spi_exit);
> +
> +MODULE_AUTHOR("Christophe Ricard (tpmsupport@st.com)");
> +MODULE_DESCRIPTION("STM TPM SPI ST33 Driver");
> +MODULE_VERSION("1.2.0");
> +MODULE_LICENSE("GPL");
> diff --git a/drivers/char/tpm/tpm_spi_stm_st33.h b/drivers/char/tpm/tpm_spi_stm_st33.h
> new file mode 100644
> index 0000000..752158e
> --- /dev/null
> +++ b/drivers/char/tpm/tpm_spi_stm_st33.h
> @@ -0,0 +1,86 @@
> +/*
> + * STMicroelectronics TPM SPI Linux driver for TPM ST33NP18
> + * Copyright (C) 2009, 2010  STMicroelectronics
> + *
> + * 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.,
> + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
> + *
> + * STMicroelectronics version 1.2.0, Copyright (C) 2010
> + * STMicroelectronics comes with ABSOLUTELY NO WARRANTY.
> + * This is free software, and you are welcome to redistribute it
> + * under certain conditions.
> + *
> + * @Author: Christophe RICARD tpmsupport@st.com
> + *
> + * @File: tpm_spi_stm_st33.h
> + *
> + * @Date: 09/15/2010
> + */
> +#ifndef __STM_ST33_TPM_SPI_MAIN_H__
> +#define __STM_ST33_TPM_SPI_MAIN_H__
> +
> +#include <linux/pci.h>
> +#include <linux/module.h>
> +#include <linux/platform_device.h>
> +#include <linux/fs.h>
> +#include <linux/miscdevice.h>
> +
> +#define MINOR_NUM_SPI		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			0x00
> +#define TPM_WRITE_DIRECTION		0x80
> +#define TPM_HEADER_SIZE			10
> +#define TPM_BUFSIZE			2048
> +
> +/* ioctl commands */
> +#define TPMIOC_CANCEL		_IO('T', 0x00)
> +#define TPMIOC_TRANSMIT		_IO('T', 0x01)
> +#define TPMIOC_HASH		_IO('T', 0x02)
> +#define TPMIOC_CHANGELOCALITY	_IO('T', 0x03)
> +
> +#define LOCALITY0		0
> +#define LOCALITY4		4
> +
> +#define TPM_ST_SPI_OK		0x5A
> +
> +
> +#define MAX_SPI_LATENCY		15
> +
> +#define TPM_ST33_SPI		"st33zp24_spi"
> +
> +struct st33zp24_platform_data {
> +	int io_serirq;
> +	int io_lpcpd;
> +	int latency;
> +	bool bchipf;
> +	u8 *tpm_spi_buffer[2]; /* 0 Request 1 Response */
> +	struct completion irq_detection;
> +};
> +
> +struct st_tpm_hash_t {
> +	int size;
> +	u8 *data;
> +};
> +
> +#endif /* __STM_ST33_TPM_SPI_MAIN_H__ */
> -- 
> 1.7.1
> 
> --
> To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
> Please read the FAQ at  http://www.tux.org/lkml/

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

* [PATCH 1/1] TPM: STMicroelectronics st33 driver SPI
@ 2013-04-22 16:20 Matthias Leblanc
  2013-04-22 18:26 ` Kent Yoder
  0 siblings, 1 reply; 21+ messages in thread
From: Matthias Leblanc @ 2013-04-22 16:20 UTC (permalink / raw)
  To: Kent Yoder, Rajiv Andrade, Marcel Selhorst, Sirrix AG, tpmdd-devel
  Cc: linux-kernel, Jean-Luc Blanc, Mathias Leblanc

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

 * STMicroelectronics version 1.2.0, Copyright (C) 2010
 * 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 SPI, in menuconfig or .config choose the tpm driver on
device --> tpm and activate the protocol of your choice before compiling
the kernel.
The driver will be accessible from within Linux.

Tested on linux x86/x64 and beagleboard REV B & XM REV C

Signed-off-by: Mathias Leblanc <mathias.leblanc@st.com>
---
 drivers/char/tpm/Kconfig            |   10 +
 drivers/char/tpm/Makefile           |    1 +
 drivers/char/tpm/tpm_spi_stm_st33.c | 1027 +++++++++++++++++++++++++++++++++++
 drivers/char/tpm/tpm_spi_stm_st33.h |   86 +++
 4 files changed, 1124 insertions(+), 0 deletions(-)
 create mode 100644 drivers/char/tpm/tpm_spi_stm_st33.c
 create mode 100644 drivers/char/tpm/tpm_spi_stm_st33.h

diff --git a/drivers/char/tpm/Kconfig b/drivers/char/tpm/Kconfig
index dbfd564..2fc1254 100644
--- a/drivers/char/tpm/Kconfig
+++ b/drivers/char/tpm/Kconfig
@@ -91,4 +91,14 @@ config TCG_ST33_I2C
         To compile this driver as a module, choose M here; the module will be
         called tpm_stm_st33_i2c.
 
+config TCG_ST33_SPI
+       tristate "STMicroelectronics ST33 SPI"
+       depends on SPI
+       depends on GPIOLIB
+       ---help---
+       If you have a TPM security chip from STMicroelectronics working with
+       an SPI bus say Yes and it will be accessible from within Linux.
+       To compile this driver as a module, choose M here; the module will be
+       called tpm_stm_st33_spi.
+
 endif # TCG_TPM
diff --git a/drivers/char/tpm/Makefile b/drivers/char/tpm/Makefile
index a3736c9..a1db3dd 100644
--- a/drivers/char/tpm/Makefile
+++ b/drivers/char/tpm/Makefile
@@ -18,3 +18,4 @@ obj-$(CONFIG_TCG_ATMEL) += tpm_atmel.o
 obj-$(CONFIG_TCG_INFINEON) += tpm_infineon.o
 obj-$(CONFIG_TCG_IBMVTPM) += tpm_ibmvtpm.o
 obj-$(CONFIG_TCG_ST33_I2C) += tpm_i2c_stm_st33.o
+obj-$(CONFIG_TCG_ST33_SPI) += tpm_spi_stm_st33.o
diff --git a/drivers/char/tpm/tpm_spi_stm_st33.c b/drivers/char/tpm/tpm_spi_stm_st33.c
new file mode 100644
index 0000000..70828dc
--- /dev/null
+++ b/drivers/char/tpm/tpm_spi_stm_st33.c
@@ -0,0 +1,1028 @@
+/*
+ * STMicroelectronics TPM SPI Linux driver for TPM ST33ZP24
+ * Copyright (C) 2009, 2010  STMicroelectronics
+ *
+ * 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.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * STMicroelectronics version 1.2.0, Copyright (C) 2010
+ * STMicroelectronics comes with ABSOLUTELY NO WARRANTY.
+ * This is free software, and you are welcome to redistribute it
+ * under certain conditions.
+ *
+ * @Author: Christophe RICARD tpmsupport@st.com
+ *
+ * @File: tpm_stm_st33_spi.c
+ *
+ * @Synopsis:
+ *	09/15/2010:	First shot driver tpm_tis driver for lpc is
+ *			used as model.
+ */
+
+#define pr_fmt(fmt)	KBUILD_MODNAME ": " fmt
+
+#include <linux/module.h>
+#include <linux/kernel.h>
+#include <linux/delay.h>
+#include <linux/init.h>
+#include <linux/spi/spi.h>
+#include <linux/wait.h>
+#include <linux/string.h>
+#include <linux/interrupt.h>
+#include <linux/spinlock.h>
+#include <linux/sysfs.h>
+#include <linux/gpio.h>
+#include <linux/sched.h>
+#include <linux/uaccess.h>
+#include <linux/io.h>
+#include <linux/slab.h>
+#include <linux/sched.h>
+
+#include "tpm.h"
+
+#include "tpm_spi_stm_st33.h"
+
+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 */
+};
+
+/*
+ * write8_reg
+ * Send byte to the TIS register according to the ST33ZP24 SPI protocol.
+ * @param: tpm, 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: should be zero if success else a negative error code.
+ */
+static int write8_reg(struct tpm_chip *tpm, u8 tpm_register,
+		      u8 *tpm_data, u16 tpm_size)
+{
+	u8 data;
+	int total_length = 0, nbr_dummy_bytes;
+	int value = 0;
+	struct spi_device *dev = (struct spi_device *)tpm->vendor.iobase;
+	struct st33zp24_platform_data *platform_data = dev->dev.platform_data;
+	u8 *data_buffer = platform_data->tpm_spi_buffer[0];
+	struct spi_transfer xfer = {
+		.tx_buf	 = data_buffer,
+		.rx_buf	 = platform_data->tpm_spi_buffer[1],
+	};
+	struct spi_message msg;
+	u8 latency = platform_data->latency + tpm_size + 1;
+
+	/* Pre-Header */
+	data = TPM_WRITE_DIRECTION | tpm->vendor.locality;
+	memcpy(data_buffer + total_length, &data, sizeof(data));
+	total_length++;
+	data = tpm_register;
+	memcpy(data_buffer + total_length, &data, sizeof(data));
+	total_length++;
+
+	if (tpm_size > 0 &&
+	     (tpm_register == TPM_DATA_FIFO ||
+	      tpm_register == TPM_HASH_DATA)) {
+		data = (tpm_size >> 8) & 0x00ff;
+		data_buffer[total_length++] = data;
+		data = tpm_size & 0x00ff;
+		data_buffer[total_length++] = data;
+		latency -= 2;
+	}
+
+	memcpy(&data_buffer[total_length], tpm_data, tpm_size);
+	total_length += tpm_size;
+
+	nbr_dummy_bytes = platform_data->latency + 1;
+	memset(&data_buffer[total_length], TPM_DUMMY_BYTE,
+					 platform_data->latency + 1);
+
+	xfer.len = total_length;
+	spi_message_init(&msg);
+	spi_message_add_tail(&xfer, &msg);
+	value = spi_sync(dev, &msg);
+
+	if (value == 0) {
+		nbr_dummy_bytes = total_length - 1 - nbr_dummy_bytes;
+		for (; nbr_dummy_bytes < total_length &&
+			 ((u8 *)xfer.rx_buf)[nbr_dummy_bytes] == 0;
+				 nbr_dummy_bytes++)
+			;
+
+		if (((u8 *)xfer.rx_buf)[nbr_dummy_bytes] != 0)
+			value = ((u8 *)xfer.rx_buf)[nbr_dummy_bytes];
+	}
+
+	return value;
+} /* write8_reg() */
+
+/*
+ * read8_reg
+ * Recv byte from the TIS register according to the ST33ZP24 SPI 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: should be zero if success else a negative error code.
+ */
+static u8 read8_reg(struct tpm_chip *tpm, u8 tpm_register,
+		    u8 *tpm_data, u16 tpm_size)
+{
+	u8 data;
+	int total_length = 0, nbr_dummy_bytes;
+	int value = 0;
+	struct spi_device *dev = (struct spi_device *)tpm->vendor.iobase;
+	struct st33zp24_platform_data *platform_data = dev->dev.platform_data;
+	u8 *data_buffer = platform_data->tpm_spi_buffer[0];
+	struct spi_transfer xfer = {
+		.tx_buf	 = data_buffer,
+		.rx_buf	 = platform_data->tpm_spi_buffer[1],
+	};
+	struct spi_message msg;
+
+	u8 latency = platform_data->latency + tpm_size + 1;
+
+	/* Pre-Header */
+
+	data = tpm->vendor.locality;
+	memcpy(data_buffer + total_length, &data, sizeof(data));
+	total_length++;
+	data = tpm_register;
+	memcpy(data_buffer + total_length, &data, sizeof(data));
+	total_length++;
+
+	nbr_dummy_bytes = platform_data->latency + 1;
+	memset(&data_buffer[total_length], TPM_DUMMY_BYTE,
+					 platform_data->latency + 1);
+
+	xfer.len = total_length;
+
+	/* header + status byte + size of the data + status byte */
+	spi_message_init(&msg);
+	spi_message_add_tail(&xfer, &msg);
+	value = spi_sync(dev, &msg);
+
+	if (tpm_size > 0 && value == 0) {
+		nbr_dummy_bytes = 2;
+		for (; nbr_dummy_bytes < total_length &&
+			 ((u8 *)xfer.rx_buf)[nbr_dummy_bytes] == 0;
+			 nbr_dummy_bytes++)
+			;
+
+		if (nbr_dummy_bytes + 1 < total_length) {
+			value = ((u8 *)xfer.rx_buf)[nbr_dummy_bytes];
+
+			if (tpm_size > 0)
+				memcpy(tpm_data, xfer.rx_buf +
+							 nbr_dummy_bytes + 1,
+								 tpm_size);
+		}
+	}
+	return value;
+} /* read8_reg() */
+
+/*
+ * spi_write_data()
+ * Send byte to the TIS register according to the ST33ZP24 SPI protocol.
+ * @param: tpm, 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: should be zero if success else the negative error code.
+ */
+static u32 SPI_WRITE_DATA(struct tpm_chip *tpm, u8 tpm_register,
+			  u8 *tpm_data, u16 tpm_size)
+{
+	u8 value = 0;
+	value = write8_reg(tpm, tpm_register, tpm_data, tpm_size);
+
+	switch (value) {
+	case TPM_ST_SPI_OK:
+		return TPM_ST_SPI_OK;
+	case 0:
+		return 0;
+	default:
+		return -EBUSY;
+	}
+} /* spi_write_data() */
+
+/*
+ * spi_read_data()
+ * Recv byte from the TIS register according to the ST33ZP24 SPI 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: should be zero if success else the negative error code.
+ */
+static u32 SPI_READ_DATA(struct tpm_chip *tpm, u8 tpm_register,
+			u8 *tpm_data, u16 tpm_size)
+{
+	u8 value = 0;
+
+	value = read8_reg(tpm, tpm_register, tpm_data, tpm_size);
+
+	switch (value) {
+	case TPM_ST_SPI_OK:
+		return TPM_ST_SPI_OK;
+	case 0:
+		return 0;
+	default:
+		return -EBUSY;
+	}
+} /* spi_read_data() */
+
+/*
+ * clear_interruption
+ * clear the TPM interrupt register.
+ * @param: tpm, the chip description
+ */
+static void clear_interruption(struct tpm_chip *tpm)
+{
+	u8 interrupt;
+	SPI_READ_DATA(tpm, TPM_INT_STATUS, &interrupt, 1);
+	/* Clear interrupts handled with TPM_EOI */
+	SPI_WRITE_DATA(tpm, TPM_INT_STATUS, &interrupt, 1);
+	SPI_READ_DATA(tpm, TPM_INT_STATUS, &interrupt, 1);
+} /* clear_interruption() */
+
+/*
+ * _wait_for_interrupt_serirq_timeout
+ * @param: tpm, the chip description
+ * @param: timeout, the timeout of the interrupt
+ * @return: the status of the interruption.
+ */
+static int _wait_for_interrupt_serirq_timeout(struct tpm_chip *chip,
+					unsigned long timeout)
+{
+	unsigned long status;
+	struct spi_device *client;
+	struct st33zp24_platform_data *pin_infos;
+
+	client = (struct spi_device *)chip->vendor.iobase;
+	pin_infos = client->dev.platform_data;
+
+	status = wait_for_completion_interruptible_timeout(
+				&pin_infos->irq_detection, timeout);
+	if (status > 0)
+		enable_irq(gpio_to_irq(pin_infos->io_serirq));
+	gpio_direction_input(pin_infos->io_serirq);
+
+	return status;
+} /* wait_for_interrupt_serirq_timeout() */
+
+unsigned long wait_for_serirq_timeout(struct tpm_chip *chip, bool condition,
+				 unsigned long timeout)
+{
+	unsigned long status = 0;
+
+	status = _wait_for_interrupt_serirq_timeout(chip, timeout);
+	if (!status) {
+		status = -EBUSY;
+		goto wait_end;
+	}
+	clear_interruption(chip);
+	if (condition)
+		status = 1;
+
+wait_end:
+	return status;
+}
+
+
+/*
+ * tpm_stm_spi_cancel, cancel is not implemented.
+ * @param: chip, the tpm chip description as specified in
+ * driver/char/tpm/tpm.h.
+ */
+static void tpm_stm_spi_cancel(struct tpm_chip *chip)
+{
+	u8 data = TPM_STS_COMMAND_READY;
+
+	/* this causes the current command to be aborted */
+	SPI_WRITE_DATA(chip, TPM_STS, &data, 1);
+} /* tpm_stm_spi_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_spi_status(struct tpm_chip *chip)
+{
+	u8 data;
+
+	SPI_READ_DATA(chip, TPM_STS, &data, 1);
+	return data;
+} /* tpm_stm_spi_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 = SPI_READ_DATA(chip, TPM_ACCESS, &data, 1);
+	if (status && (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 stop;
+	long rc;
+	u8 data;
+
+	/* Check locality */
+	if (check_locality(chip) == chip->vendor.locality)
+		return chip->vendor.locality;
+
+	/* Request locality */
+	data = TPM_ACCESS_REQUEST_USE;
+	rc = SPI_WRITE_DATA(chip, TPM_ACCESS, &data, 1);
+	if (rc < 0)
+		goto end;
+
+	/* wait for burstcount */
+	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 {
+		stop = jiffies + chip->vendor.timeout_a;
+		do {
+			if (check_locality(chip) >= 0)
+				return chip->vendor.locality;
+			msleep(TPM_TIMEOUT);
+		} while (time_before(jiffies, stop));
+	}
+end:
+	return -EACCES;
+} /* request_locality() */
+
+/*
+ * release_locality release the active locality
+ * @param: chip, the tpm chip description.
+ */
+static void release_locality(struct tpm_chip *chip)
+{
+	u8 data;
+
+	data = TPM_ACCESS_ACTIVE_LOCALITY;
+	SPI_WRITE_DATA(chip, TPM_ACCESS, &data, 1);
+} /* release_locality()*/
+
+/*
+ * 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 stop;
+	u32 burstcnt, status;
+	u8 tpm_reg, temp;
+
+	/* wait for burstcount */
+	/* which timeout value, spec has 2 answers (c & d) */
+	stop = jiffies + chip->vendor.timeout_d;
+	do {
+		tpm_reg = TPM_STS + 1;
+		status = SPI_READ_DATA(chip, tpm_reg, &temp, 1);
+		if (status < 0)
+			goto end;
+
+		tpm_reg = tpm_reg + 1;
+		burstcnt = temp;
+		status = SPI_READ_DATA(chip, tpm_reg, &temp, 1);
+		if (status < 0)
+			goto end;
+
+		burstcnt |= temp << 8;
+		if (burstcnt)
+			return burstcnt;
+		msleep(TPM_TIMEOUT);
+	} while (time_before(jiffies, stop));
+
+end:
+	return -EBUSY;
+} /* get_burstcount() */
+
+/*
+ * wait_for_stat wait for a TPM_STS value
+ * @param: chip, the tpm chip description
+ * @param: mask, the value mask to wait
+ * @param: timeout, the timeout
+ * @param: queue, the wait queue.
+ * @return: the tpm status, 0 if success, -ETIME if timeout is reached.
+ */
+static int wait_for_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout,
+			 wait_queue_head_t *queue)
+{
+	unsigned long stop;
+	long rc;
+	u8 status;
+
+	/* check current status */
+	status = tpm_stm_spi_status(chip);
+	if (!chip->vendor.irq && (status & mask) == mask)
+		return 0;
+
+	if (chip->vendor.irq) {
+		rc = wait_for_serirq_timeout(chip, ((tpm_stm_spi_status
+						    (chip) & mask) ==
+						     mask), timeout);
+		if (rc > 0)
+			return 0;
+	} else {
+		stop = jiffies + timeout;
+		do {
+			msleep(TPM_TIMEOUT);
+			status = tpm_stm_spi_status(chip);
+			if ((status & mask) == mask)
+				return 0;
+		} while (time_before(jiffies, stop));
+	}
+	return -ETIME;
+} /* wait_for_stat() */
+
+/*
+ * 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 tpm status, 0 if success, -ETIME if timeout is reached.
+ */
+static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count)
+{
+	u32 size = 0, burstcnt, status, len;
+
+	while (size < count &&
+	       wait_for_stat(chip,
+			     TPM_STS_DATA_AVAIL | TPM_STS_VALID,
+			     chip->vendor.timeout_c,
+			     &chip->vendor.read_queue)
+						== 0) {
+		burstcnt = get_burstcount(chip);
+		len = min_t(int, burstcnt, count - size);
+		status = SPI_READ_DATA(chip, TPM_DATA_FIFO, buf + size, len);
+		if (status < 0)
+			return status;
+
+
+		size += len;
+	}
+	return size;
+}
+
+/*
+ * tpm_ioserirq_handler the serirq irq handler
+ * @param: irq, the tpm chip description
+ * @param: dev_id, the description of the chip
+ * @return: the status of the handler.
+ */
+static irqreturn_t tpm_ioserirq_handler(int irq, void *dev_id)
+{
+	struct tpm_chip *chip = dev_id;
+	struct spi_device *client;
+	struct st33zp24_platform_data *pin_infos;
+
+	disable_irq_nosync(irq);
+
+	client = (struct spi_device *)chip->vendor.iobase;
+	pin_infos = client->dev.platform_data;
+
+	complete(&pin_infos->irq_detection);
+	return IRQ_HANDLED;
+} /* tpm_ioserirq_handler() */
+
+/*
+ * tpm_stm_spi_send send TPM commands through the SPI bus.
+ * @param: chip, the tpm chip description
+ * @param: buf, the tpm command buffer
+ * @param: len, the tpm command size
+ * @return: should be zero if success else the negative error code.
+ */
+static int tpm_stm_spi_send(struct tpm_chip *chip, unsigned char *buf,
+			    size_t len)
+{
+	u32 ret = 0, status, burstcnt = 0, i, size = 0;
+	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_spi_status(chip);
+	if ((status & TPM_STS_COMMAND_READY) == 0) {
+		tpm_stm_spi_cancel(chip);
+		if (wait_for_stat
+		    (chip, TPM_STS_COMMAND_READY, chip->vendor.timeout_b,
+		     &chip->vendor.int_queue) < 0) {
+			ret = -ETIME;
+			goto out_err;
+		}
+	}
+
+	for (i = 0; i < len - 1;) {
+		burstcnt = get_burstcount(chip);
+		size = min_t(int, len - i - 1, burstcnt);
+		ret = SPI_WRITE_DATA(chip, TPM_DATA_FIFO, buf, size);
+		if (ret < 0)
+			goto out_err;
+		i += size;
+	}
+
+	status = tpm_stm_spi_status(chip);
+	if ((status & TPM_STS_DATA_EXPECT) == 0) {
+		ret = -EIO;
+		goto out_err;
+	}
+
+	/* write last byte */
+	SPI_WRITE_DATA(chip, TPM_DATA_FIFO, buf + len - 1, 1);
+
+	status = tpm_stm_spi_status(chip);
+	if ((status & TPM_STS_DATA_EXPECT) != 0) {
+		ret = -EIO;
+		goto out_err;
+	}
+
+	/* go and do it */
+	data = TPM_STS_GO;
+	ret = SPI_WRITE_DATA(chip, TPM_STS, &data, 1);
+	if (ret < 0)
+		goto out_err;
+
+	return len;
+out_err:
+	tpm_stm_spi_cancel(chip);
+	release_locality(chip);
+	return ret;
+}
+
+/*
+ * tpm_stm_spi_recv received TPM response through the SPI bus.
+ * @param: chip, the tpm chip description
+ * @param: buf, the tpm command buffer
+ * @param: len, the tpm command size
+* @return: should be zero if success else the negative error code.
+ */
+static int tpm_stm_spi_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);
+
+	/* read first 10 bytes, including tag, paramsize, and result */
+	if (size < TPM_HEADER_SIZE) {
+		dev_err(chip->dev, "Unable to read header\n");
+		goto out;
+	}
+
+	expected = be32_to_cpu(*(__be32 *)(buf + 2));
+	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;
+}
+
+
+static const struct file_operations tpm_st33_spi_fops = {
+	.owner = THIS_MODULE,
+	.llseek = no_llseek,
+	.read = tpm_read,
+	.write = tpm_write,
+	.open = tpm_open,
+	.release = tpm_release,
+};
+
+static DEVICE_ATTR(pubek, S_IRUGO, tpm_show_pubek, NULL);
+static DEVICE_ATTR(pcrs, S_IRUGO, tpm_show_pcrs, NULL);
+static DEVICE_ATTR(enabled, S_IRUGO, tpm_show_enabled, NULL);
+static DEVICE_ATTR(active, S_IRUGO, tpm_show_active, NULL);
+static DEVICE_ATTR(owned, S_IRUGO, tpm_show_owned, NULL);
+static DEVICE_ATTR(temp_deactivated, S_IRUGO, tpm_show_temp_deactivated, NULL);
+static DEVICE_ATTR(caps, S_IRUGO, tpm_show_caps_1_2, NULL);
+static DEVICE_ATTR(cancel, S_IWUSR | S_IWGRP, NULL, tpm_store_cancel);
+
+static struct attribute *stm_tpm_attrs[] = {
+	&dev_attr_pubek.attr,
+	&dev_attr_pcrs.attr,
+	&dev_attr_enabled.attr,
+	&dev_attr_active.attr,
+	&dev_attr_owned.attr,
+	&dev_attr_temp_deactivated.attr,
+	&dev_attr_caps.attr,
+	&dev_attr_cancel.attr, NULL,
+};
+
+static struct attribute_group stm_tpm_attr_grp = {
+	.attrs = stm_tpm_attrs
+};
+
+static struct tpm_vendor_specific st_spi_tpm = {
+	.send = tpm_stm_spi_send,
+	.recv = tpm_stm_spi_recv,
+	.cancel = tpm_stm_spi_cancel,
+	.status = tpm_stm_spi_status,
+	.attr_group = &stm_tpm_attr_grp,
+	.miscdev = {.fops = &tpm_st33_spi_fops,},
+};
+
+static int evaluate_latency(struct tpm_chip *chip)
+{
+	int latency = 0;
+	struct spi_device *dev = (struct spi_device *)chip->vendor.iobase;
+	struct st33zp24_platform_data *platform_data = dev->dev.platform_data;
+	int status = 0;
+	u8 data;
+
+	for (; status == 0x00 && latency < MAX_SPI_LATENCY; latency++) {
+		platform_data->latency = latency;
+		status = SPI_READ_DATA(chip, TPM_INTF_CAPABILITY, &data, 1);
+	}
+	return latency - 1;
+} /* evaluate_latency() */
+
+static int interrupts;
+module_param(interrupts, int, 0444);
+MODULE_PARM_DESC(interrupts, "Enable interrupts");
+
+static int power_mgt = 1;
+module_param(power_mgt, int, 0444);
+MODULE_PARM_DESC(power_mgt, "Power Management");
+
+/*
+ * tpm_st33_spi_probe initialize the TPM device
+ * @param: client, the spi_device drescription (TPM SPI description).
+ * @param: id, the spi_device_id struct.
+ * @return: 0 in case of success.
+ *	 -1 in other case.
+ */
+static int
+tpm_st33_spi_probe(struct spi_device *dev)
+{
+	u32 err = 0;
+	u8 intmask;
+	struct tpm_chip *chip;
+	struct st33zp24_platform_data *platform_data;
+
+	/* Check SPI platform functionnalities */
+	if (dev == NULL) {
+		pr_info("dev is NULL. exiting.\n");
+		err = -ENODEV;
+		goto end;
+	}
+
+	chip = tpm_register_hardware(&dev->dev, &st_spi_tpm);
+	if (!chip) {
+		err = -ENODEV;
+		goto end;
+	}
+
+	/* Allocation of SPI buffers MISO and MOSI		*/
+	/* Size is as follow:					*/
+	/* Request burstcount value  = 0x800 = 2048		*/
+	/* +							*/
+	/* Response burstcount value = 0x400 = 1024		*/
+	/* +							*/
+	/* At least:						*/
+	/* 1 byte for direction/locality			*/
+	/* 1 byte tpm tis register				*/
+	/* 2 bytes spi data length (for request only)		*/
+	/* 2 latency bytes					*/
+	/* 1 status byte					*/
+	/* = 2048 + 1024 + 7					*/
+	/* We reserved 2048 + 1024 + 20 in case latency byte	*/
+	/* change						*/
+	platform_data = dev->dev.platform_data;
+	platform_data->tpm_spi_buffer[0] =
+	    kmalloc((TPM_BUFSIZE + (TPM_BUFSIZE / 2) +
+				 TPM_DIGEST_SIZE) * sizeof(u8), GFP_KERNEL);
+	if (platform_data->tpm_spi_buffer[0] == NULL) {
+		err = -ENOMEM;
+		goto _tpm_clean_answer;
+	}
+	platform_data->tpm_spi_buffer[1] =
+	    kmalloc((TPM_BUFSIZE + (TPM_BUFSIZE / 2) +
+				 TPM_DIGEST_SIZE) * sizeof(u8) , GFP_KERNEL);
+	if (platform_data->tpm_spi_buffer[1] == NULL) {
+		err = -ENOMEM;
+		goto _tpm_clean_response;
+	}
+
+	chip->vendor.iobase = dev;
+
+	/* Default timeouts */
+	chip->vendor.timeout_a = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
+	chip->vendor.timeout_b = msecs_to_jiffies(TIS_LONG_TIMEOUT);
+	chip->vendor.timeout_c = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
+	chip->vendor.timeout_d = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
+	chip->vendor.locality = LOCALITY0;
+
+	if (power_mgt) {
+		err = gpio_request(platform_data->io_lpcpd, "TPM IO_LPCPD");
+		if (err)
+			goto _gpio_init1;
+		gpio_set_value(platform_data->io_lpcpd, 1);
+	}
+
+	platform_data->latency = evaluate_latency(chip);
+	if (platform_data->latency <= 0x00) {
+		err = -ENODEV;
+		goto _gpio_init1;
+	}
+
+	/* Enable interrupt */
+	/* Register GPIO pin through generic Linux GPIO API */
+	if (interrupts) {
+		init_completion(&platform_data->irq_detection);
+		if (request_locality(chip) != LOCALITY0) {
+			err = -ENODEV;
+			goto _tpm_clean_response;
+		}
+		err = gpio_request(platform_data->io_serirq, "TPM IO_SERIRQ");
+		if (err)
+			goto _gpio_init2;
+
+		/* Clear all existing */
+		clear_interruption(chip);
+		err = request_irq(gpio_to_irq(platform_data->io_serirq),
+							&tpm_ioserirq_handler,
+				IRQF_TRIGGER_HIGH | IRQF_SHARED,
+				"TPM SERIRQ management", chip);
+		if (err < 0) {
+			pr_info("TPM SERIRQ signals %d not available\n",
+			gpio_to_irq(platform_data->io_serirq));
+			goto _irq_set;
+		}
+		err = SPI_READ_DATA(chip, TPM_INT_ENABLE, &intmask, 1);
+		if (err < 0)
+			goto _irq_set;
+
+		intmask |= TPM_INTF_CMD_READY_INT
+			|  TPM_INTF_FIFO_AVALAIBLE_INT
+			|  TPM_INTF_WAKE_UP_READY_INT
+			|  TPM_INTF_LOC4SOFTRELEASE_INT
+			|  TPM_INTF_LOCALITY_CHANGE_INT
+			|  TPM_INTF_STS_VALID_INT
+			|  TPM_INTF_DATA_AVAIL_INT;
+
+		err = SPI_WRITE_DATA(chip, TPM_INT_ENABLE, &intmask, 1);
+		if (err < 0)
+			goto _irq_set;
+
+		intmask = TPM_GLOBAL_INT_ENABLE;
+		err = SPI_WRITE_DATA(chip, TPM_INT_ENABLE + 3, &intmask, 1);
+		if (err < 0)
+			goto _irq_set;
+
+		err = SPI_READ_DATA(chip, TPM_INT_STATUS, &intmask, 1);
+		if (err < 0)
+			goto _irq_set;
+
+		chip->vendor.irq = interrupts;
+
+		tpm_gen_interrupt(chip);
+	}
+
+	tpm_get_timeouts(chip);
+
+	/* attach chip datas to client */
+	spi_set_drvdata(dev, chip);
+	platform_data->bchipf = false;
+
+	pr_info("TPM SPI Initialized\n");
+	return 0;
+_irq_set:
+	free_irq(gpio_to_irq(platform_data->io_serirq), chip);
+_gpio_init2:
+	if (platform_data && interrupts)
+		gpio_free(platform_data->io_serirq);
+_gpio_init1:
+	if (platform_data && power_mgt)
+		gpio_free(platform_data->io_lpcpd);
+_tpm_clean_response:
+	tpm_remove_hardware(chip->dev);
+	if (platform_data->tpm_spi_buffer[1] != NULL) {
+		kfree(platform_data->tpm_spi_buffer[1]);
+		platform_data->tpm_spi_buffer[1] = NULL;
+	}
+_tpm_clean_answer:
+	if (platform_data->tpm_spi_buffer[0] != NULL) {
+		kfree(platform_data->tpm_spi_buffer[0]);
+		platform_data->tpm_spi_buffer[0] = NULL;
+	}
+
+	platform_data->bchipf = true;
+end:
+	pr_info("TPM SPI initialisation fail\n");
+	return err;
+}
+
+/*
+ * tpm_st33_spi_remove remove the TPM device
+ * @param: client, the spi_device drescription (TPM SPI description).
+		clear_bit(0, &chip->is_open);
+ * @return: 0 in case of success.
+ */
+static int tpm_st33_spi_remove(struct spi_device *client)
+{
+	struct tpm_chip *chip = (struct tpm_chip *)spi_get_drvdata(client);
+	struct st33zp24_platform_data *pin_infos =
+		((struct spi_device *)chip->vendor.iobase)->dev.platform_data;
+
+	if (pin_infos != NULL) {
+		gpio_free(pin_infos->io_lpcpd);
+
+		/* Check if chip has been previously clean */
+		if (pin_infos->bchipf != true)
+			tpm_remove_hardware(chip->dev);
+		if (pin_infos->tpm_spi_buffer[1] != NULL) {
+			kfree(pin_infos->tpm_spi_buffer[1]);
+			pin_infos->tpm_spi_buffer[1] = NULL;
+		}
+		if (pin_infos->tpm_spi_buffer[0] != NULL) {
+			kfree(pin_infos->tpm_spi_buffer[0]);
+			pin_infos->tpm_spi_buffer[0] = NULL;
+		}
+	}
+
+	return 0;
+}
+
+/*
+ * tpm_st33_spi_pm_suspend suspend the TPM device
+ * Added: Work around when suspend and no tpm application is running, suspend
+ * may fail because chip->data_buffer is not set (only set in tpm_open in Linux
+ * TPM core)
+ * @param: client, the spi_device drescription (TPM SPI description).
+ * @param: mesg, the power management message.
+ * @return: 0 in case of success.
+ */
+static int tpm_st33_spi_pm_suspend(struct spi_device *client,
+							 pm_message_t mesg)
+{
+	struct tpm_chip *chip =
+	    (struct tpm_chip *)spi_get_drvdata(client);
+	struct st33zp24_platform_data *pin_infos =
+		((struct spi_device *)chip->vendor.iobase)->dev.platform_data;
+	int ret = 0;
+
+	if (power_mgt)
+		gpio_set_value(pin_infos->io_lpcpd, 0);
+	else {
+		if (chip->data_buffer == NULL)
+			chip->data_buffer = pin_infos->tpm_spi_buffer[0];
+		ret = tpm_pm_suspend(&client->dev);
+		}
+	return ret;
+}				/* tpm_st33_spi_suspend() */
+
+/*
+ * tpm_st33_spi_pm_resume resume the TPM device
+ * @param: spi, the spi_device drescription (TPM SPI description).
+ * @return: 0 in case of success.
+ */
+static int tpm_st33_spi_pm_resume(struct spi_device *spi)
+{
+	struct tpm_chip *chip =
+	    (struct tpm_chip *)spi_get_drvdata(spi);
+	struct st33zp24_platform_data *pin_infos =
+		((struct spi_device *)chip->vendor.iobase)->dev.platform_data;
+
+	int ret = 0;
+	if (power_mgt) {
+		gpio_set_value(pin_infos->io_lpcpd, 1);
+		ret = wait_for_serirq_timeout(chip,
+					 (chip->vendor.status(chip) &&
+					  TPM_STS_VALID) == TPM_STS_VALID,
+					  chip->vendor.timeout_b);
+	} else {
+		if (chip->data_buffer == NULL)
+			chip->data_buffer = pin_infos->tpm_spi_buffer[0];
+		ret = tpm_pm_resume(&spi->dev);
+		if (!ret)
+			tpm_do_selftest(chip);
+	}
+	return ret;
+}				/* tpm_st33_spi_pm_resume() */
+
+static struct spi_driver tpm_st33_spi_driver = {
+	.driver = {
+		   .owner = THIS_MODULE,
+		   .name = TPM_ST33_SPI,
+		   },
+	.probe = tpm_st33_spi_probe,
+	.remove = tpm_st33_spi_remove,
+	.resume = tpm_st33_spi_pm_resume,
+	.suspend = tpm_st33_spi_pm_suspend,
+};
+
+/*
+ * tpm_st33_spi_init initialize driver
+ * @return: 0 if successful, else non zero value.
+ */
+static int __init tpm_st33_spi_init(void)
+{
+	return spi_register_driver(&tpm_st33_spi_driver);
+}
+
+/*
+ * tpm_st33_spi_exit The kernel calls this function during unloading the
+ * module or during shut down process
+ */
+static void __exit tpm_st33_spi_exit(void)
+{
+	spi_unregister_driver(&tpm_st33_spi_driver);
+}
+
+module_init(tpm_st33_spi_init);
+module_exit(tpm_st33_spi_exit);
+
+MODULE_AUTHOR("Christophe Ricard (tpmsupport@st.com)");
+MODULE_DESCRIPTION("STM TPM SPI ST33 Driver");
+MODULE_VERSION("1.2.0");
+MODULE_LICENSE("GPL");
diff --git a/drivers/char/tpm/tpm_spi_stm_st33.h b/drivers/char/tpm/tpm_spi_stm_st33.h
new file mode 100644
index 0000000..752158e
--- /dev/null
+++ b/drivers/char/tpm/tpm_spi_stm_st33.h
@@ -0,0 +1,86 @@
+/*
+ * STMicroelectronics TPM SPI Linux driver for TPM ST33NP18
+ * Copyright (C) 2009, 2010  STMicroelectronics
+ *
+ * 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.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * STMicroelectronics version 1.2.0, Copyright (C) 2010
+ * STMicroelectronics comes with ABSOLUTELY NO WARRANTY.
+ * This is free software, and you are welcome to redistribute it
+ * under certain conditions.
+ *
+ * @Author: Christophe RICARD tpmsupport@st.com
+ *
+ * @File: tpm_spi_stm_st33.h
+ *
+ * @Date: 09/15/2010
+ */
+#ifndef __STM_ST33_TPM_SPI_MAIN_H__
+#define __STM_ST33_TPM_SPI_MAIN_H__
+
+#include <linux/pci.h>
+#include <linux/module.h>
+#include <linux/platform_device.h>
+#include <linux/fs.h>
+#include <linux/miscdevice.h>
+
+#define MINOR_NUM_SPI		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			0x00
+#define TPM_WRITE_DIRECTION		0x80
+#define TPM_HEADER_SIZE			10
+#define TPM_BUFSIZE			2048
+
+/* ioctl commands */
+#define TPMIOC_CANCEL		_IO('T', 0x00)
+#define TPMIOC_TRANSMIT		_IO('T', 0x01)
+#define TPMIOC_HASH		_IO('T', 0x02)
+#define TPMIOC_CHANGELOCALITY	_IO('T', 0x03)
+
+#define LOCALITY0		0
+#define LOCALITY4		4
+
+#define TPM_ST_SPI_OK		0x5A
+
+
+#define MAX_SPI_LATENCY		15
+
+#define TPM_ST33_SPI		"st33zp24_spi"
+
+struct st33zp24_platform_data {
+	int io_serirq;
+	int io_lpcpd;
+	int latency;
+	bool bchipf;
+	u8 *tpm_spi_buffer[2]; /* 0 Request 1 Response */
+	struct completion irq_detection;
+};
+
+struct st_tpm_hash_t {
+	int size;
+	u8 *data;
+};
+
+#endif /* __STM_ST33_TPM_SPI_MAIN_H__ */
-- 
1.7.1


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

* [PATCH 1/1] TPM: STMicroelectronics st33 driver SPI
@ 2013-04-22  8:50 Mathias Leblanc
  0 siblings, 0 replies; 21+ messages in thread
From: Mathias Leblanc @ 2013-04-22  8:50 UTC (permalink / raw)
  To: Kent Yoder, Rajiv Andrade, Marcel Selhorst, Sirrix AG, tpmdd-devel
  Cc: linux-kernel, Jean-Luc Blanc, Mathias Leblanc

 * STMicroelectronics version 1.2.0, Copyright (C) 2010
 * 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 SPI, in menuconfig or .config choose the tpm driver on
device --> tpm and activate the protocol of your choice before compiling
the kernel.
The driver will be accessible from within Linux.

Tested on linux x86/x64 and beagleboard REV B & XM REV C

Signed-off-by: Mathias Leblanc <mathias.leblanc@st.com>
---
 drivers/char/tpm/Kconfig            |   10 +
 drivers/char/tpm/Makefile           |    1 +
 drivers/char/tpm/tpm_spi_stm_st33.c | 1027 +++++++++++++++++++++++++++++++++++
 drivers/char/tpm/tpm_spi_stm_st33.h |   86 +++
 4 files changed, 1124 insertions(+), 0 deletions(-)
 create mode 100644 drivers/char/tpm/tpm_spi_stm_st33.c
 create mode 100644 drivers/char/tpm/tpm_spi_stm_st33.h

diff --git a/drivers/char/tpm/Kconfig b/drivers/char/tpm/Kconfig
index dbfd564..2fc1254 100644
--- a/drivers/char/tpm/Kconfig
+++ b/drivers/char/tpm/Kconfig
@@ -91,4 +91,14 @@ config TCG_ST33_I2C
         To compile this driver as a module, choose M here; the module will be
         called tpm_stm_st33_i2c.
 
+config TCG_ST33_SPI
+       tristate "STMicroelectronics ST33 SPI"
+       depends on SPI
+       depends on GPIOLIB
+       ---help---
+       If you have a TPM security chip from STMicroelectronics working with
+       an SPI bus say Yes and it will be accessible from within Linux.
+       To compile this driver as a module, choose M here; the module will be
+       called tpm_stm_st33_spi.
+
 endif # TCG_TPM
diff --git a/drivers/char/tpm/Makefile b/drivers/char/tpm/Makefile
index a3736c9..a1db3dd 100644
--- a/drivers/char/tpm/Makefile
+++ b/drivers/char/tpm/Makefile
@@ -18,3 +18,4 @@ obj-$(CONFIG_TCG_ATMEL) += tpm_atmel.o
 obj-$(CONFIG_TCG_INFINEON) += tpm_infineon.o
 obj-$(CONFIG_TCG_IBMVTPM) += tpm_ibmvtpm.o
 obj-$(CONFIG_TCG_ST33_I2C) += tpm_i2c_stm_st33.o
+obj-$(CONFIG_TCG_ST33_SPI) += tpm_spi_stm_st33.o
diff --git a/drivers/char/tpm/tpm_spi_stm_st33.c b/drivers/char/tpm/tpm_spi_stm_st33.c
new file mode 100644
index 0000000..70828dc
--- /dev/null
+++ b/drivers/char/tpm/tpm_spi_stm_st33.c
@@ -0,0 +1,1028 @@
+/*
+ * STMicroelectronics TPM SPI Linux driver for TPM ST33ZP24
+ * Copyright (C) 2009, 2010  STMicroelectronics
+ *
+ * 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.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * STMicroelectronics version 1.2.0, Copyright (C) 2010
+ * STMicroelectronics comes with ABSOLUTELY NO WARRANTY.
+ * This is free software, and you are welcome to redistribute it
+ * under certain conditions.
+ *
+ * @Author: Christophe RICARD tpmsupport@st.com
+ *
+ * @File: tpm_stm_st33_spi.c
+ *
+ * @Synopsis:
+ *	09/15/2010:	First shot driver tpm_tis driver for lpc is
+ *			used as model.
+ */
+
+#define pr_fmt(fmt)	KBUILD_MODNAME ": " fmt
+
+#include <linux/module.h>
+#include <linux/kernel.h>
+#include <linux/delay.h>
+#include <linux/init.h>
+#include <linux/spi/spi.h>
+#include <linux/wait.h>
+#include <linux/string.h>
+#include <linux/interrupt.h>
+#include <linux/spinlock.h>
+#include <linux/sysfs.h>
+#include <linux/gpio.h>
+#include <linux/sched.h>
+#include <linux/uaccess.h>
+#include <linux/io.h>
+#include <linux/slab.h>
+#include <linux/sched.h>
+
+#include "tpm.h"
+
+#include "tpm_spi_stm_st33.h"
+
+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 */
+};
+
+/*
+ * write8_reg
+ * Send byte to the TIS register according to the ST33ZP24 SPI protocol.
+ * @param: tpm, 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: should be zero if success else a negative error code.
+ */
+static int write8_reg(struct tpm_chip *tpm, u8 tpm_register,
+		      u8 *tpm_data, u16 tpm_size)
+{
+	u8 data;
+	int total_length = 0, nbr_dummy_bytes;
+	int value = 0;
+	struct spi_device *dev = (struct spi_device *)tpm->vendor.iobase;
+	struct st33zp24_platform_data *platform_data = dev->dev.platform_data;
+	u8 *data_buffer = platform_data->tpm_spi_buffer[0];
+	struct spi_transfer xfer = {
+		.tx_buf	 = data_buffer,
+		.rx_buf	 = platform_data->tpm_spi_buffer[1],
+	};
+	struct spi_message msg;
+	u8 latency = platform_data->latency + tpm_size + 1;
+
+	/* Pre-Header */
+	data = TPM_WRITE_DIRECTION | tpm->vendor.locality;
+	memcpy(data_buffer + total_length, &data, sizeof(data));
+	total_length++;
+	data = tpm_register;
+	memcpy(data_buffer + total_length, &data, sizeof(data));
+	total_length++;
+
+	if (tpm_size > 0 &&
+	     (tpm_register == TPM_DATA_FIFO ||
+	      tpm_register == TPM_HASH_DATA)) {
+		data = (tpm_size >> 8) & 0x00ff;
+		data_buffer[total_length++] = data;
+		data = tpm_size & 0x00ff;
+		data_buffer[total_length++] = data;
+		latency -= 2;
+	}
+
+	memcpy(&data_buffer[total_length], tpm_data, tpm_size);
+	total_length += tpm_size;
+
+	nbr_dummy_bytes = platform_data->latency + 1;
+	memset(&data_buffer[total_length], TPM_DUMMY_BYTE,
+					 platform_data->latency + 1);
+
+	xfer.len = total_length;
+	spi_message_init(&msg);
+	spi_message_add_tail(&xfer, &msg);
+	value = spi_sync(dev, &msg);
+
+	if (value == 0) {
+		nbr_dummy_bytes = total_length - 1 - nbr_dummy_bytes;
+		for (; nbr_dummy_bytes < total_length &&
+			 ((u8 *)xfer.rx_buf)[nbr_dummy_bytes] == 0;
+				 nbr_dummy_bytes++)
+			;
+
+		if (((u8 *)xfer.rx_buf)[nbr_dummy_bytes] != 0)
+			value = ((u8 *)xfer.rx_buf)[nbr_dummy_bytes];
+	}
+
+	return value;
+} /* write8_reg() */
+
+/*
+ * read8_reg
+ * Recv byte from the TIS register according to the ST33ZP24 SPI 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: should be zero if success else a negative error code.
+ */
+static u8 read8_reg(struct tpm_chip *tpm, u8 tpm_register,
+		    u8 *tpm_data, u16 tpm_size)
+{
+	u8 data;
+	int total_length = 0, nbr_dummy_bytes;
+	int value = 0;
+	struct spi_device *dev = (struct spi_device *)tpm->vendor.iobase;
+	struct st33zp24_platform_data *platform_data = dev->dev.platform_data;
+	u8 *data_buffer = platform_data->tpm_spi_buffer[0];
+	struct spi_transfer xfer = {
+		.tx_buf	 = data_buffer,
+		.rx_buf	 = platform_data->tpm_spi_buffer[1],
+	};
+	struct spi_message msg;
+
+	u8 latency = platform_data->latency + tpm_size + 1;
+
+	/* Pre-Header */
+
+	data = tpm->vendor.locality;
+	memcpy(data_buffer + total_length, &data, sizeof(data));
+	total_length++;
+	data = tpm_register;
+	memcpy(data_buffer + total_length, &data, sizeof(data));
+	total_length++;
+
+	nbr_dummy_bytes = platform_data->latency + 1;
+	memset(&data_buffer[total_length], TPM_DUMMY_BYTE,
+					 platform_data->latency + 1);
+
+	xfer.len = total_length;
+
+	/* header + status byte + size of the data + status byte */
+	spi_message_init(&msg);
+	spi_message_add_tail(&xfer, &msg);
+	value = spi_sync(dev, &msg);
+
+	if (tpm_size > 0 && value == 0) {
+		nbr_dummy_bytes = 2;
+		for (; nbr_dummy_bytes < total_length &&
+			 ((u8 *)xfer.rx_buf)[nbr_dummy_bytes] == 0;
+			 nbr_dummy_bytes++)
+			;
+
+		if (nbr_dummy_bytes + 1 < total_length) {
+			value = ((u8 *)xfer.rx_buf)[nbr_dummy_bytes];
+
+			if (tpm_size > 0)
+				memcpy(tpm_data, xfer.rx_buf +
+							 nbr_dummy_bytes + 1,
+								 tpm_size);
+		}
+	}
+	return value;
+} /* read8_reg() */
+
+/*
+ * SPI_WRITE_DATA
+ * Send byte to the TIS register according to the ST33ZP24 SPI protocol.
+ * @param: tpm, 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: should be zero if success else the negative error code.
+ */
+static u32 SPI_WRITE_DATA(struct tpm_chip *tpm, u8 tpm_register,
+			  u8 *tpm_data, u16 tpm_size)
+{
+	u8 value = 0;
+	value = write8_reg(tpm, tpm_register, tpm_data, tpm_size);
+
+	switch (value) {
+	case TPM_ST_SPI_OK:
+		return TPM_ST_SPI_OK;
+	case 0:
+		return 0;
+	default:
+		return -EBUSY;
+	}
+} /* SPI_WRITE_DATA() */
+
+/*
+ * SPI_READ_DATA
+ * Recv byte from the TIS register according to the ST33ZP24 SPI 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: should be zero if success else the negative error code.
+ */
+static u32 SPI_READ_DATA(struct tpm_chip *tpm, u8 tpm_register,
+			u8 *tpm_data, u16 tpm_size)
+{
+	u8 value = 0;
+
+	value = read8_reg(tpm, tpm_register, tpm_data, tpm_size);
+
+	switch (value) {
+	case TPM_ST_SPI_OK:
+		return TPM_ST_SPI_OK;
+	case 0:
+		return 0;
+	default:
+		return -EBUSY;
+	}
+} /* SPI_READ_DATA () */
+
+/*
+ * clear_interruption
+ * clear the TPM interrupt register.
+ * @param: tpm, the chip description
+ */
+static void clear_interruption(struct tpm_chip *tpm)
+{
+	u8 interrupt;
+	SPI_READ_DATA(tpm, TPM_INT_STATUS, &interrupt, 1);
+	/* Clear interrupts handled with TPM_EOI */
+	SPI_WRITE_DATA(tpm, TPM_INT_STATUS, &interrupt, 1);
+	SPI_READ_DATA(tpm, TPM_INT_STATUS, &interrupt, 1);
+} /* clear_interruption() */
+
+/*
+ * _wait_for_interrupt_serirq_timeout
+ * @param: tpm, the chip description
+ * @param: timeout, the timeout of the interrupt
+ * @return: the status of the interruption.
+ */
+static int _wait_for_interrupt_serirq_timeout(struct tpm_chip *chip,
+					unsigned long timeout)
+{
+	unsigned long status;
+	struct spi_device *client;
+	struct st33zp24_platform_data *pin_infos;
+
+	client = (struct spi_device *)chip->vendor.iobase;
+	pin_infos = client->dev.platform_data;
+
+	status = wait_for_completion_interruptible_timeout(
+				&pin_infos->irq_detection, timeout);
+	if (status > 0)
+		enable_irq(gpio_to_irq(pin_infos->io_serirq));
+	gpio_direction_input(pin_infos->io_serirq);
+
+	return status;
+} /* wait_for_interrupt_serirq_timeout() */
+
+unsigned long wait_for_serirq_timeout(struct tpm_chip *chip, bool condition,
+				 unsigned long timeout)
+{
+	unsigned long status = 0;
+
+	status = _wait_for_interrupt_serirq_timeout(chip, timeout);
+	if (!status) {
+		status = -EBUSY;
+		goto wait_end;
+	}
+	clear_interruption(chip);
+	if (condition)
+		status = 1;
+
+wait_end:
+	return status;
+}
+
+
+/*
+ * tpm_stm_spi_cancel, cancel is not implemented.
+ * @param: chip, the tpm chip description as specified in
+ * driver/char/tpm/tpm.h.
+ */
+static void tpm_stm_spi_cancel(struct tpm_chip *chip)
+{
+	u8 data = TPM_STS_COMMAND_READY;
+
+	/* this causes the current command to be aborted */
+	SPI_WRITE_DATA(chip, TPM_STS, &data, 1);
+} /* tpm_stm_spi_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_spi_status(struct tpm_chip *chip)
+{
+	u8 data;
+
+	SPI_READ_DATA(chip, TPM_STS, &data, 1);
+	return data;
+} /* tpm_stm_spi_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 = SPI_READ_DATA(chip, TPM_ACCESS, &data, 1);
+	if (status && (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 stop;
+	long rc;
+	u8 data;
+
+	/* Check locality */
+	if (check_locality(chip) == chip->vendor.locality)
+		return chip->vendor.locality;
+
+	/* Request locality */
+	data = TPM_ACCESS_REQUEST_USE;
+	rc = SPI_WRITE_DATA(chip, TPM_ACCESS, &data, 1);
+	if (rc < 0)
+		goto end;
+
+	/* wait for burstcount */
+	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 {
+		stop = jiffies + chip->vendor.timeout_a;
+		do {
+			if (check_locality(chip) >= 0)
+				return chip->vendor.locality;
+			msleep(TPM_TIMEOUT);
+		} while (time_before(jiffies, stop));
+	}
+end:
+	return -EACCES;
+} /* request_locality() */
+
+/*
+ * release_locality release the active locality
+ * @param: chip, the tpm chip description.
+ */
+static void release_locality(struct tpm_chip *chip)
+{
+	u8 data;
+
+	data = TPM_ACCESS_ACTIVE_LOCALITY;
+	SPI_WRITE_DATA(chip, TPM_ACCESS, &data, 1);
+} /* release_locality()*/
+
+/*
+ * 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 stop;
+	u32 burstcnt, status;
+	u8 tpm_reg, temp;
+
+	/* wait for burstcount */
+	/* which timeout value, spec has 2 answers (c & d) */
+	stop = jiffies + chip->vendor.timeout_d;
+	do {
+		tpm_reg = TPM_STS + 1;
+		status = SPI_READ_DATA(chip, tpm_reg, &temp, 1);
+		if (status < 0)
+			goto end;
+
+		tpm_reg = tpm_reg + 1;
+		burstcnt = temp;
+		status = SPI_READ_DATA(chip, tpm_reg, &temp, 1);
+		if (status < 0)
+			goto end;
+
+		burstcnt |= temp << 8;
+		if (burstcnt)
+			return burstcnt;
+		msleep(TPM_TIMEOUT);
+	} while (time_before(jiffies, stop));
+
+end:
+	return -EBUSY;
+} /* get_burstcount() */
+
+/*
+ * wait_for_stat wait for a TPM_STS value
+ * @param: chip, the tpm chip description
+ * @param: mask, the value mask to wait
+ * @param: timeout, the timeout
+ * @param: queue, the wait queue.
+ * @return: the tpm status, 0 if success, -ETIME if timeout is reached.
+ */
+static int wait_for_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout,
+			 wait_queue_head_t *queue)
+{
+	unsigned long stop;
+	long rc;
+	u8 status;
+
+	/* check current status */
+	status = tpm_stm_spi_status(chip);
+	if (!chip->vendor.irq && (status & mask) == mask)
+		return 0;
+
+	if (chip->vendor.irq) {
+		rc = wait_for_serirq_timeout(chip, ((tpm_stm_spi_status
+						    (chip) & mask) ==
+						     mask), timeout);
+		if (rc > 0)
+			return 0;
+	} else {
+		stop = jiffies + timeout;
+		do {
+			msleep(TPM_TIMEOUT);
+			status = tpm_stm_spi_status(chip);
+			if ((status & mask) == mask)
+				return 0;
+		} while (time_before(jiffies, stop));
+	}
+	return -ETIME;
+} /* wait_for_stat() */
+
+/*
+ * 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 tpm status, 0 if success, -ETIME if timeout is reached.
+ */
+static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count)
+{
+	u32 size = 0, burstcnt, status, len;
+
+	while (size < count &&
+	       wait_for_stat(chip,
+			     TPM_STS_DATA_AVAIL | TPM_STS_VALID,
+			     chip->vendor.timeout_c,
+			     &chip->vendor.read_queue)
+						== 0) {
+		burstcnt = get_burstcount(chip);
+		len = min_t(int, burstcnt, count - size);
+		status = SPI_READ_DATA(chip, TPM_DATA_FIFO, buf + size, len);
+		if (status < 0)
+			return status;
+
+
+		size += len;
+	}
+	return size;
+}
+
+/*
+ * tpm_ioserirq_handler the serirq irq handler
+ * @param: irq, the tpm chip description
+ * @param: dev_id, the description of the chip
+ * @return: the status of the handler.
+ */
+static irqreturn_t tpm_ioserirq_handler(int irq, void *dev_id)
+{
+	struct tpm_chip *chip = dev_id;
+	struct spi_device *client;
+	struct st33zp24_platform_data *pin_infos;
+
+	disable_irq_nosync(irq);
+
+	client = (struct spi_device *)chip->vendor.iobase;
+	pin_infos = client->dev.platform_data;
+
+	complete(&pin_infos->irq_detection);
+	return IRQ_HANDLED;
+} /* tpm_ioserirq_handler() */
+
+/*
+ * tpm_stm_spi_send send TPM commands through the SPI bus.
+ * @param: chip, the tpm chip description
+ * @param: buf, the tpm command buffer
+ * @param: len, the tpm command size
+ * @return: should be zero if success else the negative error code.
+ */
+static int tpm_stm_spi_send(struct tpm_chip *chip, unsigned char *buf,
+			    size_t len)
+{
+	u32 ret = 0, status, burstcnt = 0, i, size = 0;
+	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_spi_status(chip);
+	if ((status & TPM_STS_COMMAND_READY) == 0) {
+		tpm_stm_spi_cancel(chip);
+		if (wait_for_stat
+		    (chip, TPM_STS_COMMAND_READY, chip->vendor.timeout_b,
+		     &chip->vendor.int_queue) < 0) {
+			ret = -ETIME;
+			goto out_err;
+		}
+	}
+
+	for (i = 0; i < len - 1;) {
+		burstcnt = get_burstcount(chip);
+		size = min_t(int, len - i - 1, burstcnt);
+		ret = SPI_WRITE_DATA(chip, TPM_DATA_FIFO, buf, size);
+		if (ret < 0)
+			goto out_err;
+		i += size;
+	}
+
+	status = tpm_stm_spi_status(chip);
+	if ((status & TPM_STS_DATA_EXPECT) == 0) {
+		ret = -EIO;
+		goto out_err;
+	}
+
+	/* write last byte */
+	SPI_WRITE_DATA(chip, TPM_DATA_FIFO, buf + len - 1, 1);
+
+	status = tpm_stm_spi_status(chip);
+	if ((status & TPM_STS_DATA_EXPECT) != 0) {
+		ret = -EIO;
+		goto out_err;
+	}
+
+	/* go and do it */
+	data = TPM_STS_GO;
+	ret = SPI_WRITE_DATA(chip, TPM_STS, &data, 1);
+	if (ret < 0)
+		goto out_err;
+
+	return len;
+out_err:
+	tpm_stm_spi_cancel(chip);
+	release_locality(chip);
+	return ret;
+}
+
+/*
+ * tpm_stm_spi_recv received TPM response through the SPI bus.
+ * @param: chip, the tpm chip description
+ * @param: buf, the tpm command buffer
+ * @param: len, the tpm command size
+* @return: should be zero if success else the negative error code.
+ */
+static int tpm_stm_spi_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);
+
+	/* read first 10 bytes, including tag, paramsize, and result */
+	if (size < TPM_HEADER_SIZE) {
+		dev_err(chip->dev, "Unable to read header\n");
+		goto out;
+	}
+
+	expected = be32_to_cpu(*(__be32 *)(buf + 2));
+	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;
+}
+
+
+static const struct file_operations tpm_st33_spi_fops = {
+	.owner = THIS_MODULE,
+	.llseek = no_llseek,
+	.read = tpm_read,
+	.write = tpm_write,
+	.open = tpm_open,
+	.release = tpm_release,
+};
+
+static DEVICE_ATTR(pubek, S_IRUGO, tpm_show_pubek, NULL);
+static DEVICE_ATTR(pcrs, S_IRUGO, tpm_show_pcrs, NULL);
+static DEVICE_ATTR(enabled, S_IRUGO, tpm_show_enabled, NULL);
+static DEVICE_ATTR(active, S_IRUGO, tpm_show_active, NULL);
+static DEVICE_ATTR(owned, S_IRUGO, tpm_show_owned, NULL);
+static DEVICE_ATTR(temp_deactivated, S_IRUGO, tpm_show_temp_deactivated, NULL);
+static DEVICE_ATTR(caps, S_IRUGO, tpm_show_caps_1_2, NULL);
+static DEVICE_ATTR(cancel, S_IWUSR | S_IWGRP, NULL, tpm_store_cancel);
+
+static struct attribute *stm_tpm_attrs[] = {
+	&dev_attr_pubek.attr,
+	&dev_attr_pcrs.attr,
+	&dev_attr_enabled.attr,
+	&dev_attr_active.attr,
+	&dev_attr_owned.attr,
+	&dev_attr_temp_deactivated.attr,
+	&dev_attr_caps.attr,
+	&dev_attr_cancel.attr, NULL,
+};
+
+static struct attribute_group stm_tpm_attr_grp = {
+	.attrs = stm_tpm_attrs
+};
+
+static struct tpm_vendor_specific st_spi_tpm = {
+	.send = tpm_stm_spi_send,
+	.recv = tpm_stm_spi_recv,
+	.cancel = tpm_stm_spi_cancel,
+	.status = tpm_stm_spi_status,
+	.attr_group = &stm_tpm_attr_grp,
+	.miscdev = {.fops = &tpm_st33_spi_fops,},
+};
+
+static int evaluate_latency(struct tpm_chip *chip)
+{
+	int latency = 0;
+	struct spi_device *dev = (struct spi_device *)chip->vendor.iobase;
+	struct st33zp24_platform_data *platform_data = dev->dev.platform_data;
+	int status = 0;
+	u8 data;
+
+	for (; status == 0x00 && latency < MAX_SPI_LATENCY; latency++) {
+		platform_data->latency = latency;
+		status = SPI_READ_DATA(chip, TPM_INTF_CAPABILITY, &data, 1);
+	}
+	return latency - 1;
+} /* evaluate_latency() */
+
+static int interrupts;
+module_param(interrupts, int, 0444);
+MODULE_PARM_DESC(interrupts, "Enable interrupts");
+
+static int power_mgt = 1;
+module_param(power_mgt, int, 0444);
+MODULE_PARM_DESC(power_mgt, "Power Management");
+
+/*
+ * tpm_st33_spi_probe initialize the TPM device
+ * @param: client, the spi_device drescription (TPM SPI description).
+ * @param: id, the spi_device_id struct.
+ * @return: 0 in case of success.
+ *	 -1 in other case.
+ */
+static int
+tpm_st33_spi_probe(struct spi_device *dev)
+{
+	u32 err = 0;
+	u8 intmask;
+	struct tpm_chip *chip;
+	struct st33zp24_platform_data *platform_data;
+
+	/* Check SPI platform functionnalities */
+	if (dev == NULL) {
+		pr_info("dev is NULL. exiting.\n");
+		err = -ENODEV;
+		goto end;
+	}
+
+	chip = tpm_register_hardware(&dev->dev, &st_spi_tpm);
+	if (!chip) {
+		err = -ENODEV;
+		goto end;
+	}
+
+	/* Allocation of SPI buffers MISO and MOSI		*/
+	/* Size is as follow:					*/
+	/* Request burstcount value  = 0x800 = 2048		*/
+	/* +							*/
+	/* Response burstcount value = 0x400 = 1024		*/
+	/* +							*/
+	/* At least:						*/
+	/* 1 byte for direction/locality			*/
+	/* 1 byte tpm tis register				*/
+	/* 2 bytes spi data length (for request only)		*/
+	/* 2 latency bytes					*/
+	/* 1 status byte					*/
+	/* = 2048 + 1024 + 7					*/
+	/* We reserved 2048 + 1024 + 20 in case latency byte	*/
+	/* change						*/
+	platform_data = dev->dev.platform_data;
+	platform_data->tpm_spi_buffer[0] =
+	    kmalloc((TPM_BUFSIZE + (TPM_BUFSIZE / 2) +
+				 TPM_DIGEST_SIZE) * sizeof(u8), GFP_KERNEL);
+	if (platform_data->tpm_spi_buffer[0] == NULL) {
+		err = -ENOMEM;
+		goto _tpm_clean_answer;
+	}
+	platform_data->tpm_spi_buffer[1] =
+	    kmalloc((TPM_BUFSIZE + (TPM_BUFSIZE / 2) +
+				 TPM_DIGEST_SIZE) * sizeof(u8) , GFP_KERNEL);
+	if (platform_data->tpm_spi_buffer[1] == NULL) {
+		err = -ENOMEM;
+		goto _tpm_clean_response;
+	}
+
+	chip->vendor.iobase = dev;
+
+	/* Default timeouts */
+	chip->vendor.timeout_a = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
+	chip->vendor.timeout_b = msecs_to_jiffies(TIS_LONG_TIMEOUT);
+	chip->vendor.timeout_c = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
+	chip->vendor.timeout_d = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
+	chip->vendor.locality = LOCALITY0;
+
+	if (power_mgt) {
+		err = gpio_request(platform_data->io_lpcpd, "TPM IO_LPCPD");
+		if (err)
+			goto _gpio_init1;
+		gpio_set_value(platform_data->io_lpcpd, 1);
+	}
+
+	platform_data->latency = evaluate_latency(chip);
+	if (platform_data->latency <= 0x00) {
+		err = -ENODEV;
+		goto _gpio_init1;
+	}
+
+	/* Enable interrupt */
+	/* Register GPIO pin through generic Linux GPIO API */
+	if (interrupts) {
+		init_completion(&platform_data->irq_detection);
+		if (request_locality(chip) != LOCALITY0) {
+			err = -ENODEV;
+			goto _tpm_clean_response;
+		}
+		err = gpio_request(platform_data->io_serirq, "TPM IO_SERIRQ");
+		if (err)
+			goto _gpio_init2;
+
+		/* Clear all existing */
+		clear_interruption(chip);
+		err = request_irq(gpio_to_irq(platform_data->io_serirq),
+							&tpm_ioserirq_handler,
+				IRQF_TRIGGER_HIGH | IRQF_SHARED,
+				"TPM SERIRQ management", chip);
+		if (err < 0) {
+			pr_info("TPM SERIRQ signals %d not available\n",
+			gpio_to_irq(platform_data->io_serirq));
+			goto _irq_set;
+		}
+		err = SPI_READ_DATA(chip, TPM_INT_ENABLE, &intmask, 1);
+		if (err < 0)
+			goto _irq_set;
+
+		intmask |= TPM_INTF_CMD_READY_INT
+			|  TPM_INTF_FIFO_AVALAIBLE_INT
+			|  TPM_INTF_WAKE_UP_READY_INT
+			|  TPM_INTF_LOC4SOFTRELEASE_INT
+			|  TPM_INTF_LOCALITY_CHANGE_INT
+			|  TPM_INTF_STS_VALID_INT
+			|  TPM_INTF_DATA_AVAIL_INT;
+
+		err = SPI_WRITE_DATA(chip, TPM_INT_ENABLE, &intmask, 1);
+		if (err < 0)
+			goto _irq_set;
+
+		intmask = TPM_GLOBAL_INT_ENABLE;
+		err = SPI_WRITE_DATA(chip, TPM_INT_ENABLE + 3, &intmask, 1);
+		if (err < 0)
+			goto _irq_set;
+
+		err = SPI_READ_DATA(chip, TPM_INT_STATUS, &intmask, 1);
+		if (err < 0)
+			goto _irq_set;
+
+		chip->vendor.irq = interrupts;
+
+		tpm_gen_interrupt(chip);
+	}
+
+	tpm_get_timeouts(chip);
+
+	/* attach chip datas to client */
+	spi_set_drvdata(dev, chip);
+	platform_data->bchipf = false;
+
+	pr_info("TPM SPI Initialized\n");
+	return 0;
+_irq_set:
+	free_irq(gpio_to_irq(platform_data->io_serirq), chip);
+_gpio_init2:
+	if (platform_data && interrupts)
+		gpio_free(platform_data->io_serirq);
+_gpio_init1:
+	if (platform_data && power_mgt)
+		gpio_free(platform_data->io_lpcpd);
+_tpm_clean_response:
+	tpm_remove_hardware(chip->dev);
+	if (platform_data->tpm_spi_buffer[1] != NULL) {
+		kfree(platform_data->tpm_spi_buffer[1]);
+		platform_data->tpm_spi_buffer[1] = NULL;
+	}
+_tpm_clean_answer:
+	if (platform_data->tpm_spi_buffer[0] != NULL) {
+		kfree(platform_data->tpm_spi_buffer[0]);
+		platform_data->tpm_spi_buffer[0] = NULL;
+	}
+
+	platform_data->bchipf = true;
+end:
+	pr_info("TPM SPI initialisation fail\n");
+	return err;
+}
+
+/*
+ * tpm_st33_spi_remove remove the TPM device
+ * @param: client, the spi_device drescription (TPM SPI description).
+		clear_bit(0, &chip->is_open);
+ * @return: 0 in case of success.
+ */
+static int tpm_st33_spi_remove(struct spi_device *client)
+{
+	struct tpm_chip *chip = (struct tpm_chip *)spi_get_drvdata(client);
+	struct st33zp24_platform_data *pin_infos =
+		((struct spi_device *)chip->vendor.iobase)->dev.platform_data;
+
+	if (pin_infos != NULL) {
+		gpio_free(pin_infos->io_lpcpd);
+
+		/* Check if chip has been previously clean */
+		if (pin_infos->bchipf != true)
+			tpm_remove_hardware(chip->dev);
+		if (pin_infos->tpm_spi_buffer[1] != NULL) {
+			kfree(pin_infos->tpm_spi_buffer[1]);
+			pin_infos->tpm_spi_buffer[1] = NULL;
+		}
+		if (pin_infos->tpm_spi_buffer[0] != NULL) {
+			kfree(pin_infos->tpm_spi_buffer[0]);
+			pin_infos->tpm_spi_buffer[0] = NULL;
+		}
+	}
+
+	return 0;
+}
+
+/*
+ * tpm_st33_spi_pm_suspend suspend the TPM device
+ * Added: Work around when suspend and no tpm application is running, suspend
+ * may fail because chip->data_buffer is not set (only set in tpm_open in Linux
+ * TPM core)
+ * @param: client, the spi_device drescription (TPM SPI description).
+ * @param: mesg, the power management message.
+ * @return: 0 in case of success.
+ */
+static int tpm_st33_spi_pm_suspend(struct spi_device *client,
+							 pm_message_t mesg)
+{
+	struct tpm_chip *chip =
+	    (struct tpm_chip *)spi_get_drvdata(client);
+	struct st33zp24_platform_data *pin_infos =
+		((struct spi_device *)chip->vendor.iobase)->dev.platform_data;
+	int ret = 0;
+
+	if (power_mgt)
+		gpio_set_value(pin_infos->io_lpcpd, 0);
+	else {
+		if (chip->data_buffer == NULL)
+			chip->data_buffer = pin_infos->tpm_spi_buffer[0];
+		ret = tpm_pm_suspend(&client->dev);
+		}
+	return ret;
+}				/* tpm_st33_spi_suspend() */
+
+/*
+ * tpm_st33_spi_pm_resume resume the TPM device
+ * @param: spi, the spi_device drescription (TPM SPI description).
+ * @return: 0 in case of success.
+ */
+static int tpm_st33_spi_pm_resume(struct spi_device *spi)
+{
+	struct tpm_chip *chip =
+	    (struct tpm_chip *)spi_get_drvdata(spi);
+	struct st33zp24_platform_data *pin_infos =
+		((struct spi_device *)chip->vendor.iobase)->dev.platform_data;
+
+	int ret = 0;
+	if (power_mgt) {
+		gpio_set_value(pin_infos->io_lpcpd, 1);
+		ret = wait_for_serirq_timeout(chip,
+					 (chip->vendor.status(chip) &&
+					  TPM_STS_VALID) == TPM_STS_VALID,
+					  chip->vendor.timeout_b);
+	} else {
+		if (chip->data_buffer == NULL)
+			chip->data_buffer = pin_infos->tpm_spi_buffer[0];
+		ret = tpm_pm_resume(&spi->dev);
+		if (!ret)
+			tpm_do_selftest(chip);
+	}
+	return ret;
+}				/* tpm_st33_spi_pm_resume() */
+
+static struct spi_driver tpm_st33_spi_driver = {
+	.driver = {
+		   .owner = THIS_MODULE,
+		   .name = TPM_ST33_SPI,
+		   },
+	.probe = tpm_st33_spi_probe,
+	.remove = tpm_st33_spi_remove,
+	.resume = tpm_st33_spi_pm_resume,
+	.suspend = tpm_st33_spi_pm_suspend,
+};
+
+/*
+ * tpm_st33_spi_init initialize driver
+ * @return: 0 if successful, else non zero value.
+ */
+static int __init tpm_st33_spi_init(void)
+{
+	return spi_register_driver(&tpm_st33_spi_driver);
+}
+
+/*
+ * tpm_st33_spi_exit The kernel calls this function during unloading the
+ * module or during shut down process
+ */
+static void __exit tpm_st33_spi_exit(void)
+{
+	spi_unregister_driver(&tpm_st33_spi_driver);
+}
+
+module_init(tpm_st33_spi_init);
+module_exit(tpm_st33_spi_exit);
+
+MODULE_AUTHOR("Christophe Ricard (tpmsupport@st.com)");
+MODULE_DESCRIPTION("STM TPM SPI ST33 Driver");
+MODULE_VERSION("1.2.0");
+MODULE_LICENSE("GPL");
diff --git a/drivers/char/tpm/tpm_spi_stm_st33.h b/drivers/char/tpm/tpm_spi_stm_st33.h
new file mode 100644
index 0000000..752158e
--- /dev/null
+++ b/drivers/char/tpm/tpm_spi_stm_st33.h
@@ -0,0 +1,86 @@
+/*
+ * STMicroelectronics TPM SPI Linux driver for TPM ST33NP18
+ * Copyright (C) 2009, 2010  STMicroelectronics
+ *
+ * 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.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * STMicroelectronics version 1.2.0, Copyright (C) 2010
+ * STMicroelectronics comes with ABSOLUTELY NO WARRANTY.
+ * This is free software, and you are welcome to redistribute it
+ * under certain conditions.
+ *
+ * @Author: Christophe RICARD tpmsupport@st.com
+ *
+ * @File: tpm_spi_stm_st33.h
+ *
+ * @Date: 09/15/2010
+ */
+#ifndef __STM_ST33_TPM_SPI_MAIN_H__
+#define __STM_ST33_TPM_SPI_MAIN_H__
+
+#include <linux/pci.h>
+#include <linux/module.h>
+#include <linux/platform_device.h>
+#include <linux/fs.h>
+#include <linux/miscdevice.h>
+
+#define MINOR_NUM_SPI		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			0x00
+#define TPM_WRITE_DIRECTION		0x80
+#define TPM_HEADER_SIZE			10
+#define TPM_BUFSIZE			2048
+
+/* ioctl commands */
+#define TPMIOC_CANCEL		_IO('T', 0x00)
+#define TPMIOC_TRANSMIT		_IO('T', 0x01)
+#define TPMIOC_HASH		_IO('T', 0x02)
+#define TPMIOC_CHANGELOCALITY	_IO('T', 0x03)
+
+#define LOCALITY0		0
+#define LOCALITY4		4
+
+#define TPM_ST_SPI_OK		0x5A
+
+
+#define MAX_SPI_LATENCY		15
+
+#define TPM_ST33_SPI		"st33zp24_spi"
+
+struct st33zp24_platform_data {
+	int io_serirq;
+	int io_lpcpd;
+	int latency;
+	bool bchipf;
+	u8 *tpm_spi_buffer[2]; /* 0 Request 1 Response */
+	struct completion irq_detection;
+};
+
+struct st_tpm_hash_t {
+	int size;
+	u8 *data;
+};
+
+#endif /* __STM_ST33_TPM_SPI_MAIN_H__ */
-- 
1.7.1


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

* [PATCH 1/1] TPM: STMicroelectronics st33 driver SPI
@ 2013-04-12  8:44 Matthias Leblanc
  0 siblings, 0 replies; 21+ messages in thread
From: Matthias Leblanc @ 2013-04-12  8:44 UTC (permalink / raw)
  To: Kent Yoder, Rajiv Andrade, Marcel Selhorst, Sirrix AG, tpmdd-devel
  Cc: linux-kernel, Jean-Luc Blanc, Mathias Leblanc

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

 * STMicroelectronics version 1.2.0, Copyright (C) 2010
 * 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 SPI, in menuconfig or .config choose the tpm driver on
device --> tpm and activate the protocol of your choice before compiling
the kernel.
The driver will be accessible from within Linux.

Tested on linux x86/x64 and beagleboard REV B & XM REV C

Signed-off-by: Mathias Leblanc <mathias.leblanc@st.com>
---
 drivers/char/tpm/Kconfig             |   10 +
 drivers/char/tpm/Makefile            |    1 +
 drivers/char/tpm/tpm_spi_stm_st33.c  | 1116 ++++++++++++++++++++++++++++++++++
 drivers/char/tpm/tpm_spi_stm_st33.h  |   75 +++
 include/linux/spi/tpm_spi_stm_st33.h |   44 ++
 7 files changed, 1246 insertions(+), 161 deletions(-)
 delete mode 100644 .gitignore
 delete mode 100644 REPORTING-BUGS
 create mode 100644 drivers/char/tpm/tpm_spi_stm_st33.c
 create mode 100644 drivers/char/tpm/tpm_spi_stm_st33.h
 create mode 100644 include/linux/spi/tpm_spi_stm_st33.h

diff --git a/drivers/char/tpm/Kconfig b/drivers/char/tpm/Kconfig
index dbfd564..2fc1254 100644
--- a/drivers/char/tpm/Kconfig
+++ b/drivers/char/tpm/Kconfig
@@ -91,4 +91,14 @@ config TCG_ST33_I2C
         To compile this driver as a module, choose M here; the module will be
         called tpm_stm_st33_i2c.
 
+config TCG_ST33_SPI
+       tristate "STMicroelectronics ST33 SPI"
+       depends on SPI
+       depends on GPIOLIB
+       ---help---
+       If you have a TPM security chip from STMicroelectronics working with
+       an SPI bus say Yes and it will be accessible from within Linux.
+       To compile this driver as a module, choose M here; the module will be
+       called tpm_stm_st33_spi.
+
 endif # TCG_TPM
diff --git a/drivers/char/tpm/Makefile b/drivers/char/tpm/Makefile
index a3736c9..a1db3dd 100644
--- a/drivers/char/tpm/Makefile
+++ b/drivers/char/tpm/Makefile
@@ -18,3 +18,4 @@ obj-$(CONFIG_TCG_ATMEL) += tpm_atmel.o
 obj-$(CONFIG_TCG_INFINEON) += tpm_infineon.o
 obj-$(CONFIG_TCG_IBMVTPM) += tpm_ibmvtpm.o
 obj-$(CONFIG_TCG_ST33_I2C) += tpm_i2c_stm_st33.o
+obj-$(CONFIG_TCG_ST33_SPI) += tpm_spi_stm_st33.o
diff --git a/drivers/char/tpm/tpm_spi_stm_st33.c b/drivers/char/tpm/tpm_spi_stm_st33.c
new file mode 100644
index 0000000..9462f7c
--- /dev/null
+++ b/drivers/char/tpm/tpm_spi_stm_st33.c
@@ -0,0 +1,1116 @@
+/*
+ * STMicroelectronics TPM SPI Linux driver for TPM ST33ZP24
+ * Copyright (C) 2009, 2010  STMicroelectronics
+ *
+ * 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.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * STMicroelectronics version 1.2.0, Copyright (C) 2010
+ * STMicroelectronics comes with ABSOLUTELY NO WARRANTY.
+ * This is free software, and you are welcome to redistribute it
+ * under certain conditions.
+ *
+ * @Author: Christophe RICARD tpmsupport@st.com
+ *
+ * @File: tpm_stm_st33_spi.c
+ *
+ * @Synopsis:
+ *	09/15/2010:	First shot driver tpm_tis driver for lpc is
+ *			used as model.
+ */
+
+#define pr_fmt(fmt)	KBUILD_MODNAME ": " fmt
+
+#include <linux/module.h>
+#include <linux/kernel.h>
+#include <linux/delay.h>
+#include <linux/init.h>
+#include <linux/spi/spi.h>
+#include <linux/wait.h>
+#include <linux/string.h>
+#include <linux/interrupt.h>
+#include <linux/spinlock.h>
+#include <linux/sysfs.h>
+#include <linux/gpio.h>
+#include <linux/sched.h>
+#include <linux/uaccess.h>
+#include <linux/io.h>
+#include <linux/slab.h>
+#include <linux/sched.h>
+
+#include <linux/spi/tpm_spi_stm_st33.h>
+
+#include "tpm.h"
+
+#include "tpm_spi_stm_st33.h"
+
+#ifdef DEBUG
+#define FUNC_ENTER()	pr_info("%s\n", __func__)
+#else
+#define	FUNC_ENTER()	do {}	while (0)
+#endif
+
+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 */
+};
+
+/*
+ * write8_reg
+ * Send byte to the TIS register according to the ST33ZP24 SPI protocol.
+ * @param: tpm, 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.
+ */
+static int write8_reg(struct tpm_chip *tpm, u8 tpm_register,
+		      u8 *tpm_data, u16 tpm_size)
+{
+	/* u8 LOC+DIR u8 ADD u16 LENGHT u8* data */
+	/* u8 LATENCY u8 LATENCY */
+
+	u8 data;
+	int i = 0, j;
+	int value = 0;
+	struct spi_device *dev = (struct spi_device *)tpm->vendor.iobase;
+	struct st33zp24_platform_data *platform_data = dev->dev.platform_data;
+	u8 *data_buffer = platform_data->tpm_spi_buffer[0];
+	struct spi_transfer xfer = {
+		.tx_buf	 = data_buffer,
+		.rx_buf	 = platform_data->tpm_spi_buffer[1],
+	};
+	struct spi_message msg;
+	u8 latency = platform_data->latency + tpm_size + 1;
+
+
+	FUNC_ENTER();
+	/* Pre-Header */
+
+	data = TPM_WRITE_DIRECTION | tpm->vendor.locality;
+	memcpy(data_buffer + i, &data, sizeof(data));
+	i++;
+	data = tpm_register;
+	memcpy(data_buffer + i, &data, sizeof(data));
+	i++;
+
+	if (
+		tpm_size > 0 &&
+		(tpm_register == TPM_DATA_FIFO ||
+			tpm_register == TPM_HASH_DATA)) {
+		data = tpm_size >> 8 & 0x00ff;
+		memcpy(data_buffer + i, &data, sizeof(data));
+		i++;
+		data = tpm_size & 0x00ff;
+		memcpy(data_buffer + i, &data, sizeof(data));
+		i++;
+		latency -= 2;
+	}
+
+	memcpy(data_buffer + i, tpm_data, tpm_size);
+	i += tpm_size;
+
+	for (j = 0; j < platform_data->latency + 1; j++) {
+		data = TPM_DUMMY_BYTE;
+		memcpy(data_buffer + i, &data, sizeof(data));
+		i++;
+	}
+	xfer.len = i;
+	spi_message_init(&msg);
+	spi_message_add_tail(&xfer, &msg);
+	value = spi_sync(dev, &msg);
+
+	if (value == 0) {
+		j = i - 1 - j;
+		for (; j < i && ((u8 *)xfer.rx_buf)[j] == 0; j++)
+			;
+
+		if (((u8 *)xfer.rx_buf)[j] != 0)
+			value = ((u8 *)xfer.rx_buf)[j];
+	}
+
+	return value;
+} /* write8_reg() */
+
+/*
+ * read8_reg
+ * Recv byte from the TIS register according to the ST33ZP24 SPI 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.
+ */
+static u8 read8_reg(struct tpm_chip *tpm, u8 tpm_register,
+		    u8 *tpm_data, u16 tpm_size)
+{
+	u8 data;
+	int i = 0, j;
+	int value = 0;
+	struct spi_device *dev = (struct spi_device *)tpm->vendor.iobase;
+	struct st33zp24_platform_data *platform_data = dev->dev.platform_data;
+	u8 *data_buffer = platform_data->tpm_spi_buffer[0];
+	struct spi_transfer xfer = {
+		.tx_buf	 = data_buffer,
+		.rx_buf	 = platform_data->tpm_spi_buffer[1],
+	};
+	struct spi_message msg;
+
+	u8 latency = platform_data->latency + tpm_size + 1;
+
+	FUNC_ENTER();
+	/* Pre-Header */
+
+	data = tpm->vendor.locality;
+	memcpy(data_buffer + i, &data, sizeof(data));
+	i++;
+	data = tpm_register;
+	memcpy(data_buffer + i, &data, sizeof(data));
+	i++;
+
+	for (j = 0; j < latency; j++) {
+		data = TPM_DUMMY_BYTE;
+		memcpy(data_buffer + i, &data, sizeof(data));
+		i++;
+	}
+	xfer.len = i;
+
+	/* header + status byte + size of the data + status byte */
+	spi_message_init(&msg);
+	spi_message_add_tail(&xfer, &msg);
+	value = spi_sync(dev, &msg);
+	if (tpm_size > 0 && value == 0) {
+		j = 2;
+		for (; j < i && ((u8 *)xfer.rx_buf)[j] == 0; j++)
+			;
+
+		if (j + 1 < i) {
+			value = ((u8 *)xfer.rx_buf)[j];
+
+		if (tpm_size > 0)
+			memcpy(tpm_data, xfer.rx_buf + j + 1, tpm_size);
+		}
+	}
+	return value;
+} /* read8_reg() */
+
+/*
+ * SPI_WRITE_DATA
+ * Send byte to the TIS register according to the ST33ZP24 SPI protocol.
+ * @param: tpm, 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.
+ */
+static u32 SPI_WRITE_DATA(struct tpm_chip *tpm, u8 tpm_register,
+			  u8 *tpm_data, u16 tpm_size)
+{
+	u8 value = 0;
+
+	FUNC_ENTER();
+
+	value = write8_reg(tpm, tpm_register, tpm_data, tpm_size);
+
+	switch (value) {
+	case TPM_ST_SPI_OK:
+		return TPM_ST_SPI_OK;
+	case 0:
+		return 0;
+	default:
+		return -EBUSY;
+	}
+} /* SPI_WRITE_DATA() */
+
+/*
+ * SPI_READ_DATA
+ * Recv byte from the TIS register according to the ST33ZP24 SPI 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.
+ */
+static u32 SPI_READ_DATA(struct tpm_chip *tpm, u8 tpm_register,
+			u8 *tpm_data, u16 tpm_size)
+{
+	u8 value = 0;
+
+	FUNC_ENTER();
+
+	value = read8_reg(tpm, tpm_register, tpm_data, tpm_size);
+
+	switch (value) {
+	case TPM_ST_SPI_OK:
+		return TPM_ST_SPI_OK;
+	case 0:
+		return 0;
+	default:
+		return -EBUSY;
+	}
+} /* SPI_READ_DATA () */
+
+/*
+ * clear_interruption
+ * clear the TPM interrupt register.
+ * @param: tpm, the chip description
+ */
+static void clear_interruption(struct tpm_chip *tpm)
+{
+	u8 interrupt;
+	SPI_READ_DATA(tpm, TPM_INT_STATUS, &interrupt, 1);
+	/* Clear interrupts handled with TPM_EOI */
+	SPI_WRITE_DATA(tpm, TPM_INT_STATUS, &interrupt, 1);
+	SPI_READ_DATA(tpm, TPM_INT_STATUS, &interrupt, 1);
+} /* clear_interruption() */
+
+/*
+ * _wait_for_interrupt_serirq_timeout
+ * @param: tpm, the chip description
+ * @param: timeout, the timeout of the interrupt
+ * @return: the status of the interruption.
+ */
+static int _wait_for_interrupt_serirq_timeout(struct tpm_chip *chip,
+					unsigned long timeout)
+{
+	unsigned long status;
+	struct spi_device *client;
+	struct st33zp24_platform_data *pin_infos;
+
+	FUNC_ENTER();
+
+	client = (struct spi_device *)chip->vendor.iobase;
+	pin_infos = client->dev.platform_data;
+
+	status = wait_for_completion_interruptible_timeout(
+				&pin_infos->irq_detection, timeout);
+	if (status > 0)
+		enable_irq(gpio_to_irq(pin_infos->io_serirq));
+	gpio_direction_input(pin_infos->io_serirq);
+
+	return status;
+} /* wait_for_interrupt_serirq_timeout() */
+
+unsigned long wait_for_serirq_timeout(struct tpm_chip *chip, bool condition,
+				 unsigned long timeout)
+{
+	unsigned long status = 0;
+	struct spi_device *client;
+	struct st33zp24_platform_data *pin_infos;
+
+	client = (struct spi_device *)chip->vendor.iobase;
+	pin_infos = client->dev.platform_data;
+
+	status = _wait_for_interrupt_serirq_timeout(chip, timeout);
+	if (!status) {
+		status = -EBUSY;
+		goto wait_end;
+	}
+	clear_interruption(chip);
+	if (condition)
+		status = 1;
+
+wait_end:
+	return status;
+}
+
+
+/*
+ * tpm_stm_spi_cancel, cancel is not implemented.
+ * @param: chip, the tpm chip description as specified in
+ * driver/char/tpm/tpm.h.
+ */
+static void tpm_stm_spi_cancel(struct tpm_chip *chip)
+{
+	struct spi_device *client;
+	u8 data = TPM_STS_COMMAND_READY;
+	client = (struct spi_device *)chip->vendor.iobase;
+
+	FUNC_ENTER();
+
+	/* this causes the current command to be aborted */
+	SPI_WRITE_DATA(chip, TPM_STS, &data, 1);
+} /* tpm_stm_spi_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_spi_status(struct tpm_chip *chip)
+{
+	struct spi_device *client;
+	u8 data;
+	client = (struct spi_device *)chip->vendor.iobase;
+
+	FUNC_ENTER();
+
+	SPI_READ_DATA(chip, TPM_STS, &data, 1);
+	return data;
+} /* tpm_stm_spi_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)
+{
+	struct spi_device *client;
+	u8 data;
+	u8 status;
+
+	FUNC_ENTER();
+
+	client = (struct spi_device *)chip->vendor.iobase;
+
+	status = SPI_READ_DATA(chip, TPM_ACCESS, &data, 1);
+	if (status && (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 stop;
+	long rc;
+	u8 data;
+	struct spi_device *client;
+
+	FUNC_ENTER();
+
+	client = (struct spi_device *)chip->vendor.iobase;
+	/* Check locality */
+	if (check_locality(chip) == chip->vendor.locality)
+		return chip->vendor.locality;
+
+	/* Request locality */
+	data = TPM_ACCESS_REQUEST_USE;
+	rc = SPI_WRITE_DATA(chip, TPM_ACCESS, &data, 1);
+	if (rc < 0)
+		goto end;
+
+	/* wait for burstcount */
+	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 {
+		stop = jiffies + chip->vendor.timeout_a;
+		do {
+			if (check_locality(chip) >= 0)
+				return chip->vendor.locality;
+			msleep(TPM_TIMEOUT);
+		} while (time_before(jiffies, stop));
+	}
+end:
+	return -EACCES;
+} /* request_locality() */
+
+/*
+ * release_locality release the active locality
+ * @param: chip, the tpm chip description.
+ */
+static void release_locality(struct tpm_chip *chip)
+{
+	struct spi_device *client;
+	u8 data;
+
+	FUNC_ENTER();
+
+	client = (struct spi_device *)chip->vendor.iobase;
+	data = TPM_ACCESS_ACTIVE_LOCALITY;
+	SPI_WRITE_DATA(chip, TPM_ACCESS, &data, 1);
+} /* release_locality()*/
+
+/*
+ * 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 stop;
+	u32 burstcnt, status;
+	u8 tpm_reg, temp;
+
+	FUNC_ENTER();
+
+	/* wait for burstcount */
+	/* which timeout value, spec has 2 answers (c & d) */
+	stop = jiffies + chip->vendor.timeout_d;
+	do {
+		tpm_reg = TPM_STS + 1;
+		status = SPI_READ_DATA(chip, tpm_reg, &temp, 1);
+		if (status < 0)
+			goto end;
+
+		tpm_reg = tpm_reg + 1;
+		burstcnt = temp;
+		status = SPI_READ_DATA(chip, tpm_reg, &temp, 1);
+		if (status < 0)
+			goto end;
+
+		burstcnt |= temp << 8;
+		if (burstcnt)
+			return burstcnt;
+		msleep(TPM_TIMEOUT);
+	} while (time_before(jiffies, stop));
+
+end:
+	return -EBUSY;
+} /* get_burstcount() */
+
+/*
+ * wait_for_stat wait for a TPM_STS value
+ * @param: chip, the tpm chip description
+ * @param: mask, the value mask to wait
+ * @param: timeout, the timeout
+ * @param: queue, the wait queue.
+ * @return: the tpm status, 0 if success, -ETIME if timeout is reached.
+ */
+static int wait_for_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout,
+			 wait_queue_head_t *queue)
+{
+	unsigned long stop;
+	long rc;
+	u8 status;
+
+	FUNC_ENTER();
+
+	/* check current status */
+	status = tpm_stm_spi_status(chip);
+	if (!chip->vendor.irq && (status & mask) == mask)
+		return 0;
+
+	if (chip->vendor.irq) {
+		rc = wait_for_serirq_timeout(chip, ((tpm_stm_spi_status
+						    (chip) & mask) ==
+						     mask), timeout);
+		if (rc > 0)
+			return 0;
+	} else {
+		stop = jiffies + timeout;
+		do {
+			msleep(TPM_TIMEOUT);
+			status = tpm_stm_spi_status(chip);
+			if ((status & mask) == mask)
+				return 0;
+		} while (time_before(jiffies, stop));
+	}
+	return -ETIME;
+} /* wait_for_stat() */
+
+/*
+ * 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 tpm status, 0 if success, -ETIME if timeout is reached.
+ */
+static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count)
+{
+	u32 size = 0, burstcnt, status, len;
+	struct spi_device *client;
+
+	FUNC_ENTER();
+
+	client = (struct spi_device *)chip->vendor.iobase;
+
+	while (size < count &&
+	       wait_for_stat(chip,
+			     TPM_STS_DATA_AVAIL | TPM_STS_VALID,
+			     chip->vendor.timeout_c,
+			     &chip->vendor.read_queue)
+						== 0) {
+		burstcnt = get_burstcount(chip);
+		len = min_t(int, burstcnt, count - size);
+		status = SPI_READ_DATA(chip, TPM_DATA_FIFO, buf + size, len);
+		if (status < 0)
+			return status;
+
+
+		size += len;
+	}
+	return size;
+}
+
+/*
+ * tpm_ioserirq_handler the serirq irq handler
+ * @param: irq, the tpm chip description
+ * @param: dev_id, the description of the chip
+ * @return: the status of the handler.
+ */
+static irqreturn_t tpm_ioserirq_handler(int irq, void *dev_id)
+{
+	struct tpm_chip *chip = dev_id;
+	struct spi_device *client;
+	struct st33zp24_platform_data *pin_infos;
+
+	disable_irq_nosync(irq);
+	FUNC_ENTER();
+
+	client = (struct spi_device *)chip->vendor.iobase;
+	pin_infos = client->dev.platform_data;
+
+	complete(&pin_infos->irq_detection);
+	return IRQ_HANDLED;
+} /* tpm_ioserirq_handler() */
+
+/*
+ * tpm_stm_spi_send send TPM commands through the SPI bus.
+ * @param: chip, the tpm chip description
+ * @param: buf, the tpm command buffer
+ * @param: len, the tpm command size
+ * @return: In case of success the number of bytes sent.
+ *			In other case, a < 0 value describing the issue.
+ */
+static int tpm_stm_spi_send(struct tpm_chip *chip, unsigned char *buf,
+			    size_t len)
+{
+	u32 ret = 0, ordinal,
+	    status, burstcnt = 0, i, size = 0;
+	u8 data;
+
+	struct spi_device *client;
+	struct st33zp24_platform_data *pin_infos;
+
+	FUNC_ENTER();
+
+	if (chip == NULL)
+		return -EBUSY;
+	if (len < TPM_HEADER_SIZE)
+		return -EBUSY;
+
+	client = (struct spi_device *)chip->vendor.iobase;
+	pin_infos = client->dev.platform_data;
+
+	ordinal = be32_to_cpu(*((__be32 *)(buf + 6)));
+
+	ret = request_locality(chip);
+	if (ret < 0)
+		return ret;
+
+	status = tpm_stm_spi_status(chip);
+	if ((status & TPM_STS_COMMAND_READY) == 0) {
+		tpm_stm_spi_cancel(chip);
+		if (wait_for_stat
+		    (chip, TPM_STS_COMMAND_READY, chip->vendor.timeout_b,
+		     &chip->vendor.int_queue) < 0) {
+			ret = -ETIME;
+			goto out_err;
+		}
+	}
+
+	for (i = 0; i < len - 1;) {
+		burstcnt = get_burstcount(chip);
+		size = min_t(int, len - i - 1, burstcnt);
+		ret = SPI_WRITE_DATA(chip, TPM_DATA_FIFO, buf, size);
+		if (ret < 0)
+			goto out_err;
+		i += size;
+	}
+
+	status = tpm_stm_spi_status(chip);
+	if ((status & TPM_STS_DATA_EXPECT) == 0) {
+		ret = -EIO;
+		goto out_err;
+	}
+
+	/* write last byte */
+	SPI_WRITE_DATA(chip, TPM_DATA_FIFO, buf + len - 1, 1);
+
+	status = tpm_stm_spi_status(chip);
+	if ((status & TPM_STS_DATA_EXPECT) != 0) {
+		ret = -EIO;
+		goto out_err;
+	}
+
+	/* go and do it */
+	data = TPM_STS_GO;
+	ret = SPI_WRITE_DATA(chip, TPM_STS, &data, 1);
+	if (ret < 0)
+		goto out_err;
+
+	return len;
+out_err:
+	tpm_stm_spi_cancel(chip);
+	release_locality(chip);
+	return ret;
+}
+
+/*
+ * tpm_stm_spi_recv received TPM response through the SPI bus.
+ * @param: chip, the tpm chip description
+ * @param: buf, the tpm command buffer
+ * @param: len, the tpm command size
+* @return: In case of success the number of bytes received.
+ *			In other case, a < 0 value describing the issue.
+ */
+static int tpm_stm_spi_recv(struct tpm_chip *chip, unsigned char *buf,
+			    size_t count)
+{
+	int size = 0;
+	int expected;
+
+	struct spi_device *client;
+	struct st33zp24_platform_data *pin_infos;
+
+
+	FUNC_ENTER();
+
+	client = (struct spi_device *)chip->vendor.iobase;
+	pin_infos = client->dev.platform_data;
+
+
+	if (chip == NULL)
+		return -EBUSY;
+	if (count < TPM_HEADER_SIZE) {
+		size = -EIO;
+		goto out;
+	}
+
+	size = recv_data(chip, buf, TPM_HEADER_SIZE);
+
+	/* read first 10 bytes, including tag, paramsize, and result */
+	if (size < TPM_HEADER_SIZE) {
+		dev_err(chip->dev, "Unable to read header\n");
+		goto out;
+	}
+
+	expected = be32_to_cpu(*(__be32 *)(buf + 2));
+	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;
+}
+
+
+static const struct file_operations tpm_st33_spi_fops = {
+	.owner = THIS_MODULE,
+	.llseek = no_llseek,
+	.read = tpm_read,
+	.write = tpm_write,
+	.open = tpm_open,
+	.release = tpm_release,
+};
+
+static DEVICE_ATTR(pubek, S_IRUGO, tpm_show_pubek, NULL);
+static DEVICE_ATTR(pcrs, S_IRUGO, tpm_show_pcrs, NULL);
+static DEVICE_ATTR(enabled, S_IRUGO, tpm_show_enabled, NULL);
+static DEVICE_ATTR(active, S_IRUGO, tpm_show_active, NULL);
+static DEVICE_ATTR(owned, S_IRUGO, tpm_show_owned, NULL);
+static DEVICE_ATTR(temp_deactivated, S_IRUGO, tpm_show_temp_deactivated, NULL);
+static DEVICE_ATTR(caps, S_IRUGO, tpm_show_caps_1_2, NULL);
+static DEVICE_ATTR(cancel, S_IWUSR | S_IWGRP, NULL, tpm_store_cancel);
+
+static struct attribute *stm_tpm_attrs[] = {
+	&dev_attr_pubek.attr,
+	&dev_attr_pcrs.attr,
+	&dev_attr_enabled.attr,
+	&dev_attr_active.attr,
+	&dev_attr_owned.attr,
+	&dev_attr_temp_deactivated.attr,
+	&dev_attr_caps.attr,
+	&dev_attr_cancel.attr, NULL,
+};
+
+static struct attribute_group stm_tpm_attr_grp = {
+	.attrs = stm_tpm_attrs
+};
+
+static struct tpm_vendor_specific st_spi_tpm = {
+	.send = tpm_stm_spi_send,
+	.recv = tpm_stm_spi_recv,
+	.cancel = tpm_stm_spi_cancel,
+	.status = tpm_stm_spi_status,
+	.attr_group = &stm_tpm_attr_grp,
+	.miscdev = {.fops = &tpm_st33_spi_fops,},
+};
+
+static int evaluate_latency(struct tpm_chip *chip)
+{
+	int latency = 0;
+	struct spi_device *dev = (struct spi_device *)chip->vendor.iobase;
+	struct st33zp24_platform_data *platform_data = dev->dev.platform_data;
+	int status = 0;
+	u8 data;
+
+	for (; status == 0x00 && latency < MAX_SPI_LATENCY; latency++) {
+		platform_data->latency = latency;
+		status = SPI_READ_DATA(chip, TPM_INTF_CAPABILITY, &data, 1);
+	}
+	return latency - 1;
+} /* evaluate_latency() */
+
+static int interrupts;
+module_param(interrupts, int, 0444);
+MODULE_PARM_DESC(interrupts, "Enable interrupts");
+
+static int power_mgt = 1;
+module_param(power_mgt, int, 0444);
+MODULE_PARM_DESC(power_mgt, "Power Management");
+
+/*
+ * tpm_st33_spi_probe initialize the TPM device
+ * @param: client, the spi_device drescription (TPM SPI description).
+ * @param: id, the spi_device_id struct.
+ * @return: 0 in case of success.
+ *	 -1 in other case.
+ */
+static int
+tpm_st33_spi_probe(struct spi_device *dev)
+{
+	u32 err;
+	u8 intmask;
+	struct tpm_chip *chip;
+	struct st33zp24_platform_data *platform_data;
+
+	FUNC_ENTER();
+
+	err = 0;
+
+	/* Check SPI platform functionnalities */
+	if (dev == NULL) {
+		pr_info("dev is NULL. exiting.\n");
+		err = -ENODEV;
+		goto end;
+	}
+
+	chip = tpm_register_hardware(&dev->dev, &st_spi_tpm);
+	if (!chip) {
+		err = -ENODEV;
+		goto end;
+	}
+
+	/* Allocation of SPI buffers MISO and MOSI		*/
+	/* Size is as follow:					*/
+	/* Request burstcount value  = 0x800 = 2048		*/
+	/* +							*/
+	/* Response burstcount value = 0x400 = 1024		*/
+	/* +							*/
+	/* At least:						*/
+	/* 1 byte for direction/locality			*/
+	/* 1 byte tpm tis register				*/
+	/* 2 bytes spi data length (for request only)		*/
+	/* 2 latency bytes					*/
+	/* 1 status byte					*/
+	/* = 2048 + 1024 + 7					*/
+	/* We reserved 2048 + 1024 + 20 in case latency byte	*/
+	/* change						*/
+	platform_data = dev->dev.platform_data;
+	platform_data->tpm_spi_buffer[0] =
+	    kmalloc((TPM_BUFSIZE + (TPM_BUFSIZE / 2) +
+				 TPM_DIGEST_SIZE) * sizeof(u8), GFP_KERNEL);
+	if (platform_data->tpm_spi_buffer[0] == NULL) {
+		err = -ENOMEM;
+		goto _tpm_clean_answer;
+	}
+	platform_data->tpm_spi_buffer[1] =
+	    kmalloc((TPM_BUFSIZE + (TPM_BUFSIZE / 2) +
+				 TPM_DIGEST_SIZE) * sizeof(u8) , GFP_KERNEL);
+	if (platform_data->tpm_spi_buffer[1] == NULL) {
+		err = -ENOMEM;
+		goto _tpm_clean_response;
+	}
+
+	chip->vendor.iobase = dev;
+
+	/* Default timeouts */
+	chip->vendor.timeout_a = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
+	chip->vendor.timeout_b = msecs_to_jiffies(TIS_LONG_TIMEOUT);
+	chip->vendor.timeout_c = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
+	chip->vendor.timeout_d = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
+	chip->vendor.locality = LOCALITY0;
+
+	if (power_mgt) {
+		err = gpio_request(platform_data->io_lpcpd, "TPM IO_LPCPD");
+		if (err)
+			goto _gpio_init1;
+		gpio_set_value(platform_data->io_lpcpd, 1);
+	}
+
+	platform_data->latency = evaluate_latency(chip);
+	if (platform_data->latency <= 0x00) {
+		err = -ENODEV;
+		goto _gpio_init1;
+	}
+
+	/* Enable interrupt */
+	/* Register GPIO pin through generic Linux GPIO API */
+	if (interrupts) {
+		init_completion(&platform_data->irq_detection);
+		if (request_locality(chip) != LOCALITY0) {
+			err = -ENODEV;
+			goto _tpm_clean_response;
+		}
+		err = gpio_request(platform_data->io_serirq, "TPM IO_SERIRQ");
+		if (err)
+			goto _gpio_init2;
+
+		/* Clear all existing */
+		clear_interruption(chip);
+		err = request_irq(gpio_to_irq(platform_data->io_serirq),
+							&tpm_ioserirq_handler,
+				IRQF_TRIGGER_HIGH | IRQF_SHARED,
+				"TPM SERIRQ management", chip);
+		if (err < 0) {
+			pr_info("TPM SERIRQ signals %d not available\n",
+				gpio_to_irq(
+					platform_data->io_serirq));
+			goto _irq_set;
+		}
+		err = SPI_READ_DATA(chip, TPM_INT_ENABLE, &intmask, 1);
+		if (err < 0)
+			goto _irq_set;
+
+		intmask |= TPM_INTF_CMD_READY_INT
+			|  TPM_INTF_FIFO_AVALAIBLE_INT
+			|  TPM_INTF_WAKE_UP_READY_INT
+			|  TPM_INTF_LOC4SOFTRELEASE_INT
+			|  TPM_INTF_LOCALITY_CHANGE_INT
+			|  TPM_INTF_STS_VALID_INT
+			|  TPM_INTF_DATA_AVAIL_INT;
+
+		err = SPI_WRITE_DATA(chip, TPM_INT_ENABLE, &intmask, 1);
+		if (err < 0)
+			goto _irq_set;
+
+		intmask = TPM_GLOBAL_INT_ENABLE;
+		err = SPI_WRITE_DATA(chip, TPM_INT_ENABLE + 3, &intmask, 1);
+		if (err < 0)
+			goto _irq_set;
+
+		err = SPI_READ_DATA(chip, TPM_INT_STATUS, &intmask, 1);
+		if (err < 0)
+			goto _irq_set;
+
+		chip->vendor.irq = interrupts;
+
+		tpm_gen_interrupt(chip);
+	}
+
+	tpm_get_timeouts(chip);
+
+	/* attach chip datas to client */
+	spi_set_drvdata(dev, chip);
+	platform_data->bchipf = false;
+
+	pr_info("TPM SPI Initialized\n");
+	return 0;
+_irq_set:
+	free_irq(gpio_to_irq(platform_data->io_serirq), chip);
+_gpio_init2:
+	if (platform_data && interrupts)
+		gpio_free(platform_data->io_serirq);
+_gpio_init1:
+	if (platform_data && power_mgt)
+		gpio_free(platform_data->io_lpcpd);
+_tpm_clean_response:
+	tpm_remove_hardware(chip->dev);
+	if (platform_data->tpm_spi_buffer[1] != NULL) {
+		kfree(platform_data->tpm_spi_buffer[1]);
+		platform_data->tpm_spi_buffer[1] = NULL;
+	}
+_tpm_clean_answer:
+	if (platform_data->tpm_spi_buffer[0] != NULL) {
+		kfree(platform_data->tpm_spi_buffer[0]);
+		platform_data->tpm_spi_buffer[0] = NULL;
+	}
+
+	platform_data->bchipf = true;
+end:
+	pr_info("TPM SPI initialisation fail\n");
+	return err;
+}
+
+/*
+ * tpm_st33_spi_remove remove the TPM device
+ * @param: client, the spi_device drescription (TPM SPI description).
+		clear_bit(0, &chip->is_open);
+ * @return: 0 in case of success.
+ */
+static int tpm_st33_spi_remove(struct spi_device *client)
+{
+	struct tpm_chip *chip = (struct tpm_chip *)spi_get_drvdata(client);
+	struct st33zp24_platform_data *pin_infos =
+		((struct spi_device *)chip->vendor.iobase)->dev.platform_data;
+
+	FUNC_ENTER();
+
+	if (pin_infos != NULL) {
+		gpio_free(pin_infos->io_lpcpd);
+
+		/* Check if chip has been previously clean */
+		if (pin_infos->bchipf != true)
+			tpm_remove_hardware(chip->dev);
+		if (pin_infos->tpm_spi_buffer[1] != NULL) {
+			kfree(pin_infos->tpm_spi_buffer[1]);
+			pin_infos->tpm_spi_buffer[1] = NULL;
+		}
+		if (pin_infos->tpm_spi_buffer[0] != NULL) {
+			kfree(pin_infos->tpm_spi_buffer[0]);
+			pin_infos->tpm_spi_buffer[0] = NULL;
+		}
+	}
+
+	return 0;
+}
+
+/*
+ * tpm_st33_spi_pm_suspend suspend the TPM device
+ * Added: Work around when suspend and no tpm application is running, suspend
+ * may fail because chip->data_buffer is not set (only set in tpm_open in Linux
+ * TPM core)
+ * @param: client, the spi_device drescription (TPM SPI description).
+ * @param: mesg, the power management message.
+ * @return: 0 in case of success.
+ */
+static int tpm_st33_spi_pm_suspend(struct spi_device *client,
+							 pm_message_t mesg)
+{
+	struct tpm_chip *chip =
+	    (struct tpm_chip *)spi_get_drvdata(client);
+	struct st33zp24_platform_data *pin_infos =
+		((struct spi_device *)chip->vendor.iobase)->dev.platform_data;
+	int ret = 0;
+
+	if (power_mgt) {
+		gpio_set_value(pin_infos->io_lpcpd, 0);
+	} else {
+		if (chip->data_buffer == NULL)
+			chip->data_buffer = pin_infos->tpm_spi_buffer[0];
+		ret = tpm_pm_suspend(&client->dev);
+	}
+	return ret;
+}				/* tpm_st33_spi_suspend() */
+
+/*
+ * tpm_st33_spi_pm_resume resume the TPM device
+ * @param: spi, the spi_device drescription (TPM SPI description).
+ * @return: 0 in case of success.
+ */
+static int tpm_st33_spi_pm_resume(struct spi_device *spi)
+{
+	struct tpm_chip *chip =
+	    (struct tpm_chip *)spi_get_drvdata(spi);
+	struct st33zp24_platform_data *pin_infos =
+		((struct spi_device *)chip->vendor.iobase)->dev.platform_data;
+
+	int ret = 0;
+	if (power_mgt) {
+		gpio_set_value(pin_infos->io_lpcpd, 1);
+		ret = wait_for_serirq_timeout(chip,
+					 (chip->vendor.status(chip) &&
+					  TPM_STS_VALID) == TPM_STS_VALID,
+					  chip->vendor.timeout_b);
+	} else {
+		if (chip->data_buffer == NULL)
+			chip->data_buffer = pin_infos->tpm_spi_buffer[0];
+		ret = tpm_pm_resume(&spi->dev);
+		if (!ret)
+			tpm_do_selftest(chip);
+	}
+	return ret;
+}				/* tpm_st33_spi_pm_resume() */
+
+static struct spi_driver tpm_st33_spi_driver = {
+	.driver = {
+		   .owner = THIS_MODULE,
+		   .name = TPM_ST33_SPI,
+		   },
+	.probe = tpm_st33_spi_probe,
+	.remove = tpm_st33_spi_remove,
+	.resume = tpm_st33_spi_pm_resume,
+	.suspend = tpm_st33_spi_pm_suspend,
+};
+
+/*
+ * tpm_st33_spi_init initialize driver
+ * @return: 0 if successful, else non zero value.
+ */
+static int __init tpm_st33_spi_init(void)
+{
+	FUNC_ENTER();
+	return spi_register_driver(&tpm_st33_spi_driver);
+}
+
+/*
+ * tpm_st33_spi_exit The kernel calls this function during unloading the
+ * module or during shut down process
+ */
+static void __exit tpm_st33_spi_exit(void)
+{
+	FUNC_ENTER();
+	spi_unregister_driver(&tpm_st33_spi_driver);
+}
+
+module_init(tpm_st33_spi_init);
+module_exit(tpm_st33_spi_exit);
+
+MODULE_AUTHOR("Christophe Ricard (tpmsupport@st.com)");
+MODULE_DESCRIPTION("STM TPM SPI ST33 Driver");
+MODULE_VERSION("1.2.0");
+MODULE_LICENSE("GPL");
diff --git a/drivers/char/tpm/tpm_spi_stm_st33.h b/drivers/char/tpm/tpm_spi_stm_st33.h
new file mode 100644
index 0000000..42bae6f
--- /dev/null
+++ b/drivers/char/tpm/tpm_spi_stm_st33.h
@@ -0,0 +1,75 @@
+/*
+ * STMicroelectronics TPM SPI Linux driver for TPM ST33NP18
+ * Copyright (C) 2009, 2010  STMicroelectronics
+ *
+ * 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.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * STMicroelectronics version 1.2.0, Copyright (C) 2010
+ * STMicroelectronics comes with ABSOLUTELY NO WARRANTY.
+ * This is free software, and you are welcome to redistribute it
+ * under certain conditions.
+ *
+ * @Author: Christophe RICARD tpmsupport@st.com
+ *
+ * @File: tpm_spi_stm_st33.h
+ *
+ * @Date: 09/15/2010
+ */
+#ifndef __STM_ST33_TPM_SPI_MAIN_H__
+#define __STM_ST33_TPM_SPI_MAIN_H__
+
+#include <linux/pci.h>
+#include <linux/module.h>
+#include <linux/platform_device.h>
+#include <linux/fs.h>
+#include <linux/miscdevice.h>
+
+#define MINOR_NUM_SPI		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			0x00
+#define TPM_WRITE_DIRECTION		0x80
+#define TPM_HEADER_SIZE			10
+#define TPM_BUFSIZE			2048
+
+/* ioctl commands */
+#define TPMIOC_CANCEL		_IO('T', 0x00)
+#define TPMIOC_TRANSMIT		_IO('T', 0x01)
+#define TPMIOC_HASH		_IO('T', 0x02)
+#define TPMIOC_CHANGELOCALITY	_IO('T', 0x03)
+
+#define LOCALITY0		0
+#define LOCALITY4		4
+
+#define TPM_ST_SPI_OK		0x5A
+
+
+#define MAX_SPI_LATENCY		15
+
+struct st_tpm_hash_t {
+	int size;
+	u8 *data;
+};
+
+#endif /* __STM_ST33_TPM_SPI_MAIN_H__ */
diff --git a/include/linux/spi/tpm_spi_stm_st33.h b/include/linux/spi/tpm_spi_stm_st33.h
new file mode 100644
index 0000000..d7e10db
--- /dev/null
+++ b/include/linux/spi/tpm_spi_stm_st33.h
@@ -0,0 +1,44 @@
+/*
+* STMicroelectronics TPM SPI Linux driver for TPM ST33NP18
+* Copyright (C) 2009, 2010 STMicroelectronics
+* Christophe RICARD tpmsupport@st.com
+* 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.,
+* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*
+* STMicroelectronics version 1.2.0, Copyright (C) 2010
+* STMicroelectronics comes with ABSOLUTELY NO WARRANTY.
+* This is free software, and you are welcome to redistribute it
+* under certain conditions.
+*
+* @File: tpm_spi_stm_st33.h
+*
+* @Date: 06/15/2008
+*/
+#ifndef __STM_ST33_TPM_SPI_H__
+#define __STM_ST33_TPM_SPI_H__
+
+#include <linux/spi/spi.h>
+
+#define TPM_ST33_SPI		"st33zp24_spi"
+
+struct st33zp24_platform_data {
+	int io_serirq;
+	int io_lpcpd;
+	int latency;
+	bool bchipf;
+	u8 *tpm_spi_buffer[2]; /* 0 Request 1 Response */
+	struct completion irq_detection;
+};
+
+#endif /* __STM_ST33_TPM_SPI_H__ */
-- 
1.7.1


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

* [PATCH 1/1] TPM: STMicroelectronics st33 driver SPI
@ 2013-04-09 14:42 Matthias Leblanc
  0 siblings, 0 replies; 21+ messages in thread
From: Matthias Leblanc @ 2013-04-09 14:42 UTC (permalink / raw)
  To: Kent Yoder, Rajiv Andrade, Marcel Selhorst, Sirrix AG, tpmdd-devel
  Cc: linux-kernel, Jean-Luc Blanc, Mathias Leblanc, Mathias leblanc

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

 * STMicroelectronics version 1.2.0, Copyright (C) 2010
 * 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 SPI, in menuconfig or .config choose the tpm driver on
device --> tpm and activate the protocol of your choice before compiling
the kernel.
The driver will be accessible from within Linux.

Tested on linux x86/x64 and beagleboard REV B & XM REV C

Signed-off-by: Mathias Leblanc <mathias.leblanc@st.com>
---
 drivers/char/tpm/Kconfig             |   10 +
 drivers/char/tpm/Makefile            |    1 +
 drivers/char/tpm/tpm_spi_stm_st33.c  | 1116 ++++++++++++++++++++++++++++++++++
 drivers/char/tpm/tpm_spi_stm_st33.h  |   75 +++
 include/linux/spi/tpm_spi_stm_st33.h |   46 ++
 5 files changed, 1248 insertions(+), 0 deletions(-)
 create mode 100644 drivers/char/tpm/tpm_spi_stm_st33.c
 create mode 100644 drivers/char/tpm/tpm_spi_stm_st33.h
 create mode 100644 include/linux/spi/tpm_spi_stm_st33.h

diff --git a/drivers/char/tpm/Kconfig b/drivers/char/tpm/Kconfig
index dbfd564..2fc1254 100644
--- a/drivers/char/tpm/Kconfig
+++ b/drivers/char/tpm/Kconfig
@@ -91,4 +91,14 @@ config TCG_ST33_I2C
         To compile this driver as a module, choose M here; the module will be
         called tpm_stm_st33_i2c.
 
+config TCG_ST33_SPI
+       tristate "STMicroelectronics ST33 SPI"
+       depends on SPI
+       depends on GPIOLIB
+       ---help---
+       If you have a TPM security chip from STMicroelectronics working with
+       an SPI bus say Yes and it will be accessible from within Linux.
+       To compile this driver as a module, choose M here; the module will be
+       called tpm_stm_st33_spi.
+
 endif # TCG_TPM
diff --git a/drivers/char/tpm/Makefile b/drivers/char/tpm/Makefile
index a3736c9..a1db3dd 100644
--- a/drivers/char/tpm/Makefile
+++ b/drivers/char/tpm/Makefile
@@ -18,3 +18,4 @@ obj-$(CONFIG_TCG_ATMEL) += tpm_atmel.o
 obj-$(CONFIG_TCG_INFINEON) += tpm_infineon.o
 obj-$(CONFIG_TCG_IBMVTPM) += tpm_ibmvtpm.o
 obj-$(CONFIG_TCG_ST33_I2C) += tpm_i2c_stm_st33.o
+obj-$(CONFIG_TCG_ST33_SPI) += tpm_spi_stm_st33.o
diff --git a/drivers/char/tpm/tpm_spi_stm_st33.c b/drivers/char/tpm/tpm_spi_stm_st33.c
new file mode 100644
index 0000000..a47c647
--- /dev/null
+++ b/drivers/char/tpm/tpm_spi_stm_st33.c
@@ -0,0 +1,1116 @@
+/*
+ * STMicroelectronics TPM SPI Linux driver for TPM ST33ZP24
+ * Copyright (C) 2009, 2010  STMicroelectronics
+ *
+ * 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.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * STMicroelectronics version 1.2.0, Copyright (C) 2010
+ * STMicroelectronics comes with ABSOLUTELY NO WARRANTY.
+ * This is free software, and you are welcome to redistribute it
+ * under certain conditions.
+ *
+ * @Author: Christophe RICARD tpmsupport@st.com
+ *
+ * @File: tpm_stm_st33_spi.c
+ *
+ * @Synopsis:
+ *	09/15/2010:	First shot driver tpm_tis driver for lpc is
+ *			used as model.
+ */
+
+#define pr_fmt(fmt)	KBUILD_MODNAME ": " fmt
+
+#include <linux/module.h>
+#include <linux/kernel.h>
+#include <linux/delay.h>
+#include <linux/init.h>
+#include <linux/spi/spi.h>
+#include <linux/wait.h>
+#include <linux/string.h>
+#include <linux/interrupt.h>
+#include <linux/spinlock.h>
+#include <linux/sysfs.h>
+#include <linux/gpio.h>
+#include <linux/sched.h>
+#include <linux/uaccess.h>
+#include <linux/io.h>
+#include <linux/slab.h>
+#include <linux/sched.h>
+
+#include <linux/spi/tpm_spi_stm_st33.h>
+
+#include "tpm.h"
+
+#include "tpm_spi_stm_st33.h"
+
+#ifdef DEBUG
+#define FUNC_ENTER()	pr_info("%s\n", __func__)
+#else
+#define	FUNC_ENTER()	do {}	while (0)
+#endif
+
+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 */
+};
+
+/*
+ * write8_reg
+ * Send byte to the TIS register according to the ST33ZP24 SPI protocol.
+ * @param: tpm, 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.
+ */
+static int write8_reg(struct tpm_chip *tpm, u8 tpm_register,
+		      u8 *tpm_data, u16 tpm_size)
+{
+	/* u8 LOC+DIR u8 ADD u16 LENGHT u8* data */
+	/* u8 LATENCY u8 LATENCY */
+
+	u8 data;
+	int i = 0, j;
+	int value = 0;
+	struct spi_device *dev = (struct spi_device *) tpm->vendor.iobase;
+	struct st33zp24_platform_data *platform_data = dev->dev.platform_data;
+	u8 *data_buffer = platform_data->tpm_spi_buffer[0];
+	struct spi_transfer xfer = {
+		.tx_buf	 = data_buffer,
+		.rx_buf	 = platform_data->tpm_spi_buffer[1],
+	};
+	struct spi_message msg;
+	u8 latency = platform_data->latency + tpm_size + 1;
+
+
+	FUNC_ENTER();
+	/* Pre-Header */
+
+	data = TPM_WRITE_DIRECTION | tpm->vendor.locality;
+	memcpy(data_buffer + i, &data, sizeof(data));
+	i++;
+	data = tpm_register;
+	memcpy(data_buffer + i, &data, sizeof(data));
+	i++;
+
+	if (tpm_size > 0 &&
+	     (tpm_register == TPM_DATA_FIFO ||
+	      tpm_register == TPM_HASH_DATA)) {
+		data = tpm_size >> 8 & 0x00ff;
+		memcpy(data_buffer + i, &data, sizeof(data));
+		i++;
+		data = tpm_size & 0x00ff;
+		memcpy(data_buffer + i, &data, sizeof(data));
+		i++;
+		latency -= 2;
+	}
+
+	memcpy(data_buffer + i, tpm_data, tpm_size);
+	i += tpm_size;
+
+	for (j = 0 ; j < platform_data->latency + 1; j++) {
+		data = TPM_DUMMY_BYTE;
+		memcpy(data_buffer + i, &data, sizeof(data));
+		i++;
+	}
+	xfer.len = i;
+	spi_message_init(&msg);
+	spi_message_add_tail(&xfer, &msg);
+	value = spi_sync(dev, &msg);
+
+	if (value == 0) {
+		j = i - 1 - j;
+		for (; j < i && ((u8 *) xfer.rx_buf)[j] == 0; j++)
+			;
+
+		if (((u8 *) xfer.rx_buf)[j] != 0)
+			value = ((u8 *) xfer.rx_buf)[j];
+	}
+
+	return value;
+} /* write8_reg() */
+
+/*
+ * read8_reg
+ * Recv byte from the TIS register according to the ST33ZP24 SPI 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.
+ */
+static u8 read8_reg(struct tpm_chip *tpm, u8 tpm_register,
+		    u8 *tpm_data, u16 tpm_size)
+{
+	u8 data;
+	int i = 0, j;
+	int value = 0;
+	struct spi_device *dev = (struct spi_device *) tpm->vendor.iobase;
+	struct st33zp24_platform_data *platform_data = dev->dev.platform_data;
+	u8 *data_buffer = platform_data->tpm_spi_buffer[0];
+	struct spi_transfer xfer = {
+		.tx_buf	 = data_buffer,
+		.rx_buf	 = platform_data->tpm_spi_buffer[1],
+	};
+	struct spi_message msg;
+
+	u8 latency = platform_data->latency + tpm_size + 1;
+
+	FUNC_ENTER();
+	/* Pre-Header */
+
+	data = tpm->vendor.locality;
+	memcpy(data_buffer + i, &data, sizeof(data));
+	i++;
+	data = tpm_register;
+	memcpy(data_buffer + i, &data, sizeof(data));
+	i++;
+
+	for (j = 0 ; j < latency; j++) {
+		data = TPM_DUMMY_BYTE;
+		memcpy(data_buffer + i, &data, sizeof(data));
+		i++;
+	}
+	xfer.len = i;
+
+	/* header + status byte + size of the data + status byte */
+	spi_message_init(&msg);
+	spi_message_add_tail(&xfer, &msg);
+	value = spi_sync(dev, &msg);
+	if (tpm_size > 0 && value == 0) {
+		j = 2;
+		for (; j < i && ((u8 *) xfer.rx_buf)[j] == 0; j++)
+			;
+
+		if (j + 1 < i) {
+			value = ((u8 *) xfer.rx_buf)[j];
+
+			if (tpm_size > 0)
+				memcpy(tpm_data, xfer.rx_buf + j + 1,
+								 tpm_size);
+		}
+	}
+	return value;
+} /* read8_reg() */
+
+/*
+ * SPI_WRITE_DATA
+ * Send byte to the TIS register according to the ST33ZP24 SPI protocol.
+ * @param: tpm, 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.
+ */
+static u32 SPI_WRITE_DATA(struct tpm_chip *tpm, u8 tpm_register,
+			  u8 *tpm_data, u16 tpm_size)
+{
+	u8 value = 0;
+
+	FUNC_ENTER();
+
+	value = write8_reg(tpm, tpm_register, tpm_data, tpm_size);
+
+	switch (value) {
+	case TPM_ST_SPI_OK:
+		return TPM_ST_SPI_OK;
+	case 0:
+		return 0;
+	default:
+		return -EBUSY;
+	}
+} /* SPI_WRITE_DATA() */
+
+/*
+ * SPI_READ_DATA
+ * Recv byte from the TIS register according to the ST33ZP24 SPI 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.
+ */
+static u32 SPI_READ_DATA(struct tpm_chip *tpm, u8 tpm_register,
+			u8 *tpm_data, u16 tpm_size)
+{
+	u8 value = 0;
+
+	FUNC_ENTER();
+
+	value = read8_reg(tpm, tpm_register, tpm_data, tpm_size);
+
+	switch (value) {
+	case TPM_ST_SPI_OK:
+		return TPM_ST_SPI_OK;
+	case 0:
+		return 0;
+	default:
+		return -EBUSY;
+	}
+} /* SPI_READ_DATA () */
+
+/*
+ * clear_interruption
+ * clear the TPM interrupt register.
+ * @param: tpm, the chip description
+ */
+static void clear_interruption(struct tpm_chip *tpm)
+{
+	u8 interrupt;
+	SPI_READ_DATA(tpm, TPM_INT_STATUS, &interrupt, 1);
+	/* Clear interrupts handled with TPM_EOI */
+	SPI_WRITE_DATA(tpm, TPM_INT_STATUS, &interrupt, 1);
+	SPI_READ_DATA(tpm, TPM_INT_STATUS, &interrupt, 1);
+} /* clear_interruption() */
+
+/*
+ * _wait_for_interrupt_serirq_timeout
+ * @param: tpm, the chip description
+ * @param: timeout, the timeout of the interrupt
+ * @return: the status of the interruption.
+ */
+static int _wait_for_interrupt_serirq_timeout(struct tpm_chip *chip,
+					unsigned long timeout)
+{
+	unsigned long status;
+	struct spi_device *client;
+	struct st33zp24_platform_data *pin_infos;
+
+	FUNC_ENTER();
+
+	client = (struct spi_device *) chip->vendor.iobase;
+	pin_infos = client->dev.platform_data;
+
+	status = wait_for_completion_interruptible_timeout(
+				&pin_infos->irq_detection, timeout);
+	if (status > 0)
+		enable_irq(gpio_to_irq(pin_infos->io_serirq));
+	gpio_direction_input(pin_infos->io_serirq);
+
+	return status;
+} /* wait_for_interrupt_serirq_timeout() */
+
+unsigned long wait_for_serirq_timeout(struct tpm_chip *chip, bool condition,
+				 unsigned long timeout)
+{
+	unsigned long status = 0;
+	struct spi_device *client;
+	struct st33zp24_platform_data *pin_infos;
+
+	client = (struct spi_device *) chip->vendor.iobase;
+	pin_infos = client->dev.platform_data;
+
+	status = _wait_for_interrupt_serirq_timeout(chip, timeout);
+	if (!status) {
+		status = -EBUSY;
+		goto wait_end;
+	}
+	clear_interruption(chip);
+	if (condition)
+		status = 1;
+
+wait_end:
+	return status;
+}
+
+
+/*
+ * tpm_stm_spi_cancel, cancel is not implemented.
+ * @param: chip, the tpm chip description as specified in
+ * driver/char/tpm/tpm.h.
+ */
+static void tpm_stm_spi_cancel(struct tpm_chip *chip)
+{
+	struct spi_device *client;
+	u8 data = TPM_STS_COMMAND_READY;
+	client = (struct spi_device *) chip->vendor.iobase;
+
+	FUNC_ENTER();
+
+	/* this causes the current command to be aborted */
+	SPI_WRITE_DATA(chip, TPM_STS, &data, 1);
+} /* tpm_stm_spi_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_spi_status(struct tpm_chip *chip)
+{
+	struct spi_device *client;
+	u8 data;
+	client = (struct spi_device *) chip->vendor.iobase;
+
+	FUNC_ENTER();
+
+	SPI_READ_DATA(chip, TPM_STS, &data, 1);
+	return data;
+} /* tpm_stm_spi_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)
+{
+	struct spi_device *client;
+	u8 data;
+	u8 status;
+
+	FUNC_ENTER();
+
+	client = (struct spi_device *) chip->vendor.iobase;
+
+	status = SPI_READ_DATA(chip, TPM_ACCESS, &data, 1);
+	if (status && (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 stop;
+	long rc;
+	u8 data;
+	struct spi_device *client;
+
+	FUNC_ENTER();
+
+	client = (struct spi_device *) chip->vendor.iobase;
+	/* Check locality */
+	if (check_locality(chip) == chip->vendor.locality)
+		return chip->vendor.locality;
+
+	/* Request locality */
+	data = TPM_ACCESS_REQUEST_USE;
+	rc = SPI_WRITE_DATA(chip, TPM_ACCESS, &data, 1);
+	if (rc < 0)
+		goto end;
+
+	/* wait for burstcount */
+	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 {
+		stop = jiffies + chip->vendor.timeout_a;
+		do {
+			if (check_locality(chip) >= 0)
+				return chip->vendor.locality;
+			msleep(TPM_TIMEOUT);
+		} while (time_before(jiffies, stop));
+	}
+end:
+	return -EACCES;
+} /* request_locality() */
+
+/*
+ * release_locality release the active locality
+ * @param: chip, the tpm chip description.
+ */
+static void release_locality(struct tpm_chip *chip)
+{
+	struct spi_device *client;
+	u8 data;
+
+	FUNC_ENTER();
+
+	client = (struct spi_device *) chip->vendor.iobase;
+	data = TPM_ACCESS_ACTIVE_LOCALITY;
+	SPI_WRITE_DATA(chip, TPM_ACCESS, &data, 1);
+} /* release_locality()*/
+
+/*
+ * 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 stop;
+	u32 burstcnt, status;
+	u8 tpm_reg, temp;
+
+	FUNC_ENTER();
+
+	/* wait for burstcount */
+	/* which timeout value, spec has 2 answers (c & d) */
+	stop = jiffies + chip->vendor.timeout_d;
+	do {
+		tpm_reg = TPM_STS + 1;
+		status = SPI_READ_DATA(chip, tpm_reg, &temp, 1);
+		if (status < 0)
+			goto end;
+
+		tpm_reg = tpm_reg + 1;
+		burstcnt = temp;
+		status = SPI_READ_DATA(chip, tpm_reg, &temp, 1);
+		if (status < 0)
+			goto end;
+
+		burstcnt |= temp << 8;
+		if (burstcnt)
+			return burstcnt;
+		msleep(TPM_TIMEOUT);
+	} while (time_before(jiffies, stop));
+
+end:
+	return -EBUSY;
+} /* get_burstcount() */
+
+/*
+ * wait_for_stat wait for a TPM_STS value
+ * @param: chip, the tpm chip description
+ * @param: mask, the value mask to wait
+ * @param: timeout, the timeout
+ * @param: queue, the wait queue.
+ * @return: the tpm status, 0 if success, -ETIME if timeout is reached.
+ */
+static int wait_for_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout,
+			 wait_queue_head_t *queue)
+{
+	unsigned long stop;
+	long rc;
+	u8 status;
+
+	FUNC_ENTER();
+
+	/* check current status */
+	status = tpm_stm_spi_status(chip);
+	if (!chip->vendor.irq && (status & mask) == mask)
+		return 0;
+
+	if (chip->vendor.irq) {
+		rc = wait_for_serirq_timeout(chip, ((tpm_stm_spi_status
+						    (chip) & mask) ==
+						     mask), timeout);
+		if (rc > 0)
+			return 0;
+	} else {
+		stop = jiffies + timeout;
+		do {
+			msleep(TPM_TIMEOUT);
+			status = tpm_stm_spi_status(chip);
+			if ((status & mask) == mask)
+				return 0;
+		} while (time_before(jiffies, stop));
+	}
+	return -ETIME;
+} /* wait_for_stat() */
+
+/*
+ * 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 tpm status, 0 if success, -ETIME if timeout is reached.
+ */
+static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count)
+{
+	u32 size = 0, burstcnt, status, len;
+	struct spi_device *client;
+
+	FUNC_ENTER();
+
+	client = (struct spi_device *) chip->vendor.iobase;
+
+	while (size < count &&
+	       wait_for_stat(chip,
+			     TPM_STS_DATA_AVAIL | TPM_STS_VALID,
+			     chip->vendor.timeout_c,
+			     &chip->vendor.read_queue)
+						== 0) {
+		burstcnt = get_burstcount(chip);
+		len = min_t(int, burstcnt, count - size);
+		status = SPI_READ_DATA(chip, TPM_DATA_FIFO, buf + size, len);
+		if (status < 0)
+			return status;
+
+
+		size += len;
+	}
+	return size;
+}
+
+/*
+ * tpm_ioserirq_handler the serirq irq handler
+ * @param: irq, the tpm chip description
+ * @param: dev_id, the description of the chip
+ * @return: the status of the handler.
+ */
+static irqreturn_t tpm_ioserirq_handler(int irq, void *dev_id)
+{
+	struct tpm_chip *chip = dev_id;
+	struct spi_device *client;
+	struct st33zp24_platform_data *pin_infos;
+
+	disable_irq_nosync(irq);
+	FUNC_ENTER();
+
+	client = (struct spi_device *) chip->vendor.iobase;
+	pin_infos = client->dev.platform_data;
+
+	complete(&pin_infos->irq_detection);
+	return IRQ_HANDLED;
+} /* tpm_ioserirq_handler() */
+
+/*
+ * tpm_stm_spi_send send TPM commands through the SPI bus.
+ * @param: chip, the tpm chip description
+ * @param: buf, the tpm command buffer
+ * @param: len, the tpm command size
+ * @return: In case of success the number of bytes sent.
+ *			In other case, a < 0 value describing the issue.
+ */
+static int tpm_stm_spi_send(struct tpm_chip *chip, unsigned char *buf,
+			    size_t len)
+{
+	u32 ret = 0, ordinal,
+	    status, burstcnt = 0, i, size = 0;
+	u8 data;
+
+	struct spi_device *client;
+	struct st33zp24_platform_data *pin_infos;
+
+	FUNC_ENTER();
+
+	if (chip == NULL)
+		return -EBUSY;
+	if (len < TPM_HEADER_SIZE)
+		return -EBUSY;
+
+	client = (struct spi_device *)chip->vendor.iobase;
+	pin_infos = client->dev.platform_data;
+
+	ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));
+
+	ret = request_locality(chip);
+	if (ret < 0)
+		return ret;
+
+	status = tpm_stm_spi_status(chip);
+	if ((status & TPM_STS_COMMAND_READY) == 0) {
+		tpm_stm_spi_cancel(chip);
+		if (wait_for_stat
+		    (chip, TPM_STS_COMMAND_READY, chip->vendor.timeout_b,
+		     &chip->vendor.int_queue) < 0) {
+			ret = -ETIME;
+			goto out_err;
+		}
+	}
+
+	for (i = 0 ; i < len - 1 ;) {
+		burstcnt = get_burstcount(chip);
+		size = min_t(int, len - i - 1, burstcnt);
+		ret = SPI_WRITE_DATA(chip, TPM_DATA_FIFO, buf, size);
+		if (ret < 0)
+			goto out_err;
+		i += size;
+	}
+
+	status = tpm_stm_spi_status(chip);
+	if ((status & TPM_STS_DATA_EXPECT) == 0) {
+		ret = -EIO;
+		goto out_err;
+	}
+
+	/* write last byte */
+	SPI_WRITE_DATA(chip, TPM_DATA_FIFO, buf + len - 1, 1);
+
+	status = tpm_stm_spi_status(chip);
+	if ((status & TPM_STS_DATA_EXPECT) != 0) {
+		ret = -EIO;
+		goto out_err;
+	}
+
+	/* go and do it */
+	data = TPM_STS_GO;
+	ret = SPI_WRITE_DATA(chip, TPM_STS, &data, 1);
+	if (ret < 0)
+		goto out_err;
+
+	return len;
+out_err:
+	tpm_stm_spi_cancel(chip);
+	release_locality(chip);
+	return ret;
+}
+
+/*
+ * tpm_stm_spi_recv received TPM response through the SPI bus.
+ * @param: chip, the tpm chip description
+ * @param: buf, the tpm command buffer
+ * @param: len, the tpm command size
+* @return: In case of success the number of bytes received.
+ *			In other case, a < 0 value describing the issue.
+ */
+static int tpm_stm_spi_recv(struct tpm_chip *chip, unsigned char *buf,
+			    size_t count)
+{
+	int size = 0;
+	int expected;
+
+	struct spi_device *client;
+	struct st33zp24_platform_data *pin_infos;
+
+
+	FUNC_ENTER();
+
+	client = (struct spi_device *)chip->vendor.iobase;
+	pin_infos = client->dev.platform_data;
+
+
+	if (chip == NULL)
+		return -EBUSY;
+	if (count < TPM_HEADER_SIZE) {
+		size = -EIO;
+		goto out;
+	}
+
+	size = recv_data(chip, buf, TPM_HEADER_SIZE);
+
+	/* read first 10 bytes, including tag, paramsize, and result */
+	if (size < TPM_HEADER_SIZE) {
+		dev_err(chip->dev, "Unable to read header\n");
+		goto out;
+	}
+
+	expected = be32_to_cpu(*(__be32 *) (buf + 2));
+	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;
+}
+
+
+static const struct file_operations tpm_st33_spi_fops = {
+	.owner = THIS_MODULE,
+	.llseek = no_llseek,
+	.read = tpm_read,
+	.write = tpm_write,
+	.open = tpm_open,
+	.release = tpm_release,
+};
+
+static DEVICE_ATTR(pubek, S_IRUGO, tpm_show_pubek, NULL);
+static DEVICE_ATTR(pcrs, S_IRUGO, tpm_show_pcrs, NULL);
+static DEVICE_ATTR(enabled, S_IRUGO, tpm_show_enabled, NULL);
+static DEVICE_ATTR(active, S_IRUGO, tpm_show_active, NULL);
+static DEVICE_ATTR(owned, S_IRUGO, tpm_show_owned, NULL);
+static DEVICE_ATTR(temp_deactivated, S_IRUGO, tpm_show_temp_deactivated, NULL);
+static DEVICE_ATTR(caps, S_IRUGO, tpm_show_caps_1_2, NULL);
+static DEVICE_ATTR(cancel, S_IWUSR | S_IWGRP, NULL, tpm_store_cancel);
+
+static struct attribute *stm_tpm_attrs[] = {
+	&dev_attr_pubek.attr,
+	&dev_attr_pcrs.attr,
+	&dev_attr_enabled.attr,
+	&dev_attr_active.attr,
+	&dev_attr_owned.attr,
+	&dev_attr_temp_deactivated.attr,
+	&dev_attr_caps.attr,
+	&dev_attr_cancel.attr, NULL,
+};
+
+static struct attribute_group stm_tpm_attr_grp = {
+	.attrs = stm_tpm_attrs
+};
+
+static struct tpm_vendor_specific st_spi_tpm = {
+	.send = tpm_stm_spi_send,
+	.recv = tpm_stm_spi_recv,
+	.cancel = tpm_stm_spi_cancel,
+	.status = tpm_stm_spi_status,
+	.attr_group = &stm_tpm_attr_grp,
+	.miscdev = {.fops = &tpm_st33_spi_fops,},
+};
+
+static int evaluate_latency(struct tpm_chip *chip)
+{
+	int latency = 0;
+	struct spi_device *dev = (struct spi_device *) chip->vendor.iobase;
+	struct st33zp24_platform_data *platform_data = dev->dev.platform_data;
+	int status = 0;
+	u8 data;
+
+	for (; status == 0x00 && latency < MAX_SPI_LATENCY ; latency++) {
+		platform_data->latency = latency;
+		status = SPI_READ_DATA(chip, TPM_INTF_CAPABILITY, &data, 1);
+	}
+	return latency - 1;
+} /* evaluate_latency() */
+
+static int interrupts;
+module_param(interrupts, int, 0444);
+MODULE_PARM_DESC(interrupts, "Enable interrupts");
+
+static int power_mgt = 1;
+module_param(power_mgt, int, 0444);
+MODULE_PARM_DESC(power_mgt, "Power Management");
+
+/*
+ * tpm_st33_spi_probe initialize the TPM device
+ * @param: client, the spi_device drescription (TPM SPI description).
+ * @param: id, the spi_device_id struct.
+ * @return: 0 in case of success.
+ *	 -1 in other case.
+ */
+static int
+tpm_st33_spi_probe(struct spi_device *dev)
+{
+	u32 err;
+	u8 intmask;
+	struct tpm_chip *chip;
+	struct st33zp24_platform_data *platform_data;
+
+	FUNC_ENTER();
+
+	err = 0;
+
+	/* Check SPI platform functionnalities */
+	if (dev == NULL) {
+		pr_info("dev is NULL. exiting.\n");
+		err = -ENODEV;
+		goto end;
+	}
+
+	chip = tpm_register_hardware(&dev->dev, &st_spi_tpm);
+	if (!chip) {
+		err = -ENODEV;
+		goto end;
+	}
+
+	/* Allocation of SPI buffers MISO and MOSI		*/
+	/* Size is as follow:					*/
+	/* Request burstcount value  = 0x800 = 2048		*/
+	/* +							*/
+	/* Response burstcount value = 0x400 = 1024		*/
+	/* +							*/
+	/* At least:						*/
+	/* 1 byte for direction/locality			*/
+	/* 1 byte tpm tis register				*/
+	/* 2 bytes spi data length (for request only)		*/
+	/* 2 latency bytes					*/
+	/* 1 status byte					*/
+	/* = 2048 + 1024 + 7					*/
+	/* We reserved 2048 + 1024 + 20 in case latency byte	*/
+	/* change						*/
+	platform_data = dev->dev.platform_data;
+	platform_data->tpm_spi_buffer[0] =
+	    kmalloc((TPM_BUFSIZE + (TPM_BUFSIZE / 2) +
+				 TPM_DIGEST_SIZE) * sizeof(u8), GFP_KERNEL);
+	if (platform_data->tpm_spi_buffer[0] == NULL) {
+		err = -ENOMEM;
+		goto _tpm_clean_answer;
+	}
+	platform_data->tpm_spi_buffer[1] =
+	    kmalloc((TPM_BUFSIZE + (TPM_BUFSIZE / 2) +
+				 TPM_DIGEST_SIZE) * sizeof(u8) , GFP_KERNEL);
+	if (platform_data->tpm_spi_buffer[1] == NULL) {
+		err = -ENOMEM;
+		goto _tpm_clean_response;
+	}
+
+	chip->vendor.iobase = dev;
+
+	/* Default timeouts */
+	chip->vendor.timeout_a = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
+	chip->vendor.timeout_b = msecs_to_jiffies(TIS_LONG_TIMEOUT);
+	chip->vendor.timeout_c = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
+	chip->vendor.timeout_d = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
+	chip->vendor.locality = LOCALITY0;
+
+	if (power_mgt) {
+		err = gpio_request(platform_data->io_lpcpd, "TPM IO_LPCPD");
+		if (err)
+			goto _gpio_init1;
+		gpio_set_value(platform_data->io_lpcpd, 1);
+	}
+
+	platform_data->latency = evaluate_latency(chip);
+	if (platform_data->latency <= 0x00) {
+		err = -ENODEV;
+		goto _gpio_init1;
+	}
+
+	/* Enable interrupt */
+	/* Register GPIO pin through generic Linux GPIO API */
+	if (interrupts) {
+		init_completion(&platform_data->irq_detection);
+		if (request_locality(chip) != LOCALITY0) {
+			err = -ENODEV;
+			goto _tpm_clean_response;
+		}
+		err = gpio_request(platform_data->io_serirq, "TPM IO_SERIRQ");
+		if (err)
+			goto _gpio_init2;
+
+		/* Clear all existing */
+		clear_interruption(chip);
+		err = request_irq(gpio_to_irq(platform_data->io_serirq),
+							&tpm_ioserirq_handler,
+				IRQF_TRIGGER_HIGH | IRQF_SHARED,
+				"TPM SERIRQ management", chip);
+		if (err < 0) {
+
+			pr_info("TPM SERIRQ signals %d not available\n",
+			gpio_to_irq(platform_data->io_serirq));
+			goto _irq_set;
+		}
+		err = SPI_READ_DATA(chip, TPM_INT_ENABLE, &intmask, 1);
+		if (err < 0)
+			goto _irq_set;
+
+		intmask |= TPM_INTF_CMD_READY_INT
+			|  TPM_INTF_FIFO_AVALAIBLE_INT
+			|  TPM_INTF_WAKE_UP_READY_INT
+			|  TPM_INTF_LOC4SOFTRELEASE_INT
+			|  TPM_INTF_LOCALITY_CHANGE_INT
+			|  TPM_INTF_STS_VALID_INT
+			|  TPM_INTF_DATA_AVAIL_INT;
+
+		err = SPI_WRITE_DATA(chip, TPM_INT_ENABLE, &intmask, 1);
+		if (err < 0)
+			goto _irq_set;
+
+		intmask = TPM_GLOBAL_INT_ENABLE;
+		err = SPI_WRITE_DATA(chip, TPM_INT_ENABLE + 3, &intmask, 1);
+		if (err < 0)
+			goto _irq_set;
+
+		err = SPI_READ_DATA(chip, TPM_INT_STATUS, &intmask, 1);
+		if (err < 0)
+			goto _irq_set;
+
+		chip->vendor.irq = interrupts;
+
+		tpm_gen_interrupt(chip);
+	}
+
+	tpm_get_timeouts(chip);
+
+	/* attach chip datas to client */
+	spi_set_drvdata(dev, chip);
+	platform_data->bchipf = false;
+
+	pr_info("TPM SPI Initialized\n");
+	return 0;
+_irq_set:
+	free_irq(gpio_to_irq(platform_data->io_serirq), chip);
+_gpio_init2:
+	if (platform_data && interrupts)
+		gpio_free(platform_data->io_serirq);
+_gpio_init1:
+	if (platform_data && power_mgt)
+		gpio_free(platform_data->io_lpcpd);
+_tpm_clean_response:
+	tpm_remove_hardware(chip->dev);
+	if (platform_data->tpm_spi_buffer[1] != NULL) {
+		kfree(platform_data->tpm_spi_buffer[1]);
+		platform_data->tpm_spi_buffer[1] = NULL;
+	}
+_tpm_clean_answer:
+	if (platform_data->tpm_spi_buffer[0] != NULL) {
+		kfree(platform_data->tpm_spi_buffer[0]);
+		platform_data->tpm_spi_buffer[0] = NULL;
+	}
+
+	platform_data->bchipf = true;
+end:
+	pr_info("TPM SPI initialisation fail\n");
+	return err;
+}
+
+/*
+ * tpm_st33_spi_remove remove the TPM device
+ * @param: client, the spi_device drescription (TPM SPI description).
+		clear_bit(0, &chip->is_open);
+ * @return: 0 in case of success.
+ */
+static int tpm_st33_spi_remove(struct spi_device *client)
+{
+	struct tpm_chip *chip = (struct tpm_chip *)spi_get_drvdata(client);
+	struct st33zp24_platform_data *pin_infos =
+		((struct spi_device *) chip->vendor.iobase)->dev.platform_data;
+
+	FUNC_ENTER();
+
+	if (pin_infos != NULL) {
+		gpio_free(pin_infos->io_lpcpd);
+
+		/* Check if chip has been previously clean */
+		if (pin_infos->bchipf != true)
+			tpm_remove_hardware(chip->dev);
+		if (pin_infos->tpm_spi_buffer[1] != NULL) {
+			kfree(pin_infos->tpm_spi_buffer[1]);
+			pin_infos->tpm_spi_buffer[1] = NULL;
+		}
+		if (pin_infos->tpm_spi_buffer[0] != NULL) {
+			kfree(pin_infos->tpm_spi_buffer[0]);
+			pin_infos->tpm_spi_buffer[0] = NULL;
+		}
+	}
+
+	return 0;
+}
+
+/*
+ * tpm_st33_spi_pm_suspend suspend the TPM device
+ * Added: Work around when suspend and no tpm application is running, suspend
+ * may fail because chip->data_buffer is not set (only set in tpm_open in Linux
+ * TPM core)
+ * @param: client, the spi_device drescription (TPM SPI description).
+ * @param: mesg, the power management message.
+ * @return: 0 in case of success.
+ */
+static int tpm_st33_spi_pm_suspend(struct spi_device *client,
+							 pm_message_t mesg)
+{
+	struct tpm_chip *chip =
+	    (struct tpm_chip *)spi_get_drvdata(client);
+	struct st33zp24_platform_data *pin_infos =
+		((struct spi_device *)chip->vendor.iobase)->dev.platform_data;
+	int ret = 0;
+
+	if (power_mgt)
+		gpio_set_value(pin_infos->io_lpcpd, 0);
+	else {
+		if (chip->data_buffer == NULL)
+			chip->data_buffer = pin_infos->tpm_spi_buffer[0];
+		ret = tpm_pm_suspend(&client->dev);
+	}
+	return ret;
+}				/* tpm_st33_spi_suspend() */
+
+/*
+ * tpm_st33_spi_pm_resume resume the TPM device
+ * @param: spi, the spi_device drescription (TPM SPI description).
+ * @return: 0 in case of success.
+ */
+static int tpm_st33_spi_pm_resume(struct spi_device *spi)
+{
+	struct tpm_chip *chip =
+	    (struct tpm_chip *)spi_get_drvdata(spi);
+	struct st33zp24_platform_data *pin_infos =
+		((struct spi_device *)chip->vendor.iobase)->dev.platform_data;
+
+	int ret = 0;
+	if (power_mgt) {
+		gpio_set_value(pin_infos->io_lpcpd, 1);
+		ret = wait_for_serirq_timeout(chip,
+					 (chip->vendor.status(chip) &&
+					  TPM_STS_VALID) == TPM_STS_VALID,
+					  chip->vendor.timeout_b);
+	} else {
+		if (chip->data_buffer == NULL)
+			chip->data_buffer = pin_infos->tpm_spi_buffer[0];
+		ret = tpm_pm_resume(&spi->dev);
+		if (!ret)
+			tpm_do_selftest(chip);
+	}
+	return ret;
+}				/* tpm_st33_spi_pm_resume() */
+
+static struct spi_driver tpm_st33_spi_driver = {
+	.driver = {
+		   .owner = THIS_MODULE,
+		   .name = TPM_ST33_SPI,
+		   },
+	.probe = tpm_st33_spi_probe,
+	.remove = tpm_st33_spi_remove,
+	.resume = tpm_st33_spi_pm_resume,
+	.suspend = tpm_st33_spi_pm_suspend,
+};
+
+/*
+ * tpm_st33_spi_init initialize driver
+ * @return: 0 if successful, else non zero value.
+ */
+static int __init tpm_st33_spi_init(void)
+{
+	FUNC_ENTER();
+	return spi_register_driver(&tpm_st33_spi_driver);
+}
+
+/*
+ * tpm_st33_spi_exit The kernel calls this function during unloading the
+ * module or during shut down process
+ */
+static void __exit tpm_st33_spi_exit(void)
+{
+	FUNC_ENTER();
+	spi_unregister_driver(&tpm_st33_spi_driver);
+}
+
+module_init(tpm_st33_spi_init);
+module_exit(tpm_st33_spi_exit);
+
+MODULE_AUTHOR("Christophe Ricard (tpmsupport@st.com)");
+MODULE_DESCRIPTION("STM TPM SPI ST33 Driver");
+MODULE_VERSION("1.2.0");
+MODULE_LICENSE("GPL");
diff --git a/drivers/char/tpm/tpm_spi_stm_st33.h b/drivers/char/tpm/tpm_spi_stm_st33.h
new file mode 100644
index 0000000..42bae6f
--- /dev/null
+++ b/drivers/char/tpm/tpm_spi_stm_st33.h
@@ -0,0 +1,75 @@
+/*
+ * STMicroelectronics TPM SPI Linux driver for TPM ST33NP18
+ * Copyright (C) 2009, 2010  STMicroelectronics
+ *
+ * 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.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * STMicroelectronics version 1.2.0, Copyright (C) 2010
+ * STMicroelectronics comes with ABSOLUTELY NO WARRANTY.
+ * This is free software, and you are welcome to redistribute it
+ * under certain conditions.
+ *
+ * @Author: Christophe RICARD tpmsupport@st.com
+ *
+ * @File: tpm_spi_stm_st33.h
+ *
+ * @Date: 09/15/2010
+ */
+#ifndef __STM_ST33_TPM_SPI_MAIN_H__
+#define __STM_ST33_TPM_SPI_MAIN_H__
+
+#include <linux/pci.h>
+#include <linux/module.h>
+#include <linux/platform_device.h>
+#include <linux/fs.h>
+#include <linux/miscdevice.h>
+
+#define MINOR_NUM_SPI		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			0x00
+#define TPM_WRITE_DIRECTION		0x80
+#define TPM_HEADER_SIZE			10
+#define TPM_BUFSIZE			2048
+
+/* ioctl commands */
+#define TPMIOC_CANCEL		_IO('T', 0x00)
+#define TPMIOC_TRANSMIT		_IO('T', 0x01)
+#define TPMIOC_HASH		_IO('T', 0x02)
+#define TPMIOC_CHANGELOCALITY	_IO('T', 0x03)
+
+#define LOCALITY0		0
+#define LOCALITY4		4
+
+#define TPM_ST_SPI_OK		0x5A
+
+
+#define MAX_SPI_LATENCY		15
+
+struct st_tpm_hash_t {
+	int size;
+	u8 *data;
+};
+
+#endif /* __STM_ST33_TPM_SPI_MAIN_H__ */
diff --git a/include/linux/spi/tpm_spi_stm_st33.h b/include/linux/spi/tpm_spi_stm_st33.h
new file mode 100644
index 0000000..c233ff6
--- /dev/null
+++ b/include/linux/spi/tpm_spi_stm_st33.h
@@ -0,0 +1,46 @@
+/*
+* STMicroelectronics TPM SPI Linux driver for TPM ST33NP18
+* Copyright (C) 2009, 2010 STMicroelectronics
+* Christophe RICARD tpmsupport@st.com
+* 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.,
+* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*
+* STMicroelectronics version 1.2.0, Copyright (C) 2010
+* STMicroelectronics comes with ABSOLUTELY NO WARRANTY.
+* This is free software, and you are welcome to redistribute it
+* under certain conditions.
+*
+* @File: tpm_spi_stm_st33.h
+*
+* @Date: 06/15/2008
+*/
+#ifndef __STM_ST33_TPM_SPI_H__
+#define __STM_ST33_TPM_SPI_H__
+
+#include <linux/spi/spi.h>
+
+#define TPM_ST33_SPI		"st33zp24_spi"
+
+#ifndef __STM_ST33_TPM_SPI_H__
+struct st33zp24_platform_data {
+	int io_serirq;
+	int io_lpcpd;
+	int latency;
+	bool bchipf;
+	u8 *tpm_spi_buffer[2]; /* 0 Request 1 Response */
+	struct completion irq_detection;
+};
+#endif
+
+#endif /* __STM_ST33_TPM_SPI_H__ */
-- 
1.7.1


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

* [PATCH 1/1] TPM: STMicroelectronics st33 driver SPI
@ 2013-04-08 14:02 Mathias Leblanc
  0 siblings, 0 replies; 21+ messages in thread
From: Mathias Leblanc @ 2013-04-08 14:02 UTC (permalink / raw)
  To: Kent Yoder, Rajiv Andrade, Marcel Selhorst, Sirrix AG, tpmdd-devel
  Cc: linux-kernel, Jean-Luc Blanc, Mathias Leblanc

 * STMicroelectronics version 1.2.0, Copyright (C) 2010
 * 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/SPI, in menuconfig or .config choose the tpm driver on
device --> tpm and activate the protocol of your choice before compiling
the kernel.
The driver will be accessible from within Linux.

Tested on linux x86/x64 and beagleboard REV B & XM REV C

Signed-off-by: Mathias Leblanc <mathias.leblanc@st.com>
---
 arch/arm/mach-omap2/board-omap3beagle.c |   40 ++
 drivers/char/tpm/Kconfig                |   10 +
 drivers/char/tpm/Makefile               |    1 +
 drivers/char/tpm/tpm_spi_stm_st33.c     | 1116 +++++++++++++++++++++++++++++++
 drivers/char/tpm/tpm_spi_stm_st33.h     |   75 ++
 include/linux/spi/tpm_spi_stm_st33.h    |   46 ++
 6 files changed, 1287 insertions(+), 0 deletions(-)
 create mode 100644 drivers/char/tpm/tpm_spi_stm_st33.c
 create mode 100644 drivers/char/tpm/tpm_spi_stm_st33.h
 create mode 100644 include/linux/spi/tpm_spi_stm_st33.h

diff --git a/arch/arm/mach-omap2/board-omap3beagle.c b/arch/arm/mach-omap2/board-omap3beagle.c
index c3558f9..72e6513 100644
--- a/arch/arm/mach-omap2/board-omap3beagle.c
+++ b/arch/arm/mach-omap2/board-omap3beagle.c
@@ -46,6 +46,11 @@
 #include <video/omap-panel-tfp410.h>
 #include <linux/platform_data/mtd-nand-omap2.h>
 
+#ifdef CONFIG_TCG_ST33_SPI
+#include <linux/spi/tpm_spi_stm_st33.h>
+#include <linux/spi/spi.h>
+#endif /* CONFIG_TCG_ST33_SPI */
+
 #include "common.h"
 #include "omap_device.h"
 #include "gpmc.h"
@@ -375,6 +380,26 @@ static struct i2c_board_info __initdata beagle_i2c_eeprom[] = {
        },
 };
 
+#if defined CONFIG_TCG_ST33_I2C || defined CONFIG_TCG_ST33_SPI
+static struct st33zp24_platform_data tpm_data = {
+	.io_serirq = 136,
+	.io_lpcpd  = 143,
+};
+#endif
+
+#ifdef CONFIG_TCG_ST33_SPI
+static struct  spi_board_info tpm_st33_spi_board_info[] = {
+	{
+		.modalias = TPM_ST33_SPI,
+		.max_speed_hz = 10000000,
+		.bus_num = 4,
+		.chip_select = 0,
+		.mode = SPI_MODE_0,
+		.platform_data = &tpm_data,
+	},
+};
+#endif
+
 static int __init omap3_beagle_i2c_init(void)
 {
 	omap3_pmic_get_config(&beagle_twldata,
@@ -556,6 +581,20 @@ static void __init omap3_beagle_init(void)
 	omap_mux_init_signal("sdrc_cke0", OMAP_PIN_OUTPUT);
 	omap_mux_init_signal("sdrc_cke1", OMAP_PIN_OUTPUT);
 
+	#if defined CONFIG_TCG_ST33_I2C || defined CONFIG_TCG_ST33_SPI
+		omap_mux_init_gpio(tpm_data.io_serirq, OMAP_PIN_INPUT);
+		omap_mux_init_gpio(tpm_data.io_lpcpd, OMAP_PIN_OUTPUT);
+	#endif
+
+	#ifdef CONFIG_TCG_ST33_SPI
+		omap_mux_init_signal("mcspi4_clk", OMAP_PIN_INPUT);
+		omap_mux_init_signal("mcspi4_cs0", OMAP_PIN_OUTPUT);
+		omap_mux_init_signal("mcspi4_simo", OMAP_PIN_OUTPUT);
+		omap_mux_init_signal("mcspi4_somi", OMAP_PIN_INPUT);
+
+		spi_register_board_info(tpm_st33_spi_board_info,
+			 ARRAY_SIZE(tpm_st33_spi_board_info));
+	#endif
+
 	pwm_add_table(pwm_lookup, ARRAY_SIZE(pwm_lookup));
 }
 
diff --git a/drivers/char/tpm/Kconfig b/drivers/char/tpm/Kconfig
index dbfd564..2fc1254 100644
--- a/drivers/char/tpm/Kconfig
+++ b/drivers/char/tpm/Kconfig
@@ -91,4 +91,14 @@ config TCG_ST33_I2C
         To compile this driver as a module, choose M here; the module will be
         called tpm_stm_st33_i2c.
 
+config TCG_ST33_SPI
+       tristate "STMicroelectronics ST33 SPI"
+       depends on SPI
+       depends on GPIOLIB
+       ---help---
+       If you have a TPM security chip from STMicroelectronics working with
+       an SPI bus say Yes and it will be accessible from within Linux.
+       To compile this driver as a module, choose M here; the module will be
+       called tpm_stm_st33_spi.
+
 endif # TCG_TPM
diff --git a/drivers/char/tpm/Makefile b/drivers/char/tpm/Makefile
index a3736c9..a1db3dd 100644
--- a/drivers/char/tpm/Makefile
+++ b/drivers/char/tpm/Makefile
@@ -18,3 +18,4 @@ obj-$(CONFIG_TCG_ATMEL) += tpm_atmel.o
 obj-$(CONFIG_TCG_INFINEON) += tpm_infineon.o
 obj-$(CONFIG_TCG_IBMVTPM) += tpm_ibmvtpm.o
 obj-$(CONFIG_TCG_ST33_I2C) += tpm_i2c_stm_st33.o
+obj-$(CONFIG_TCG_ST33_SPI) += tpm_spi_stm_st33.o
diff --git a/drivers/char/tpm/tpm_spi_stm_st33.c b/drivers/char/tpm/tpm_spi_stm_st33.c
new file mode 100644
index 0000000..a47c647
--- /dev/null
+++ b/drivers/char/tpm/tpm_spi_stm_st33.c
@@ -0,0 +1,1116 @@
+/*
+ * STMicroelectronics TPM SPI Linux driver for TPM ST33ZP24
+ * Copyright (C) 2009, 2010  STMicroelectronics
+ *
+ * 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.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * STMicroelectronics version 1.2.0, Copyright (C) 2010
+ * STMicroelectronics comes with ABSOLUTELY NO WARRANTY.
+ * This is free software, and you are welcome to redistribute it
+ * under certain conditions.
+ *
+ * @Author: Christophe RICARD tpmsupport@st.com
+ *
+ * @File: tpm_stm_st33_spi.c
+ *
+ * @Synopsis:
+ *	09/15/2010:	First shot driver tpm_tis driver for lpc is
+ *			used as model.
+ */
+
+#define pr_fmt(fmt)	KBUILD_MODNAME ": " fmt
+
+#include <linux/module.h>
+#include <linux/kernel.h>
+#include <linux/delay.h>
+#include <linux/init.h>
+#include <linux/spi/spi.h>
+#include <linux/wait.h>
+#include <linux/string.h>
+#include <linux/interrupt.h>
+#include <linux/spinlock.h>
+#include <linux/sysfs.h>
+#include <linux/gpio.h>
+#include <linux/sched.h>
+#include <linux/uaccess.h>
+#include <linux/io.h>
+#include <linux/slab.h>
+#include <linux/sched.h>
+
+#include <linux/spi/tpm_spi_stm_st33.h>
+
+#include "tpm.h"
+
+#include "tpm_spi_stm_st33.h"
+
+#ifdef DEBUG
+#define FUNC_ENTER()	pr_info("%s\n", __func__)
+#else
+#define	FUNC_ENTER()	do {}	while (0)
+#endif
+
+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 */
+};
+
+/*
+ * write8_reg
+ * Send byte to the TIS register according to the ST33ZP24 SPI protocol.
+ * @param: tpm, 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.
+ */
+static int write8_reg(struct tpm_chip *tpm, u8 tpm_register,
+		      u8 *tpm_data, u16 tpm_size)
+{
+	/* u8 LOC+DIR u8 ADD u16 LENGHT u8* data */
+	/* u8 LATENCY u8 LATENCY */
+
+	u8 data;
+	int i = 0, j;
+	int value = 0;
+	struct spi_device *dev = (struct spi_device *) tpm->vendor.iobase;
+	struct st33zp24_platform_data *platform_data = dev->dev.platform_data;
+	u8 *data_buffer = platform_data->tpm_spi_buffer[0];
+	struct spi_transfer xfer = {
+		.tx_buf	 = data_buffer,
+		.rx_buf	 = platform_data->tpm_spi_buffer[1],
+	};
+	struct spi_message msg;
+	u8 latency = platform_data->latency + tpm_size + 1;
+
+
+	FUNC_ENTER();
+	/* Pre-Header */
+
+	data = TPM_WRITE_DIRECTION | tpm->vendor.locality;
+	memcpy(data_buffer + i, &data, sizeof(data));
+	i++;
+	data = tpm_register;
+	memcpy(data_buffer + i, &data, sizeof(data));
+	i++;
+
+	if (tpm_size > 0 &&
+	     (tpm_register == TPM_DATA_FIFO ||
+	      tpm_register == TPM_HASH_DATA)) {
+		data = tpm_size >> 8 & 0x00ff;
+		memcpy(data_buffer + i, &data, sizeof(data));
+		i++;
+		data = tpm_size & 0x00ff;
+		memcpy(data_buffer + i, &data, sizeof(data));
+		i++;
+		latency -= 2;
+	}
+
+	memcpy(data_buffer + i, tpm_data, tpm_size);
+	i += tpm_size;
+
+	for (j = 0 ; j < platform_data->latency + 1; j++) {
+		data = TPM_DUMMY_BYTE;
+		memcpy(data_buffer + i, &data, sizeof(data));
+		i++;
+	}
+	xfer.len = i;
+	spi_message_init(&msg);
+	spi_message_add_tail(&xfer, &msg);
+	value = spi_sync(dev, &msg);
+
+	if (value == 0) {
+		j = i - 1 - j;
+		for (; j < i && ((u8 *) xfer.rx_buf)[j] == 0; j++)
+			;
+
+		if (((u8 *) xfer.rx_buf)[j] != 0)
+			value = ((u8 *) xfer.rx_buf)[j];
+	}
+
+	return value;
+} /* write8_reg() */
+
+/*
+ * read8_reg
+ * Recv byte from the TIS register according to the ST33ZP24 SPI 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.
+ */
+static u8 read8_reg(struct tpm_chip *tpm, u8 tpm_register,
+		    u8 *tpm_data, u16 tpm_size)
+{
+	u8 data;
+	int i = 0, j;
+	int value = 0;
+	struct spi_device *dev = (struct spi_device *) tpm->vendor.iobase;
+	struct st33zp24_platform_data *platform_data = dev->dev.platform_data;
+	u8 *data_buffer = platform_data->tpm_spi_buffer[0];
+	struct spi_transfer xfer = {
+		.tx_buf	 = data_buffer,
+		.rx_buf	 = platform_data->tpm_spi_buffer[1],
+	};
+	struct spi_message msg;
+
+	u8 latency = platform_data->latency + tpm_size + 1;
+
+	FUNC_ENTER();
+	/* Pre-Header */
+
+	data = tpm->vendor.locality;
+	memcpy(data_buffer + i, &data, sizeof(data));
+	i++;
+	data = tpm_register;
+	memcpy(data_buffer + i, &data, sizeof(data));
+	i++;
+
+	for (j = 0 ; j < latency; j++) {
+		data = TPM_DUMMY_BYTE;
+		memcpy(data_buffer + i, &data, sizeof(data));
+		i++;
+	}
+	xfer.len = i;
+
+	/* header + status byte + size of the data + status byte */
+	spi_message_init(&msg);
+	spi_message_add_tail(&xfer, &msg);
+	value = spi_sync(dev, &msg);
+	if (tpm_size > 0 && value == 0) {
+		j = 2;
+		for (; j < i && ((u8 *) xfer.rx_buf)[j] == 0; j++)
+			;
+
+		if (j + 1 < i) {
+			value = ((u8 *) xfer.rx_buf)[j];
+
+			if (tpm_size > 0)
+				memcpy(tpm_data, xfer.rx_buf + j + 1,
+								 tpm_size);
+		}
+	}
+	return value;
+} /* read8_reg() */
+
+/*
+ * SPI_WRITE_DATA
+ * Send byte to the TIS register according to the ST33ZP24 SPI protocol.
+ * @param: tpm, 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.
+ */
+static u32 SPI_WRITE_DATA(struct tpm_chip *tpm, u8 tpm_register,
+			  u8 *tpm_data, u16 tpm_size)
+{
+	u8 value = 0;
+
+	FUNC_ENTER();
+
+	value = write8_reg(tpm, tpm_register, tpm_data, tpm_size);
+
+	switch (value) {
+	case TPM_ST_SPI_OK:
+		return TPM_ST_SPI_OK;
+	case 0:
+		return 0;
+	default:
+		return -EBUSY;
+	}
+} /* SPI_WRITE_DATA() */
+
+/*
+ * SPI_READ_DATA
+ * Recv byte from the TIS register according to the ST33ZP24 SPI 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.
+ */
+static u32 SPI_READ_DATA(struct tpm_chip *tpm, u8 tpm_register,
+			u8 *tpm_data, u16 tpm_size)
+{
+	u8 value = 0;
+
+	FUNC_ENTER();
+
+	value = read8_reg(tpm, tpm_register, tpm_data, tpm_size);
+
+	switch (value) {
+	case TPM_ST_SPI_OK:
+		return TPM_ST_SPI_OK;
+	case 0:
+		return 0;
+	default:
+		return -EBUSY;
+	}
+} /* SPI_READ_DATA () */
+
+/*
+ * clear_interruption
+ * clear the TPM interrupt register.
+ * @param: tpm, the chip description
+ */
+static void clear_interruption(struct tpm_chip *tpm)
+{
+	u8 interrupt;
+	SPI_READ_DATA(tpm, TPM_INT_STATUS, &interrupt, 1);
+	/* Clear interrupts handled with TPM_EOI */
+	SPI_WRITE_DATA(tpm, TPM_INT_STATUS, &interrupt, 1);
+	SPI_READ_DATA(tpm, TPM_INT_STATUS, &interrupt, 1);
+} /* clear_interruption() */
+
+/*
+ * _wait_for_interrupt_serirq_timeout
+ * @param: tpm, the chip description
+ * @param: timeout, the timeout of the interrupt
+ * @return: the status of the interruption.
+ */
+static int _wait_for_interrupt_serirq_timeout(struct tpm_chip *chip,
+					unsigned long timeout)
+{
+	unsigned long status;
+	struct spi_device *client;
+	struct st33zp24_platform_data *pin_infos;
+
+	FUNC_ENTER();
+
+	client = (struct spi_device *) chip->vendor.iobase;
+	pin_infos = client->dev.platform_data;
+
+	status = wait_for_completion_interruptible_timeout(
+				&pin_infos->irq_detection, timeout);
+	if (status > 0)
+		enable_irq(gpio_to_irq(pin_infos->io_serirq));
+	gpio_direction_input(pin_infos->io_serirq);
+
+	return status;
+} /* wait_for_interrupt_serirq_timeout() */
+
+unsigned long wait_for_serirq_timeout(struct tpm_chip *chip, bool condition,
+				 unsigned long timeout)
+{
+	unsigned long status = 0;
+	struct spi_device *client;
+	struct st33zp24_platform_data *pin_infos;
+
+	client = (struct spi_device *) chip->vendor.iobase;
+	pin_infos = client->dev.platform_data;
+
+	status = _wait_for_interrupt_serirq_timeout(chip, timeout);
+	if (!status) {
+		status = -EBUSY;
+		goto wait_end;
+	}
+	clear_interruption(chip);
+	if (condition)
+		status = 1;
+
+wait_end:
+	return status;
+}
+
+
+/*
+ * tpm_stm_spi_cancel, cancel is not implemented.
+ * @param: chip, the tpm chip description as specified in
+ * driver/char/tpm/tpm.h.
+ */
+static void tpm_stm_spi_cancel(struct tpm_chip *chip)
+{
+	struct spi_device *client;
+	u8 data = TPM_STS_COMMAND_READY;
+	client = (struct spi_device *) chip->vendor.iobase;
+
+	FUNC_ENTER();
+
+	/* this causes the current command to be aborted */
+	SPI_WRITE_DATA(chip, TPM_STS, &data, 1);
+} /* tpm_stm_spi_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_spi_status(struct tpm_chip *chip)
+{
+	struct spi_device *client;
+	u8 data;
+	client = (struct spi_device *) chip->vendor.iobase;
+
+	FUNC_ENTER();
+
+	SPI_READ_DATA(chip, TPM_STS, &data, 1);
+	return data;
+} /* tpm_stm_spi_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)
+{
+	struct spi_device *client;
+	u8 data;
+	u8 status;
+
+	FUNC_ENTER();
+
+	client = (struct spi_device *) chip->vendor.iobase;
+
+	status = SPI_READ_DATA(chip, TPM_ACCESS, &data, 1);
+	if (status && (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 stop;
+	long rc;
+	u8 data;
+	struct spi_device *client;
+
+	FUNC_ENTER();
+
+	client = (struct spi_device *) chip->vendor.iobase;
+	/* Check locality */
+	if (check_locality(chip) == chip->vendor.locality)
+		return chip->vendor.locality;
+
+	/* Request locality */
+	data = TPM_ACCESS_REQUEST_USE;
+	rc = SPI_WRITE_DATA(chip, TPM_ACCESS, &data, 1);
+	if (rc < 0)
+		goto end;
+
+	/* wait for burstcount */
+	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 {
+		stop = jiffies + chip->vendor.timeout_a;
+		do {
+			if (check_locality(chip) >= 0)
+				return chip->vendor.locality;
+			msleep(TPM_TIMEOUT);
+		} while (time_before(jiffies, stop));
+	}
+end:
+	return -EACCES;
+} /* request_locality() */
+
+/*
+ * release_locality release the active locality
+ * @param: chip, the tpm chip description.
+ */
+static void release_locality(struct tpm_chip *chip)
+{
+	struct spi_device *client;
+	u8 data;
+
+	FUNC_ENTER();
+
+	client = (struct spi_device *) chip->vendor.iobase;
+	data = TPM_ACCESS_ACTIVE_LOCALITY;
+	SPI_WRITE_DATA(chip, TPM_ACCESS, &data, 1);
+} /* release_locality()*/
+
+/*
+ * 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 stop;
+	u32 burstcnt, status;
+	u8 tpm_reg, temp;
+
+	FUNC_ENTER();
+
+	/* wait for burstcount */
+	/* which timeout value, spec has 2 answers (c & d) */
+	stop = jiffies + chip->vendor.timeout_d;
+	do {
+		tpm_reg = TPM_STS + 1;
+		status = SPI_READ_DATA(chip, tpm_reg, &temp, 1);
+		if (status < 0)
+			goto end;
+
+		tpm_reg = tpm_reg + 1;
+		burstcnt = temp;
+		status = SPI_READ_DATA(chip, tpm_reg, &temp, 1);
+		if (status < 0)
+			goto end;
+
+		burstcnt |= temp << 8;
+		if (burstcnt)
+			return burstcnt;
+		msleep(TPM_TIMEOUT);
+	} while (time_before(jiffies, stop));
+
+end:
+	return -EBUSY;
+} /* get_burstcount() */
+
+/*
+ * wait_for_stat wait for a TPM_STS value
+ * @param: chip, the tpm chip description
+ * @param: mask, the value mask to wait
+ * @param: timeout, the timeout
+ * @param: queue, the wait queue.
+ * @return: the tpm status, 0 if success, -ETIME if timeout is reached.
+ */
+static int wait_for_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout,
+			 wait_queue_head_t *queue)
+{
+	unsigned long stop;
+	long rc;
+	u8 status;
+
+	FUNC_ENTER();
+
+	/* check current status */
+	status = tpm_stm_spi_status(chip);
+	if (!chip->vendor.irq && (status & mask) == mask)
+		return 0;
+
+	if (chip->vendor.irq) {
+		rc = wait_for_serirq_timeout(chip, ((tpm_stm_spi_status
+						    (chip) & mask) ==
+						     mask), timeout);
+		if (rc > 0)
+			return 0;
+	} else {
+		stop = jiffies + timeout;
+		do {
+			msleep(TPM_TIMEOUT);
+			status = tpm_stm_spi_status(chip);
+			if ((status & mask) == mask)
+				return 0;
+		} while (time_before(jiffies, stop));
+	}
+	return -ETIME;
+} /* wait_for_stat() */
+
+/*
+ * 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 tpm status, 0 if success, -ETIME if timeout is reached.
+ */
+static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count)
+{
+	u32 size = 0, burstcnt, status, len;
+	struct spi_device *client;
+
+	FUNC_ENTER();
+
+	client = (struct spi_device *) chip->vendor.iobase;
+
+	while (size < count &&
+	       wait_for_stat(chip,
+			     TPM_STS_DATA_AVAIL | TPM_STS_VALID,
+			     chip->vendor.timeout_c,
+			     &chip->vendor.read_queue)
+						== 0) {
+		burstcnt = get_burstcount(chip);
+		len = min_t(int, burstcnt, count - size);
+		status = SPI_READ_DATA(chip, TPM_DATA_FIFO, buf + size, len);
+		if (status < 0)
+			return status;
+
+
+		size += len;
+	}
+	return size;
+}
+
+/*
+ * tpm_ioserirq_handler the serirq irq handler
+ * @param: irq, the tpm chip description
+ * @param: dev_id, the description of the chip
+ * @return: the status of the handler.
+ */
+static irqreturn_t tpm_ioserirq_handler(int irq, void *dev_id)
+{
+	struct tpm_chip *chip = dev_id;
+	struct spi_device *client;
+	struct st33zp24_platform_data *pin_infos;
+
+	disable_irq_nosync(irq);
+	FUNC_ENTER();
+
+	client = (struct spi_device *) chip->vendor.iobase;
+	pin_infos = client->dev.platform_data;
+
+	complete(&pin_infos->irq_detection);
+	return IRQ_HANDLED;
+} /* tpm_ioserirq_handler() */
+
+/*
+ * tpm_stm_spi_send send TPM commands through the SPI bus.
+ * @param: chip, the tpm chip description
+ * @param: buf, the tpm command buffer
+ * @param: len, the tpm command size
+ * @return: In case of success the number of bytes sent.
+ *			In other case, a < 0 value describing the issue.
+ */
+static int tpm_stm_spi_send(struct tpm_chip *chip, unsigned char *buf,
+			    size_t len)
+{
+	u32 ret = 0, ordinal,
+	    status, burstcnt = 0, i, size = 0;
+	u8 data;
+
+	struct spi_device *client;
+	struct st33zp24_platform_data *pin_infos;
+
+	FUNC_ENTER();
+
+	if (chip == NULL)
+		return -EBUSY;
+	if (len < TPM_HEADER_SIZE)
+		return -EBUSY;
+
+	client = (struct spi_device *)chip->vendor.iobase;
+	pin_infos = client->dev.platform_data;
+
+	ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));
+
+	ret = request_locality(chip);
+	if (ret < 0)
+		return ret;
+
+	status = tpm_stm_spi_status(chip);
+	if ((status & TPM_STS_COMMAND_READY) == 0) {
+		tpm_stm_spi_cancel(chip);
+		if (wait_for_stat
+		    (chip, TPM_STS_COMMAND_READY, chip->vendor.timeout_b,
+		     &chip->vendor.int_queue) < 0) {
+			ret = -ETIME;
+			goto out_err;
+		}
+	}
+
+	for (i = 0 ; i < len - 1 ;) {
+		burstcnt = get_burstcount(chip);
+		size = min_t(int, len - i - 1, burstcnt);
+		ret = SPI_WRITE_DATA(chip, TPM_DATA_FIFO, buf, size);
+		if (ret < 0)
+			goto out_err;
+		i += size;
+	}
+
+	status = tpm_stm_spi_status(chip);
+	if ((status & TPM_STS_DATA_EXPECT) == 0) {
+		ret = -EIO;
+		goto out_err;
+	}
+
+	/* write last byte */
+	SPI_WRITE_DATA(chip, TPM_DATA_FIFO, buf + len - 1, 1);
+
+	status = tpm_stm_spi_status(chip);
+	if ((status & TPM_STS_DATA_EXPECT) != 0) {
+		ret = -EIO;
+		goto out_err;
+	}
+
+	/* go and do it */
+	data = TPM_STS_GO;
+	ret = SPI_WRITE_DATA(chip, TPM_STS, &data, 1);
+	if (ret < 0)
+		goto out_err;
+
+	return len;
+out_err:
+	tpm_stm_spi_cancel(chip);
+	release_locality(chip);
+	return ret;
+}
+
+/*
+ * tpm_stm_spi_recv received TPM response through the SPI bus.
+ * @param: chip, the tpm chip description
+ * @param: buf, the tpm command buffer
+ * @param: len, the tpm command size
+* @return: In case of success the number of bytes received.
+ *			In other case, a < 0 value describing the issue.
+ */
+static int tpm_stm_spi_recv(struct tpm_chip *chip, unsigned char *buf,
+			    size_t count)
+{
+	int size = 0;
+	int expected;
+
+	struct spi_device *client;
+	struct st33zp24_platform_data *pin_infos;
+
+
+	FUNC_ENTER();
+
+	client = (struct spi_device *)chip->vendor.iobase;
+	pin_infos = client->dev.platform_data;
+
+
+	if (chip == NULL)
+		return -EBUSY;
+	if (count < TPM_HEADER_SIZE) {
+		size = -EIO;
+		goto out;
+	}
+
+	size = recv_data(chip, buf, TPM_HEADER_SIZE);
+
+	/* read first 10 bytes, including tag, paramsize, and result */
+	if (size < TPM_HEADER_SIZE) {
+		dev_err(chip->dev, "Unable to read header\n");
+		goto out;
+	}
+
+	expected = be32_to_cpu(*(__be32 *) (buf + 2));
+	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;
+}
+
+
+static const struct file_operations tpm_st33_spi_fops = {
+	.owner = THIS_MODULE,
+	.llseek = no_llseek,
+	.read = tpm_read,
+	.write = tpm_write,
+	.open = tpm_open,
+	.release = tpm_release,
+};
+
+static DEVICE_ATTR(pubek, S_IRUGO, tpm_show_pubek, NULL);
+static DEVICE_ATTR(pcrs, S_IRUGO, tpm_show_pcrs, NULL);
+static DEVICE_ATTR(enabled, S_IRUGO, tpm_show_enabled, NULL);
+static DEVICE_ATTR(active, S_IRUGO, tpm_show_active, NULL);
+static DEVICE_ATTR(owned, S_IRUGO, tpm_show_owned, NULL);
+static DEVICE_ATTR(temp_deactivated, S_IRUGO, tpm_show_temp_deactivated, NULL);
+static DEVICE_ATTR(caps, S_IRUGO, tpm_show_caps_1_2, NULL);
+static DEVICE_ATTR(cancel, S_IWUSR | S_IWGRP, NULL, tpm_store_cancel);
+
+static struct attribute *stm_tpm_attrs[] = {
+	&dev_attr_pubek.attr,
+	&dev_attr_pcrs.attr,
+	&dev_attr_enabled.attr,
+	&dev_attr_active.attr,
+	&dev_attr_owned.attr,
+	&dev_attr_temp_deactivated.attr,
+	&dev_attr_caps.attr,
+	&dev_attr_cancel.attr, NULL,
+};
+
+static struct attribute_group stm_tpm_attr_grp = {
+	.attrs = stm_tpm_attrs
+};
+
+static struct tpm_vendor_specific st_spi_tpm = {
+	.send = tpm_stm_spi_send,
+	.recv = tpm_stm_spi_recv,
+	.cancel = tpm_stm_spi_cancel,
+	.status = tpm_stm_spi_status,
+	.attr_group = &stm_tpm_attr_grp,
+	.miscdev = {.fops = &tpm_st33_spi_fops,},
+};
+
+static int evaluate_latency(struct tpm_chip *chip)
+{
+	int latency = 0;
+	struct spi_device *dev = (struct spi_device *) chip->vendor.iobase;
+	struct st33zp24_platform_data *platform_data = dev->dev.platform_data;
+	int status = 0;
+	u8 data;
+
+	for (; status == 0x00 && latency < MAX_SPI_LATENCY ; latency++) {
+		platform_data->latency = latency;
+		status = SPI_READ_DATA(chip, TPM_INTF_CAPABILITY, &data, 1);
+	}
+	return latency - 1;
+} /* evaluate_latency() */
+
+static int interrupts;
+module_param(interrupts, int, 0444);
+MODULE_PARM_DESC(interrupts, "Enable interrupts");
+
+static int power_mgt = 1;
+module_param(power_mgt, int, 0444);
+MODULE_PARM_DESC(power_mgt, "Power Management");
+
+/*
+ * tpm_st33_spi_probe initialize the TPM device
+ * @param: client, the spi_device drescription (TPM SPI description).
+ * @param: id, the spi_device_id struct.
+ * @return: 0 in case of success.
+ *	 -1 in other case.
+ */
+static int
+tpm_st33_spi_probe(struct spi_device *dev)
+{
+	u32 err;
+	u8 intmask;
+	struct tpm_chip *chip;
+	struct st33zp24_platform_data *platform_data;
+
+	FUNC_ENTER();
+
+	err = 0;
+
+	/* Check SPI platform functionnalities */
+	if (dev == NULL) {
+		pr_info("dev is NULL. exiting.\n");
+		err = -ENODEV;
+		goto end;
+	}
+
+	chip = tpm_register_hardware(&dev->dev, &st_spi_tpm);
+	if (!chip) {
+		err = -ENODEV;
+		goto end;
+	}
+
+	/* Allocation of SPI buffers MISO and MOSI		*/
+	/* Size is as follow:					*/
+	/* Request burstcount value  = 0x800 = 2048		*/
+	/* +							*/
+	/* Response burstcount value = 0x400 = 1024		*/
+	/* +							*/
+	/* At least:						*/
+	/* 1 byte for direction/locality			*/
+	/* 1 byte tpm tis register				*/
+	/* 2 bytes spi data length (for request only)		*/
+	/* 2 latency bytes					*/
+	/* 1 status byte					*/
+	/* = 2048 + 1024 + 7					*/
+	/* We reserved 2048 + 1024 + 20 in case latency byte	*/
+	/* change						*/
+	platform_data = dev->dev.platform_data;
+	platform_data->tpm_spi_buffer[0] =
+	    kmalloc((TPM_BUFSIZE + (TPM_BUFSIZE / 2) +
+				 TPM_DIGEST_SIZE) * sizeof(u8), GFP_KERNEL);
+	if (platform_data->tpm_spi_buffer[0] == NULL) {
+		err = -ENOMEM;
+		goto _tpm_clean_answer;
+	}
+	platform_data->tpm_spi_buffer[1] =
+	    kmalloc((TPM_BUFSIZE + (TPM_BUFSIZE / 2) +
+				 TPM_DIGEST_SIZE) * sizeof(u8) , GFP_KERNEL);
+	if (platform_data->tpm_spi_buffer[1] == NULL) {
+		err = -ENOMEM;
+		goto _tpm_clean_response;
+	}
+
+	chip->vendor.iobase = dev;
+
+	/* Default timeouts */
+	chip->vendor.timeout_a = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
+	chip->vendor.timeout_b = msecs_to_jiffies(TIS_LONG_TIMEOUT);
+	chip->vendor.timeout_c = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
+	chip->vendor.timeout_d = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
+	chip->vendor.locality = LOCALITY0;
+
+	if (power_mgt) {
+		err = gpio_request(platform_data->io_lpcpd, "TPM IO_LPCPD");
+		if (err)
+			goto _gpio_init1;
+		gpio_set_value(platform_data->io_lpcpd, 1);
+	}
+
+	platform_data->latency = evaluate_latency(chip);
+	if (platform_data->latency <= 0x00) {
+		err = -ENODEV;
+		goto _gpio_init1;
+	}
+
+	/* Enable interrupt */
+	/* Register GPIO pin through generic Linux GPIO API */
+	if (interrupts) {
+		init_completion(&platform_data->irq_detection);
+		if (request_locality(chip) != LOCALITY0) {
+			err = -ENODEV;
+			goto _tpm_clean_response;
+		}
+		err = gpio_request(platform_data->io_serirq, "TPM IO_SERIRQ");
+		if (err)
+			goto _gpio_init2;
+
+		/* Clear all existing */
+		clear_interruption(chip);
+		err = request_irq(gpio_to_irq(platform_data->io_serirq),
+							&tpm_ioserirq_handler,
+				IRQF_TRIGGER_HIGH | IRQF_SHARED,
+				"TPM SERIRQ management", chip);
+		if (err < 0) {
+
+			pr_info("TPM SERIRQ signals %d not available\n",
+			gpio_to_irq(platform_data->io_serirq));
+			goto _irq_set;
+		}
+		err = SPI_READ_DATA(chip, TPM_INT_ENABLE, &intmask, 1);
+		if (err < 0)
+			goto _irq_set;
+
+		intmask |= TPM_INTF_CMD_READY_INT
+			|  TPM_INTF_FIFO_AVALAIBLE_INT
+			|  TPM_INTF_WAKE_UP_READY_INT
+			|  TPM_INTF_LOC4SOFTRELEASE_INT
+			|  TPM_INTF_LOCALITY_CHANGE_INT
+			|  TPM_INTF_STS_VALID_INT
+			|  TPM_INTF_DATA_AVAIL_INT;
+
+		err = SPI_WRITE_DATA(chip, TPM_INT_ENABLE, &intmask, 1);
+		if (err < 0)
+			goto _irq_set;
+
+		intmask = TPM_GLOBAL_INT_ENABLE;
+		err = SPI_WRITE_DATA(chip, TPM_INT_ENABLE + 3, &intmask, 1);
+		if (err < 0)
+			goto _irq_set;
+
+		err = SPI_READ_DATA(chip, TPM_INT_STATUS, &intmask, 1);
+		if (err < 0)
+			goto _irq_set;
+
+		chip->vendor.irq = interrupts;
+
+		tpm_gen_interrupt(chip);
+	}
+
+	tpm_get_timeouts(chip);
+
+	/* attach chip datas to client */
+	spi_set_drvdata(dev, chip);
+	platform_data->bChipF = false;
+
+	pr_info("TPM SPI Initialized\n");
+	return 0;
+_irq_set:
+	free_irq(gpio_to_irq(platform_data->io_serirq), chip);
+_gpio_init2:
+	if (platform_data && interrupts)
+		gpio_free(platform_data->io_serirq);
+_gpio_init1:
+	if (platform_data && power_mgt)
+		gpio_free(platform_data->io_lpcpd);
+_tpm_clean_response:
+	tpm_remove_hardware(chip->dev);
+	if (platform_data->tpm_spi_buffer[1] != NULL) {
+		kfree(platform_data->tpm_spi_buffer[1]);
+		platform_data->tpm_spi_buffer[1] = NULL;
+	}
+_tpm_clean_answer:
+	if (platform_data->tpm_spi_buffer[0] != NULL) {
+		kfree(platform_data->tpm_spi_buffer[0]);
+		platform_data->tpm_spi_buffer[0] = NULL;
+	}
+
+	platform_data->bChipF = true;
+end:
+	pr_info("TPM SPI initialisation fail\n");
+	return err;
+}
+
+/*
+ * tpm_st33_spi_remove remove the TPM device
+ * @param: client, the spi_device drescription (TPM SPI description).
+		clear_bit(0, &chip->is_open);
+ * @return: 0 in case of success.
+ */
+static int tpm_st33_spi_remove(struct spi_device *client)
+{
+	struct tpm_chip *chip = (struct tpm_chip *)spi_get_drvdata(client);
+	struct st33zp24_platform_data *pin_infos =
+		((struct spi_device *) chip->vendor.iobase)->dev.platform_data;
+
+	FUNC_ENTER();
+
+	if (pin_infos != NULL) {
+		gpio_free(pin_infos->io_lpcpd);
+
+		/* Check if chip has been previously clean */
+		if (pin_infos->bChipF != true)
+			tpm_remove_hardware(chip->dev);
+		if (pin_infos->tpm_spi_buffer[1] != NULL) {
+			kfree(pin_infos->tpm_spi_buffer[1]);
+			pin_infos->tpm_spi_buffer[1] = NULL;
+		}
+		if (pin_infos->tpm_spi_buffer[0] != NULL) {
+			kfree(pin_infos->tpm_spi_buffer[0]);
+			pin_infos->tpm_spi_buffer[0] = NULL;
+		}
+	}
+
+	return 0;
+}
+
+/*
+ * tpm_st33_spi_pm_suspend suspend the TPM device
+ * Added: Work around when suspend and no tpm application is running, suspend
+ * may fail because chip->data_buffer is not set (only set in tpm_open in Linux
+ * TPM core)
+ * @param: client, the spi_device drescription (TPM SPI description).
+ * @param: mesg, the power management message.
+ * @return: 0 in case of success.
+ */
+static int tpm_st33_spi_pm_suspend(struct spi_device *client,
+							 pm_message_t mesg)
+{
+	struct tpm_chip *chip =
+	    (struct tpm_chip *)spi_get_drvdata(client);
+	struct st33zp24_platform_data *pin_infos =
+		((struct spi_device *)chip->vendor.iobase)->dev.platform_data;
+	int ret = 0;
+
+	if (power_mgt)
+		gpio_set_value(pin_infos->io_lpcpd, 0);
+	else {
+		if (chip->data_buffer == NULL)
+			chip->data_buffer = pin_infos->tpm_spi_buffer[0];
+		ret = tpm_pm_suspend(&client->dev);
+	}
+	return ret;
+}				/* tpm_st33_spi_suspend() */
+
+/*
+ * tpm_st33_spi_pm_resume resume the TPM device
+ * @param: spi, the spi_device drescription (TPM SPI description).
+ * @return: 0 in case of success.
+ */
+static int tpm_st33_spi_pm_resume(struct spi_device *spi)
+{
+	struct tpm_chip *chip =
+	    (struct tpm_chip *)spi_get_drvdata(spi);
+	struct st33zp24_platform_data *pin_infos =
+		((struct spi_device *)chip->vendor.iobase)->dev.platform_data;
+
+	int ret = 0;
+	if (power_mgt) {
+		gpio_set_value(pin_infos->io_lpcpd, 1);
+		ret = wait_for_serirq_timeout(chip,
+					 (chip->vendor.status(chip) &&
+					  TPM_STS_VALID) == TPM_STS_VALID,
+					  chip->vendor.timeout_b);
+	} else {
+		if (chip->data_buffer == NULL)
+			chip->data_buffer = pin_infos->tpm_spi_buffer[0];
+		ret = tpm_pm_resume(&spi->dev);
+		if (!ret)
+			tpm_do_selftest(chip);
+	}
+	return ret;
+}				/* tpm_st33_spi_pm_resume() */
+
+static struct spi_driver tpm_st33_spi_driver = {
+	.driver = {
+		   .owner = THIS_MODULE,
+		   .name = TPM_ST33_SPI,
+		   },
+	.probe = tpm_st33_spi_probe,
+	.remove = tpm_st33_spi_remove,
+	.resume = tpm_st33_spi_pm_resume,
+	.suspend = tpm_st33_spi_pm_suspend,
+};
+
+/*
+ * tpm_st33_spi_init initialize driver
+ * @return: 0 if successful, else non zero value.
+ */
+static int __init tpm_st33_spi_init(void)
+{
+	FUNC_ENTER();
+	return spi_register_driver(&tpm_st33_spi_driver);
+}
+
+/*
+ * tpm_st33_spi_exit The kernel calls this function during unloading the
+ * module or during shut down process
+ */
+static void __exit tpm_st33_spi_exit(void)
+{
+	FUNC_ENTER();
+	spi_unregister_driver(&tpm_st33_spi_driver);
+}
+
+module_init(tpm_st33_spi_init);
+module_exit(tpm_st33_spi_exit);
+
+MODULE_AUTHOR("Christophe Ricard (tpmsupport@st.com)");
+MODULE_DESCRIPTION("STM TPM SPI ST33 Driver");
+MODULE_VERSION("1.2.0");
+MODULE_LICENSE("GPL");
diff --git a/drivers/char/tpm/tpm_spi_stm_st33.h b/drivers/char/tpm/tpm_spi_stm_st33.h
new file mode 100644
index 0000000..42bae6f
--- /dev/null
+++ b/drivers/char/tpm/tpm_spi_stm_st33.h
@@ -0,0 +1,75 @@
+/*
+ * STMicroelectronics TPM SPI Linux driver for TPM ST33NP18
+ * Copyright (C) 2009, 2010  STMicroelectronics
+ *
+ * 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.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * STMicroelectronics version 1.2.0, Copyright (C) 2010
+ * STMicroelectronics comes with ABSOLUTELY NO WARRANTY.
+ * This is free software, and you are welcome to redistribute it
+ * under certain conditions.
+ *
+ * @Author: Christophe RICARD tpmsupport@st.com
+ *
+ * @File: tpm_spi_stm_st33.h
+ *
+ * @Date: 09/15/2010
+ */
+#ifndef __STM_ST33_TPM_SPI_MAIN_H__
+#define __STM_ST33_TPM_SPI_MAIN_H__
+
+#include <linux/pci.h>
+#include <linux/module.h>
+#include <linux/platform_device.h>
+#include <linux/fs.h>
+#include <linux/miscdevice.h>
+
+#define MINOR_NUM_SPI		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			0x00
+#define TPM_WRITE_DIRECTION		0x80
+#define TPM_HEADER_SIZE			10
+#define TPM_BUFSIZE			2048
+
+/* ioctl commands */
+#define TPMIOC_CANCEL		_IO('T', 0x00)
+#define TPMIOC_TRANSMIT		_IO('T', 0x01)
+#define TPMIOC_HASH		_IO('T', 0x02)
+#define TPMIOC_CHANGELOCALITY	_IO('T', 0x03)
+
+#define LOCALITY0		0
+#define LOCALITY4		4
+
+#define TPM_ST_SPI_OK		0x5A
+
+
+#define MAX_SPI_LATENCY		15
+
+struct st_tpm_hash_t {
+	int size;
+	u8 *data;
+};
+
+#endif /* __STM_ST33_TPM_SPI_MAIN_H__ */
diff --git a/include/linux/spi/tpm_spi_stm_st33.h b/include/linux/spi/tpm_spi_stm_st33.h
new file mode 100644
index 0000000..c233ff6
--- /dev/null
+++ b/include/linux/spi/tpm_spi_stm_st33.h
@@ -0,0 +1,46 @@
+/*
+* STMicroelectronics TPM SPI Linux driver for TPM ST33NP18
+* Copyright (C) 2009, 2010 STMicroelectronics
+* Christophe RICARD tpmsupport@st.com
+* 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.,
+* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*
+* STMicroelectronics version 1.2.0, Copyright (C) 2010
+* STMicroelectronics comes with ABSOLUTELY NO WARRANTY.
+* This is free software, and you are welcome to redistribute it
+* under certain conditions.
+*
+* @File: tpm_spi_stm_st33.h
+*
+* @Date: 06/15/2008
+*/
+#ifndef __STM_ST33_TPM_SPI_H__
+#define __STM_ST33_TPM_SPI_H__
+
+#include <linux/spi/spi.h>
+
+#define TPM_ST33_SPI		"st33zp24_spi"
+
+#ifndef __STM_ST33_TPM_SPI_H__
+struct st33zp24_platform_data {
+	int io_serirq;
+	int io_lpcpd;
+	int latency;
+	bool bChipF;
+	u8 *tpm_spi_buffer[2]; /* 0 Request 1 Response */
+	struct completion irq_detection;
+};
+#endif
+
+#endif /* __STM_ST33_TPM_SPI_H__ */
-- 
1.7.1


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

* [PATCH 1/1] TPM: STMicroelectronics st33 driver SPI
@ 2013-03-25 15:08 Matthias Leblanc
  0 siblings, 0 replies; 21+ messages in thread
From: Matthias Leblanc @ 2013-03-25 15:08 UTC (permalink / raw)
  To: Kent Yoder, Rajiv Andrade, Marcel Selhorst, Sirrix AG, tpmdd-devel
  Cc: linux-kernel, Jean-Luc Blanc, Mathias Leblanc

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

 * STMicroelectronics version 1.2.0, Copyright (C) 2010
 * 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/SPI, in menuconfig or .config choose the tpm driver on
device --> tpm and activate the protocol of your choice before compiling
the kernel.
The driver will be accessible from within Linux.

Tested on linux x86/x64 and beagleboard REV B & XM REV C

Signed-off-by: admin01 <admin01@admin01-desktop.(none)>
---
 arch/arm/mach-omap2/board-omap3beagle.c |   40 ++
 drivers/char/tpm/Kconfig                |   10 +
 drivers/char/tpm/Makefile               |    1 +
 drivers/char/tpm/tpm_spi_stm_st33.c     | 1116 +++++++++++++++++++++++++++++++
 drivers/char/tpm/tpm_spi_stm_st33.h     |   75 ++
 include/linux/spi/tpm_spi_stm_st33.h    |   46 ++
 6 files changed, 1287 insertions(+), 0 deletions(-)
 create mode 100644 drivers/char/tpm/tpm_spi_stm_st33.c
 create mode 100644 drivers/char/tpm/tpm_spi_stm_st33.h
 create mode 100644 include/linux/spi/tpm_spi_stm_st33.h

diff --git a/arch/arm/mach-omap2/board-omap3beagle.c b/arch/arm/mach-omap2/board-omap3beagle.c
index c3558f9..72e6513 100644
--- a/arch/arm/mach-omap2/board-omap3beagle.c
+++ b/arch/arm/mach-omap2/board-omap3beagle.c
@@ -46,6 +46,11 @@
 #include <video/omap-panel-tfp410.h>
 #include <linux/platform_data/mtd-nand-omap2.h>
 
+#ifdef CONFIG_TCG_ST33_SPI
+#include <linux/spi/tpm_spi_stm_st33.h>
+#include <linux/spi/spi.h>
+#endif /* CONFIG_TCG_ST33_SPI */
+
 #include "common.h"
 #include "omap_device.h"
 #include "gpmc.h"
@@ -375,6 +380,26 @@ static struct i2c_board_info __initdata beagle_i2c_eeprom[] = {
        },
 };
 
+#if defined CONFIG_TCG_ST33_I2C || defined CONFIG_TCG_ST33_SPI
+static struct st33zp24_platform_data tpm_data = {
+	.io_serirq = 136,
+	.io_lpcpd  = 143,
+};
+#endif
+
+#ifdef CONFIG_TCG_ST33_SPI
+static struct  spi_board_info tpm_st33_spi_board_info[] = {
+	{
+		.modalias = TPM_ST33_SPI,
+		.max_speed_hz = 10000000,
+		.bus_num = 4,
+		.chip_select = 0,
+		.mode = SPI_MODE_0,
+		.platform_data = &tpm_data,
+	},
+};
+#endif
+
 static int __init omap3_beagle_i2c_init(void)
 {
 	omap3_pmic_get_config(&beagle_twldata,
@@ -556,6 +581,20 @@ static void __init omap3_beagle_init(void)
 	omap_mux_init_signal("sdrc_cke0", OMAP_PIN_OUTPUT);
 	omap_mux_init_signal("sdrc_cke1", OMAP_PIN_OUTPUT);
 
+	#if defined CONFIG_TCG_ST33_I2C || defined CONFIG_TCG_ST33_SPI
+		omap_mux_init_gpio(tpm_data.io_serirq, OMAP_PIN_INPUT);
+		omap_mux_init_gpio(tpm_data.io_lpcpd, OMAP_PIN_OUTPUT);
+	#endif
+
+	#ifdef CONFIG_TCG_ST33_SPI
+		omap_mux_init_signal("mcspi4_clk", OMAP_PIN_INPUT);
+		omap_mux_init_signal("mcspi4_cs0", OMAP_PIN_OUTPUT);
+		omap_mux_init_signal("mcspi4_simo", OMAP_PIN_OUTPUT);
+		omap_mux_init_signal("mcspi4_somi", OMAP_PIN_INPUT);
+
+		spi_register_board_info(tpm_st33_spi_board_info,
+			 ARRAY_SIZE(tpm_st33_spi_board_info));
+	#endif
+
 	pwm_add_table(pwm_lookup, ARRAY_SIZE(pwm_lookup));
 }
 
diff --git a/drivers/char/tpm/Kconfig b/drivers/char/tpm/Kconfig
index dbfd564..2fc1254 100644
--- a/drivers/char/tpm/Kconfig
+++ b/drivers/char/tpm/Kconfig
@@ -91,4 +91,14 @@ config TCG_ST33_I2C
         To compile this driver as a module, choose M here; the module will be
         called tpm_stm_st33_i2c.
 
+config TCG_ST33_SPI
+       tristate "STMicroelectronics ST33 SPI"
+       depends on SPI
+       depends on GPIOLIB
+       ---help---
+       If you have a TPM security chip from STMicroelectronics working with
+       an SPI bus say Yes and it will be accessible from within Linux.
+       To compile this driver as a module, choose M here; the module will be
+       called tpm_stm_st33_spi.
+
 endif # TCG_TPM
diff --git a/drivers/char/tpm/Makefile b/drivers/char/tpm/Makefile
index a3736c9..a1db3dd 100644
--- a/drivers/char/tpm/Makefile
+++ b/drivers/char/tpm/Makefile
@@ -18,3 +18,4 @@ obj-$(CONFIG_TCG_ATMEL) += tpm_atmel.o
 obj-$(CONFIG_TCG_INFINEON) += tpm_infineon.o
 obj-$(CONFIG_TCG_IBMVTPM) += tpm_ibmvtpm.o
 obj-$(CONFIG_TCG_ST33_I2C) += tpm_i2c_stm_st33.o
+obj-$(CONFIG_TCG_ST33_SPI) += tpm_spi_stm_st33.o
diff --git a/drivers/char/tpm/tpm_spi_stm_st33.c b/drivers/char/tpm/tpm_spi_stm_st33.c
new file mode 100644
index 0000000..a47c647
--- /dev/null
+++ b/drivers/char/tpm/tpm_spi_stm_st33.c
@@ -0,0 +1,1116 @@
+/*
+ * STMicroelectronics TPM SPI Linux driver for TPM ST33ZP24
+ * Copyright (C) 2009, 2010  STMicroelectronics
+ *
+ * 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.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * STMicroelectronics version 1.2.0, Copyright (C) 2010
+ * STMicroelectronics comes with ABSOLUTELY NO WARRANTY.
+ * This is free software, and you are welcome to redistribute it
+ * under certain conditions.
+ *
+ * @Author: Christophe RICARD tpmsupport@st.com
+ *
+ * @File: tpm_stm_st33_spi.c
+ *
+ * @Synopsis:
+ *	09/15/2010:	First shot driver tpm_tis driver for lpc is
+ *			used as model.
+ */
+
+#define pr_fmt(fmt)	KBUILD_MODNAME ": " fmt
+
+#include <linux/module.h>
+#include <linux/kernel.h>
+#include <linux/delay.h>
+#include <linux/init.h>
+#include <linux/spi/spi.h>
+#include <linux/wait.h>
+#include <linux/string.h>
+#include <linux/interrupt.h>
+#include <linux/spinlock.h>
+#include <linux/sysfs.h>
+#include <linux/gpio.h>
+#include <linux/sched.h>
+#include <linux/uaccess.h>
+#include <linux/io.h>
+#include <linux/slab.h>
+#include <linux/sched.h>
+
+#include <linux/spi/tpm_spi_stm_st33.h>
+
+#include "tpm.h"
+
+#include "tpm_spi_stm_st33.h"
+
+#ifdef DEBUG
+#define FUNC_ENTER()	pr_info("%s\n", __func__)
+#else
+#define	FUNC_ENTER()	do {}	while (0)
+#endif
+
+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 */
+};
+
+/*
+ * write8_reg
+ * Send byte to the TIS register according to the ST33ZP24 SPI protocol.
+ * @param: tpm, 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.
+ */
+static int write8_reg(struct tpm_chip *tpm, u8 tpm_register,
+		      u8 *tpm_data, u16 tpm_size)
+{
+	/* u8 LOC+DIR u8 ADD u16 LENGHT u8* data */
+	/* u8 LATENCY u8 LATENCY */
+
+	u8 data;
+	int i = 0, j;
+	int value = 0;
+	struct spi_device *dev = (struct spi_device *) tpm->vendor.iobase;
+	struct st33zp24_platform_data *platform_data = dev->dev.platform_data;
+	u8 *data_buffer = platform_data->tpm_spi_buffer[0];
+	struct spi_transfer xfer = {
+		.tx_buf	 = data_buffer,
+		.rx_buf	 = platform_data->tpm_spi_buffer[1],
+	};
+	struct spi_message msg;
+	u8 latency = platform_data->latency + tpm_size + 1;
+
+
+	FUNC_ENTER();
+	/* Pre-Header */
+
+	data = TPM_WRITE_DIRECTION | tpm->vendor.locality;
+	memcpy(data_buffer + i, &data, sizeof(data));
+	i++;
+	data = tpm_register;
+	memcpy(data_buffer + i, &data, sizeof(data));
+	i++;
+
+	if (tpm_size > 0 &&
+	     (tpm_register == TPM_DATA_FIFO ||
+	      tpm_register == TPM_HASH_DATA)) {
+		data = tpm_size >> 8 & 0x00ff;
+		memcpy(data_buffer + i, &data, sizeof(data));
+		i++;
+		data = tpm_size & 0x00ff;
+		memcpy(data_buffer + i, &data, sizeof(data));
+		i++;
+		latency -= 2;
+	}
+
+	memcpy(data_buffer + i, tpm_data, tpm_size);
+	i += tpm_size;
+
+	for (j = 0 ; j < platform_data->latency + 1; j++) {
+		data = TPM_DUMMY_BYTE;
+		memcpy(data_buffer + i, &data, sizeof(data));
+		i++;
+	}
+	xfer.len = i;
+	spi_message_init(&msg);
+	spi_message_add_tail(&xfer, &msg);
+	value = spi_sync(dev, &msg);
+
+	if (value == 0) {
+		j = i - 1 - j;
+		for (; j < i && ((u8 *) xfer.rx_buf)[j] == 0; j++)
+			;
+
+		if (((u8 *) xfer.rx_buf)[j] != 0)
+			value = ((u8 *) xfer.rx_buf)[j];
+	}
+
+	return value;
+} /* write8_reg() */
+
+/*
+ * read8_reg
+ * Recv byte from the TIS register according to the ST33ZP24 SPI 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.
+ */
+static u8 read8_reg(struct tpm_chip *tpm, u8 tpm_register,
+		    u8 *tpm_data, u16 tpm_size)
+{
+	u8 data;
+	int i = 0, j;
+	int value = 0;
+	struct spi_device *dev = (struct spi_device *) tpm->vendor.iobase;
+	struct st33zp24_platform_data *platform_data = dev->dev.platform_data;
+	u8 *data_buffer = platform_data->tpm_spi_buffer[0];
+	struct spi_transfer xfer = {
+		.tx_buf	 = data_buffer,
+		.rx_buf	 = platform_data->tpm_spi_buffer[1],
+	};
+	struct spi_message msg;
+
+	u8 latency = platform_data->latency + tpm_size + 1;
+
+	FUNC_ENTER();
+	/* Pre-Header */
+
+	data = tpm->vendor.locality;
+	memcpy(data_buffer + i, &data, sizeof(data));
+	i++;
+	data = tpm_register;
+	memcpy(data_buffer + i, &data, sizeof(data));
+	i++;
+
+	for (j = 0 ; j < latency; j++) {
+		data = TPM_DUMMY_BYTE;
+		memcpy(data_buffer + i, &data, sizeof(data));
+		i++;
+	}
+	xfer.len = i;
+
+	/* header + status byte + size of the data + status byte */
+	spi_message_init(&msg);
+	spi_message_add_tail(&xfer, &msg);
+	value = spi_sync(dev, &msg);
+	if (tpm_size > 0 && value == 0) {
+		j = 2;
+		for (; j < i && ((u8 *) xfer.rx_buf)[j] == 0; j++)
+			;
+
+		if (j + 1 < i) {
+			value = ((u8 *) xfer.rx_buf)[j];
+
+			if (tpm_size > 0)
+				memcpy(tpm_data, xfer.rx_buf + j + 1,
+								 tpm_size);
+		}
+	}
+	return value;
+} /* read8_reg() */
+
+/*
+ * SPI_WRITE_DATA
+ * Send byte to the TIS register according to the ST33ZP24 SPI protocol.
+ * @param: tpm, 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.
+ */
+static u32 SPI_WRITE_DATA(struct tpm_chip *tpm, u8 tpm_register,
+			  u8 *tpm_data, u16 tpm_size)
+{
+	u8 value = 0;
+
+	FUNC_ENTER();
+
+	value = write8_reg(tpm, tpm_register, tpm_data, tpm_size);
+
+	switch (value) {
+	case TPM_ST_SPI_OK:
+		return TPM_ST_SPI_OK;
+	case 0:
+		return 0;
+	default:
+		return -EBUSY;
+	}
+} /* SPI_WRITE_DATA() */
+
+/*
+ * SPI_READ_DATA
+ * Recv byte from the TIS register according to the ST33ZP24 SPI 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.
+ */
+static u32 SPI_READ_DATA(struct tpm_chip *tpm, u8 tpm_register,
+			u8 *tpm_data, u16 tpm_size)
+{
+	u8 value = 0;
+
+	FUNC_ENTER();
+
+	value = read8_reg(tpm, tpm_register, tpm_data, tpm_size);
+
+	switch (value) {
+	case TPM_ST_SPI_OK:
+		return TPM_ST_SPI_OK;
+	case 0:
+		return 0;
+	default:
+		return -EBUSY;
+	}
+} /* SPI_READ_DATA () */
+
+/*
+ * clear_interruption
+ * clear the TPM interrupt register.
+ * @param: tpm, the chip description
+ */
+static void clear_interruption(struct tpm_chip *tpm)
+{
+	u8 interrupt;
+	SPI_READ_DATA(tpm, TPM_INT_STATUS, &interrupt, 1);
+	/* Clear interrupts handled with TPM_EOI */
+	SPI_WRITE_DATA(tpm, TPM_INT_STATUS, &interrupt, 1);
+	SPI_READ_DATA(tpm, TPM_INT_STATUS, &interrupt, 1);
+} /* clear_interruption() */
+
+/*
+ * _wait_for_interrupt_serirq_timeout
+ * @param: tpm, the chip description
+ * @param: timeout, the timeout of the interrupt
+ * @return: the status of the interruption.
+ */
+static int _wait_for_interrupt_serirq_timeout(struct tpm_chip *chip,
+					unsigned long timeout)
+{
+	unsigned long status;
+	struct spi_device *client;
+	struct st33zp24_platform_data *pin_infos;
+
+	FUNC_ENTER();
+
+	client = (struct spi_device *) chip->vendor.iobase;
+	pin_infos = client->dev.platform_data;
+
+	status = wait_for_completion_interruptible_timeout(
+				&pin_infos->irq_detection, timeout);
+	if (status > 0)
+		enable_irq(gpio_to_irq(pin_infos->io_serirq));
+	gpio_direction_input(pin_infos->io_serirq);
+
+	return status;
+} /* wait_for_interrupt_serirq_timeout() */
+
+unsigned long wait_for_serirq_timeout(struct tpm_chip *chip, bool condition,
+				 unsigned long timeout)
+{
+	unsigned long status = 0;
+	struct spi_device *client;
+	struct st33zp24_platform_data *pin_infos;
+
+	client = (struct spi_device *) chip->vendor.iobase;
+	pin_infos = client->dev.platform_data;
+
+	status = _wait_for_interrupt_serirq_timeout(chip, timeout);
+	if (!status) {
+		status = -EBUSY;
+		goto wait_end;
+	}
+	clear_interruption(chip);
+	if (condition)
+		status = 1;
+
+wait_end:
+	return status;
+}
+
+
+/*
+ * tpm_stm_spi_cancel, cancel is not implemented.
+ * @param: chip, the tpm chip description as specified in
+ * driver/char/tpm/tpm.h.
+ */
+static void tpm_stm_spi_cancel(struct tpm_chip *chip)
+{
+	struct spi_device *client;
+	u8 data = TPM_STS_COMMAND_READY;
+	client = (struct spi_device *) chip->vendor.iobase;
+
+	FUNC_ENTER();
+
+	/* this causes the current command to be aborted */
+	SPI_WRITE_DATA(chip, TPM_STS, &data, 1);
+} /* tpm_stm_spi_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_spi_status(struct tpm_chip *chip)
+{
+	struct spi_device *client;
+	u8 data;
+	client = (struct spi_device *) chip->vendor.iobase;
+
+	FUNC_ENTER();
+
+	SPI_READ_DATA(chip, TPM_STS, &data, 1);
+	return data;
+} /* tpm_stm_spi_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)
+{
+	struct spi_device *client;
+	u8 data;
+	u8 status;
+
+	FUNC_ENTER();
+
+	client = (struct spi_device *) chip->vendor.iobase;
+
+	status = SPI_READ_DATA(chip, TPM_ACCESS, &data, 1);
+	if (status && (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 stop;
+	long rc;
+	u8 data;
+	struct spi_device *client;
+
+	FUNC_ENTER();
+
+	client = (struct spi_device *) chip->vendor.iobase;
+	/* Check locality */
+	if (check_locality(chip) == chip->vendor.locality)
+		return chip->vendor.locality;
+
+	/* Request locality */
+	data = TPM_ACCESS_REQUEST_USE;
+	rc = SPI_WRITE_DATA(chip, TPM_ACCESS, &data, 1);
+	if (rc < 0)
+		goto end;
+
+	/* wait for burstcount */
+	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 {
+		stop = jiffies + chip->vendor.timeout_a;
+		do {
+			if (check_locality(chip) >= 0)
+				return chip->vendor.locality;
+			msleep(TPM_TIMEOUT);
+		} while (time_before(jiffies, stop));
+	}
+end:
+	return -EACCES;
+} /* request_locality() */
+
+/*
+ * release_locality release the active locality
+ * @param: chip, the tpm chip description.
+ */
+static void release_locality(struct tpm_chip *chip)
+{
+	struct spi_device *client;
+	u8 data;
+
+	FUNC_ENTER();
+
+	client = (struct spi_device *) chip->vendor.iobase;
+	data = TPM_ACCESS_ACTIVE_LOCALITY;
+	SPI_WRITE_DATA(chip, TPM_ACCESS, &data, 1);
+} /* release_locality()*/
+
+/*
+ * 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 stop;
+	u32 burstcnt, status;
+	u8 tpm_reg, temp;
+
+	FUNC_ENTER();
+
+	/* wait for burstcount */
+	/* which timeout value, spec has 2 answers (c & d) */
+	stop = jiffies + chip->vendor.timeout_d;
+	do {
+		tpm_reg = TPM_STS + 1;
+		status = SPI_READ_DATA(chip, tpm_reg, &temp, 1);
+		if (status < 0)
+			goto end;
+
+		tpm_reg = tpm_reg + 1;
+		burstcnt = temp;
+		status = SPI_READ_DATA(chip, tpm_reg, &temp, 1);
+		if (status < 0)
+			goto end;
+
+		burstcnt |= temp << 8;
+		if (burstcnt)
+			return burstcnt;
+		msleep(TPM_TIMEOUT);
+	} while (time_before(jiffies, stop));
+
+end:
+	return -EBUSY;
+} /* get_burstcount() */
+
+/*
+ * wait_for_stat wait for a TPM_STS value
+ * @param: chip, the tpm chip description
+ * @param: mask, the value mask to wait
+ * @param: timeout, the timeout
+ * @param: queue, the wait queue.
+ * @return: the tpm status, 0 if success, -ETIME if timeout is reached.
+ */
+static int wait_for_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout,
+			 wait_queue_head_t *queue)
+{
+	unsigned long stop;
+	long rc;
+	u8 status;
+
+	FUNC_ENTER();
+
+	/* check current status */
+	status = tpm_stm_spi_status(chip);
+	if (!chip->vendor.irq && (status & mask) == mask)
+		return 0;
+
+	if (chip->vendor.irq) {
+		rc = wait_for_serirq_timeout(chip, ((tpm_stm_spi_status
+						    (chip) & mask) ==
+						     mask), timeout);
+		if (rc > 0)
+			return 0;
+	} else {
+		stop = jiffies + timeout;
+		do {
+			msleep(TPM_TIMEOUT);
+			status = tpm_stm_spi_status(chip);
+			if ((status & mask) == mask)
+				return 0;
+		} while (time_before(jiffies, stop));
+	}
+	return -ETIME;
+} /* wait_for_stat() */
+
+/*
+ * 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 tpm status, 0 if success, -ETIME if timeout is reached.
+ */
+static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count)
+{
+	u32 size = 0, burstcnt, status, len;
+	struct spi_device *client;
+
+	FUNC_ENTER();
+
+	client = (struct spi_device *) chip->vendor.iobase;
+
+	while (size < count &&
+	       wait_for_stat(chip,
+			     TPM_STS_DATA_AVAIL | TPM_STS_VALID,
+			     chip->vendor.timeout_c,
+			     &chip->vendor.read_queue)
+						== 0) {
+		burstcnt = get_burstcount(chip);
+		len = min_t(int, burstcnt, count - size);
+		status = SPI_READ_DATA(chip, TPM_DATA_FIFO, buf + size, len);
+		if (status < 0)
+			return status;
+
+
+		size += len;
+	}
+	return size;
+}
+
+/*
+ * tpm_ioserirq_handler the serirq irq handler
+ * @param: irq, the tpm chip description
+ * @param: dev_id, the description of the chip
+ * @return: the status of the handler.
+ */
+static irqreturn_t tpm_ioserirq_handler(int irq, void *dev_id)
+{
+	struct tpm_chip *chip = dev_id;
+	struct spi_device *client;
+	struct st33zp24_platform_data *pin_infos;
+
+	disable_irq_nosync(irq);
+	FUNC_ENTER();
+
+	client = (struct spi_device *) chip->vendor.iobase;
+	pin_infos = client->dev.platform_data;
+
+	complete(&pin_infos->irq_detection);
+	return IRQ_HANDLED;
+} /* tpm_ioserirq_handler() */
+
+/*
+ * tpm_stm_spi_send send TPM commands through the SPI bus.
+ * @param: chip, the tpm chip description
+ * @param: buf, the tpm command buffer
+ * @param: len, the tpm command size
+ * @return: In case of success the number of bytes sent.
+ *			In other case, a < 0 value describing the issue.
+ */
+static int tpm_stm_spi_send(struct tpm_chip *chip, unsigned char *buf,
+			    size_t len)
+{
+	u32 ret = 0, ordinal,
+	    status, burstcnt = 0, i, size = 0;
+	u8 data;
+
+	struct spi_device *client;
+	struct st33zp24_platform_data *pin_infos;
+
+	FUNC_ENTER();
+
+	if (chip == NULL)
+		return -EBUSY;
+	if (len < TPM_HEADER_SIZE)
+		return -EBUSY;
+
+	client = (struct spi_device *)chip->vendor.iobase;
+	pin_infos = client->dev.platform_data;
+
+	ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));
+
+	ret = request_locality(chip);
+	if (ret < 0)
+		return ret;
+
+	status = tpm_stm_spi_status(chip);
+	if ((status & TPM_STS_COMMAND_READY) == 0) {
+		tpm_stm_spi_cancel(chip);
+		if (wait_for_stat
+		    (chip, TPM_STS_COMMAND_READY, chip->vendor.timeout_b,
+		     &chip->vendor.int_queue) < 0) {
+			ret = -ETIME;
+			goto out_err;
+		}
+	}
+
+	for (i = 0 ; i < len - 1 ;) {
+		burstcnt = get_burstcount(chip);
+		size = min_t(int, len - i - 1, burstcnt);
+		ret = SPI_WRITE_DATA(chip, TPM_DATA_FIFO, buf, size);
+		if (ret < 0)
+			goto out_err;
+		i += size;
+	}
+
+	status = tpm_stm_spi_status(chip);
+	if ((status & TPM_STS_DATA_EXPECT) == 0) {
+		ret = -EIO;
+		goto out_err;
+	}
+
+	/* write last byte */
+	SPI_WRITE_DATA(chip, TPM_DATA_FIFO, buf + len - 1, 1);
+
+	status = tpm_stm_spi_status(chip);
+	if ((status & TPM_STS_DATA_EXPECT) != 0) {
+		ret = -EIO;
+		goto out_err;
+	}
+
+	/* go and do it */
+	data = TPM_STS_GO;
+	ret = SPI_WRITE_DATA(chip, TPM_STS, &data, 1);
+	if (ret < 0)
+		goto out_err;
+
+	return len;
+out_err:
+	tpm_stm_spi_cancel(chip);
+	release_locality(chip);
+	return ret;
+}
+
+/*
+ * tpm_stm_spi_recv received TPM response through the SPI bus.
+ * @param: chip, the tpm chip description
+ * @param: buf, the tpm command buffer
+ * @param: len, the tpm command size
+* @return: In case of success the number of bytes received.
+ *			In other case, a < 0 value describing the issue.
+ */
+static int tpm_stm_spi_recv(struct tpm_chip *chip, unsigned char *buf,
+			    size_t count)
+{
+	int size = 0;
+	int expected;
+
+	struct spi_device *client;
+	struct st33zp24_platform_data *pin_infos;
+
+
+	FUNC_ENTER();
+
+	client = (struct spi_device *)chip->vendor.iobase;
+	pin_infos = client->dev.platform_data;
+
+
+	if (chip == NULL)
+		return -EBUSY;
+	if (count < TPM_HEADER_SIZE) {
+		size = -EIO;
+		goto out;
+	}
+
+	size = recv_data(chip, buf, TPM_HEADER_SIZE);
+
+	/* read first 10 bytes, including tag, paramsize, and result */
+	if (size < TPM_HEADER_SIZE) {
+		dev_err(chip->dev, "Unable to read header\n");
+		goto out;
+	}
+
+	expected = be32_to_cpu(*(__be32 *) (buf + 2));
+	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;
+}
+
+
+static const struct file_operations tpm_st33_spi_fops = {
+	.owner = THIS_MODULE,
+	.llseek = no_llseek,
+	.read = tpm_read,
+	.write = tpm_write,
+	.open = tpm_open,
+	.release = tpm_release,
+};
+
+static DEVICE_ATTR(pubek, S_IRUGO, tpm_show_pubek, NULL);
+static DEVICE_ATTR(pcrs, S_IRUGO, tpm_show_pcrs, NULL);
+static DEVICE_ATTR(enabled, S_IRUGO, tpm_show_enabled, NULL);
+static DEVICE_ATTR(active, S_IRUGO, tpm_show_active, NULL);
+static DEVICE_ATTR(owned, S_IRUGO, tpm_show_owned, NULL);
+static DEVICE_ATTR(temp_deactivated, S_IRUGO, tpm_show_temp_deactivated, NULL);
+static DEVICE_ATTR(caps, S_IRUGO, tpm_show_caps_1_2, NULL);
+static DEVICE_ATTR(cancel, S_IWUSR | S_IWGRP, NULL, tpm_store_cancel);
+
+static struct attribute *stm_tpm_attrs[] = {
+	&dev_attr_pubek.attr,
+	&dev_attr_pcrs.attr,
+	&dev_attr_enabled.attr,
+	&dev_attr_active.attr,
+	&dev_attr_owned.attr,
+	&dev_attr_temp_deactivated.attr,
+	&dev_attr_caps.attr,
+	&dev_attr_cancel.attr, NULL,
+};
+
+static struct attribute_group stm_tpm_attr_grp = {
+	.attrs = stm_tpm_attrs
+};
+
+static struct tpm_vendor_specific st_spi_tpm = {
+	.send = tpm_stm_spi_send,
+	.recv = tpm_stm_spi_recv,
+	.cancel = tpm_stm_spi_cancel,
+	.status = tpm_stm_spi_status,
+	.attr_group = &stm_tpm_attr_grp,
+	.miscdev = {.fops = &tpm_st33_spi_fops,},
+};
+
+static int evaluate_latency(struct tpm_chip *chip)
+{
+	int latency = 0;
+	struct spi_device *dev = (struct spi_device *) chip->vendor.iobase;
+	struct st33zp24_platform_data *platform_data = dev->dev.platform_data;
+	int status = 0;
+	u8 data;
+
+	for (; status == 0x00 && latency < MAX_SPI_LATENCY ; latency++) {
+		platform_data->latency = latency;
+		status = SPI_READ_DATA(chip, TPM_INTF_CAPABILITY, &data, 1);
+	}
+	return latency - 1;
+} /* evaluate_latency() */
+
+static int interrupts;
+module_param(interrupts, int, 0444);
+MODULE_PARM_DESC(interrupts, "Enable interrupts");
+
+static int power_mgt = 1;
+module_param(power_mgt, int, 0444);
+MODULE_PARM_DESC(power_mgt, "Power Management");
+
+/*
+ * tpm_st33_spi_probe initialize the TPM device
+ * @param: client, the spi_device drescription (TPM SPI description).
+ * @param: id, the spi_device_id struct.
+ * @return: 0 in case of success.
+ *	 -1 in other case.
+ */
+static int
+tpm_st33_spi_probe(struct spi_device *dev)
+{
+	u32 err;
+	u8 intmask;
+	struct tpm_chip *chip;
+	struct st33zp24_platform_data *platform_data;
+
+	FUNC_ENTER();
+
+	err = 0;
+
+	/* Check SPI platform functionnalities */
+	if (dev == NULL) {
+		pr_info("dev is NULL. exiting.\n");
+		err = -ENODEV;
+		goto end;
+	}
+
+	chip = tpm_register_hardware(&dev->dev, &st_spi_tpm);
+	if (!chip) {
+		err = -ENODEV;
+		goto end;
+	}
+
+	/* Allocation of SPI buffers MISO and MOSI		*/
+	/* Size is as follow:					*/
+	/* Request burstcount value  = 0x800 = 2048		*/
+	/* +							*/
+	/* Response burstcount value = 0x400 = 1024		*/
+	/* +							*/
+	/* At least:						*/
+	/* 1 byte for direction/locality			*/
+	/* 1 byte tpm tis register				*/
+	/* 2 bytes spi data length (for request only)		*/
+	/* 2 latency bytes					*/
+	/* 1 status byte					*/
+	/* = 2048 + 1024 + 7					*/
+	/* We reserved 2048 + 1024 + 20 in case latency byte	*/
+	/* change						*/
+	platform_data = dev->dev.platform_data;
+	platform_data->tpm_spi_buffer[0] =
+	    kmalloc((TPM_BUFSIZE + (TPM_BUFSIZE / 2) +
+				 TPM_DIGEST_SIZE) * sizeof(u8), GFP_KERNEL);
+	if (platform_data->tpm_spi_buffer[0] == NULL) {
+		err = -ENOMEM;
+		goto _tpm_clean_answer;
+	}
+	platform_data->tpm_spi_buffer[1] =
+	    kmalloc((TPM_BUFSIZE + (TPM_BUFSIZE / 2) +
+				 TPM_DIGEST_SIZE) * sizeof(u8) , GFP_KERNEL);
+	if (platform_data->tpm_spi_buffer[1] == NULL) {
+		err = -ENOMEM;
+		goto _tpm_clean_response;
+	}
+
+	chip->vendor.iobase = dev;
+
+	/* Default timeouts */
+	chip->vendor.timeout_a = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
+	chip->vendor.timeout_b = msecs_to_jiffies(TIS_LONG_TIMEOUT);
+	chip->vendor.timeout_c = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
+	chip->vendor.timeout_d = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
+	chip->vendor.locality = LOCALITY0;
+
+	if (power_mgt) {
+		err = gpio_request(platform_data->io_lpcpd, "TPM IO_LPCPD");
+		if (err)
+			goto _gpio_init1;
+		gpio_set_value(platform_data->io_lpcpd, 1);
+	}
+
+	platform_data->latency = evaluate_latency(chip);
+	if (platform_data->latency <= 0x00) {
+		err = -ENODEV;
+		goto _gpio_init1;
+	}
+
+	/* Enable interrupt */
+	/* Register GPIO pin through generic Linux GPIO API */
+	if (interrupts) {
+		init_completion(&platform_data->irq_detection);
+		if (request_locality(chip) != LOCALITY0) {
+			err = -ENODEV;
+			goto _tpm_clean_response;
+		}
+		err = gpio_request(platform_data->io_serirq, "TPM IO_SERIRQ");
+		if (err)
+			goto _gpio_init2;
+
+		/* Clear all existing */
+		clear_interruption(chip);
+		err = request_irq(gpio_to_irq(platform_data->io_serirq),
+							&tpm_ioserirq_handler,
+				IRQF_TRIGGER_HIGH | IRQF_SHARED,
+				"TPM SERIRQ management", chip);
+		if (err < 0) {
+
+			pr_info("TPM SERIRQ signals %d not available\n",
+			gpio_to_irq(platform_data->io_serirq));
+			goto _irq_set;
+		}
+		err = SPI_READ_DATA(chip, TPM_INT_ENABLE, &intmask, 1);
+		if (err < 0)
+			goto _irq_set;
+
+		intmask |= TPM_INTF_CMD_READY_INT
+			|  TPM_INTF_FIFO_AVALAIBLE_INT
+			|  TPM_INTF_WAKE_UP_READY_INT
+			|  TPM_INTF_LOC4SOFTRELEASE_INT
+			|  TPM_INTF_LOCALITY_CHANGE_INT
+			|  TPM_INTF_STS_VALID_INT
+			|  TPM_INTF_DATA_AVAIL_INT;
+
+		err = SPI_WRITE_DATA(chip, TPM_INT_ENABLE, &intmask, 1);
+		if (err < 0)
+			goto _irq_set;
+
+		intmask = TPM_GLOBAL_INT_ENABLE;
+		err = SPI_WRITE_DATA(chip, TPM_INT_ENABLE + 3, &intmask, 1);
+		if (err < 0)
+			goto _irq_set;
+
+		err = SPI_READ_DATA(chip, TPM_INT_STATUS, &intmask, 1);
+		if (err < 0)
+			goto _irq_set;
+
+		chip->vendor.irq = interrupts;
+
+		tpm_gen_interrupt(chip);
+	}
+
+	tpm_get_timeouts(chip);
+
+	/* attach chip datas to client */
+	spi_set_drvdata(dev, chip);
+	platform_data->bChipF = false;
+
+	pr_info("TPM SPI Initialized\n");
+	return 0;
+_irq_set:
+	free_irq(gpio_to_irq(platform_data->io_serirq), chip);
+_gpio_init2:
+	if (platform_data && interrupts)
+		gpio_free(platform_data->io_serirq);
+_gpio_init1:
+	if (platform_data && power_mgt)
+		gpio_free(platform_data->io_lpcpd);
+_tpm_clean_response:
+	tpm_remove_hardware(chip->dev);
+	if (platform_data->tpm_spi_buffer[1] != NULL) {
+		kfree(platform_data->tpm_spi_buffer[1]);
+		platform_data->tpm_spi_buffer[1] = NULL;
+	}
+_tpm_clean_answer:
+	if (platform_data->tpm_spi_buffer[0] != NULL) {
+		kfree(platform_data->tpm_spi_buffer[0]);
+		platform_data->tpm_spi_buffer[0] = NULL;
+	}
+
+	platform_data->bChipF = true;
+end:
+	pr_info("TPM SPI initialisation fail\n");
+	return err;
+}
+
+/*
+ * tpm_st33_spi_remove remove the TPM device
+ * @param: client, the spi_device drescription (TPM SPI description).
+		clear_bit(0, &chip->is_open);
+ * @return: 0 in case of success.
+ */
+static int tpm_st33_spi_remove(struct spi_device *client)
+{
+	struct tpm_chip *chip = (struct tpm_chip *)spi_get_drvdata(client);
+	struct st33zp24_platform_data *pin_infos =
+		((struct spi_device *) chip->vendor.iobase)->dev.platform_data;
+
+	FUNC_ENTER();
+
+	if (pin_infos != NULL) {
+		gpio_free(pin_infos->io_lpcpd);
+
+		/* Check if chip has been previously clean */
+		if (pin_infos->bChipF != true)
+			tpm_remove_hardware(chip->dev);
+		if (pin_infos->tpm_spi_buffer[1] != NULL) {
+			kfree(pin_infos->tpm_spi_buffer[1]);
+			pin_infos->tpm_spi_buffer[1] = NULL;
+		}
+		if (pin_infos->tpm_spi_buffer[0] != NULL) {
+			kfree(pin_infos->tpm_spi_buffer[0]);
+			pin_infos->tpm_spi_buffer[0] = NULL;
+		}
+	}
+
+	return 0;
+}
+
+/*
+ * tpm_st33_spi_pm_suspend suspend the TPM device
+ * Added: Work around when suspend and no tpm application is running, suspend
+ * may fail because chip->data_buffer is not set (only set in tpm_open in Linux
+ * TPM core)
+ * @param: client, the spi_device drescription (TPM SPI description).
+ * @param: mesg, the power management message.
+ * @return: 0 in case of success.
+ */
+static int tpm_st33_spi_pm_suspend(struct spi_device *client,
+							 pm_message_t mesg)
+{
+	struct tpm_chip *chip =
+	    (struct tpm_chip *)spi_get_drvdata(client);
+	struct st33zp24_platform_data *pin_infos =
+		((struct spi_device *)chip->vendor.iobase)->dev.platform_data;
+	int ret = 0;
+
+	if (power_mgt)
+		gpio_set_value(pin_infos->io_lpcpd, 0);
+	else {
+		if (chip->data_buffer == NULL)
+			chip->data_buffer = pin_infos->tpm_spi_buffer[0];
+		ret = tpm_pm_suspend(&client->dev);
+	}
+	return ret;
+}				/* tpm_st33_spi_suspend() */
+
+/*
+ * tpm_st33_spi_pm_resume resume the TPM device
+ * @param: spi, the spi_device drescription (TPM SPI description).
+ * @return: 0 in case of success.
+ */
+static int tpm_st33_spi_pm_resume(struct spi_device *spi)
+{
+	struct tpm_chip *chip =
+	    (struct tpm_chip *)spi_get_drvdata(spi);
+	struct st33zp24_platform_data *pin_infos =
+		((struct spi_device *)chip->vendor.iobase)->dev.platform_data;
+
+	int ret = 0;
+	if (power_mgt) {
+		gpio_set_value(pin_infos->io_lpcpd, 1);
+		ret = wait_for_serirq_timeout(chip,
+					 (chip->vendor.status(chip) &&
+					  TPM_STS_VALID) == TPM_STS_VALID,
+					  chip->vendor.timeout_b);
+	} else {
+		if (chip->data_buffer == NULL)
+			chip->data_buffer = pin_infos->tpm_spi_buffer[0];
+		ret = tpm_pm_resume(&spi->dev);
+		if (!ret)
+			tpm_do_selftest(chip);
+	}
+	return ret;
+}				/* tpm_st33_spi_pm_resume() */
+
+static struct spi_driver tpm_st33_spi_driver = {
+	.driver = {
+		   .owner = THIS_MODULE,
+		   .name = TPM_ST33_SPI,
+		   },
+	.probe = tpm_st33_spi_probe,
+	.remove = tpm_st33_spi_remove,
+	.resume = tpm_st33_spi_pm_resume,
+	.suspend = tpm_st33_spi_pm_suspend,
+};
+
+/*
+ * tpm_st33_spi_init initialize driver
+ * @return: 0 if successful, else non zero value.
+ */
+static int __init tpm_st33_spi_init(void)
+{
+	FUNC_ENTER();
+	return spi_register_driver(&tpm_st33_spi_driver);
+}
+
+/*
+ * tpm_st33_spi_exit The kernel calls this function during unloading the
+ * module or during shut down process
+ */
+static void __exit tpm_st33_spi_exit(void)
+{
+	FUNC_ENTER();
+	spi_unregister_driver(&tpm_st33_spi_driver);
+}
+
+module_init(tpm_st33_spi_init);
+module_exit(tpm_st33_spi_exit);
+
+MODULE_AUTHOR("Christophe Ricard (tpmsupport@st.com)");
+MODULE_DESCRIPTION("STM TPM SPI ST33 Driver");
+MODULE_VERSION("1.2.0");
+MODULE_LICENSE("GPL");
diff --git a/drivers/char/tpm/tpm_spi_stm_st33.h b/drivers/char/tpm/tpm_spi_stm_st33.h
new file mode 100644
index 0000000..42bae6f
--- /dev/null
+++ b/drivers/char/tpm/tpm_spi_stm_st33.h
@@ -0,0 +1,75 @@
+/*
+ * STMicroelectronics TPM SPI Linux driver for TPM ST33NP18
+ * Copyright (C) 2009, 2010  STMicroelectronics
+ *
+ * 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.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * STMicroelectronics version 1.2.0, Copyright (C) 2010
+ * STMicroelectronics comes with ABSOLUTELY NO WARRANTY.
+ * This is free software, and you are welcome to redistribute it
+ * under certain conditions.
+ *
+ * @Author: Christophe RICARD tpmsupport@st.com
+ *
+ * @File: tpm_spi_stm_st33.h
+ *
+ * @Date: 09/15/2010
+ */
+#ifndef __STM_ST33_TPM_SPI_MAIN_H__
+#define __STM_ST33_TPM_SPI_MAIN_H__
+
+#include <linux/pci.h>
+#include <linux/module.h>
+#include <linux/platform_device.h>
+#include <linux/fs.h>
+#include <linux/miscdevice.h>
+
+#define MINOR_NUM_SPI		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			0x00
+#define TPM_WRITE_DIRECTION		0x80
+#define TPM_HEADER_SIZE			10
+#define TPM_BUFSIZE			2048
+
+/* ioctl commands */
+#define TPMIOC_CANCEL		_IO('T', 0x00)
+#define TPMIOC_TRANSMIT		_IO('T', 0x01)
+#define TPMIOC_HASH		_IO('T', 0x02)
+#define TPMIOC_CHANGELOCALITY	_IO('T', 0x03)
+
+#define LOCALITY0		0
+#define LOCALITY4		4
+
+#define TPM_ST_SPI_OK		0x5A
+
+
+#define MAX_SPI_LATENCY		15
+
+struct st_tpm_hash_t {
+	int size;
+	u8 *data;
+};
+
+#endif /* __STM_ST33_TPM_SPI_MAIN_H__ */
diff --git a/include/linux/spi/tpm_spi_stm_st33.h b/include/linux/spi/tpm_spi_stm_st33.h
new file mode 100644
index 0000000..c233ff6
--- /dev/null
+++ b/include/linux/spi/tpm_spi_stm_st33.h
@@ -0,0 +1,46 @@
+/*
+* STMicroelectronics TPM SPI Linux driver for TPM ST33NP18
+* Copyright (C) 2009, 2010 STMicroelectronics
+* Christophe RICARD tpmsupport@st.com
+* 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.,
+* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*
+* STMicroelectronics version 1.2.0, Copyright (C) 2010
+* STMicroelectronics comes with ABSOLUTELY NO WARRANTY.
+* This is free software, and you are welcome to redistribute it
+* under certain conditions.
+*
+* @File: tpm_spi_stm_st33.h
+*
+* @Date: 06/15/2008
+*/
+#ifndef __STM_ST33_TPM_SPI_H__
+#define __STM_ST33_TPM_SPI_H__
+
+#include <linux/spi/spi.h>
+
+#define TPM_ST33_SPI		"st33zp24_spi"
+
+#ifndef __STM_ST33_TPM_SPI_H__
+struct st33zp24_platform_data {
+	int io_serirq;
+	int io_lpcpd;
+	int latency;
+	bool bChipF;
+	u8 *tpm_spi_buffer[2]; /* 0 Request 1 Response */
+	struct completion irq_detection;
+};
+#endif
+
+#endif /* __STM_ST33_TPM_SPI_H__ */
-- 
1.7.1


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

* [PATCH 1/1] TPM: STMicroelectronics st33 driver SPI
@ 2013-01-07 18:50 Mathias Leblanc
  0 siblings, 0 replies; 21+ messages in thread
From: Mathias Leblanc @ 2013-01-07 18:50 UTC (permalink / raw)
  To: Kent Yoder, Rajiv Andrade, Marcel Selhorst, Sirrix AG,
	tpmdd-devel, Debora Velarde
  Cc: linux-kernel, Jean-Luc Blanc, Mathias Leblanc

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

 * STMicroelectronics version 1.2.0, Copyright (C) 2010
 * 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/SPI, in menuconfig or .config choose the tpm driver on
device --> tpm and activate the protocol of your choice before compiling
the kernel.
The driver will be accessible from within Linux.

Tested on linux x86/x64 and beagleboard REV B & XM REV C

Signed-off-by: admin01 <admin01@admin01-desktop.(none)>
---
 drivers/char/tpm/Kconfig             |   10 +
 drivers/char/tpm/Makefile            |    1 +
 drivers/char/tpm/tpm_stm_st33_spi.c  | 1282 ++++++++++++++++++++++++++++++++++
 drivers/char/tpm/tpm_stm_st33_spi.h  |   79 +++
 include/linux/spi/tpm_stm_st33_spi.h |   46 ++
 5 files changed, 1418 insertions(+), 0 deletions(-)
 create mode 100644 drivers/char/tpm/tpm_stm_st33_spi.c
 create mode 100644 drivers/char/tpm/tpm_stm_st33_spi.h
 create mode 100644 include/linux/spi/tpm_stm_st33_spi.h

diff --git a/drivers/char/tpm/Kconfig b/drivers/char/tpm/Kconfig
index 915875e..63649f4 100644
--- a/drivers/char/tpm/Kconfig
+++ b/drivers/char/tpm/Kconfig
@@ -81,4 +81,14 @@ config TCG_IBMVTPM
 	  will be accessible from within Linux.  To compile this driver
 	  as a module, choose M here; the module will be called tpm_ibmvtpm.
 
+config TCG_ST33_SPI
+       tristate "STMicroelectronics ST33 SPI"
+       depends on SPI
+       depends on GPIOLIB
+       ---help---
+       If you have a TPM security chip from STMicroelectronics working with
+       an SPI bus say Yes and it will be accessible from within Linux.
+       To compile this driver as a module, choose M here; the module will be
+       called tpm_stm_st33_spi.
+
 endif # TCG_TPM
diff --git a/drivers/char/tpm/Makefile b/drivers/char/tpm/Makefile
index 5b3fc8b..44bf6e5 100644
--- a/drivers/char/tpm/Makefile
+++ b/drivers/char/tpm/Makefile
@@ -17,3 +17,4 @@ obj-$(CONFIG_TCG_NSC) += tpm_nsc.o
 obj-$(CONFIG_TCG_ATMEL) += tpm_atmel.o
 obj-$(CONFIG_TCG_INFINEON) += tpm_infineon.o
 obj-$(CONFIG_TCG_IBMVTPM) += tpm_ibmvtpm.o
++obj-$(CONFIG_TCG_ST33_SPI) += tpm_stm_st33_spi.o
diff --git a/drivers/char/tpm/tpm_stm_st33_spi.c b/drivers/char/tpm/tpm_stm_st33_spi.c
new file mode 100644
index 0000000..baa88b6
--- /dev/null
+++ b/drivers/char/tpm/tpm_stm_st33_spi.c
@@ -0,0 +1,1282 @@
+/*
+ * STMicroelectronics TPM SPI Linux driver for TPM ST33ZP24
+ * Copyright (C) 2009, 2010  STMicroelectronics
+ *
+ * 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.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * STMicroelectronics version 1.2.0, Copyright (C) 2010
+ * STMicroelectronics comes with ABSOLUTELY NO WARRANTY.
+ * This is free software, and you are welcome to redistribute it
+ * under certain conditions.
+ *
+ * @Author: Christophe RICARD tpmsupport@st.com
+ *
+ * @File: tpm_stm_st33_spi.c
+ *
+ * @Synopsis:
+ *	09/15/2010:	First shot driver tpm_tis driver for lpc is
+ *			used as model.
+ */
+
+#define pr_fmt(fmt)	KBUILD_MODNAME ": " fmt
+
+#include <linux/module.h>
+#include <linux/kernel.h>
+#include <linux/delay.h>
+#include <linux/init.h>
+#include <linux/spi/spi.h>
+#include <linux/wait.h>
+#include <linux/string.h>
+#include <linux/interrupt.h>
+#include <linux/spinlock.h>
+#include <linux/sysfs.h>
+#include <linux/gpio.h>
+#include <linux/sched.h>
+#include <linux/uaccess.h>
+#include <linux/io.h>
+#include <linux/slab.h>
+#include <linux/sched.h>
+
+#include <linux/spi/tpm_stm_st33_spi.h>
+
+#include "tpm.h"
+
+#include "tpm_stm_st33_spi.h"
+
+#ifdef DEBUG
+#define FUNC_ENTER()	pr_info("%s\n", __func__)
+#else
+#define	FUNC_ENTER()	do {}	while (0)
+#endif
+
+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 */
+};
+
+/*
+ * write8_reg
+ * Send byte to the TIS register according to the ST33ZP24 SPI protocol.
+ * @param: tpm, 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.
+ */
+static int write8_reg(struct tpm_chip *tpm, u8 tpm_register,
+		      u8 *tpm_data, u16 tpm_size)
+{
+	/* u8 LOC+DIR u8 ADD u16 LENGHT u8* data */
+	/* u8 LATENCY u8 LATENCY */
+
+	u8 data;
+	int i = 0, j;
+	int value = 0;
+	struct spi_device *dev = (struct spi_device *) tpm->vendor.iobase;
+	struct st33zp24_platform_data *platform_data = dev->dev.platform_data;
+	u8 *data_buffer = platform_data->tpm_spi_buffer[0];
+	struct spi_transfer xfer = {
+		.tx_buf	 = data_buffer,
+		.rx_buf	 = platform_data->tpm_spi_buffer[1],
+	};
+	struct spi_message msg;
+	u8 latency = platform_data->latency + tpm_size + 1;
+
+
+	FUNC_ENTER();
+	/* Pre-Header */
+
+	data = TPM_WRITE_DIRECTION | tpm->vendor.locality;
+	memcpy(data_buffer + i, &data, sizeof(data));
+	i++;
+	data = tpm_register;
+	memcpy(data_buffer + i, &data, sizeof(data));
+	i++;
+
+	if (tpm_size > 0 &&
+	     (tpm_register == TPM_DATA_FIFO ||
+	      tpm_register == TPM_HASH_DATA)) {
+		data = tpm_size >> 8 & 0x00ff;
+		memcpy(data_buffer + i, &data, sizeof(data));
+		i++;
+		data = tpm_size & 0x00ff;
+		memcpy(data_buffer + i, &data, sizeof(data));
+		i++;
+		latency -= 2;
+	}
+
+	memcpy(data_buffer + i, tpm_data, tpm_size);
+	i += tpm_size;
+
+	for (j = 0 ; j < platform_data->latency + 1; j++) {
+		data = TPM_DUMMY_BYTE;
+		memcpy(data_buffer + i, &data, sizeof(data));
+		i++;
+	}
+	xfer.len = i;
+	spi_message_init(&msg);
+	spi_message_add_tail(&xfer, &msg);
+	value = spi_sync(dev, &msg);
+
+	if (value == 0) {
+		j = i - 1 - j;
+		for (; j < i && ((u8 *) xfer.rx_buf)[j] == 0; j++)
+			;
+
+		if (((u8 *) xfer.rx_buf)[j] != 0)
+			value = ((u8 *) xfer.rx_buf)[j];
+	}
+
+	return value;
+} /* write8_reg() */
+
+/*
+ * read8_reg
+ * Recv byte from the TIS register according to the ST33ZP24 SPI 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.
+ */
+static u8 read8_reg(struct tpm_chip *tpm, u8 tpm_register,
+		    u8 *tpm_data, u16 tpm_size)
+{
+	u8 data;
+	int i = 0, j;
+	int value = 0;
+	struct spi_device *dev = (struct spi_device *) tpm->vendor.iobase;
+	struct st33zp24_platform_data *platform_data = dev->dev.platform_data;
+	u8 *data_buffer = platform_data->tpm_spi_buffer[0];
+	struct spi_transfer xfer = {
+		.tx_buf	 = data_buffer,
+		.rx_buf	 = platform_data->tpm_spi_buffer[1],
+	};
+	struct spi_message msg;
+
+	u8 latency = platform_data->latency + tpm_size + 1;
+
+	FUNC_ENTER();
+	/* Pre-Header */
+
+	data = tpm->vendor.locality;
+	memcpy(data_buffer + i, &data, sizeof(data));
+	i++;
+	data = tpm_register;
+	memcpy(data_buffer + i, &data, sizeof(data));
+	i++;
+
+	for (j = 0 ; j < latency; j++) {
+		data = TPM_DUMMY_BYTE;
+		memcpy(data_buffer + i, &data, sizeof(data));
+		i++;
+	}
+	xfer.len = i;
+
+	/* header + status byte + size of the data + status byte */
+	spi_message_init(&msg);
+	spi_message_add_tail(&xfer, &msg);
+	value = spi_sync(dev, &msg);
+	if (tpm_size > 0 && value == 0) {
+		j = 2;
+		for (; j < i && ((u8 *) xfer.rx_buf)[j] == 0; j++)
+			;
+
+		if (j + 1 < i) {
+			value = ((u8 *) xfer.rx_buf)[j];
+
+			if (tpm_size > 0)
+				memcpy(tpm_data, xfer.rx_buf + j + 1,
+								 tpm_size);
+		}
+	}
+	return value;
+} /* read8_reg() */
+
+/*
+ * SPI_WRITE_DATA
+ * Send byte to the TIS register according to the ST33ZP24 SPI protocol.
+ * @param: tpm, 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.
+ */
+static u32 SPI_WRITE_DATA(struct tpm_chip *tpm, u8 tpm_register,
+			  u8 *tpm_data, u16 tpm_size)
+{
+	u8 value = 0;
+
+	FUNC_ENTER();
+
+	value = write8_reg(tpm, tpm_register, tpm_data, tpm_size);
+
+	switch (value) {
+	case TPM_ST_SPI_OK:
+		return TPM_ST_SPI_OK;
+	case 0:
+		return 0;
+	default:
+		return -EBUSY;
+	}
+} /* SPI_WRITE_DATA() */
+
+/*
+ * SPI_READ_DATA
+ * Recv byte from the TIS register according to the ST33ZP24 SPI 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.
+ */
+static u32 SPI_READ_DATA(struct tpm_chip *tpm, u8 tpm_register,
+			u8 *tpm_data, u16 tpm_size)
+{
+	u8 value = 0;
+
+	FUNC_ENTER();
+
+	value = read8_reg(tpm, tpm_register, tpm_data, tpm_size);
+
+	switch (value) {
+	case TPM_ST_SPI_OK:
+		return TPM_ST_SPI_OK;
+	case 0:
+		return 0;
+	default:
+		return -EBUSY;
+	}
+} /* SPI_READ_DATA () */
+
+/*
+ * clear_interruption
+ * clear the TPM interrupt register.
+ * @param: tpm, the chip description
+ */
+static void clear_interruption(struct tpm_chip *tpm)
+{
+	u8 interrupt;
+	SPI_READ_DATA(tpm, TPM_INT_STATUS, &interrupt, 1);
+	/* Clear interrupts handled with TPM_EOI */
+	SPI_WRITE_DATA(tpm, TPM_INT_STATUS, &interrupt, 1);
+	SPI_READ_DATA(tpm, TPM_INT_STATUS, &interrupt, 1);
+} /* clear_interruption() */
+
+/*
+ * _wait_for_interrupt_serirq_timeout
+ * @param: tpm, the chip description
+ * @param: timeout, the timeout of the interrupt
+ * @return: the status of the interruption.
+ */
+static int _wait_for_interrupt_serirq_timeout(struct tpm_chip *chip,
+					unsigned long timeout)
+{
+	unsigned long status;
+	struct spi_device *client;
+	struct st33zp24_platform_data *pin_infos;
+
+	FUNC_ENTER();
+
+	client = (struct spi_device *) chip->vendor.iobase;
+	pin_infos = client->dev.platform_data;
+
+	status = wait_for_completion_interruptible_timeout(
+				&pin_infos->irq_detection, timeout);
+	if (status > 0)
+		enable_irq(gpio_to_irq(pin_infos->io_serirq));
+	gpio_direction_input(pin_infos->io_serirq);
+
+	return status;
+} /* wait_for_interrupt_serirq_timeout() */
+
+unsigned long wait_for_serirq_timeout(struct tpm_chip *chip, bool condition,
+				 unsigned long timeout)
+{
+	unsigned long status = 0;
+	struct spi_device *client;
+	struct st33zp24_platform_data *pin_infos;
+
+	client = (struct spi_device *) chip->vendor.iobase;
+	pin_infos = client->dev.platform_data;
+
+	status = _wait_for_interrupt_serirq_timeout(chip, timeout);
+	if (!status) {
+		status = -EBUSY;
+		goto wait_end;
+	}
+	clear_interruption(chip);
+	if (condition)
+		status = 1;
+
+wait_end:
+	status;
+}
+
+
+/*
+ * tpm_stm_spi_cancel, cancel is not implemented.
+ * @param: chip, the tpm chip description as specified in
+ * driver/char/tpm/tpm.h.
+ */
+static void tpm_stm_spi_cancel(struct tpm_chip *chip)
+{
+	struct spi_device *client;
+	u8 data = TPM_STS_COMMAND_READY;
+	client = (struct spi_device *) chip->vendor.iobase;
+
+	FUNC_ENTER();
+
+	/* this causes the current command to be aborted */
+	SPI_WRITE_DATA(chip, TPM_STS, &data, 1);
+} /* tpm_stm_spi_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_spi_status(struct tpm_chip *chip)
+{
+	struct spi_device *client;
+	u8 data;
+	client = (struct spi_device *) chip->vendor.iobase;
+
+	FUNC_ENTER();
+
+	SPI_READ_DATA(chip, TPM_STS, &data, 1);
+	return data;
+} /* tpm_stm_spi_status() */
+
+/*
+ * responseSize return the command size
+ * @param: buffer, command buffer.
+ * @param: size, the buffer size.
+ * @return: the command size.
+ */
+static int responseSize(const char *buffer, size_t size)
+{
+	size_t val = 0;
+
+	FUNC_ENTER();
+
+	if (size >= TPM_HEADER_SIZE) {
+		val = (size_t) (((unsigned)buffer[2]) << 24
+				| ((unsigned)buffer[3]) << 16
+				| ((unsigned)buffer[4]) << 8 | (unsigned)
+				buffer[5]);
+	}
+
+	if (val < TPM_BUFSIZE)
+		return val;
+	else
+		return TPM_BUFSIZE;
+}
+
+/*
+ * 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)
+{
+	struct spi_device *client;
+	u8 data;
+	u8 status;
+
+	FUNC_ENTER();
+
+	client = (struct spi_device *) chip->vendor.iobase;
+
+	status = SPI_READ_DATA(chip, TPM_ACCESS, &data, 1);
+	if (status && (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 stop;
+	long rc;
+	u8 data;
+	struct spi_device *client;
+
+	FUNC_ENTER();
+
+	client = (struct spi_device *) chip->vendor.iobase;
+	/* Check locality */
+	if (check_locality(chip) == chip->vendor.locality)
+		return chip->vendor.locality;
+
+	/* Request locality */
+	data = TPM_ACCESS_REQUEST_USE;
+	rc = SPI_WRITE_DATA(chip, TPM_ACCESS, &data, 1);
+	if (rc < 0)
+		goto end;
+
+	/* wait for burstcount */
+	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 {
+		stop = jiffies + chip->vendor.timeout_a;
+		do {
+			if (check_locality(chip) >= 0)
+				return chip->vendor.locality;
+			msleep(TPM_TIMEOUT);
+		} while (time_before(jiffies, stop));
+	}
+end:
+	return -EACCES;
+} /* request_locality() */
+
+/*
+ * release_locality release the active locality
+ * @param: chip, the tpm chip description.
+ */
+static void release_locality(struct tpm_chip *chip)
+{
+	struct spi_device *client;
+	u8 data;
+
+	FUNC_ENTER();
+
+	client = (struct spi_device *) chip->vendor.iobase;
+	data = TPM_ACCESS_ACTIVE_LOCALITY;
+	SPI_WRITE_DATA(chip, TPM_ACCESS, &data, 1);
+} /* release_locality()*/
+
+/*
+ * 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 stop;
+	u32 burstcnt, status;
+	u8 tpm_reg, temp;
+
+	FUNC_ENTER();
+
+	/* wait for burstcount */
+	/* which timeout value, spec has 2 answers (c & d) */
+	stop = jiffies + chip->vendor.timeout_d;
+	do {
+		tpm_reg = TPM_STS + 1;
+		status = SPI_READ_DATA(chip, tpm_reg, &temp, 1);
+		if (status < 0)
+			goto end;
+
+		tpm_reg = tpm_reg + 1;
+		burstcnt = temp;
+		status = SPI_READ_DATA(chip, tpm_reg, &temp, 1);
+		if (status < 0)
+			goto end;
+
+		burstcnt |= temp << 8;
+		if (burstcnt)
+			return burstcnt;
+		msleep(TPM_TIMEOUT);
+	} while (time_before(jiffies, stop));
+
+end:
+	return -EBUSY;
+} /* get_burstcount() */
+
+/*
+ * wait_for_stat wait for a TPM_STS value
+ * @param: chip, the tpm chip description
+ * @param: mask, the value mask to wait
+ * @param: timeout, the timeout
+ * @param: queue, the wait queue.
+ * @return: the tpm status, 0 if success, -ETIME if timeout is reached.
+ */
+static int wait_for_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout,
+			 wait_queue_head_t *queue)
+{
+	unsigned long stop;
+	long rc;
+	u8 status;
+
+	FUNC_ENTER();
+
+	/* check current status */
+	status = tpm_stm_spi_status(chip);
+	if (!chip->vendor.irq && (status & mask) == mask)
+		return 0;
+
+	if (chip->vendor.irq) {
+		rc = wait_for_serirq_timeout(chip, ((tpm_stm_spi_status
+						    (chip) & mask) ==
+						     mask), timeout);
+		if (rc > 0)
+			return 0;
+	} else {
+		stop = jiffies + timeout;
+		do {
+			msleep(TPM_TIMEOUT);
+			status = tpm_stm_spi_status(chip);
+			if ((status & mask) == mask)
+				return 0;
+		} while (time_before(jiffies, stop));
+	}
+	return -ETIME;
+} /* wait_for_stat() */
+
+/*
+ * 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 tpm status, 0 if success, -ETIME if timeout is reached.
+ */
+static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count)
+{
+	u32 size = 0, burstcnt, status, len;
+	struct spi_device *client;
+
+	FUNC_ENTER();
+
+	client = (struct spi_device *) chip->vendor.iobase;
+
+	while (size < count &&
+	       wait_for_stat(chip,
+			     TPM_STS_DATA_AVAIL | TPM_STS_VALID,
+			     chip->vendor.timeout_c,
+			     &chip->vendor.read_queue)
+						== 0) {
+		burstcnt = get_burstcount(chip);
+		len = min_t(int, burstcnt, count - size);
+		status = SPI_READ_DATA(chip, TPM_DATA_FIFO, buf + size, len);
+		if (status < 0)
+			return status;
+
+
+		size += len;
+	}
+	return size;
+}
+
+/*
+ * tpm_ioserirq_handler the serirq irq handler
+ * @param: irq, the tpm chip description
+ * @param: dev_id, the description of the chip
+ * @return: the status of the handler.
+ */
+static irqreturn_t tpm_ioserirq_handler(int irq, void *dev_id)
+{
+	struct tpm_chip *chip = dev_id;
+	struct spi_device *client;
+	struct st33zp24_platform_data *pin_infos;
+
+	disable_irq_nosync(irq);
+	FUNC_ENTER();
+
+	client = (struct spi_device *) chip->vendor.iobase;
+	pin_infos = client->dev.platform_data;
+
+	complete(&pin_infos->irq_detection);
+	return IRQ_HANDLED;
+} /* tpm_ioserirq_handler() */
+
+/*
+ * tpm_stm_spi_send send TPM commands through the SPI bus.
+ * @param: chip, the tpm chip description
+ * @param: buf, the tpm command buffer
+ * @param: len, the tpm command size
+ * @return: In case of success the number of bytes sent.
+ *			In other case, a < 0 value describing the issue.
+ */
+static int tpm_stm_spi_send(struct tpm_chip *chip, unsigned char *buf,
+			    size_t len)
+{
+	u32 ret = 0, ordinal,
+	    status, burstcnt = 0, i, size = 0;
+	u8 data;
+
+	struct spi_device *client;
+	struct st33zp24_platform_data *pin_infos;
+
+	FUNC_ENTER();
+
+	if (chip == NULL)
+		return -EBUSY;
+	if (len < TPM_HEADER_SIZE)
+		return -EBUSY;
+
+	client = (struct spi_device *)chip->vendor.iobase;
+	pin_infos = client->dev.platform_data;
+
+	ordinal = be32_to_cpu(*((__be32 *) (buf + 6)));
+
+	ret = request_locality(chip);
+	if (ret < 0)
+		return ret;
+
+	status = tpm_stm_spi_status(chip);
+	if ((status & TPM_STS_COMMAND_READY) == 0) {
+		tpm_stm_spi_cancel(chip);
+		if (wait_for_stat
+		    (chip, TPM_STS_COMMAND_READY, chip->vendor.timeout_b,
+		     &chip->vendor.int_queue) < 0) {
+			ret = -ETIME;
+			goto out_err;
+		}
+	}
+
+	for (i = 0 ; i < len - 1 ;) {
+		burstcnt = get_burstcount(chip);
+		size = min_t(int, len - i - 1, burstcnt);
+		ret = SPI_WRITE_DATA(chip, TPM_DATA_FIFO, buf, size);
+		if (ret < 0)
+			goto out_err;
+		i += size;
+	}
+
+	status = tpm_stm_spi_status(chip);
+	if ((status & TPM_STS_DATA_EXPECT) == 0) {
+		ret = -EIO;
+		goto out_err;
+	}
+
+	/* write last byte */
+	SPI_WRITE_DATA(chip, TPM_DATA_FIFO, buf + len - 1, 1);
+
+	status = tpm_stm_spi_status(chip);
+	if ((status & TPM_STS_DATA_EXPECT) != 0) {
+		ret = -EIO;
+		goto out_err;
+	}
+
+	/* go and do it */
+	data = TPM_STS_GO;
+	ret = SPI_WRITE_DATA(chip, TPM_STS, &data, 1);
+	if (ret < 0)
+		goto out_err;
+
+	return len;
+out_err:
+	tpm_stm_spi_cancel(chip);
+	release_locality(chip);
+	return ret;
+}
+
+/*
+ * tpm_stm_spi_recv received TPM response through the SPI bus.
+ * @param: chip, the tpm chip description
+ * @param: buf, the tpm command buffer
+ * @param: len, the tpm command size
+* @return: In case of success the number of bytes received.
+ *			In other case, a < 0 value describing the issue.
+ */
+static int tpm_stm_spi_recv(struct tpm_chip *chip, unsigned char *buf,
+			    size_t count)
+{
+	int size = 0;
+	int expected;
+
+	struct spi_device *client;
+	struct st33zp24_platform_data *pin_infos;
+
+
+	FUNC_ENTER();
+
+	client = (struct spi_device *)chip->vendor.iobase;
+	pin_infos = client->dev.platform_data;
+
+
+	if (chip == NULL)
+		return -EBUSY;
+	if (count < TPM_HEADER_SIZE) {
+		size = -EIO;
+		goto out;
+	}
+
+	size = recv_data(chip, buf, TPM_HEADER_SIZE);
+
+	/* read first 10 bytes, including tag, paramsize, and result */
+	if (size < TPM_HEADER_SIZE) {
+		dev_err(chip->dev, "Unable to read header\n");
+		goto out;
+	}
+
+	expected = be32_to_cpu(*(__be32 *) (buf + 2));
+	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_spi_send send TPM locality 4 hash through the SPI bus.
+ * @param: chip, the tpm chip description
+ * @param: buf, the tpm data buffer
+ * @param: len, the tpm data size
+ * @return: In case of success the number of bytes sent.
+ *			In other case, a < 0 value describing the issue.
+ */
+static int tpm_stm_spi_send_hash(struct tpm_chip *chip, unsigned char *buf,
+			    size_t len)
+{
+	u32 ret = 0, burstcnt, i, size;
+	u8 data;
+
+	struct spi_device *client;
+	struct st33zp24_platform_data *pin_infos;
+
+	FUNC_ENTER();
+
+	if (chip == NULL)
+		return -EBUSY;
+
+	client = (struct spi_device *)chip->vendor.iobase;
+	pin_infos = client->dev.platform_data;
+
+	ret = request_locality(chip);
+	if (ret < 0)
+		goto end;
+
+	data = TPM_DUMMY_BYTE;
+	/* HASH_START */
+	ret = SPI_WRITE_DATA(chip, TPM_HASH_START, &data, 1);
+	if (ret < 0)
+		goto end;
+
+	/* Send data  */
+	for (i = 0 ; i < len ;) {
+		burstcnt = get_burstcount(chip);
+		if (burstcnt < 0)
+			goto end;
+		size = min_t(int, len - i, burstcnt);
+		ret = SPI_WRITE_DATA(chip, TPM_DATA_FIFO, buf + i, size);
+		if (ret < 0)
+			goto end;
+		i += size;
+	}
+
+end:
+	/* HASH_END   */
+	ret = SPI_WRITE_DATA(chip, TPM_HASH_END, &data, 1);
+	return ret;
+}
+
+
+/*
+ * tpm_st33_spi_ioctl provides 2 handles:
+ * - TPMIOC_CANCEL: allow to CANCEL a TPM commands execution.
+ *   See tpm_stm_spi_cancel description above
+ * - TPMIOC_TRANSMIT: allow to transmit a TPM commands.
+ * - TPMIOC_HASH: allow to compute a locality 4 hash
+ * - TPMIOC_CHANGELOCALITY: allow to change the current locality
+ *
+ * @return: In case of success, return TPM response size.
+ * In other case return < 0 value describing the issue.
+ */
+static ssize_t tpm_st33_spi_ioctl(struct inode *inode, struct file *file,
+				  unsigned int cmd, unsigned long arg)
+{
+	int in_size = 0, out_size = 0, rc;
+	int status;
+	unsigned long stop = 0;
+	struct tpm_chip *chip = file->private_data;
+	st_tpm_hash_t hash_data;
+
+	FUNC_ENTER();
+
+	switch (cmd) {
+	case TPMIOC_CANCEL:
+		tpm_stm_spi_cancel(chip);
+		return 0;
+	case TPMIOC_TRANSMIT:
+		if (copy_from_user(chip->data_buffer,
+		    (const char *)arg, TPM_HEADER_SIZE))
+			return -EFAULT;
+		in_size = responseSize(chip->data_buffer,
+				      TPM_HEADER_SIZE);
+		if (copy_from_user(chip->data_buffer,
+				   (const char *)arg, in_size))
+			return -EFAULT;
+		tpm_stm_spi_send(chip, chip->data_buffer, in_size);
+
+		out_size = tpm_stm_spi_recv(chip, chip->data_buffer,
+					    TPM_BUFSIZE);
+
+		if (copy_to_user((char *)arg, chip->data_buffer,
+				 out_size))
+			return -EFAULT;
+
+		return out_size;
+	case TPMIOC_HASH:
+		if (chip->vendor.locality == LOCALITY4) {
+			if (copy_from_user(&hash_data,
+					 (st_tpm_hash_t __user *)arg,
+						sizeof(st_tpm_hash_t)))
+				return -EFAULT;
+			tpm_stm_spi_send_hash(chip, hash_data.data,
+							 hash_data.size);
+
+			if (chip->vendor.irq) {
+				rc = wait_event_interruptible_timeout(
+						chip->vendor.read_queue,
+			(check_locality(chip) != chip->vendor.locality),
+						 chip->vendor.timeout_a);
+				if (rc < 0)
+					return -ETIME;
+				return hash_data.size;
+			}
+
+			stop = jiffies + chip->vendor.timeout_a;
+			do {
+				msleep(TPM_TIMEOUT);
+				status = check_locality(chip);
+				if (status != chip->vendor.locality)
+					return hash_data.size;
+			} while (time_before(jiffies, stop));
+
+			return -ETIME;
+		}
+		return -ENOSYS;
+		break;
+	case TPMIOC_CHANGELOCALITY:
+		if (arg >= LOCALITY0 && arg <= LOCALITY4) {
+			chip->vendor.locality = arg;
+			return 0;
+		}
+		return -ENOSYS;
+		break;
+	default:
+		return -ENOTTY;
+	}
+	return -ENOTTY;
+}
+
+static const struct file_operations tpm_st33_spi_fops = {
+	.owner = THIS_MODULE,
+	.llseek = no_llseek,
+	.read = tpm_read,
+	.unlocked_ioctl = tpm_st33_spi_ioctl,
+	.write = tpm_write,
+	.open = tpm_open,
+	.release = tpm_release,
+};
+
+static DEVICE_ATTR(pubek, S_IRUGO, tpm_show_pubek, NULL);
+static DEVICE_ATTR(pcrs, S_IRUGO, tpm_show_pcrs, NULL);
+static DEVICE_ATTR(enabled, S_IRUGO, tpm_show_enabled, NULL);
+static DEVICE_ATTR(active, S_IRUGO, tpm_show_active, NULL);
+static DEVICE_ATTR(owned, S_IRUGO, tpm_show_owned, NULL);
+static DEVICE_ATTR(temp_deactivated, S_IRUGO, tpm_show_temp_deactivated, NULL);
+static DEVICE_ATTR(caps, S_IRUGO, tpm_show_caps_1_2, NULL);
+static DEVICE_ATTR(cancel, S_IWUSR | S_IWGRP, NULL, tpm_store_cancel);
+
+static struct attribute *stm_tpm_attrs[] = {
+	&dev_attr_pubek.attr,
+	&dev_attr_pcrs.attr,
+	&dev_attr_enabled.attr,
+	&dev_attr_active.attr,
+	&dev_attr_owned.attr,
+	&dev_attr_temp_deactivated.attr,
+	&dev_attr_caps.attr,
+	&dev_attr_cancel.attr, NULL,
+};
+
+static struct attribute_group stm_tpm_attr_grp = {
+	.attrs = stm_tpm_attrs
+};
+
+static struct tpm_vendor_specific st_spi_tpm = {
+	.send = tpm_stm_spi_send,
+	.recv = tpm_stm_spi_recv,
+	.cancel = tpm_stm_spi_cancel,
+	.status = tpm_stm_spi_status,
+	.attr_group = &stm_tpm_attr_grp,
+	.miscdev = {.fops = &tpm_st33_spi_fops,},
+};
+
+static int evaluate_latency(struct tpm_chip *chip)
+{
+	int latency = 0;
+	struct spi_device *dev = (struct spi_device *) chip->vendor.iobase;
+	struct st33zp24_platform_data *platform_data = dev->dev.platform_data;
+	int status = 0;
+	u8 data;
+
+	for (; status == 0x00 && latency < MAX_SPI_LATENCY ; latency++) {
+		platform_data->latency = latency;
+		status = SPI_READ_DATA(chip, TPM_INTF_CAPABILITY, &data, 1);
+	}
+	return latency - 1;
+} /* evaluate_latency() */
+
+static int interrupts;
+module_param(interrupts, bool, 0444);
+MODULE_PARM_DESC(interrupts, "Enable interrupts");
+
+static int power_mgt = 1;
+module_param(power_mgt, bool, 0444);
+MODULE_PARM_DESC(power_mgt, "Power Management");
+
+/*
+ * tpm_st33_spi_probe initialize the TPM device
+ * @param: client, the spi_device drescription (TPM SPI description).
+ * @param: id, the spi_device_id struct.
+ * @return: 0 in case of success.
+ *	 -1 in other case.
+ */
+static int
+tpm_st33_spi_probe(struct spi_device *dev)
+{
+	u32 err;
+	u8 intmask;
+	struct tpm_chip *chip;
+	struct st33zp24_platform_data *platform_data;
+
+	FUNC_ENTER();
+
+	err = 0;
+
+	/* Check SPI platform functionnalities */
+	if (dev == NULL) {
+		pr_info("dev is NULL. exiting.\n");
+		err = -ENODEV;
+		goto end;
+	}
+
+	chip = tpm_register_hardware(&dev->dev, &st_spi_tpm);
+	if (!chip) {
+		err = -ENODEV;
+		goto end;
+	}
+
+	/* Allocation of SPI buffers MISO and MOSI		*/
+	/* Size is as follow:					*/
+	/* Request burstcount value  = 0x800 = 2048		*/
+	/* +							*/
+	/* Response burstcount value = 0x400 = 1024		*/
+	/* +							*/
+	/* At least:						*/
+	/* 1 byte for direction/locality			*/
+	/* 1 byte tpm tis register				*/
+	/* 2 bytes spi data length (for request only)		*/
+	/* 2 latency bytes					*/
+	/* 1 status byte					*/
+	/* = 2048 + 1024 + 7					*/
+	/* We reserved 2048 + 1024 + 20 in case latency byte	*/
+	/* change						*/
+	platform_data = dev->dev.platform_data;
+	platform_data->tpm_spi_buffer[0] =
+	    kmalloc((TPM_BUFSIZE + (TPM_BUFSIZE / 2) +
+				 TPM_DIGEST_SIZE) * sizeof(u8), GFP_KERNEL);
+	if (platform_data->tpm_spi_buffer[0] == NULL) {
+		err = -ENOMEM;
+		goto _tpm_clean_answer;
+	}
+	platform_data->tpm_spi_buffer[1] =
+	    kmalloc((TPM_BUFSIZE + (TPM_BUFSIZE / 2) +
+				 TPM_DIGEST_SIZE) * sizeof(u8) , GFP_KERNEL);
+	if (platform_data->tpm_spi_buffer[1] == NULL) {
+		err = -ENOMEM;
+		goto _tpm_clean_response;
+	}
+
+	chip->vendor.iobase = dev;
+
+	/* Default timeouts */
+	chip->vendor.timeout_a = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
+	chip->vendor.timeout_b = msecs_to_jiffies(TIS_LONG_TIMEOUT);
+	chip->vendor.timeout_c = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
+	chip->vendor.timeout_d = msecs_to_jiffies(TIS_SHORT_TIMEOUT);
+	chip->vendor.locality = LOCALITY0;
+
+	if (power_mgt) {
+		err = gpio_request(platform_data->io_lpcpd, "TPM IO_LPCPD");
+		if (err)
+			goto _gpio_init1;
+		gpio_set_value(platform_data->io_lpcpd, 1);
+	}
+
+	platform_data->latency = evaluate_latency(chip);
+	if (platform_data->latency <= 0x00) {
+		err = -ENODEV;
+		goto _gpio_init1;
+	}
+
+	/* Enable interrupt */
+	/* Register GPIO pin through generic Linux GPIO API */
+	if (interrupts) {
+		init_completion(&platform_data->irq_detection);
+		if (request_locality(chip) != LOCALITY0) {
+			err = -ENODEV;
+			goto _tpm_clean_response;
+		}
+		err = gpio_request(platform_data->io_serirq, "TPM IO_SERIRQ");
+		if (err)
+			goto _gpio_init2;
+
+		/* Clear all existing */
+		clear_interruption(chip);
+		err = request_irq(gpio_to_irq(platform_data->io_serirq),
+							&tpm_ioserirq_handler,
+				IRQF_TRIGGER_HIGH | IRQF_SHARED,
+				"TPM SERIRQ management", chip);
+		if (err < 0) {
+
+			pr_info("TPM SERIRQ signals %d not available\n",
+			gpio_to_irq(platform_data->io_serirq));
+			goto _irq_set;
+		}
+		err = SPI_READ_DATA(chip, TPM_INT_ENABLE, &intmask, 1);
+		if (err < 0)
+			goto _irq_set;
+
+		intmask |= TPM_INTF_CMD_READY_INT
+			|  TPM_INTF_FIFO_AVALAIBLE_INT
+			|  TPM_INTF_WAKE_UP_READY_INT
+			|  TPM_INTF_LOC4SOFTRELEASE_INT
+			|  TPM_INTF_LOCALITY_CHANGE_INT
+			|  TPM_INTF_STS_VALID_INT
+			|  TPM_INTF_DATA_AVAIL_INT;
+
+		err = SPI_WRITE_DATA(chip, TPM_INT_ENABLE, &intmask, 1);
+		if (err < 0)
+			goto _irq_set;
+
+		intmask = TPM_GLOBAL_INT_ENABLE;
+		err = SPI_WRITE_DATA(chip, TPM_INT_ENABLE + 3, &intmask, 1);
+		if (err < 0)
+			goto _irq_set;
+
+		err = SPI_READ_DATA(chip, TPM_INT_STATUS, &intmask, 1);
+		if (err < 0)
+			goto _irq_set;
+
+		chip->vendor.irq = interrupts;
+
+		tpm_gen_interrupt(chip);
+	}
+
+	tpm_get_timeouts(chip);
+
+	/* attach chip datas to client */
+	spi_set_drvdata(dev, chip);
+	platform_data->bChipF = false;
+
+	pr_info("TPM SPI Initialized\n");
+	return 0;
+_irq_set:
+	free_irq(gpio_to_irq(platform_data->io_serirq), chip);
+_gpio_init2:
+	if (platform_data && interrupts)
+		gpio_free(platform_data->io_serirq);
+_gpio_init1:
+	if (platform_data && power_mgt)
+		gpio_free(platform_data->io_lpcpd);
+_tpm_clean_response:
+	tpm_remove_hardware(chip->dev);
+	if (platform_data->tpm_spi_buffer[1] != NULL) {
+		kfree(platform_data->tpm_spi_buffer[1]);
+		platform_data->tpm_spi_buffer[1] = NULL;
+	}
+_tpm_clean_answer:
+	if (platform_data->tpm_spi_buffer[0] != NULL) {
+		kfree(platform_data->tpm_spi_buffer[0]);
+		platform_data->tpm_spi_buffer[0] = NULL;
+	}
+
+	platform_data->bChipF = true;
+end:
+	pr_info("TPM SPI initialisation fail\n");
+	return err;
+}
+
+/*
+ * tpm_st33_spi_remove remove the TPM device
+ * @param: client, the spi_device drescription (TPM SPI description).
+		clear_bit(0, &chip->is_open);
+ * @return: 0 in case of success.
+ */
+static __devexit int tpm_st33_spi_remove(struct spi_device *client)
+{
+	struct tpm_chip *chip = (struct tpm_chip *)spi_get_drvdata(client);
+	struct st33zp24_platform_data *pin_infos =
+		((struct spi_device *) chip->vendor.iobase)->dev.platform_data;
+
+	FUNC_ENTER();
+
+	if (pin_infos != NULL) {
+		gpio_free(pin_infos->io_lpcpd);
+
+		/* Check if chip has been previously clean */
+		if (pin_infos->bChipF != true)
+			tpm_remove_hardware(chip->dev);
+		if (pin_infos->tpm_spi_buffer[1] != NULL) {
+			kfree(pin_infos->tpm_spi_buffer[1]);
+			pin_infos->tpm_spi_buffer[1] = NULL;
+		}
+		if (pin_infos->tpm_spi_buffer[0] != NULL) {
+			kfree(pin_infos->tpm_spi_buffer[0]);
+			pin_infos->tpm_spi_buffer[0] = NULL;
+		}
+	}
+
+	return 0;
+}
+
+/*
+ * tpm_st33_spi_pm_suspend suspend the TPM device
+ * Added: Work around when suspend and no tpm application is running, suspend
+ * may fail because chip->data_buffer is not set (only set in tpm_open in Linux
+ * TPM core)
+ * @param: client, the spi_device drescription (TPM SPI description).
+ * @param: mesg, the power management message.
+ * @return: 0 in case of success.
+ */
+static int tpm_st33_spi_pm_suspend(struct spi_device *client,
+							 pm_message_t mesg)
+{
+	struct tpm_chip *chip =
+	    (struct tpm_chip *)spi_get_drvdata(client);
+	struct st33zp24_platform_data *pin_infos =
+		((struct spi_device *)chip->vendor.iobase)->dev.platform_data;
+	int ret = 0;
+
+	if (power_mgt)
+		gpio_set_value(pin_infos->io_lpcpd, 0);
+	else {
+		if (chip->data_buffer == NULL)
+			chip->data_buffer = pin_infos->tpm_spi_buffer[0];
+		ret = tpm_pm_suspend(&client->dev, mesg);
+	}
+	return ret;
+}				/* tpm_st33_spi_suspend() */
+
+/*
+ * tpm_st33_spi_pm_resume resume the TPM device
+ * @param: spi, the spi_device drescription (TPM SPI description).
+ * @return: 0 in case of success.
+ */
+static int tpm_st33_spi_pm_resume(struct spi_device *spi)
+{
+	struct tpm_chip *chip =
+	    (struct tpm_chip *)spi_get_drvdata(spi);
+	struct st33zp24_platform_data *pin_infos =
+		((struct spi_device *)chip->vendor.iobase)->dev.platform_data;
+
+	int ret = 0;
+	if (power_mgt) {
+		gpio_set_value(pin_infos->io_lpcpd, 1);
+		ret = wait_for_serirq_timeout(chip,
+					 (chip->vendor.status(chip) &&
+					  TPM_STS_VALID) == TPM_STS_VALID,
+					  chip->vendor.timeout_b);
+	} else {
+		if (chip->data_buffer == NULL)
+			chip->data_buffer = pin_infos->tpm_spi_buffer[0];
+		ret = tpm_pm_resume(&spi->dev);
+		if (!ret)
+			tpm_continue_selftest(chip);
+	}
+	return ret;
+}				/* tpm_st33_spi_pm_resume() */
+
+static struct spi_driver tpm_st33_spi_driver = {
+	.driver = {
+		   .owner = THIS_MODULE,
+		   .name = TPM_ST33_SPI,
+		   },
+	.probe = tpm_st33_spi_probe,
+	.remove = tpm_st33_spi_remove,
+	.resume = tpm_st33_spi_pm_resume,
+	.suspend = tpm_st33_spi_pm_suspend,
+};
+
+/*
+ * tpm_st33_spi_init initialize driver
+ * @return: 0 if successful, else non zero value.
+ */
+static int __init tpm_st33_spi_init(void)
+{
+	FUNC_ENTER();
+	return spi_register_driver(&tpm_st33_spi_driver);
+}
+
+/*
+ * tpm_st33_spi_exit The kernel calls this function during unloading the
+ * module or during shut down process
+ */
+static void __exit tpm_st33_spi_exit(void)
+{
+	FUNC_ENTER();
+	spi_unregister_driver(&tpm_st33_spi_driver);
+}
+
+module_init(tpm_st33_spi_init);
+module_exit(tpm_st33_spi_exit);
+
+MODULE_AUTHOR("Christophe Ricard (tpmsupport@st.com)");
+MODULE_DESCRIPTION("STM TPM SPI ST33 Driver");
+MODULE_VERSION("1.2.0");
+MODULE_LICENSE("GPL");
diff --git a/drivers/char/tpm/tpm_stm_st33_spi.h b/drivers/char/tpm/tpm_stm_st33_spi.h
new file mode 100644
index 0000000..96e91ba
--- /dev/null
+++ b/drivers/char/tpm/tpm_stm_st33_spi.h
@@ -0,0 +1,79 @@
+/*
+ * STMicroelectronics TPM I2C Linux driver for TPM ST33NP18
+ * Copyright (C) 2009, 2010  STMicroelectronics
+ *
+ * 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.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * STMicroelectronics version 1.2.0, Copyright (C) 2010
+ * STMicroelectronics comes with ABSOLUTELY NO WARRANTY.
+ * This is free software, and you are welcome to redistribute it
+ * under certain conditions.
+ *
+ * @Author: Christophe RICARD tpmsupport@st.com
+ *
+ * @File: stm_st33_tpm_i2c.h
+ *
+ * @Date: 09/15/2010
+ */
+#ifndef __STM_ST33_TPM_I2C_MAIN_H__
+#define __STM_ST33_TPM_I2C_MAIN_H__
+
+#include <linux/pci.h>
+#include <linux/module.h>
+#include <linux/platform_device.h>
+#include <linux/i2c.h>
+#include <linux/i2c-id.h>
+#include <linux/fs.h>
+#include <linux/miscdevice.h>
+
+#define MINOR_NUM_I2C		224
+
+/*#define TPM_DRIVER_NAME		"st33np18_i2c"*/
+
+#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			0x00
+#define TPM_WRITE_DIRECTION		0x80
+#define TPM_HEADER_SIZE			10
+#define TPM_BUFSIZE			2048
+
+/* ioctl commands */
+#define TPMIOC_CANCEL		_IO('T', 0x00)
+#define TPMIOC_TRANSMIT		_IO('T', 0x01)
+#define TPMIOC_HASH		_IO('T', 0x02)
+#define TPMIOC_CHANGELOCALITY	_IO('T', 0x03)
+
+#define LOCALITY0		0
+#define LOCALITY4		4
+
+#define TPM_ST_SPI_OK		0x5A
+
+
+#define MAX_SPI_LATENCY		15
+
+struct st_tpm_hash {
+	int size;
+	u8 *data;
+} st_tpm_hash_t;
+
+#endif /* __STM_ST33_TPM_I2C_MAIN_H__ */
diff --git a/include/linux/spi/tpm_stm_st33_spi.h b/include/linux/spi/tpm_stm_st33_spi.h
new file mode 100644
index 0000000..4bf31c4
--- /dev/null
+++ b/include/linux/spi/tpm_stm_st33_spi.h
@@ -0,0 +1,46 @@
+/*
+* STMicroelectronics TPM SPI Linux driver for TPM ST33NP18
+* Copyright (C) 2009, 2010 STMicroelectronics
+* Christophe RICARD tpmsupport@st.com
+* 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.,
+* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*
+* STMicroelectronics version 1.2.0, Copyright (C) 2010
+* STMicroelectronics comes with ABSOLUTELY NO WARRANTY.
+* This is free software, and you are welcome to redistribute it
+* under certain conditions.
+*
+* @File: stm_st33_tpm_spi.h
+*
+* @Date: 06/15/2008
+*/
+#ifndef __STM_ST33_TPM_SPI_H__
+#define __STM_ST33_TPM_SPI_H__
+
+#include <linux/spi/spi.h>
+
+#define TPM_ST33_SPI		"st33zp24_spi"
+
+#ifndef __STM_ST33_TPM_I2C_H__
+struct st33zp24_platform_data {
+	int io_serirq;
+	int io_lpcpd;
+	int latency;
+	bool bChipF;
+	u8 *tpm_spi_buffer[2]; /* 0 Request 1 Response */
+	struct completion irq_detection;
+};
+#endif
+
+#endif /* __STM_ST33_TPM_SPI_H__ */
-- 
1.7.1


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

end of thread, other threads:[~2013-07-25 15:20 UTC | newest]

Thread overview: 21+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-05-07 10:16 [PATCH 1/1] TPM: STMicroelectronics st33 driver SPI Matthias Leblanc
     [not found] ` <-7985513476024686594@unknownmsgid>
2013-05-10 15:06   ` [tpmdd-devel] " Kent Yoder
2013-05-13 15:30     ` Mathias LEBLANC
2013-05-13 15:56       ` Kent Yoder
  -- strict thread matches above, loose matches on Subject: below --
2013-07-25 15:20 Mathias leblanc
2013-05-17 13:10 Matthias Leblanc
2013-05-19 10:36 ` Andy Shevchenko
2013-05-23  7:43   ` Mathias LEBLANC
2013-05-23  8:05     ` Andy Shevchenko
2013-05-15 13:53 Matthias Leblanc
2013-05-06 11:14 Matthias Leblanc
2013-05-06 19:12 ` Kent Yoder
2013-04-29 13:39 Matthias Leblanc
2013-04-22 16:20 Matthias Leblanc
2013-04-22 18:26 ` Kent Yoder
2013-04-22  8:50 Mathias Leblanc
2013-04-12  8:44 Matthias Leblanc
2013-04-09 14:42 Matthias Leblanc
2013-04-08 14:02 Mathias Leblanc
2013-03-25 15:08 Matthias Leblanc
2013-01-07 18:50 Mathias Leblanc

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®