* [PATCH 1/1] TPM: STMicroelectronics st33 driver SPI
@ 2013-04-09 14:42 Matthias Leblanc
2013-04-10 20:32 ` [tpmdd-devel] " Peter Hüwe
0 siblings, 1 reply; 22+ 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 related [flat|nested] 22+ messages in thread
* Re: [tpmdd-devel] [PATCH 1/1] TPM: STMicroelectronics st33 driver SPI
2013-04-09 14:42 [PATCH 1/1] TPM: STMicroelectronics st33 driver SPI Matthias Leblanc
@ 2013-04-10 20:32 ` Peter Hüwe
2013-04-11 8:58 ` Mathias LEBLANC
0 siblings, 1 reply; 22+ messages in thread
From: Peter Hüwe @ 2013-04-10 20:32 UTC (permalink / raw)
To: tpmdd-devel, Matthias Leblanc, Kent Yoder, Jean-Luc Blanc
Cc: linux-kernel, Mathias leblanc
Hi Matthias,
Am Dienstag, 9. April 2013, 16:42:42 schrieb Matthias 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>
fails to compile for me.
Steps to reproduce (on x86_64):
make defconfig
git reset --hard tpmdd/tpmdd-03-04-13
make defconfig
git am /tmp/\[tpmdd-devel\]\ \[PATCH\ 1_1\]\ TPM_STMicroelectronics\ st33\ driver\ SPI.mbox
make menuconfig -> enable spi, gpio, and the TPM
make
LANG=C make
make[1]: Nothing to be done for `all'.
make[1]: Nothing to be done for `relocs'.
CHK include/generated/uapi/linux/version.h
CHK include/generated/utsrelease.h
CALL scripts/checksyscalls.sh
CHK include/generated/compile.h
make[3]: `arch/x86/realmode/rm/realmode.bin' is up to date.
LD [M] drivers/char/tpm/tpm_bios.o
CC [M] drivers/char/tpm/tpm_spi_stm_st33.o
drivers/char/tpm/tpm_spi_stm_st33.c: In function 'write8_reg':
drivers/char/tpm/tpm_spi_stm_st33.c:115:33: error: dereferencing pointer to incomplete type
drivers/char/tpm/tpm_spi_stm_st33.c:118:27: error: dereferencing pointer to incomplete type
drivers/char/tpm/tpm_spi_stm_st33.c:121:28: error: dereferencing pointer to incomplete type
drivers/char/tpm/tpm_spi_stm_st33.c:149:32: error: dereferencing pointer to incomplete type
drivers/char/tpm/tpm_spi_stm_st33.c: In function 'read8_reg':
drivers/char/tpm/tpm_spi_stm_st33.c:188:33: error: dereferencing pointer to incomplete type
drivers/char/tpm/tpm_spi_stm_st33.c:191:27: error: dereferencing pointer to incomplete type
drivers/char/tpm/tpm_spi_stm_st33.c:195:28: error: dereferencing pointer to incomplete type
drivers/char/tpm/tpm_spi_stm_st33.c: In function '_wait_for_interrupt_serirq_timeout':
drivers/char/tpm/tpm_spi_stm_st33.c:323:15: error: dereferencing pointer to incomplete type
drivers/char/tpm/tpm_spi_stm_st33.c:325:35: error: dereferencing pointer to incomplete type
drivers/char/tpm/tpm_spi_stm_st33.c:326:32: error: dereferencing pointer to incomplete type
drivers/char/tpm/tpm_spi_stm_st33.c: In function 'tpm_ioserirq_handler':
drivers/char/tpm/tpm_spi_stm_st33.c:605:21: error: dereferencing pointer to incomplete type
drivers/char/tpm/tpm_spi_stm_st33.c: In function 'evaluate_latency':
drivers/char/tpm/tpm_spi_stm_st33.c:802:16: error: dereferencing pointer to incomplete type
drivers/char/tpm/tpm_spi_stm_st33.c: In function 'tpm_st33_spi_probe':
drivers/char/tpm/tpm_spi_stm_st33.c:864:15: error: dereferencing pointer to incomplete type
drivers/char/tpm/tpm_spi_stm_st33.c:867:19: error: dereferencing pointer to incomplete type
drivers/char/tpm/tpm_spi_stm_st33.c:871:15: error: dereferencing pointer to incomplete type
drivers/char/tpm/tpm_spi_stm_st33.c:874:19: error: dereferencing pointer to incomplete type
drivers/char/tpm/tpm_spi_stm_st33.c:889:35: error: dereferencing pointer to incomplete type
drivers/char/tpm/tpm_spi_stm_st33.c:892:31: error: dereferencing pointer to incomplete type
drivers/char/tpm/tpm_spi_stm_st33.c:895:15: error: dereferencing pointer to incomplete type
drivers/char/tpm/tpm_spi_stm_st33.c:896:19: error: dereferencing pointer to incomplete type
drivers/char/tpm/tpm_spi_stm_st33.c:904:33: error: dereferencing pointer to incomplete type
drivers/char/tpm/tpm_spi_stm_st33.c:909:35: error: dereferencing pointer to incomplete type
drivers/char/tpm/tpm_spi_stm_st33.c:915:46: error: dereferencing pointer to incomplete type
drivers/char/tpm/tpm_spi_stm_st33.c:921:4: error: dereferencing pointer to incomplete type
drivers/char/tpm/tpm_spi_stm_st33.c:959:15: error: dereferencing pointer to incomplete type
drivers/char/tpm/tpm_spi_stm_st33.c:964:36: error: dereferencing pointer to incomplete type
drivers/char/tpm/tpm_spi_stm_st33.c:967:26: error: dereferencing pointer to incomplete type
drivers/char/tpm/tpm_spi_stm_st33.c:970:26: error: dereferencing pointer to incomplete type
drivers/char/tpm/tpm_spi_stm_st33.c:973:19: error: dereferencing pointer to incomplete type
drivers/char/tpm/tpm_spi_stm_st33.c:974:22: error: dereferencing pointer to incomplete type
drivers/char/tpm/tpm_spi_stm_st33.c:975:16: error: dereferencing pointer to incomplete type
drivers/char/tpm/tpm_spi_stm_st33.c:978:19: error: dereferencing pointer to incomplete type
drivers/char/tpm/tpm_spi_stm_st33.c:979:22: error: dereferencing pointer to incomplete type
drivers/char/tpm/tpm_spi_stm_st33.c:980:16: error: dereferencing pointer to incomplete type
drivers/char/tpm/tpm_spi_stm_st33.c:983:15: error: dereferencing pointer to incomplete type
drivers/char/tpm/tpm_spi_stm_st33.c: In function 'tpm_st33_spi_remove':
drivers/char/tpm/tpm_spi_stm_st33.c:1004:22: error: dereferencing pointer to incomplete type
drivers/char/tpm/tpm_spi_stm_st33.c:1007:16: error: dereferencing pointer to incomplete type
drivers/char/tpm/tpm_spi_stm_st33.c:1009:16: error: dereferencing pointer to incomplete type
drivers/char/tpm/tpm_spi_stm_st33.c:1010:19: error: dereferencing pointer to incomplete type
drivers/char/tpm/tpm_spi_stm_st33.c:1011:13: error: dereferencing pointer to incomplete type
drivers/char/tpm/tpm_spi_stm_st33.c:1013:16: error: dereferencing pointer to incomplete type
drivers/char/tpm/tpm_spi_stm_st33.c:1014:19: error: dereferencing pointer to incomplete type
drivers/char/tpm/tpm_spi_stm_st33.c:1015:13: error: dereferencing pointer to incomplete type
drivers/char/tpm/tpm_spi_stm_st33.c: In function 'tpm_st33_spi_pm_suspend':
drivers/char/tpm/tpm_spi_stm_st33.c:1041:27: error: dereferencing pointer to incomplete type
drivers/char/tpm/tpm_spi_stm_st33.c:1044:33: error: dereferencing pointer to incomplete type
drivers/char/tpm/tpm_spi_stm_st33.c: In function 'tpm_st33_spi_pm_resume':
drivers/char/tpm/tpm_spi_stm_st33.c:1064:27: error: dereferencing pointer to incomplete type
drivers/char/tpm/tpm_spi_stm_st33.c:1071:33: error: dereferencing pointer to incomplete type
make[3]: *** [drivers/char/tpm/tpm_spi_stm_st33.o] Error 1
make[2]: *** [drivers/char/tpm] Error 2
make[1]: *** [drivers/char] Error 2
make: *** [drivers] Error 2
This is caused by your double inclusion guard in
include/linux/spi/tpm_spi_stm_st33.h
#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__ <------- the define was just a few lines before
struct st33zp24_platform_data {
Regards,
Peter
^ permalink raw reply [flat|nested] 22+ messages in thread
* RE: [tpmdd-devel] [PATCH 1/1] TPM: STMicroelectronics st33 driver SPI
2013-04-10 20:32 ` [tpmdd-devel] " Peter Hüwe
@ 2013-04-11 8:58 ` Mathias LEBLANC
2013-04-11 21:44 ` Peter Hüwe
0 siblings, 1 reply; 22+ messages in thread
From: Mathias LEBLANC @ 2013-04-11 8:58 UTC (permalink / raw)
To: Peter Hüwe, tpmdd-devel, Kent Yoder, Jean-Luc BLANC
Cc: linux-kernel, Mathias leblanc
Hi Peter,
Yes, you have right, these error are generated by #ifndef __STM_ST33_TPM_SPI_H__
Just remove it and the #endif next the structure.
Did you have these warning when you have run checkpatch script?
Thanks,
Mathias
-----Original Message-----
From: Peter Hüwe [mailto:PeterHuewe@gmx.de]
Sent: 10 April, 2013 22:32
To: tpmdd-devel@lists.sourceforge.net; Mathias LEBLANC; Kent Yoder; Jean-Luc BLANC
Cc: linux-kernel@vger.kernel.org; Mathias leblanc
Subject: Re: [tpmdd-devel] [PATCH 1/1] TPM: STMicroelectronics st33 driver SPI
Hi Matthias,
Am Dienstag, 9. April 2013, 16:42:42 schrieb Matthias 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>
fails to compile for me.
Steps to reproduce (on x86_64):
make defconfig
git reset --hard tpmdd/tpmdd-03-04-13
make defconfig
git am /tmp/\[tpmdd-devel\]\ \[PATCH\ 1_1\]\ TPM_STMicroelectronics\ st33\ driver\ SPI.mbox make menuconfig -> enable spi, gpio, and the TPM make
LANG=C make
make[1]: Nothing to be done for `all'.
make[1]: Nothing to be done for `relocs'.
CHK include/generated/uapi/linux/version.h
CHK include/generated/utsrelease.h
CALL scripts/checksyscalls.sh
CHK include/generated/compile.h
make[3]: `arch/x86/realmode/rm/realmode.bin' is up to date.
LD [M] drivers/char/tpm/tpm_bios.o
CC [M] drivers/char/tpm/tpm_spi_stm_st33.o
drivers/char/tpm/tpm_spi_stm_st33.c: In function 'write8_reg':
drivers/char/tpm/tpm_spi_stm_st33.c:115:33: error: dereferencing pointer to incomplete type
drivers/char/tpm/tpm_spi_stm_st33.c:118:27: error: dereferencing pointer to incomplete type
drivers/char/tpm/tpm_spi_stm_st33.c:121:28: error: dereferencing pointer to incomplete type
drivers/char/tpm/tpm_spi_stm_st33.c:149:32: error: dereferencing pointer to incomplete type
drivers/char/tpm/tpm_spi_stm_st33.c: In function 'read8_reg':
drivers/char/tpm/tpm_spi_stm_st33.c:188:33: error: dereferencing pointer to incomplete type
drivers/char/tpm/tpm_spi_stm_st33.c:191:27: error: dereferencing pointer to incomplete type
drivers/char/tpm/tpm_spi_stm_st33.c:195:28: error: dereferencing pointer to incomplete type
drivers/char/tpm/tpm_spi_stm_st33.c: In function '_wait_for_interrupt_serirq_timeout':
drivers/char/tpm/tpm_spi_stm_st33.c:323:15: error: dereferencing pointer to incomplete type
drivers/char/tpm/tpm_spi_stm_st33.c:325:35: error: dereferencing pointer to incomplete type
drivers/char/tpm/tpm_spi_stm_st33.c:326:32: error: dereferencing pointer to incomplete type
drivers/char/tpm/tpm_spi_stm_st33.c: In function 'tpm_ioserirq_handler':
drivers/char/tpm/tpm_spi_stm_st33.c:605:21: error: dereferencing pointer to incomplete type
drivers/char/tpm/tpm_spi_stm_st33.c: In function 'evaluate_latency':
drivers/char/tpm/tpm_spi_stm_st33.c:802:16: error: dereferencing pointer to incomplete type
drivers/char/tpm/tpm_spi_stm_st33.c: In function 'tpm_st33_spi_probe':
drivers/char/tpm/tpm_spi_stm_st33.c:864:15: error: dereferencing pointer to incomplete type
drivers/char/tpm/tpm_spi_stm_st33.c:867:19: error: dereferencing pointer to incomplete type
drivers/char/tpm/tpm_spi_stm_st33.c:871:15: error: dereferencing pointer to incomplete type
drivers/char/tpm/tpm_spi_stm_st33.c:874:19: error: dereferencing pointer to incomplete type
drivers/char/tpm/tpm_spi_stm_st33.c:889:35: error: dereferencing pointer to incomplete type
drivers/char/tpm/tpm_spi_stm_st33.c:892:31: error: dereferencing pointer to incomplete type
drivers/char/tpm/tpm_spi_stm_st33.c:895:15: error: dereferencing pointer to incomplete type
drivers/char/tpm/tpm_spi_stm_st33.c:896:19: error: dereferencing pointer to incomplete type
drivers/char/tpm/tpm_spi_stm_st33.c:904:33: error: dereferencing pointer to incomplete type
drivers/char/tpm/tpm_spi_stm_st33.c:909:35: error: dereferencing pointer to incomplete type
drivers/char/tpm/tpm_spi_stm_st33.c:915:46: error: dereferencing pointer to incomplete type
drivers/char/tpm/tpm_spi_stm_st33.c:921:4: error: dereferencing pointer to incomplete type
drivers/char/tpm/tpm_spi_stm_st33.c:959:15: error: dereferencing pointer to incomplete type
drivers/char/tpm/tpm_spi_stm_st33.c:964:36: error: dereferencing pointer to incomplete type
drivers/char/tpm/tpm_spi_stm_st33.c:967:26: error: dereferencing pointer to incomplete type
drivers/char/tpm/tpm_spi_stm_st33.c:970:26: error: dereferencing pointer to incomplete type
drivers/char/tpm/tpm_spi_stm_st33.c:973:19: error: dereferencing pointer to incomplete type
drivers/char/tpm/tpm_spi_stm_st33.c:974:22: error: dereferencing pointer to incomplete type
drivers/char/tpm/tpm_spi_stm_st33.c:975:16: error: dereferencing pointer to incomplete type
drivers/char/tpm/tpm_spi_stm_st33.c:978:19: error: dereferencing pointer to incomplete type
drivers/char/tpm/tpm_spi_stm_st33.c:979:22: error: dereferencing pointer to incomplete type
drivers/char/tpm/tpm_spi_stm_st33.c:980:16: error: dereferencing pointer to incomplete type
drivers/char/tpm/tpm_spi_stm_st33.c:983:15: error: dereferencing pointer to incomplete type
drivers/char/tpm/tpm_spi_stm_st33.c: In function 'tpm_st33_spi_remove':
drivers/char/tpm/tpm_spi_stm_st33.c:1004:22: error: dereferencing pointer to incomplete type
drivers/char/tpm/tpm_spi_stm_st33.c:1007:16: error: dereferencing pointer to incomplete type
drivers/char/tpm/tpm_spi_stm_st33.c:1009:16: error: dereferencing pointer to incomplete type
drivers/char/tpm/tpm_spi_stm_st33.c:1010:19: error: dereferencing pointer to incomplete type
drivers/char/tpm/tpm_spi_stm_st33.c:1011:13: error: dereferencing pointer to incomplete type
drivers/char/tpm/tpm_spi_stm_st33.c:1013:16: error: dereferencing pointer to incomplete type
drivers/char/tpm/tpm_spi_stm_st33.c:1014:19: error: dereferencing pointer to incomplete type
drivers/char/tpm/tpm_spi_stm_st33.c:1015:13: error: dereferencing pointer to incomplete type
drivers/char/tpm/tpm_spi_stm_st33.c: In function 'tpm_st33_spi_pm_suspend':
drivers/char/tpm/tpm_spi_stm_st33.c:1041:27: error: dereferencing pointer to incomplete type
drivers/char/tpm/tpm_spi_stm_st33.c:1044:33: error: dereferencing pointer to incomplete type
drivers/char/tpm/tpm_spi_stm_st33.c: In function 'tpm_st33_spi_pm_resume':
drivers/char/tpm/tpm_spi_stm_st33.c:1064:27: error: dereferencing pointer to incomplete type
drivers/char/tpm/tpm_spi_stm_st33.c:1071:33: error: dereferencing pointer to incomplete type
make[3]: *** [drivers/char/tpm/tpm_spi_stm_st33.o] Error 1
make[2]: *** [drivers/char/tpm] Error 2
make[1]: *** [drivers/char] Error 2
make: *** [drivers] Error 2
This is caused by your double inclusion guard in
include/linux/spi/tpm_spi_stm_st33.h
#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__ <------- the define was just a few lines before
struct st33zp24_platform_data {
Regards,
Peter
^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: [tpmdd-devel] [PATCH 1/1] TPM: STMicroelectronics st33 driver SPI
2013-04-11 8:58 ` Mathias LEBLANC
@ 2013-04-11 21:44 ` Peter Hüwe
0 siblings, 0 replies; 22+ messages in thread
From: Peter Hüwe @ 2013-04-11 21:44 UTC (permalink / raw)
To: tpmdd-devel, Matthias Leblanc
Cc: Mathias LEBLANC, Kent Yoder, Jean-Luc BLANC, linux-kernel,
Mathias leblanc
Hi Matthias,
Am Donnerstag, 11. April 2013, 10:58:59 schrieb Mathias LEBLANC:
> Hi Peter,
>
> Yes, you have right, these error are generated by #ifndef
> __STM_ST33_TPM_SPI_H__ Just remove it and the #endif next the structure.
Yes, I know that - for the submission to be included this has to be changed by
you.
> Did you have these warning when you have run checkpatch script?
You mean the sparse warnings I sent you?
I removed the superflous ifdef to compile it and then let sparse run against
it.
The driver passes normal checkpatch check, unless you call it with -strict
Peter
^ permalink raw reply [flat|nested] 22+ messages in thread
* RE: [tpmdd-devel] [PATCH 1/1] TPM: STMicroelectronics st33 driver SPI
2013-05-16 19:03 ` Peter Hüwe
@ 2013-05-17 12:26 ` Mathias LEBLANC
0 siblings, 0 replies; 22+ messages in thread
From: Mathias LEBLANC @ 2013-05-17 12:26 UTC (permalink / raw)
To: Peter Hüwe, tpmdd-devel
Cc: Jean-Luc BLANC, linux-kernel, Rajiv Andrade, Sirrix AG
Hi Peter,
Thanks for this new function.
I had already changed the for loop but i would to be sure that it's the best way and that you expected.
Don't hesitate if you have any other recommendation or any comments for this driver.
Regards,
Mathias Leblanc
________________________________________
From: Peter Hüwe [PeterHuewe@gmx.de]
Sent: 16 May 2013 21:03
To: tpmdd-devel@lists.sourceforge.net
Cc: Mathias LEBLANC; Jean-Luc BLANC; linux-kernel@vger.kernel.org; Rajiv Andrade; Sirrix AG
Subject: Re: [tpmdd-devel] [PATCH 1/1] TPM: STMicroelectronics st33 driver SPI
Hi Matthias:
> Regarding the while loop, I don't see how can I check the number of dummy
> byte differently?
Checking the number of dummy bytes is fine -
in line #144 you check it with a while loop which looks fine
in line #198 you check it with a for loop with an empty body, which in my
opinion looks less nice than the while in #144.
So simply replace the second instance in #198 and I'm happy with that part ;)
Maybe also have a look at memchr_inv
/**
* memchr_inv - Find an unmatching character in an area of memory.
* @start: The memory area
* @c: Find a character other than c
* @bytes: The size of the area.
*
* returns the address of the first character other than @c, or %NULL
* if the whole buffer contains just @c.
*/
void *memchr_inv(const void *start, int c, size_t bytes)
so #198 would look something like:
nbr_dummy_bytes = memchr_inv (xfer.rx_buf+nbr_dummy_bytes,
0,
total_length-nbr_dummy_bytes) - xfer.rx_buf;
But I'm not sure whether that's more readable, maybe give it a try.
I'm also fine with the while loop.
Peter
^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: [tpmdd-devel] [PATCH 1/1] TPM: STMicroelectronics st33 driver SPI
2013-05-16 8:45 ` Mathias LEBLANC
@ 2013-05-16 19:03 ` Peter Hüwe
2013-05-17 12:26 ` Mathias LEBLANC
0 siblings, 1 reply; 22+ messages in thread
From: Peter Hüwe @ 2013-05-16 19:03 UTC (permalink / raw)
To: tpmdd-devel
Cc: Mathias LEBLANC, Jean-Luc BLANC, linux-kernel, Rajiv Andrade, Sirrix AG
Hi Matthias:
> Regarding the while loop, I don't see how can I check the number of dummy
> byte differently?
Checking the number of dummy bytes is fine -
in line #144 you check it with a while loop which looks fine
in line #198 you check it with a for loop with an empty body, which in my
opinion looks less nice than the while in #144.
So simply replace the second instance in #198 and I'm happy with that part ;)
Maybe also have a look at memchr_inv
/**
* memchr_inv - Find an unmatching character in an area of memory.
* @start: The memory area
* @c: Find a character other than c
* @bytes: The size of the area.
*
* returns the address of the first character other than @c, or %NULL
* if the whole buffer contains just @c.
*/
void *memchr_inv(const void *start, int c, size_t bytes)
so #198 would look something like:
nbr_dummy_bytes = memchr_inv (xfer.rx_buf+nbr_dummy_bytes,
0,
total_length-nbr_dummy_bytes) - xfer.rx_buf;
But I'm not sure whether that's more readable, maybe give it a try.
I'm also fine with the while loop.
Peter
^ permalink raw reply [flat|nested] 22+ messages in thread
* RE: [tpmdd-devel] [PATCH 1/1] TPM: STMicroelectronics st33 driver SPI
2013-05-15 22:29 ` [tpmdd-devel] " Peter Hüwe
@ 2013-05-16 8:45 ` Mathias LEBLANC
2013-05-16 19:03 ` Peter Hüwe
0 siblings, 1 reply; 22+ messages in thread
From: Mathias LEBLANC @ 2013-05-16 8:45 UTC (permalink / raw)
To: Peter Hüwe, tpmdd-devel
Cc: Kent Yoder, Rajiv Andrade, Marcel Selhorst, Sirrix AG,
Jean-Luc BLANC, linux-kernel
Hi Peter,
Thanks for your feedback.
You are right, this function looks like the spi_sync_transfer;
So, I will use it in the next version of the driver.
Regarding the while loop, I don't see how can I check the number of dummy byte differently?
For the module error, Yes I have modified the patch file :s, apparently I haven't correctly modified the number of lines
Finaly, These sparse warning are finaly fix ;).
Best regards,
Mathias Leblanc
________________________________________
From: Peter Hüwe [PeterHuewe@gmx.de]
Sent: 16 May 2013 00:29
To: tpmdd-devel@lists.sourceforge.net
Cc: Mathias LEBLANC; Kent Yoder; Rajiv Andrade; Marcel Selhorst; Sirrix AG; Jean-Luc BLANC; linux-kernel@vger.kernel.org
Subject: Re: [tpmdd-devel] [PATCH 1/1] TPM: STMicroelectronics st33 driver SPI
Hi Matthias,
Am Mittwoch, 15. Mai 2013, 15:53:19 schrieb Matthias Leblanc:
> +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);
> +}
This looks extremely like the predefined spi_sync_transfer in include/linux/spi.h
/**
* spi_sync_transfer - synchronous SPI data transfer
* @spi: device with which data will be exchanged
* @xfers: An array of spi_transfers
* @num_xfers: Number of items in the xfer array
* Context: can sleep
*
* Does a synchronous SPI data transfer of the given spi_transfer array.
*
* For more specific semantics see spi_sync().
*
* It returns zero on success, else a negative error code.
*/
static inline int
spi_sync_transfer(struct spi_device *spi, struct spi_transfer *xfers,
unsigned int num_xfers)
{
struct spi_message msg;
spi_message_init_with_transfers(&msg, xfers, num_xfers);
return spi_sync(spi, &msg);
}
where spi_message_init_with_transfers is defined as
static inline void
spi_message_init_with_transfers(struct spi_message *m,
struct spi_transfer *xfers, unsigned int num_xfers)
{
unsigned int i;
spi_message_init(m);
for (i = 0; i < num_xfers; ++i)
spi_message_add_tail(&xfers[i], m);
}
If you combine these functions you end up (more or less) with your implementation
So please use spi_sync_transfer
OR as an alternative use spi_write / spi_read as an alternative as mentioned in a previous email
And here is a rather subjective remark:
> + for (; nbr_dummy_bytes < total_length &&
> + ((u8 *)xfer.rx_buf)[nbr_dummy_bytes] == 0;
> + nbr_dummy_bytes++)
> + ;
Not sure if it would be easier to read using a while and putting the increment
into the loop body
>+ while (nbr_dummy_bytes < total_length &&
>+ ((u8 *)xfer.rx_buf)[nbr_dummy_bytes] == 0)
>+ nbr_dummy_bytes++;
I usually don't like empty loop bodies, as they tend to be overlooked.
> +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");
Something is strange here - if I apply your patch, the module license gets lost - so I get a compile warning:
WARNING: modpost: missing MODULE_LICENSE() in /data/data-old/linux-2.6/drivers/char/tpm/tpm_spi_stm_st33.o
Do you edit the patch by hand before sending?
Steps to reproduce:
drivers/char/tpm $ git reset --hard tpmdd/tpmdd-04-26-13
HEAD is now at d224213 tpm_tis: missing platform_driver_unregister() on error in init_tis()
drivers/char/tpm $ git am /tmp/\[tpmdd-devel\]\ \[PATCH\ 1_1\]\ TPM_STMicroelectronics\ st33\ driver\ SPI.mbox
Applying: TPM: STMicroelectronics st33 driver SPI
drivers/char/tpm $ tail tpm_spi_stm_st33.c },
.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");
And the usual sparse warnings ;)
drivers/char/tpm $ make M=`pwd` -C /data/data-old/linux-2.6/ modules C=1 CHECK=sparse
make: Entering directory `/data/data-old/linux-2.6'
CHECK /data/data-old/linux-2.6/drivers/char/tpm/tpm_spi_stm_st33.c
/data/data-old/linux-2.6/drivers/char/tpm/tpm_spi_stm_st33.c:104:35: warning: cast removes address space of expression
/data/data-old/linux-2.6/drivers/char/tpm/tpm_spi_stm_st33.c:170:35: warning: cast removes address space of expression
/data/data-old/linux-2.6/drivers/char/tpm/tpm_spi_stm_st33.c:242:19: warning: cast removes address space of expression
/data/data-old/linux-2.6/drivers/char/tpm/tpm_spi_stm_st33.c:235:15: warning: symbol 'wait_for_serirq_timeout' was not declared. Should it be static?
/data/data-old/linux-2.6/drivers/char/tpm/tpm_spi_stm_st33.c:482:19: warning: cast removes address space of expression
Regards,
Peter
^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: [tpmdd-devel] [PATCH 1/1] TPM: STMicroelectronics st33 driver SPI
2013-05-15 13:53 Matthias Leblanc
@ 2013-05-15 22:29 ` Peter Hüwe
2013-05-16 8:45 ` Mathias LEBLANC
0 siblings, 1 reply; 22+ messages in thread
From: Peter Hüwe @ 2013-05-15 22:29 UTC (permalink / raw)
To: tpmdd-devel
Cc: Matthias Leblanc, Kent Yoder, Rajiv Andrade, Marcel Selhorst,
Sirrix AG, Jean-Luc Blanc, linux-kernel
Hi Matthias,
Am Mittwoch, 15. Mai 2013, 15:53:19 schrieb Matthias Leblanc:
> +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);
> +}
This looks extremely like the predefined spi_sync_transfer in include/linux/spi.h
/**
* spi_sync_transfer - synchronous SPI data transfer
* @spi: device with which data will be exchanged
* @xfers: An array of spi_transfers
* @num_xfers: Number of items in the xfer array
* Context: can sleep
*
* Does a synchronous SPI data transfer of the given spi_transfer array.
*
* For more specific semantics see spi_sync().
*
* It returns zero on success, else a negative error code.
*/
static inline int
spi_sync_transfer(struct spi_device *spi, struct spi_transfer *xfers,
unsigned int num_xfers)
{
struct spi_message msg;
spi_message_init_with_transfers(&msg, xfers, num_xfers);
return spi_sync(spi, &msg);
}
where spi_message_init_with_transfers is defined as
static inline void
spi_message_init_with_transfers(struct spi_message *m,
struct spi_transfer *xfers, unsigned int num_xfers)
{
unsigned int i;
spi_message_init(m);
for (i = 0; i < num_xfers; ++i)
spi_message_add_tail(&xfers[i], m);
}
If you combine these functions you end up (more or less) with your implementation
So please use spi_sync_transfer
OR as an alternative use spi_write / spi_read as an alternative as mentioned in a previous email
And here is a rather subjective remark:
> + for (; nbr_dummy_bytes < total_length &&
> + ((u8 *)xfer.rx_buf)[nbr_dummy_bytes] == 0;
> + nbr_dummy_bytes++)
> + ;
Not sure if it would be easier to read using a while and putting the increment
into the loop body
>+ while (nbr_dummy_bytes < total_length &&
>+ ((u8 *)xfer.rx_buf)[nbr_dummy_bytes] == 0)
>+ nbr_dummy_bytes++;
I usually don't like empty loop bodies, as they tend to be overlooked.
> +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");
Something is strange here - if I apply your patch, the module license gets lost - so I get a compile warning:
WARNING: modpost: missing MODULE_LICENSE() in /data/data-old/linux-2.6/drivers/char/tpm/tpm_spi_stm_st33.o
Do you edit the patch by hand before sending?
Steps to reproduce:
drivers/char/tpm $ git reset --hard tpmdd/tpmdd-04-26-13
HEAD is now at d224213 tpm_tis: missing platform_driver_unregister() on error in init_tis()
drivers/char/tpm $ git am /tmp/\[tpmdd-devel\]\ \[PATCH\ 1_1\]\ TPM_STMicroelectronics\ st33\ driver\ SPI.mbox
Applying: TPM: STMicroelectronics st33 driver SPI
drivers/char/tpm $ tail tpm_spi_stm_st33.c },
.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");
And the usual sparse warnings ;)
drivers/char/tpm $ make M=`pwd` -C /data/data-old/linux-2.6/ modules C=1 CHECK=sparse
make: Entering directory `/data/data-old/linux-2.6'
CHECK /data/data-old/linux-2.6/drivers/char/tpm/tpm_spi_stm_st33.c
/data/data-old/linux-2.6/drivers/char/tpm/tpm_spi_stm_st33.c:104:35: warning: cast removes address space of expression
/data/data-old/linux-2.6/drivers/char/tpm/tpm_spi_stm_st33.c:170:35: warning: cast removes address space of expression
/data/data-old/linux-2.6/drivers/char/tpm/tpm_spi_stm_st33.c:242:19: warning: cast removes address space of expression
/data/data-old/linux-2.6/drivers/char/tpm/tpm_spi_stm_st33.c:235:15: warning: symbol 'wait_for_serirq_timeout' was not declared. Should it be static?
/data/data-old/linux-2.6/drivers/char/tpm/tpm_spi_stm_st33.c:482:19: warning: cast removes address space of expression
Regards,
Peter
^ permalink raw reply [flat|nested] 22+ 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; 22+ 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] 22+ 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; 22+ 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] 22+ 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; 22+ 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] 22+ messages in thread
* Re: [tpmdd-devel] [PATCH 1/1] TPM: STMicroelectronics st33 driver SPI
2013-04-28 1:16 ` Peter Hüwe
@ 2013-04-29 14:15 ` Kent Yoder
0 siblings, 0 replies; 22+ messages in thread
From: Kent Yoder @ 2013-04-29 14:15 UTC (permalink / raw)
To: Peter Hüwe
Cc: tpmdd-devel, Mathias Leblanc, Rajiv Andrade, Marcel Selhorst,
Sirrix AG, Jean-Luc Blanc, linux-kernel, Matthias Leblanc
On Sun, Apr 28, 2013 at 03:16:33AM +0200, Peter Hüwe wrote:
> Hi Matthias,
>
> it's nice to see that you consider most of the comments, unfortunately I still
> have some left ;)
>
> > +/*
> > + * 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);
>
> Please use module_spi_driver(&tpm_st33_spi_driver); instead
> Boilerplate code sucks.
>
>
> > + 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;
> >...
> > + xfer.len = total_length;
> > + spi_message_init(&msg);
> > + spi_message_add_tail(&xfer, &msg);
> > + value = spi_sync(dev, &msg);
>
>
> Doesn't spi_write fit here ?
>
> static inline int
> spi_write(struct spi_device *spi, const void *buf, size_t len)
> {
> struct spi_transfer t = {
> .tx_buf = buf,
> .len = len,
> };
> struct spi_message m;
>
> spi_message_init(&m);
> spi_message_add_tail(&t, &m);
> return spi_sync(spi, &m);
> }
>
> Seems pretty identical.
> -> This would save some lines of code,
> and again - boilerplate code sucks.
> Same applies to spi_read,
>
>
>
> The driver generates some gcc warnings:
> drivers/char/tpm/tpm_spi_stm_st33.c: In function 'read8_reg':
> drivers/char/tpm/tpm_spi_stm_st33.c:180:5: warning: unused variable 'latency'
> [-Wunused-variable]
> drivers/char/tpm/tpm_spi_stm_st33.c: In function 'tpm_stm_spi_status':
> drivers/char/tpm/tpm_spi_stm_st33.c:353:2: warning: 'data' is used
> uninitialized in this function [-Wuninitialized]
> drivers/char/tpm/tpm_spi_stm_st33.c: In function 'get_burstcount':
> drivers/char/tpm/tpm_spi_stm_st33.c:450:12: warning: 'temp' is used
> uninitialized in this function [-Wuninitialized]
> drivers/char/tpm/tpm_spi_stm_st33.c: In function 'check_locality':
> drivers/char/tpm/tpm_spi_stm_st33.c:369:22: warning: 'data' may be used
> uninitialized in this function [-Wuninitialized]
Thanks for pointing this out Peter -- it made me realize my bash
aliases for checking were not working. :-)
Please take a look at these too:
drivers/char/tpm/tpm_spi_stm_st33.c:446 get_burstcount() warn: unsigned
'status' is never less than zero.
drivers/char/tpm/tpm_spi_stm_st33.c:452 get_burstcount() warn: unsigned
'status' is never less than zero.
drivers/char/tpm/tpm_spi_stm_st33.c:523 recv_data() warn: unsigned
'status' is never less than zero.
drivers/char/tpm/tpm_spi_stm_st33.c:572 tpm_stm_spi_send() warn:
unsigned 'ret' is never less than zero.
drivers/char/tpm/tpm_spi_stm_st33.c:590 tpm_stm_spi_send() warn:
unsigned 'ret' is never less than zero.
drivers/char/tpm/tpm_spi_stm_st33.c:613 tpm_stm_spi_send() warn:
unsigned 'ret' is never less than zero.
drivers/char/tpm/tpm_spi_stm_st33.c:836 tpm_st33_spi_probe() warn:
unsigned 'err' is never less than zero.
drivers/char/tpm/tpm_spi_stm_st33.c:842 tpm_st33_spi_probe() warn:
unsigned 'err' is never less than zero.
drivers/char/tpm/tpm_spi_stm_st33.c:854 tpm_st33_spi_probe() warn:
unsigned 'err' is never less than zero.
drivers/char/tpm/tpm_spi_stm_st33.c:859 tpm_st33_spi_probe() warn:
unsigned 'err' is never less than zero.
drivers/char/tpm/tpm_spi_stm_st33.c:863 tpm_st33_spi_probe() warn:
unsigned 'err' is never less than zero.
Kent
>
>
> The driver does give me some smatch errors:
>
> $ LANG=C make -j16 C=1 CHECK=smatch
> CHECK drivers/char/tpm/tpm_spi_stm_st33.c
> drivers/char/tpm/tpm_spi_stm_st33.c:882 tpm_st33_spi_probe() warn: variable
> dereferenced before check 'platform_data' (see line 781)
> drivers/char/tpm/tpm_spi_stm_st33.c:980 tpm_st33_spi_pm_resume() warn: should
> this be a bitwise op?
> drivers/char/tpm/tpm_spi_stm_st33.c:980 tpm_st33_spi_pm_resume() warn: should
> this be a bitwise op?
>
>
>
> and some sparse errors:
> drivers/char/tpm/tpm_spi_stm_st33.c:102:35: warning: cast removes address
> space of expression
> drivers/char/tpm/tpm_spi_stm_st33.c:171:35: warning: cast removes address
> space of expression
> drivers/char/tpm/tpm_spi_stm_st33.c:299:19: warning: cast removes address
> space of expression
> drivers/char/tpm/tpm_spi_stm_st33.c:311:15: warning: symbol
> 'wait_for_serirq_timeout' was not declared. Should it be static?
> drivers/char/tpm/tpm_spi_stm_st33.c:546:19: warning: cast removes address
> space of expression
>
>
>
>
>
> And here are some other, rather subjective remarks:
>
> > + for (; nbr_dummy_bytes < total_length &&
> > + ((u8 *)xfer.rx_buf)[nbr_dummy_bytes] == 0;
> > + nbr_dummy_bytes++)
> > + ;
>
> Not sure if it would be easier to read using a while and putting the increment
> into the loop body
>
> >+ while (nbr_dummy_bytes < total_length &&
> >+ ((u8 *)xfer.rx_buf)[nbr_dummy_bytes] == 0)
> >+ nbr_dummy_bytes++;
>
> I usually don't like empty loop bodies, as they tend to be overlooked.
>
>
>
> > +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() */
>
> Why do you need a wrapper function here for the return code?
> write8_reg is only called from this location, so why doesn't it return the
> correct error code directly?
> *confused*
>
>
> Same applies to read8_reg.
>
>
>
>
> > +static int _wait_for_interrupt_serirq_timeout(struct tpm_chip *chip,
> > + unsigned long timeout)
> Is only called from wait_for_serirq_timeout - I'm not really sure if it helps
> readability to have a separate function.
>
>
>
>
> > + .resume = tpm_st33_spi_pm_resume,
> > + .suspend = tpm_st33_spi_pm_suspend,
> Maybe use
> SIMPLE_DEV_PM_OPS
> instead as it is (afaik) the new standard way for PM_OPS.
> The conversion should be pretty similar to the one in your I2C TPM driver:
> https://github.com/shpedoikal/linux/commit/d459335381eca1cb91fefb87021d3d172342e55a
>
>
> Regards,
> Peter
>
^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: [tpmdd-devel] [PATCH 1/1] TPM: STMicroelectronics st33 driver SPI
2013-04-22 8:50 Mathias Leblanc
2013-04-22 15:26 ` [tpmdd-devel] " Kent Yoder
@ 2013-04-28 1:16 ` Peter Hüwe
2013-04-29 14:15 ` Kent Yoder
1 sibling, 1 reply; 22+ messages in thread
From: Peter Hüwe @ 2013-04-28 1:16 UTC (permalink / raw)
To: tpmdd-devel
Cc: Mathias Leblanc, Kent Yoder, Rajiv Andrade, Marcel Selhorst,
Sirrix AG, Jean-Luc Blanc, linux-kernel, Matthias Leblanc
Hi Matthias,
it's nice to see that you consider most of the comments, unfortunately I still
have some left ;)
> +/*
> + * 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);
Please use module_spi_driver(&tpm_st33_spi_driver); instead
Boilerplate code sucks.
> + 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;
>...
> + xfer.len = total_length;
> + spi_message_init(&msg);
> + spi_message_add_tail(&xfer, &msg);
> + value = spi_sync(dev, &msg);
Doesn't spi_write fit here ?
static inline int
spi_write(struct spi_device *spi, const void *buf, size_t len)
{
struct spi_transfer t = {
.tx_buf = buf,
.len = len,
};
struct spi_message m;
spi_message_init(&m);
spi_message_add_tail(&t, &m);
return spi_sync(spi, &m);
}
Seems pretty identical.
-> This would save some lines of code,
and again - boilerplate code sucks.
Same applies to spi_read,
The driver generates some gcc warnings:
drivers/char/tpm/tpm_spi_stm_st33.c: In function 'read8_reg':
drivers/char/tpm/tpm_spi_stm_st33.c:180:5: warning: unused variable 'latency'
[-Wunused-variable]
drivers/char/tpm/tpm_spi_stm_st33.c: In function 'tpm_stm_spi_status':
drivers/char/tpm/tpm_spi_stm_st33.c:353:2: warning: 'data' is used
uninitialized in this function [-Wuninitialized]
drivers/char/tpm/tpm_spi_stm_st33.c: In function 'get_burstcount':
drivers/char/tpm/tpm_spi_stm_st33.c:450:12: warning: 'temp' is used
uninitialized in this function [-Wuninitialized]
drivers/char/tpm/tpm_spi_stm_st33.c: In function 'check_locality':
drivers/char/tpm/tpm_spi_stm_st33.c:369:22: warning: 'data' may be used
uninitialized in this function [-Wuninitialized]
The driver does give me some smatch errors:
$ LANG=C make -j16 C=1 CHECK=smatch
CHECK drivers/char/tpm/tpm_spi_stm_st33.c
drivers/char/tpm/tpm_spi_stm_st33.c:882 tpm_st33_spi_probe() warn: variable
dereferenced before check 'platform_data' (see line 781)
drivers/char/tpm/tpm_spi_stm_st33.c:980 tpm_st33_spi_pm_resume() warn: should
this be a bitwise op?
drivers/char/tpm/tpm_spi_stm_st33.c:980 tpm_st33_spi_pm_resume() warn: should
this be a bitwise op?
and some sparse errors:
drivers/char/tpm/tpm_spi_stm_st33.c:102:35: warning: cast removes address
space of expression
drivers/char/tpm/tpm_spi_stm_st33.c:171:35: warning: cast removes address
space of expression
drivers/char/tpm/tpm_spi_stm_st33.c:299:19: warning: cast removes address
space of expression
drivers/char/tpm/tpm_spi_stm_st33.c:311:15: warning: symbol
'wait_for_serirq_timeout' was not declared. Should it be static?
drivers/char/tpm/tpm_spi_stm_st33.c:546:19: warning: cast removes address
space of expression
And here are some other, rather subjective remarks:
> + for (; nbr_dummy_bytes < total_length &&
> + ((u8 *)xfer.rx_buf)[nbr_dummy_bytes] == 0;
> + nbr_dummy_bytes++)
> + ;
Not sure if it would be easier to read using a while and putting the increment
into the loop body
>+ while (nbr_dummy_bytes < total_length &&
>+ ((u8 *)xfer.rx_buf)[nbr_dummy_bytes] == 0)
>+ nbr_dummy_bytes++;
I usually don't like empty loop bodies, as they tend to be overlooked.
> +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() */
Why do you need a wrapper function here for the return code?
write8_reg is only called from this location, so why doesn't it return the
correct error code directly?
*confused*
Same applies to read8_reg.
> +static int _wait_for_interrupt_serirq_timeout(struct tpm_chip *chip,
> + unsigned long timeout)
Is only called from wait_for_serirq_timeout - I'm not really sure if it helps
readability to have a separate function.
> + .resume = tpm_st33_spi_pm_resume,
> + .suspend = tpm_st33_spi_pm_suspend,
Maybe use
SIMPLE_DEV_PM_OPS
instead as it is (afaik) the new standard way for PM_OPS.
The conversion should be pretty similar to the one in your I2C TPM driver:
https://github.com/shpedoikal/linux/commit/d459335381eca1cb91fefb87021d3d172342e55a
Regards,
Peter
^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: [tpmdd-devel] [PATCH 1/1] TPM: STMicroelectronics st33 driver SPI
2013-04-25 15:40 ` Mathias LEBLANC
@ 2013-04-26 14:29 ` Kent Yoder
0 siblings, 0 replies; 22+ messages in thread
From: Kent Yoder @ 2013-04-26 14:29 UTC (permalink / raw)
To: Mathias LEBLANC
Cc: Kent Yoder, Rajiv Andrade, Marcel Selhorst, Sirrix AG,
tpmdd-devel, Jean-Luc BLANC, linux-kernel
On Thu, Apr 25, 2013 at 05:40:28PM +0200, Mathias LEBLANC wrote:
> Hi Kent,
>
> When the TPM does not respond, the -EBUSY error code is return after a timeout.
> The kernel SPI driver use the chip select line to address some bytes to a device, and release the SS line if does not receive response after a timeout.
> If the TPM does not respond, there are no consequences for the SPI bus and the communication with the other devices.
Thanks for the explanation. I think all that's left is the all caps
function names fixes and this will be ready to apply.
Kent
> Regards,
>
> Mathias
>
> -----Original Message-----
> From: Kent Yoder [mailto:shpedoikal@gmail.com]
> Sent: 22 April, 2013 20:31
> To: Mathias LEBLANC
> Cc: Kent Yoder; Rajiv Andrade; Marcel Selhorst; Sirrix AG; tpmdd-devel@lists.sourceforge.net; Jean-Luc BLANC; linux-kernel@vger.kernel.org
> Subject: Re: [tpmdd-devel] [PATCH 1/1] TPM: STMicroelectronics st33 driver SPI
>
> On Mon, Apr 22, 2013 at 06:32:05PM +0200, Mathias LEBLANC wrote:
> > Hi Kent,
> >
> > > + 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);
> >
> > Kent: burstcnt as -EBUSY still needs checking.
> >
> > The get_burtcount function get the number of byte to read in the TPM
> > FIFO, The min_t function get the minus between the burstcount and the size of the command response.
> > We don't need to recheck the burstcount number.
> >
> > Regarding the -EBUSY flag, It's the error code of the SPI_WRITE_DATA and SPI_READ_DATA.
> > In the recv_data function, the status variable is checked to know if the device is BUSY.
>
> So I'm no SPI expert, but it sounds like you're saying that TPM status == SPI bus status here. You're under the tpm_mutex, so I think you're safe from other TPM writes, but what about other devices on the bus?
> Can they contend for the read/write?
>
> Kent
>
> > Regards,
> >
> > Mathias Leblanc
> >
> > -----Original Message-----
> > From: Kent Yoder [mailto:shpedoikal@gmail.com]
> > Sent: 22 April, 2013 17:26
> > To: Mathias LEBLANC
> > Cc: Kent Yoder; Rajiv Andrade; Marcel Selhorst; Sirrix AG;
> > tpmdd-devel@lists.sourceforge.net; Jean-Luc BLANC;
> > linux-kernel@vger.kernel.org
> > Subject: Re: [tpmdd-devel] [PATCH 1/1] TPM: STMicroelectronics st33
> > driver SPI
> >
> > On Mon, Apr 22, 2013 at 10:50:06AM +0200, Mathias Leblanc wrote:
> > > * 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
> >
> > Still need this as lower case, and SPI_WRITE_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);
> >
> > burstcnt as -EBUSY still needs checking.
> >
> > > + 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);
> >
> > Same burstcnt check here.
> >
> > > + 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)
> >
> > You can still get rid of bchipf too. If probe() fails remove() won't be called - that seems to be all bchipf is checking...
> >
> > Kent
> >
> > > + 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
> > >
> > >
> > > --------------------------------------------------------------------
> > > --
> > > -------- Precog is a next-generation analytics platform capable of
> > > advanced analytics on semi-structured data. The platform includes
> > > APIs for building apps and a phenomenal toolset for data science.
> > > Developers can use our toolset for easy data analysis & visualization.
> > > Get a free account!
> > > http://www2.precog.com/precogplatform/slashdotnewsletter
> > > _______________________________________________
> > > tpmdd-devel mailing list
> > > tpmdd-devel@lists.sourceforge.net
> > > https://lists.sourceforge.net/lists/listinfo/tpmdd-devel
^ permalink raw reply [flat|nested] 22+ messages in thread
* RE: [tpmdd-devel] [PATCH 1/1] TPM: STMicroelectronics st33 driver SPI
2013-04-22 18:31 ` Kent Yoder
@ 2013-04-25 15:40 ` Mathias LEBLANC
2013-04-26 14:29 ` Kent Yoder
0 siblings, 1 reply; 22+ messages in thread
From: Mathias LEBLANC @ 2013-04-25 15:40 UTC (permalink / raw)
To: Kent Yoder
Cc: Kent Yoder, Rajiv Andrade, Marcel Selhorst, Sirrix AG,
tpmdd-devel, Jean-Luc BLANC, linux-kernel
Hi Kent,
When the TPM does not respond, the -EBUSY error code is return after a timeout.
The kernel SPI driver use the chip select line to address some bytes to a device, and release the SS line if does not receive response after a timeout.
If the TPM does not respond, there are no consequences for the SPI bus and the communication with the other devices.
Regards,
Mathias
-----Original Message-----
From: Kent Yoder [mailto:shpedoikal@gmail.com]
Sent: 22 April, 2013 20:31
To: Mathias LEBLANC
Cc: Kent Yoder; Rajiv Andrade; Marcel Selhorst; Sirrix AG; tpmdd-devel@lists.sourceforge.net; Jean-Luc BLANC; linux-kernel@vger.kernel.org
Subject: Re: [tpmdd-devel] [PATCH 1/1] TPM: STMicroelectronics st33 driver SPI
On Mon, Apr 22, 2013 at 06:32:05PM +0200, Mathias LEBLANC wrote:
> Hi Kent,
>
> > + 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);
>
> Kent: burstcnt as -EBUSY still needs checking.
>
> The get_burtcount function get the number of byte to read in the TPM
> FIFO, The min_t function get the minus between the burstcount and the size of the command response.
> We don't need to recheck the burstcount number.
>
> Regarding the -EBUSY flag, It's the error code of the SPI_WRITE_DATA and SPI_READ_DATA.
> In the recv_data function, the status variable is checked to know if the device is BUSY.
So I'm no SPI expert, but it sounds like you're saying that TPM status == SPI bus status here. You're under the tpm_mutex, so I think you're safe from other TPM writes, but what about other devices on the bus?
Can they contend for the read/write?
Kent
> Regards,
>
> Mathias Leblanc
>
> -----Original Message-----
> From: Kent Yoder [mailto:shpedoikal@gmail.com]
> Sent: 22 April, 2013 17:26
> To: Mathias LEBLANC
> Cc: Kent Yoder; Rajiv Andrade; Marcel Selhorst; Sirrix AG;
> tpmdd-devel@lists.sourceforge.net; Jean-Luc BLANC;
> linux-kernel@vger.kernel.org
> Subject: Re: [tpmdd-devel] [PATCH 1/1] TPM: STMicroelectronics st33
> driver SPI
>
> On Mon, Apr 22, 2013 at 10:50:06AM +0200, Mathias Leblanc wrote:
> > * 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
>
> Still need this as lower case, and SPI_WRITE_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);
>
> burstcnt as -EBUSY still needs checking.
>
> > + 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);
>
> Same burstcnt check here.
>
> > + 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)
>
> You can still get rid of bchipf too. If probe() fails remove() won't be called - that seems to be all bchipf is checking...
>
> Kent
>
> > + 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
> >
> >
> > --------------------------------------------------------------------
> > --
> > -------- Precog is a next-generation analytics platform capable of
> > advanced analytics on semi-structured data. The platform includes
> > APIs for building apps and a phenomenal toolset for data science.
> > Developers can use our toolset for easy data analysis & visualization.
> > Get a free account!
> > http://www2.precog.com/precogplatform/slashdotnewsletter
> > _______________________________________________
> > tpmdd-devel mailing list
> > tpmdd-devel@lists.sourceforge.net
> > https://lists.sourceforge.net/lists/listinfo/tpmdd-devel
^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: [tpmdd-devel] [PATCH 1/1] TPM: STMicroelectronics st33 driver SPI
2013-04-22 16:32 ` Mathias LEBLANC
@ 2013-04-22 18:31 ` Kent Yoder
2013-04-25 15:40 ` Mathias LEBLANC
0 siblings, 1 reply; 22+ messages in thread
From: Kent Yoder @ 2013-04-22 18:31 UTC (permalink / raw)
To: Mathias LEBLANC
Cc: Kent Yoder, Rajiv Andrade, Marcel Selhorst, Sirrix AG,
tpmdd-devel, Jean-Luc BLANC, linux-kernel
On Mon, Apr 22, 2013 at 06:32:05PM +0200, Mathias LEBLANC wrote:
> Hi Kent,
>
> > + 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);
>
> Kent: burstcnt as -EBUSY still needs checking.
>
> The get_burtcount function get the number of byte to read in the TPM FIFO,
> The min_t function get the minus between the burstcount and the size of the command response.
> We don't need to recheck the burstcount number.
>
> Regarding the -EBUSY flag, It's the error code of the SPI_WRITE_DATA and SPI_READ_DATA.
> In the recv_data function, the status variable is checked to know if the device is BUSY.
So I'm no SPI expert, but it sounds like you're saying that TPM status
== SPI bus status here. You're under the tpm_mutex, so I think you're
safe from other TPM writes, but what about other devices on the bus?
Can they contend for the read/write?
Kent
> Regards,
>
> Mathias Leblanc
>
> -----Original Message-----
> From: Kent Yoder [mailto:shpedoikal@gmail.com]
> Sent: 22 April, 2013 17:26
> To: Mathias LEBLANC
> Cc: Kent Yoder; Rajiv Andrade; Marcel Selhorst; Sirrix AG; tpmdd-devel@lists.sourceforge.net; Jean-Luc BLANC; linux-kernel@vger.kernel.org
> Subject: Re: [tpmdd-devel] [PATCH 1/1] TPM: STMicroelectronics st33 driver SPI
>
> On Mon, Apr 22, 2013 at 10:50:06AM +0200, Mathias Leblanc wrote:
> > * 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
>
> Still need this as lower case, and SPI_WRITE_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);
>
> burstcnt as -EBUSY still needs checking.
>
> > + 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);
>
> Same burstcnt check here.
>
> > + 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)
>
> You can still get rid of bchipf too. If probe() fails remove() won't be called - that seems to be all bchipf is checking...
>
> Kent
>
> > + 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
> >
> >
> > ----------------------------------------------------------------------
> > -------- Precog is a next-generation analytics platform capable of
> > advanced analytics on semi-structured data. The platform includes APIs
> > for building apps and a phenomenal toolset for data science.
> > Developers can use our toolset for easy data analysis & visualization.
> > Get a free account!
> > http://www2.precog.com/precogplatform/slashdotnewsletter
> > _______________________________________________
> > tpmdd-devel mailing list
> > tpmdd-devel@lists.sourceforge.net
> > https://lists.sourceforge.net/lists/listinfo/tpmdd-devel
^ permalink raw reply [flat|nested] 22+ messages in thread
* RE: [tpmdd-devel] [PATCH 1/1] TPM: STMicroelectronics st33 driver SPI
2013-04-22 15:26 ` [tpmdd-devel] " Kent Yoder
@ 2013-04-22 16:32 ` Mathias LEBLANC
2013-04-22 18:31 ` Kent Yoder
0 siblings, 1 reply; 22+ messages in thread
From: Mathias LEBLANC @ 2013-04-22 16:32 UTC (permalink / raw)
To: Kent Yoder
Cc: Kent Yoder, Rajiv Andrade, Marcel Selhorst, Sirrix AG,
tpmdd-devel, Jean-Luc BLANC, linux-kernel
Hi Kent,
> + 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);
Kent: burstcnt as -EBUSY still needs checking.
The get_burtcount function get the number of byte to read in the TPM FIFO,
The min_t function get the minus between the burstcount and the size of the command response.
We don't need to recheck the burstcount number.
Regarding the -EBUSY flag, It's the error code of the SPI_WRITE_DATA and SPI_READ_DATA.
In the recv_data function, the status variable is checked to know if the device is BUSY.
Regards,
Mathias Leblanc
-----Original Message-----
From: Kent Yoder [mailto:shpedoikal@gmail.com]
Sent: 22 April, 2013 17:26
To: Mathias LEBLANC
Cc: Kent Yoder; Rajiv Andrade; Marcel Selhorst; Sirrix AG; tpmdd-devel@lists.sourceforge.net; Jean-Luc BLANC; linux-kernel@vger.kernel.org
Subject: Re: [tpmdd-devel] [PATCH 1/1] TPM: STMicroelectronics st33 driver SPI
On Mon, Apr 22, 2013 at 10:50:06AM +0200, Mathias Leblanc wrote:
> * 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
Still need this as lower case, and SPI_WRITE_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);
burstcnt as -EBUSY still needs checking.
> + 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);
Same burstcnt check here.
> + 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)
You can still get rid of bchipf too. If probe() fails remove() won't be called - that seems to be all bchipf is checking...
Kent
> + 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
>
>
> ----------------------------------------------------------------------
> -------- Precog is a next-generation analytics platform capable of
> advanced analytics on semi-structured data. The platform includes APIs
> for building apps and a phenomenal toolset for data science.
> Developers can use our toolset for easy data analysis & visualization.
> Get a free account!
> http://www2.precog.com/precogplatform/slashdotnewsletter
> _______________________________________________
> tpmdd-devel mailing list
> tpmdd-devel@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/tpmdd-devel
^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: [tpmdd-devel] [PATCH 1/1] TPM: STMicroelectronics st33 driver SPI
2013-04-22 8:50 Mathias Leblanc
@ 2013-04-22 15:26 ` Kent Yoder
2013-04-22 16:32 ` Mathias LEBLANC
2013-04-28 1:16 ` Peter Hüwe
1 sibling, 1 reply; 22+ messages in thread
From: Kent Yoder @ 2013-04-22 15:26 UTC (permalink / raw)
To: Mathias Leblanc
Cc: Kent Yoder, Rajiv Andrade, Marcel Selhorst, Sirrix AG,
tpmdd-devel, Jean-Luc Blanc, linux-kernel
On Mon, Apr 22, 2013 at 10:50:06AM +0200, Mathias Leblanc wrote:
> * 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
Still need this as lower case, and SPI_WRITE_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);
burstcnt as -EBUSY still needs checking.
> + 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);
Same burstcnt check here.
> + 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)
You can still get rid of bchipf too. If probe() fails remove() won't
be called - that seems to be all bchipf is checking...
Kent
> + 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
>
>
> ------------------------------------------------------------------------------
> Precog is a next-generation analytics platform capable of advanced
> analytics on semi-structured data. The platform includes APIs for building
> apps and a phenomenal toolset for data science. Developers can use
> our toolset for easy data analysis & visualization. Get a free account!
> http://www2.precog.com/precogplatform/slashdotnewsletter
> _______________________________________________
> tpmdd-devel mailing list
> tpmdd-devel@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/tpmdd-devel
^ permalink raw reply [flat|nested] 22+ messages in thread
* RE: [tpmdd-devel] [PATCH 1/1] TPM: STMicroelectronics st33 driver SPI
2013-04-17 21:31 ` [tpmdd-devel] " Kent Yoder
@ 2013-04-19 16:06 ` Mathias LEBLANC
0 siblings, 0 replies; 22+ messages in thread
From: Mathias LEBLANC @ 2013-04-19 16:06 UTC (permalink / raw)
To: Kent Yoder
Cc: Kent Yoder, Rajiv Andrade, Marcel Selhorst, Sirrix AG,
tpmdd-devel, Jean-Luc BLANC, linux-kernel
Hi Kent,
> + /* u8 LOC+DIR u8 ADD u16 LENGHT u8* data */
> + /* u8 LATENCY u8 LATENCY */
Kent: What do these comments mean?
Mathias: These comments describe the I2C command protocol.
Regarding the return value of these W/R functions, it's an error of comment, these functions return 0 on success else a negative error code.
Thanks,
Mathias Leblanc
-----Original Message-----
From: Kent Yoder [mailto:shpedoikal@gmail.com]
Sent: 17 April, 2013 23:32
To: Mathias LEBLANC
Cc: Kent Yoder; Rajiv Andrade; Marcel Selhorst; Sirrix AG; tpmdd-devel@lists.sourceforge.net; Jean-Luc BLANC; linux-kernel@vger.kernel.org
Subject: Re: [tpmdd-devel] [PATCH 1/1] TPM: STMicroelectronics st33 driver SPI
Hi Matthias,
On Fri, Apr 12, 2013 at 10:44:11AM +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 | 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
This looks unused, please remove.
> +#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>
Is there a need for this header to live in linux/spi? You should move it into the local header.
> +#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
Please remove FUNC_ENTER().
> +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 */
What do these comments mean?
> +
> + 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)) {
Well that looks weird. This is more readable:
if (tpm_size > 0 &&
(tpm_register == TPM_DATA_FIFO ||
tpm_register == TPM_HASH_DATA)) {
But probably more relevant is that write8_reg is never called with tpm_register == TPM_HASH_DATA. Can you remove it?
> + data = tpm_size >> 8 & 0x00ff;
I had to google to figure out which operator took precedence here, please add some parenthesis.
> + memcpy(data_buffer + i, &data, sizeof(data));
> + i++;
> + data = tpm_size & 0x00ff;
> + memcpy(data_buffer + i, &data, sizeof(data));
> + i++;
IIRC Peter fixed this in the I2C driver -- where you can avoid
memcpy() of a single byte by using an assignment, please do.
> + 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++;
> + }
Isn't the above loop just this?
memset(&data_buffer[i], TPM_DUMMY_BYTE, platform_data->latency + 1);
> + 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];
Code like this is where vague variable names make it difficult. Can you rename i and j here? j is the number of dummy bytes and i is the total length of the transfer, right?
> + }
> +
> + 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() */
Same comments as write8_reg -- converge into simpler memcpy/memset's where you can, rename variables.
> +
> +/*
> + * 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.
Why would one be success if it returns the number of bytes written?
> + */
> +static u32 SPI_WRITE_DATA(struct tpm_chip *tpm, u8 tpm_register,
> + u8 *tpm_data, u16 tpm_size)
An all caps function name will make me think its a macro, please use lower case.
> +{
> + 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;
pin_infos isn't used here, please remove.
> + 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;
client is unused?
> + 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;
client is unused?
> +
> + 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;
client is unused?
> +
> + 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;
client is unused?
> + /* 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;
client is unused?
> + 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)
queue is unused?
> +{
> + 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;
client is unused?
> +
> + 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;
pin_infos and client are unused?
> +
> + ordinal = be32_to_cpu(*((__be32 *)(buf + 6)));
ordinal is unused?
> +
> + 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);
burstcnt can be -EBUSY here, which would cause size to become a large negative number. Better to check its sane here before using it.
> + 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;
pin_infos and client are unused?
> +
> +
> + 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;
Please take a look at git commit 3d7a7bd7 and make the same changes. The way these labels are jumped to can use some cleaning up. I suspect that you can also remove bchipf here like in 1fbc5e95356a4600fab3a04a82dc8bb49591aedd.
Thanks,
Kent
> +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
>
>
> ----------------------------------------------------------------------
> -------- Precog is a next-generation analytics platform capable of
> advanced analytics on semi-structured data. The platform includes APIs
> for building apps and a phenomenal toolset for data science.
> Developers can use our toolset for easy data analysis & visualization.
> Get a free account!
> http://www2.precog.com/precogplatform/slashdotnewsletter
> _______________________________________________
> tpmdd-devel mailing list
> tpmdd-devel@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/tpmdd-devel
^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: [tpmdd-devel] [PATCH 1/1] TPM: STMicroelectronics st33 driver SPI
2013-04-12 8:44 Matthias Leblanc
@ 2013-04-17 21:31 ` Kent Yoder
2013-04-19 16:06 ` Mathias LEBLANC
0 siblings, 1 reply; 22+ messages in thread
From: Kent Yoder @ 2013-04-17 21:31 UTC (permalink / raw)
To: Matthias Leblanc
Cc: Kent Yoder, Rajiv Andrade, Marcel Selhorst, Sirrix AG,
tpmdd-devel, Jean-Luc Blanc, linux-kernel
Hi Matthias,
On Fri, Apr 12, 2013 at 10:44:11AM +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 | 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
This looks unused, please remove.
> +#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>
Is there a need for this header to live in linux/spi? You should move
it into the local header.
> +#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
Please remove FUNC_ENTER().
> +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 */
What do these comments mean?
> +
> + 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)) {
Well that looks weird. This is more readable:
if (tpm_size > 0 &&
(tpm_register == TPM_DATA_FIFO ||
tpm_register == TPM_HASH_DATA)) {
But probably more relevant is that write8_reg is never called with
tpm_register == TPM_HASH_DATA. Can you remove it?
> + data = tpm_size >> 8 & 0x00ff;
I had to google to figure out which operator took precedence here,
please add some parenthesis.
> + memcpy(data_buffer + i, &data, sizeof(data));
> + i++;
> + data = tpm_size & 0x00ff;
> + memcpy(data_buffer + i, &data, sizeof(data));
> + i++;
IIRC Peter fixed this in the I2C driver -- where you can avoid
memcpy() of a single byte by using an assignment, please do.
> + 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++;
> + }
Isn't the above loop just this?
memset(&data_buffer[i], TPM_DUMMY_BYTE, platform_data->latency + 1);
> + 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];
Code like this is where vague variable names make it difficult. Can
you rename i and j here? j is the number of dummy bytes and i is the
total length of the transfer, right?
> + }
> +
> + 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() */
Same comments as write8_reg -- converge into simpler memcpy/memset's
where you can, rename variables.
> +
> +/*
> + * 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.
Why would one be success if it returns the number of bytes written?
> + */
> +static u32 SPI_WRITE_DATA(struct tpm_chip *tpm, u8 tpm_register,
> + u8 *tpm_data, u16 tpm_size)
An all caps function name will make me think its a macro, please use
lower case.
> +{
> + 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;
pin_infos isn't used here, please remove.
> + 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;
client is unused?
> + 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;
client is unused?
> +
> + 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;
client is unused?
> +
> + 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;
client is unused?
> + /* 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;
client is unused?
> + 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)
queue is unused?
> +{
> + 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;
client is unused?
> +
> + 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;
pin_infos and client are unused?
> +
> + ordinal = be32_to_cpu(*((__be32 *)(buf + 6)));
ordinal is unused?
> +
> + 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);
burstcnt can be -EBUSY here, which would cause size to become a large
negative number. Better to check its sane here before using it.
> + 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;
pin_infos and client are unused?
> +
> +
> + 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;
Please take a look at git commit 3d7a7bd7 and make the same changes. The
way these labels are jumped to can use some cleaning up. I suspect that
you can also remove bchipf here like in
1fbc5e95356a4600fab3a04a82dc8bb49591aedd.
Thanks,
Kent
> +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
>
>
> ------------------------------------------------------------------------------
> Precog is a next-generation analytics platform capable of advanced
> analytics on semi-structured data. The platform includes APIs for building
> apps and a phenomenal toolset for data science. Developers can use
> our toolset for easy data analysis & visualization. Get a free account!
> http://www2.precog.com/precogplatform/slashdotnewsletter
> _______________________________________________
> tpmdd-devel mailing list
> tpmdd-devel@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/tpmdd-devel
^ permalink raw reply [flat|nested] 22+ messages in thread
* RE: [tpmdd-devel] [PATCH 1/1] TPM: STMicroelectronics st33 driver SPI
2013-03-25 16:44 ` [tpmdd-devel] " Kent Yoder
@ 2013-03-25 16:48 ` Mathias LEBLANC
0 siblings, 0 replies; 22+ messages in thread
From: Mathias LEBLANC @ 2013-03-25 16:48 UTC (permalink / raw)
To: Kent Yoder
Cc: Kent Yoder, Rajiv Andrade, Marcel Selhorst, Sirrix AG,
tpmdd-devel, Jean-Luc BLANC, linux-kernel
Sorry for this mistake, admin01 the name of my platform development , git is not configured on it.
-----Original Message-----
From: Kent Yoder [mailto:shpedoikal@gmail.com]
Sent: 25 March, 2013 17:44
To: Mathias LEBLANC
Cc: Kent Yoder; Rajiv Andrade; Marcel Selhorst; Sirrix AG; tpmdd-devel@lists.sourceforge.net; Jean-Luc BLANC; linux-kernel@vger.kernel.org
Subject: Re: [tpmdd-devel] [PATCH 1/1] TPM: STMicroelectronics st33 driver SPI
On Mon, Mar 25, 2013 at 10:08 AM, Matthias Leblanc <mathias.leblanc@st.com> wrote:
> 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)>
Who is admin01 and why is he signing off your patches? :-)
Kent
> ---
> 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
>
>
> ----------------------------------------------------------------------
> -------- Everyone hates slow websites. So do we.
> Make your web apps faster with AppDynamics Download AppDynamics Lite
> for free today:
> http://p.sf.net/sfu/appdyn_d2d_mar
> _______________________________________________
> tpmdd-devel mailing list
> tpmdd-devel@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/tpmdd-devel
^ permalink raw reply [flat|nested] 22+ messages in thread
* Re: [tpmdd-devel] [PATCH 1/1] TPM: STMicroelectronics st33 driver SPI
2013-03-25 15:08 Matthias Leblanc
@ 2013-03-25 16:44 ` Kent Yoder
2013-03-25 16:48 ` Mathias LEBLANC
0 siblings, 1 reply; 22+ messages in thread
From: Kent Yoder @ 2013-03-25 16:44 UTC (permalink / raw)
To: Matthias Leblanc
Cc: Kent Yoder, Rajiv Andrade, Marcel Selhorst, Sirrix AG,
tpmdd-devel, Jean-Luc Blanc, linux-kernel
On Mon, Mar 25, 2013 at 10:08 AM, Matthias Leblanc
<mathias.leblanc@st.com> wrote:
> 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)>
Who is admin01 and why is he signing off your patches? :-)
Kent
> ---
> 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
>
>
> ------------------------------------------------------------------------------
> Everyone hates slow websites. So do we.
> Make your web apps faster with AppDynamics
> Download AppDynamics Lite for free today:
> http://p.sf.net/sfu/appdyn_d2d_mar
> _______________________________________________
> tpmdd-devel mailing list
> tpmdd-devel@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/tpmdd-devel
^ permalink raw reply [flat|nested] 22+ messages in thread
end of thread, other threads:[~2013-05-17 12:30 UTC | newest]
Thread overview: 22+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-04-09 14:42 [PATCH 1/1] TPM: STMicroelectronics st33 driver SPI Matthias Leblanc
2013-04-10 20:32 ` [tpmdd-devel] " Peter Hüwe
2013-04-11 8:58 ` Mathias LEBLANC
2013-04-11 21:44 ` Peter Hüwe
-- strict thread matches above, loose matches on Subject: below --
2013-05-15 13:53 Matthias Leblanc
2013-05-15 22:29 ` [tpmdd-devel] " Peter Hüwe
2013-05-16 8:45 ` Mathias LEBLANC
2013-05-16 19:03 ` Peter Hüwe
2013-05-17 12:26 ` Mathias LEBLANC
2013-05-07 10:16 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
2013-04-22 8:50 Mathias Leblanc
2013-04-22 15:26 ` [tpmdd-devel] " Kent Yoder
2013-04-22 16:32 ` Mathias LEBLANC
2013-04-22 18:31 ` Kent Yoder
2013-04-25 15:40 ` Mathias LEBLANC
2013-04-26 14:29 ` Kent Yoder
2013-04-28 1:16 ` Peter Hüwe
2013-04-29 14:15 ` Kent Yoder
2013-04-12 8:44 Matthias Leblanc
2013-04-17 21:31 ` [tpmdd-devel] " Kent Yoder
2013-04-19 16:06 ` Mathias LEBLANC
2013-03-25 15:08 Matthias Leblanc
2013-03-25 16:44 ` [tpmdd-devel] " Kent Yoder
2013-03-25 16:48 ` Mathias LEBLANC
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).