Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

tpm/tpm_tis_spi: Add support for spi phy

Spi protocol standardized by the TCG is now supported by most of TPM
vendors.

It supports SPI Bit Protocol as describe in the TCG PTP
specification (chapter 6.4.6 SPI Bit Protocol).

Irq mode is not supported.

This commit is based on the initial work by Peter Huewe.

Signed-off-by: Peter Huewe <peter.huewe@infineon.com>
Signed-off-by: Alexander Steffen <Alexander.Steffen@infineon.com>
Signed-off-by: Christophe Ricard <christophe-h.ricard@st.com>
Reviewed-by: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
Tested-by: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
Tested-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
Reviewed-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
Signed-off-by: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>

authored by

Christophe Ricard and committed by
Jarkko Sakkinen
0edbfea5 41a5e1cf

+309
+24
Documentation/devicetree/bindings/security/tpm/tpm_tis_spi.txt
··· 1 + Required properties: 2 + - compatible: should be one of the following 3 + "st,st33htpm-spi" 4 + "infineon,slb9670" 5 + "tcg,tpm_tis-spi" 6 + - spi-max-frequency: Maximum SPI frequency (depends on TPMs). 7 + 8 + Optional SoC Specific Properties: 9 + - pinctrl-names: Contains only one value - "default". 10 + - pintctrl-0: Specifies the pin control groups used for this controller. 11 + 12 + Example (for ARM-based BeagleBoard xM with TPM_TIS on SPI4): 13 + 14 + &mcspi4 { 15 + 16 + status = "okay"; 17 + 18 + tpm_tis@0 { 19 + 20 + compatible = "tcg,tpm_tis-spi"; 21 + 22 + spi-max-frequency = <10000000>; 23 + }; 24 + };
+12
drivers/char/tpm/Kconfig
··· 41 41 within Linux. To compile this driver as a module, choose M here; 42 42 the module will be called tpm_tis. 43 43 44 + config TCG_TIS_SPI 45 + tristate "TPM Interface Specification 1.3 Interface / TPM 2.0 FIFO Interface - (SPI)" 46 + depends on SPI 47 + select TCG_TIS_CORE 48 + ---help--- 49 + If you have a TPM security chip which is connected to a regular, 50 + non-tcg SPI master (i.e. most embedded platforms) that is compliant with the 51 + TCG TIS 1.3 TPM specification (TPM1.2) or the TCG PTP FIFO 52 + specification (TPM2.0) say Yes and it will be accessible from 53 + within Linux. To compile this driver as a module, choose M here; 54 + the module will be called tpm_tis_spi. 55 + 44 56 config TCG_TIS_I2C_ATMEL 45 57 tristate "TPM Interface Specification 1.2 Interface (I2C - Atmel)" 46 58 depends on I2C
+1
drivers/char/tpm/Makefile
··· 14 14 endif 15 15 obj-$(CONFIG_TCG_TIS_CORE) += tpm_tis_core.o 16 16 obj-$(CONFIG_TCG_TIS) += tpm_tis.o 17 + obj-$(CONFIG_TCG_TIS_SPI) += tpm_tis_spi.o 17 18 obj-$(CONFIG_TCG_TIS_I2C_ATMEL) += tpm_i2c_atmel.o 18 19 obj-$(CONFIG_TCG_TIS_I2C_INFINEON) += tpm_i2c_infineon.o 19 20 obj-$(CONFIG_TCG_TIS_I2C_NUVOTON) += tpm_i2c_nuvoton.o
+272
drivers/char/tpm/tpm_tis_spi.c
··· 1 + /* 2 + * Copyright (C) 2015 Infineon Technologies AG 3 + * Copyright (C) 2016 STMicroelectronics SAS 4 + * 5 + * Authors: 6 + * Peter Huewe <peter.huewe@infineon.com> 7 + * Christophe Ricard <christophe-h.ricard@st.com> 8 + * 9 + * Maintained by: <tpmdd-devel@lists.sourceforge.net> 10 + * 11 + * Device driver for TCG/TCPA TPM (trusted platform module). 12 + * Specifications at www.trustedcomputinggroup.org 13 + * 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 16 + * SPI access_. 17 + * 18 + * It is based on the original tpm_tis device driver from Leendert van 19 + * Dorn and Kyleen Hall and Jarko Sakkinnen. 20 + * 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 24 + * License. 25 + */ 26 + 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> 35 + 36 + #include <linux/module.h> 37 + #include <linux/spi/spi.h> 38 + #include <linux/gpio.h> 39 + #include <linux/of_irq.h> 40 + #include <linux/of_gpio.h> 41 + #include <linux/tpm.h> 42 + #include "tpm.h" 43 + #include "tpm_tis_core.h" 44 + 45 + #define MAX_SPI_FRAMESIZE 64 46 + 47 + struct tpm_tis_spi_phy { 48 + struct tpm_tis_data priv; 49 + struct spi_device *spi_device; 50 + 51 + u8 tx_buf[MAX_SPI_FRAMESIZE + 4]; 52 + u8 rx_buf[MAX_SPI_FRAMESIZE + 4]; 53 + }; 54 + 55 + static inline struct tpm_tis_spi_phy *to_tpm_tis_spi_phy(struct tpm_tis_data *data) 56 + { 57 + return container_of(data, struct tpm_tis_spi_phy, priv); 58 + } 59 + 60 + static int tpm_tis_spi_read_bytes(struct tpm_tis_data *data, u32 addr, 61 + u16 len, u8 *result) 62 + { 63 + struct tpm_tis_spi_phy *phy = to_tpm_tis_spi_phy(data); 64 + int ret, i; 65 + struct spi_message m; 66 + struct spi_transfer spi_xfer = { 67 + .tx_buf = phy->tx_buf, 68 + .rx_buf = phy->rx_buf, 69 + .len = 4, 70 + }; 71 + 72 + if (len > MAX_SPI_FRAMESIZE) 73 + return -ENOMEM; 74 + 75 + phy->tx_buf[0] = 0x80 | (len - 1); 76 + phy->tx_buf[1] = 0xd4; 77 + phy->tx_buf[2] = (addr >> 8) & 0xFF; 78 + phy->tx_buf[3] = addr & 0xFF; 79 + 80 + spi_xfer.cs_change = 1; 81 + spi_message_init(&m); 82 + spi_message_add_tail(&spi_xfer, &m); 83 + 84 + spi_bus_lock(phy->spi_device->master); 85 + ret = spi_sync_locked(phy->spi_device, &m); 86 + if (ret < 0) 87 + goto exit; 88 + 89 + memset(phy->tx_buf, 0, len); 90 + 91 + /* According to TCG PTP specification, if there is no TPM present at 92 + * all, then the design has a weak pull-up on MISO. If a TPM is not 93 + * present, a pull-up on MISO means that the SB controller sees a 1, 94 + * and will latch in 0xFF on the read. 95 + */ 96 + for (i = 0; (phy->rx_buf[0] & 0x01) == 0 && i < TPM_RETRY; i++) { 97 + spi_xfer.len = 1; 98 + spi_message_init(&m); 99 + spi_message_add_tail(&spi_xfer, &m); 100 + ret = spi_sync_locked(phy->spi_device, &m); 101 + if (ret < 0) 102 + goto exit; 103 + } 104 + 105 + spi_xfer.cs_change = 0; 106 + spi_xfer.len = len; 107 + spi_xfer.rx_buf = result; 108 + 109 + spi_message_init(&m); 110 + spi_message_add_tail(&spi_xfer, &m); 111 + ret = spi_sync_locked(phy->spi_device, &m); 112 + 113 + exit: 114 + spi_bus_unlock(phy->spi_device->master); 115 + return ret; 116 + } 117 + 118 + static int tpm_tis_spi_write_bytes(struct tpm_tis_data *data, u32 addr, 119 + u16 len, u8 *value) 120 + { 121 + struct tpm_tis_spi_phy *phy = to_tpm_tis_spi_phy(data); 122 + int ret, i; 123 + struct spi_message m; 124 + struct spi_transfer spi_xfer = { 125 + .tx_buf = phy->tx_buf, 126 + .rx_buf = phy->rx_buf, 127 + .len = 4, 128 + }; 129 + 130 + if (len > MAX_SPI_FRAMESIZE) 131 + return -ENOMEM; 132 + 133 + phy->tx_buf[0] = len - 1; 134 + phy->tx_buf[1] = 0xd4; 135 + phy->tx_buf[2] = (addr >> 8) & 0xFF; 136 + phy->tx_buf[3] = addr & 0xFF; 137 + 138 + spi_xfer.cs_change = 1; 139 + spi_message_init(&m); 140 + spi_message_add_tail(&spi_xfer, &m); 141 + 142 + spi_bus_lock(phy->spi_device->master); 143 + ret = spi_sync_locked(phy->spi_device, &m); 144 + if (ret < 0) 145 + goto exit; 146 + 147 + memset(phy->tx_buf, 0, len); 148 + 149 + /* According to TCG PTP specification, if there is no TPM present at 150 + * all, then the design has a weak pull-up on MISO. If a TPM is not 151 + * present, a pull-up on MISO means that the SB controller sees a 1, 152 + * and will latch in 0xFF on the read. 153 + */ 154 + for (i = 0; (phy->rx_buf[0] & 0x01) == 0 && i < TPM_RETRY; i++) { 155 + spi_xfer.len = 1; 156 + spi_message_init(&m); 157 + spi_message_add_tail(&spi_xfer, &m); 158 + ret = spi_sync_locked(phy->spi_device, &m); 159 + if (ret < 0) 160 + goto exit; 161 + } 162 + 163 + spi_xfer.len = len; 164 + spi_xfer.tx_buf = value; 165 + spi_xfer.cs_change = 0; 166 + spi_xfer.tx_buf = value; 167 + spi_message_init(&m); 168 + spi_message_add_tail(&spi_xfer, &m); 169 + ret = spi_sync_locked(phy->spi_device, &m); 170 + 171 + exit: 172 + spi_bus_unlock(phy->spi_device->master); 173 + return ret; 174 + } 175 + 176 + static int tpm_tis_spi_read16(struct tpm_tis_data *data, u32 addr, u16 *result) 177 + { 178 + int rc; 179 + 180 + rc = data->phy_ops->read_bytes(data, addr, sizeof(u16), (u8 *)result); 181 + if (!rc) 182 + *result = le16_to_cpu(*result); 183 + return rc; 184 + } 185 + 186 + static int tpm_tis_spi_read32(struct tpm_tis_data *data, u32 addr, u32 *result) 187 + { 188 + int rc; 189 + 190 + rc = data->phy_ops->read_bytes(data, addr, sizeof(u32), (u8 *)result); 191 + if (!rc) 192 + *result = le32_to_cpu(*result); 193 + return rc; 194 + } 195 + 196 + static int tpm_tis_spi_write32(struct tpm_tis_data *data, u32 addr, u32 value) 197 + { 198 + value = cpu_to_le32(value); 199 + return data->phy_ops->write_bytes(data, addr, sizeof(u32), 200 + (u8 *)&value); 201 + } 202 + 203 + static const struct tpm_tis_phy_ops tpm_spi_phy_ops = { 204 + .read_bytes = tpm_tis_spi_read_bytes, 205 + .write_bytes = tpm_tis_spi_write_bytes, 206 + .read16 = tpm_tis_spi_read16, 207 + .read32 = tpm_tis_spi_read32, 208 + .write32 = tpm_tis_spi_write32, 209 + }; 210 + 211 + static int tpm_tis_spi_probe(struct spi_device *dev) 212 + { 213 + struct tpm_tis_spi_phy *phy; 214 + 215 + phy = devm_kzalloc(&dev->dev, sizeof(struct tpm_tis_spi_phy), 216 + GFP_KERNEL); 217 + if (!phy) 218 + return -ENOMEM; 219 + 220 + phy->spi_device = dev; 221 + 222 + return tpm_tis_core_init(&dev->dev, &phy->priv, -1, &tpm_spi_phy_ops, 223 + NULL); 224 + } 225 + 226 + static SIMPLE_DEV_PM_OPS(tpm_tis_pm, tpm_pm_suspend, tpm_tis_resume); 227 + 228 + static int tpm_tis_spi_remove(struct spi_device *dev) 229 + { 230 + struct tpm_chip *chip = spi_get_drvdata(dev); 231 + 232 + tpm_chip_unregister(chip); 233 + tpm_tis_remove(chip); 234 + return 0; 235 + } 236 + 237 + static const struct spi_device_id tpm_tis_spi_id[] = { 238 + {"tpm_tis_spi", 0}, 239 + {} 240 + }; 241 + MODULE_DEVICE_TABLE(spi, tpm_tis_spi_id); 242 + 243 + static const struct of_device_id of_tis_spi_match[] = { 244 + { .compatible = "st,st33htpm-spi", }, 245 + { .compatible = "infineon,slb9670", }, 246 + { .compatible = "tcg,tpm_tis-spi", }, 247 + {} 248 + }; 249 + MODULE_DEVICE_TABLE(of, of_tis_spi_match); 250 + 251 + static const struct acpi_device_id acpi_tis_spi_match[] = { 252 + {"SMO0768", 0}, 253 + {} 254 + }; 255 + MODULE_DEVICE_TABLE(acpi, acpi_tis_spi_match); 256 + 257 + static struct spi_driver tpm_tis_spi_driver = { 258 + .driver = { 259 + .owner = THIS_MODULE, 260 + .name = "tpm_tis_spi", 261 + .pm = &tpm_tis_pm, 262 + .of_match_table = of_match_ptr(of_tis_spi_match), 263 + .acpi_match_table = ACPI_PTR(acpi_tis_spi_match), 264 + }, 265 + .probe = tpm_tis_spi_probe, 266 + .remove = tpm_tis_spi_remove, 267 + .id_table = tpm_tis_spi_id, 268 + }; 269 + module_spi_driver(tpm_tis_spi_driver); 270 + 271 + MODULE_DESCRIPTION("TPM Driver for native SPI access"); 272 + MODULE_LICENSE("GPL");