1 // SPDX-License-Identifier: GPL-2.0
4 * Miquel Raynal <miquel.raynal@bootlin.com>
7 * SPI-level driver for TCG/TIS TPM (trusted platform module).
8 * Specifications at www.trustedcomputinggroup.org
10 * This device driver implements the TPM interface as defined in
11 * the TCG SPI protocol stack version 2.0.
13 * It is based on the U-Boot driver tpm_tis_infineon_i2c.c.
22 #include <linux/bitops.h>
23 #include <linux/delay.h>
24 #include <linux/errno.h>
25 #include <linux/compiler.h>
26 #include <linux/types.h>
27 #include <linux/unaligned/be_byteshift.h>
28 #include <asm-generic/gpio.h>
31 #include "tpm_internal.h"
33 #define MAX_SPI_FRAMESIZE 64
35 /* Number of wait states to wait for */
36 #define TPM_WAIT_STATES 100
39 * struct tpm_tis_chip_data - Non-discoverable TPM information
41 * @pcr_count: Number of PCR per bank
42 * @pcr_select_min: Size in octets of the pcrSelect array
44 struct tpm_tis_chip_data {
45 unsigned int pcr_count;
46 unsigned int pcr_select_min;
47 unsigned int time_before_first_cmd_ms;
51 * tpm_tis_spi_read() - Read from TPM register
53 * @addr: register address to read from
54 * @buffer: provided by caller
55 * @len: number of bytes to read
57 * Read len bytes from TPM register and put them into
58 * buffer (little-endian format, i.e. first byte is put into buffer[0]).
60 * NOTE: TPM is big-endian for multi-byte values. Multi-byte
61 * values have to be swapped.
63 * Return: -EIO on error, 0 on success.
65 static int tpm_tis_spi_xfer(struct udevice *dev, u32 addr, const u8 *out,
68 struct spi_slave *slave = dev_get_parent_priv(dev);
69 int transfer_len, ret;
70 u8 tx_buf[MAX_SPI_FRAMESIZE];
71 u8 rx_buf[MAX_SPI_FRAMESIZE];
74 log(LOGC_NONE, LOGL_ERR, "%s: can't do full duplex\n",
79 ret = spi_claim_bus(slave);
81 log(LOGC_NONE, LOGL_ERR, "%s: could not claim bus\n", __func__);
87 transfer_len = min_t(u16, len, MAX_SPI_FRAMESIZE);
88 tx_buf[0] = (in ? BIT(7) : 0) | (transfer_len - 1);
90 tx_buf[2] = addr >> 8;
93 ret = spi_xfer(slave, 4 * 8, tx_buf, rx_buf, SPI_XFER_BEGIN);
95 log(LOGC_NONE, LOGL_ERR,
96 "%s: spi request transfer failed (err: %d)\n",
102 if (!(rx_buf[3] & 0x1)) {
105 for (i = 0; i < TPM_WAIT_STATES; i++) {
106 ret = spi_xfer(slave, 1 * 8, NULL, rx_buf, 0);
108 log(LOGC_NONE, LOGL_ERR,
109 "%s: wait state failed: %d\n",
118 if (i == TPM_WAIT_STATES) {
119 log(LOGC_NONE, LOGL_ERR,
120 "%s: timeout on wait state\n", __func__);
128 memcpy(tx_buf, out, transfer_len);
132 ret = spi_xfer(slave, transfer_len * 8,
137 log(LOGC_NONE, LOGL_ERR,
138 "%s: spi read transfer failed (err: %d)\n",
144 memcpy(in, rx_buf, transfer_len);
152 /* If an error occurred, release the chip by deasserting the CS */
154 spi_xfer(slave, 0, NULL, NULL, SPI_XFER_END);
156 spi_release_bus(slave);
161 static int tpm_tis_spi_read(struct udevice *dev, u32 addr, u16 len, u8 *in)
163 return tpm_tis_spi_xfer(dev, addr, NULL, in, len);
166 static int tpm_tis_spi_read32(struct udevice *dev, u32 addr, u32 *result)
171 ret = tpm_tis_spi_read(dev, addr, sizeof(u32), (u8 *)&result_le);
173 *result = le32_to_cpu(result_le);
179 static int tpm_tis_spi_write(struct udevice *dev, u32 addr, u16 len, const u8 *out)
181 return tpm_tis_spi_xfer(dev, addr, out, NULL, len);
184 static int tpm_tis_spi_write32(struct udevice *dev, u32 addr, u32 value)
186 __le32 value_le = cpu_to_le32(value);
188 return tpm_tis_spi_write(dev, addr, sizeof(value), (u8 *)&value_le);
191 static int tpm_tis_wait_init(struct udevice *dev, int loc)
193 struct tpm_chip *chip = dev_get_priv(dev);
194 unsigned long start, stop;
198 start = get_timer(0);
199 stop = chip->timeout_b;
201 mdelay(TPM_TIMEOUT_MS);
203 ret = tpm_tis_spi_read(dev, TPM_ACCESS(loc), 1, &status);
207 if (status & TPM_ACCESS_VALID)
209 } while (get_timer(start) < stop);
214 static struct tpm_tis_phy_ops phy_ops = {
215 .read_bytes = tpm_tis_spi_read,
216 .write_bytes = tpm_tis_spi_write,
217 .read32 = tpm_tis_spi_read32,
218 .write32 = tpm_tis_spi_write32,
221 static int tpm_tis_spi_probe(struct udevice *dev)
223 struct tpm_tis_chip_data *drv_data = (void *)dev_get_driver_data(dev);
224 struct tpm_chip_priv *priv = dev_get_uclass_priv(dev);
225 struct tpm_chip *chip = dev_get_priv(dev);
228 /* Use the TPM v2 stack */
229 priv->version = TPM_V2;
231 if (CONFIG_IS_ENABLED(DM_GPIO)) {
232 struct gpio_desc reset_gpio;
234 ret = gpio_request_by_name(dev, "reset-gpios", 0,
235 &reset_gpio, GPIOD_IS_OUT);
238 ret = gpio_request_by_name(dev, "gpio-reset", 0,
239 &reset_gpio, GPIOD_IS_OUT);
241 log(LOGC_NONE, LOGL_NOTICE,
242 "%s: missing reset GPIO\n", __func__);
245 log(LOGC_NONE, LOGL_NOTICE,
246 "%s: gpio-reset is deprecated\n", __func__);
248 dm_gpio_set_value(&reset_gpio, 1);
250 dm_gpio_set_value(&reset_gpio, 0);
253 /* Ensure a minimum amount of time elapsed since reset of the TPM */
254 mdelay(drv_data->time_before_first_cmd_ms);
256 ret = tpm_tis_wait_init(dev, chip->locality);
258 log(LOGC_DM, LOGL_ERR, "%s: no device found\n", __func__);
262 tpm_tis_ops_register(dev, &phy_ops);
263 ret = tpm_tis_init(dev);
267 priv->pcr_count = drv_data->pcr_count;
268 priv->pcr_select_min = drv_data->pcr_select_min;
269 priv->version = TPM_V2;
276 static int tpm_tis_spi_remove(struct udevice *udev)
278 return tpm_tis_cleanup(udev);
281 static const struct tpm_ops tpm_tis_spi_ops = {
282 .open = tpm_tis_open,
283 .close = tpm_tis_close,
284 .get_desc = tpm_tis_get_desc,
285 .send = tpm_tis_send,
286 .recv = tpm_tis_recv,
287 .cleanup = tpm_tis_cleanup,
290 static const struct tpm_tis_chip_data tpm_tis_std_chip_data = {
293 .time_before_first_cmd_ms = 30,
296 static const struct udevice_id tpm_tis_spi_ids[] = {
298 .compatible = "tcg,tpm_tis-spi",
299 .data = (ulong)&tpm_tis_std_chip_data,
304 U_BOOT_DRIVER(tpm_tis_spi) = {
305 .name = "tpm_tis_spi",
307 .of_match = tpm_tis_spi_ids,
308 .ops = &tpm_tis_spi_ops,
309 .probe = tpm_tis_spi_probe,
310 .remove = tpm_tis_spi_remove,
311 .priv_auto = sizeof(struct tpm_chip),