2 * Copyright (C) 2015 Infineon Technologies AG
3 * Copyright (C) 2016 STMicroelectronics SAS
6 * Peter Huewe <peter.huewe@infineon.com>
7 * Christophe Ricard <christophe-h.ricard@st.com>
9 * Maintained by: <tpmdd-devel@lists.sourceforge.net>
11 * Device driver for TCG/TCPA TPM (trusted platform module).
12 * Specifications at www.trustedcomputinggroup.org
14 * This device driver implements the TPM interface as defined in
15 * the TCG TPM Interface Spec version 1.3, revision 27 via _raw/native
18 * It is based on the original tpm_tis device driver from Leendert van
19 * Dorn and Kyleen Hall and Jarko Sakkinnen.
21 * This program is free software; you can redistribute it and/or
22 * modify it under the terms of the GNU General Public License as
23 * published by the Free Software Foundation, version 2 of the
27 #include <linux/init.h>
28 #include <linux/module.h>
29 #include <linux/moduleparam.h>
30 #include <linux/slab.h>
31 #include <linux/interrupt.h>
32 #include <linux/wait.h>
33 #include <linux/acpi.h>
34 #include <linux/freezer.h>
36 #include <linux/spi/spi.h>
37 #include <linux/gpio.h>
38 #include <linux/of_irq.h>
39 #include <linux/of_gpio.h>
40 #include <linux/tpm.h>
42 #include "tpm_tis_core.h"
44 #define MAX_SPI_FRAMESIZE 64
46 struct tpm_tis_spi_phy {
47 struct tpm_tis_data priv;
48 struct spi_device *spi_device;
52 static inline struct tpm_tis_spi_phy *to_tpm_tis_spi_phy(struct tpm_tis_data *data)
54 return container_of(data, struct tpm_tis_spi_phy, priv);
57 static int tpm_tis_spi_transfer(struct tpm_tis_data *data, u32 addr, u16 len,
58 u8 *in, const u8 *out)
60 struct tpm_tis_spi_phy *phy = to_tpm_tis_spi_phy(data);
64 struct spi_transfer spi_xfer;
67 spi_bus_lock(phy->spi_device->master);
70 transfer_len = min_t(u16, len, MAX_SPI_FRAMESIZE);
72 phy->iobuf[0] = (in ? 0x80 : 0) | (transfer_len - 1);
74 phy->iobuf[2] = addr >> 8;
77 memset(&spi_xfer, 0, sizeof(spi_xfer));
78 spi_xfer.tx_buf = phy->iobuf;
79 spi_xfer.rx_buf = phy->iobuf;
81 spi_xfer.cs_change = 1;
84 spi_message_add_tail(&spi_xfer, &m);
85 ret = spi_sync_locked(phy->spi_device, &m);
89 if ((phy->iobuf[3] & 0x01) == 0) {
90 // handle SPI wait states
93 for (i = 0; i < TPM_RETRY; i++) {
96 spi_message_add_tail(&spi_xfer, &m);
97 ret = spi_sync_locked(phy->spi_device, &m);
100 if (phy->iobuf[0] & 0x01)
104 if (i == TPM_RETRY) {
110 spi_xfer.cs_change = 0;
111 spi_xfer.len = transfer_len;
112 spi_xfer.delay_usecs = 5;
115 spi_xfer.tx_buf = NULL;
117 spi_xfer.rx_buf = NULL;
118 memcpy(phy->iobuf, out, transfer_len);
122 spi_message_init(&m);
123 spi_message_add_tail(&spi_xfer, &m);
124 ret = spi_sync_locked(phy->spi_device, &m);
129 memcpy(in, phy->iobuf, transfer_len);
137 spi_bus_unlock(phy->spi_device->master);
141 static int tpm_tis_spi_read_bytes(struct tpm_tis_data *data, u32 addr,
144 return tpm_tis_spi_transfer(data, addr, len, result, NULL);
147 static int tpm_tis_spi_write_bytes(struct tpm_tis_data *data, u32 addr,
148 u16 len, const u8 *value)
150 return tpm_tis_spi_transfer(data, addr, len, NULL, value);
153 static int tpm_tis_spi_read16(struct tpm_tis_data *data, u32 addr, u16 *result)
157 rc = data->phy_ops->read_bytes(data, addr, sizeof(u16), (u8 *)result);
159 *result = le16_to_cpu(*result);
163 static int tpm_tis_spi_read32(struct tpm_tis_data *data, u32 addr, u32 *result)
167 rc = data->phy_ops->read_bytes(data, addr, sizeof(u32), (u8 *)result);
169 *result = le32_to_cpu(*result);
173 static int tpm_tis_spi_write32(struct tpm_tis_data *data, u32 addr, u32 value)
175 value = cpu_to_le32(value);
176 return data->phy_ops->write_bytes(data, addr, sizeof(u32),
180 static const struct tpm_tis_phy_ops tpm_spi_phy_ops = {
181 .read_bytes = tpm_tis_spi_read_bytes,
182 .write_bytes = tpm_tis_spi_write_bytes,
183 .read16 = tpm_tis_spi_read16,
184 .read32 = tpm_tis_spi_read32,
185 .write32 = tpm_tis_spi_write32,
188 static int tpm_tis_spi_probe(struct spi_device *dev)
190 struct tpm_tis_spi_phy *phy;
193 phy = devm_kzalloc(&dev->dev, sizeof(struct tpm_tis_spi_phy),
198 phy->spi_device = dev;
200 phy->iobuf = devm_kmalloc(&dev->dev, MAX_SPI_FRAMESIZE, GFP_KERNEL);
204 /* If the SPI device has an IRQ then use that */
210 return tpm_tis_core_init(&dev->dev, &phy->priv, irq, &tpm_spi_phy_ops,
214 static SIMPLE_DEV_PM_OPS(tpm_tis_pm, tpm_pm_suspend, tpm_tis_resume);
216 static int tpm_tis_spi_remove(struct spi_device *dev)
218 struct tpm_chip *chip = spi_get_drvdata(dev);
220 tpm_chip_unregister(chip);
221 tpm_tis_remove(chip);
225 static const struct spi_device_id tpm_tis_spi_id[] = {
229 MODULE_DEVICE_TABLE(spi, tpm_tis_spi_id);
231 static const struct of_device_id of_tis_spi_match[] = {
232 { .compatible = "st,st33htpm-spi", },
233 { .compatible = "infineon,slb9670", },
234 { .compatible = "tcg,tpm_tis-spi", },
237 MODULE_DEVICE_TABLE(of, of_tis_spi_match);
239 static const struct acpi_device_id acpi_tis_spi_match[] = {
243 MODULE_DEVICE_TABLE(acpi, acpi_tis_spi_match);
245 static struct spi_driver tpm_tis_spi_driver = {
247 .owner = THIS_MODULE,
248 .name = "tpm_tis_spi",
250 .of_match_table = of_match_ptr(of_tis_spi_match),
251 .acpi_match_table = ACPI_PTR(acpi_tis_spi_match),
253 .probe = tpm_tis_spi_probe,
254 .remove = tpm_tis_spi_remove,
255 .id_table = tpm_tis_spi_id,
257 module_spi_driver(tpm_tis_spi_driver);
259 MODULE_DESCRIPTION("TPM Driver for native SPI access");
260 MODULE_LICENSE("GPL");