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/delay.h>
23 #include <linux/errno.h>
24 #include <linux/compiler.h>
25 #include <linux/types.h>
26 #include <linux/unaligned/be_byteshift.h>
27 #include <asm-generic/gpio.h>
30 #include "tpm_internal.h"
32 DECLARE_GLOBAL_DATA_PTR;
34 #define TPM_ACCESS(l) (0x0000 | ((l) << 12))
35 #define TPM_INT_ENABLE(l) (0x0008 | ((l) << 12))
36 #define TPM_STS(l) (0x0018 | ((l) << 12))
37 #define TPM_DATA_FIFO(l) (0x0024 | ((l) << 12))
38 #define TPM_DID_VID(l) (0x0F00 | ((l) << 12))
39 #define TPM_RID(l) (0x0F04 | ((l) << 12))
41 #define MAX_SPI_FRAMESIZE 64
43 /* Number of wait states to wait for */
44 #define TPM_WAIT_STATES 100
47 * struct tpm_tis_chip_data - Non-discoverable TPM information
49 * @pcr_count: Number of PCR per bank
50 * @pcr_select_min: Size in octets of the pcrSelect array
52 struct tpm_tis_chip_data {
53 unsigned int pcr_count;
54 unsigned int pcr_select_min;
55 unsigned int time_before_first_cmd_ms;
59 * tpm_tis_spi_read() - Read from TPM register
61 * @addr: register address to read from
62 * @buffer: provided by caller
63 * @len: number of bytes to read
65 * Read len bytes from TPM register and put them into
66 * buffer (little-endian format, i.e. first byte is put into buffer[0]).
68 * NOTE: TPM is big-endian for multi-byte values. Multi-byte
69 * values have to be swapped.
71 * @return -EIO on error, 0 on success.
73 static int tpm_tis_spi_xfer(struct udevice *dev, u32 addr, const u8 *out,
76 struct spi_slave *slave = dev_get_parent_priv(dev);
77 int transfer_len, ret;
78 u8 tx_buf[MAX_SPI_FRAMESIZE];
79 u8 rx_buf[MAX_SPI_FRAMESIZE];
82 log(LOGC_NONE, LOGL_ERR, "%s: can't do full duplex\n",
87 ret = spi_claim_bus(slave);
89 log(LOGC_NONE, LOGL_ERR, "%s: could not claim bus\n", __func__);
95 transfer_len = min_t(u16, len, MAX_SPI_FRAMESIZE);
96 tx_buf[0] = (in ? BIT(7) : 0) | (transfer_len - 1);
98 tx_buf[2] = addr >> 8;
101 ret = spi_xfer(slave, 4 * 8, tx_buf, rx_buf, SPI_XFER_BEGIN);
103 log(LOGC_NONE, LOGL_ERR,
104 "%s: spi request transfer failed (err: %d)\n",
110 if (!(rx_buf[3] & 0x1)) {
113 for (i = 0; i < TPM_WAIT_STATES; i++) {
114 ret = spi_xfer(slave, 1 * 8, NULL, rx_buf, 0);
116 log(LOGC_NONE, LOGL_ERR,
117 "%s: wait state failed: %d\n",
126 if (i == TPM_WAIT_STATES) {
127 log(LOGC_NONE, LOGL_ERR,
128 "%s: timeout on wait state\n", __func__);
136 memcpy(tx_buf, out, transfer_len);
140 ret = spi_xfer(slave, transfer_len * 8,
145 log(LOGC_NONE, LOGL_ERR,
146 "%s: spi read transfer failed (err: %d)\n",
152 memcpy(in, rx_buf, transfer_len);
160 /* If an error occurred, release the chip by deasserting the CS */
162 spi_xfer(slave, 0, NULL, NULL, SPI_XFER_END);
164 spi_release_bus(slave);
169 static int tpm_tis_spi_read(struct udevice *dev, u16 addr, u8 *in, u16 len)
171 return tpm_tis_spi_xfer(dev, addr, NULL, in, len);
174 static int tpm_tis_spi_read32(struct udevice *dev, u32 addr, u32 *result)
179 ret = tpm_tis_spi_read(dev, addr, (u8 *)&result_le, sizeof(u32));
181 *result = le32_to_cpu(result_le);
186 static int tpm_tis_spi_write(struct udevice *dev, u16 addr, const u8 *out,
189 return tpm_tis_spi_xfer(dev, addr, out, NULL, len);
192 static int tpm_tis_spi_check_locality(struct udevice *dev, int loc)
194 const u8 mask = TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID;
195 struct tpm_chip *chip = dev_get_priv(dev);
199 ret = tpm_tis_spi_read(dev, TPM_ACCESS(loc), &buf, 1);
203 if ((buf & mask) == mask) {
204 chip->locality = loc;
211 static void tpm_tis_spi_release_locality(struct udevice *dev, int loc,
214 const u8 mask = TPM_ACCESS_REQUEST_PENDING | TPM_ACCESS_VALID;
217 if (tpm_tis_spi_read(dev, TPM_ACCESS(loc), &buf, 1) < 0)
220 if (force || (buf & mask) == mask) {
221 buf = TPM_ACCESS_ACTIVE_LOCALITY;
222 tpm_tis_spi_write(dev, TPM_ACCESS(loc), &buf, 1);
226 static int tpm_tis_spi_request_locality(struct udevice *dev, int loc)
228 struct tpm_chip *chip = dev_get_priv(dev);
229 unsigned long start, stop;
230 u8 buf = TPM_ACCESS_REQUEST_USE;
233 ret = tpm_tis_spi_check_locality(dev, loc);
237 if (ret != -ENOENT) {
238 log(LOGC_NONE, LOGL_ERR, "%s: Failed to get locality: %d\n",
243 ret = tpm_tis_spi_write(dev, TPM_ACCESS(loc), &buf, 1);
245 log(LOGC_NONE, LOGL_ERR, "%s: Failed to write to TPM: %d\n",
250 start = get_timer(0);
251 stop = chip->timeout_a;
253 ret = tpm_tis_spi_check_locality(dev, loc);
257 if (ret != -ENOENT) {
258 log(LOGC_NONE, LOGL_ERR,
259 "%s: Failed to get locality: %d\n", __func__, ret);
263 mdelay(TPM_TIMEOUT_MS);
264 } while (get_timer(start) < stop);
266 log(LOGC_NONE, LOGL_ERR, "%s: Timeout getting locality: %d\n", __func__,
272 static u8 tpm_tis_spi_status(struct udevice *dev, u8 *status)
274 struct tpm_chip *chip = dev_get_priv(dev);
276 return tpm_tis_spi_read(dev, TPM_STS(chip->locality), status, 1);
279 static int tpm_tis_spi_wait_for_stat(struct udevice *dev, u8 mask,
280 unsigned long timeout, u8 *status)
282 unsigned long start = get_timer(0);
283 unsigned long stop = timeout;
287 mdelay(TPM_TIMEOUT_MS);
288 ret = tpm_tis_spi_status(dev, status);
292 if ((*status & mask) == mask)
294 } while (get_timer(start) < stop);
299 static u8 tpm_tis_spi_valid_status(struct udevice *dev, u8 *status)
301 struct tpm_chip *chip = dev_get_priv(dev);
303 return tpm_tis_spi_wait_for_stat(dev, TPM_STS_VALID,
304 chip->timeout_c, status);
307 static int tpm_tis_spi_get_burstcount(struct udevice *dev)
309 struct tpm_chip *chip = dev_get_priv(dev);
310 unsigned long start, stop;
313 /* wait for burstcount */
314 start = get_timer(0);
315 stop = chip->timeout_d;
317 ret = tpm_tis_spi_read32(dev, TPM_STS(chip->locality),
322 burstcount = (burstcount >> 8) & 0xFFFF;
326 mdelay(TPM_TIMEOUT_MS);
327 } while (get_timer(start) < stop);
332 static int tpm_tis_spi_cancel(struct udevice *dev)
334 struct tpm_chip *chip = dev_get_priv(dev);
335 u8 data = TPM_STS_COMMAND_READY;
337 return tpm_tis_spi_write(dev, TPM_STS(chip->locality), &data, 1);
340 static int tpm_tis_spi_recv_data(struct udevice *dev, u8 *buf, size_t count)
342 struct tpm_chip *chip = dev_get_priv(dev);
343 int size = 0, burstcnt, len, ret;
346 while (size < count &&
347 tpm_tis_spi_wait_for_stat(dev,
348 TPM_STS_DATA_AVAIL | TPM_STS_VALID,
349 chip->timeout_c, &status) == 0) {
350 burstcnt = tpm_tis_spi_get_burstcount(dev);
354 len = min_t(int, burstcnt, count - size);
355 ret = tpm_tis_spi_read(dev, TPM_DATA_FIFO(chip->locality),
366 static int tpm_tis_spi_recv(struct udevice *dev, u8 *buf, size_t count)
368 struct tpm_chip *chip = dev_get_priv(dev);
374 if (count < TPM_HEADER_SIZE) {
379 size = tpm_tis_spi_recv_data(dev, buf, TPM_HEADER_SIZE);
380 if (size < TPM_HEADER_SIZE) {
381 log(LOGC_NONE, LOGL_ERR, "TPM error, unable to read header\n");
385 expected = get_unaligned_be32(buf + 2);
386 if (expected > count) {
391 size += tpm_tis_spi_recv_data(dev, &buf[TPM_HEADER_SIZE],
392 expected - TPM_HEADER_SIZE);
393 if (size < expected) {
394 log(LOGC_NONE, LOGL_ERR,
395 "TPM error, unable to read remaining bytes of result\n");
401 tpm_tis_spi_cancel(dev);
402 tpm_tis_spi_release_locality(dev, chip->locality, false);
407 static int tpm_tis_spi_send(struct udevice *dev, const u8 *buf, size_t len)
409 struct tpm_chip *chip = dev_get_priv(dev);
418 if (len > TPM_DEV_BUFSIZE)
419 return -E2BIG; /* Command is too long for our tpm, sorry */
421 ret = tpm_tis_spi_request_locality(dev, 0);
426 * Check if the TPM is ready. If not, if not, cancel the pending command
427 * and poll on the status to be finally ready.
429 ret = tpm_tis_spi_status(dev, &status);
433 if (!(status & TPM_STS_COMMAND_READY)) {
434 /* Force the transition, usually this will be done at startup */
435 ret = tpm_tis_spi_cancel(dev);
437 log(LOGC_NONE, LOGL_ERR,
438 "%s: Could not cancel previous operation\n",
443 ret = tpm_tis_spi_wait_for_stat(dev, TPM_STS_COMMAND_READY,
444 chip->timeout_b, &status);
445 if (ret < 0 || !(status & TPM_STS_COMMAND_READY)) {
446 log(LOGC_NONE, LOGL_ERR,
447 "status %d after wait for stat returned %d\n",
453 for (i = 0; i < len - 1;) {
454 burstcnt = tpm_tis_spi_get_burstcount(dev);
458 size = min_t(int, len - i - 1, burstcnt);
459 ret = tpm_tis_spi_write(dev, TPM_DATA_FIFO(chip->locality),
467 ret = tpm_tis_spi_valid_status(dev, &status);
471 if ((status & TPM_STS_DATA_EXPECT) == 0) {
476 ret = tpm_tis_spi_write(dev, TPM_DATA_FIFO(chip->locality),
481 ret = tpm_tis_spi_valid_status(dev, &status);
485 if ((status & TPM_STS_DATA_EXPECT) != 0) {
491 ret = tpm_tis_spi_write(dev, TPM_STS(chip->locality), &data, 1);
498 tpm_tis_spi_cancel(dev);
499 tpm_tis_spi_release_locality(dev, chip->locality, false);
504 static int tpm_tis_spi_cleanup(struct udevice *dev)
506 struct tpm_chip *chip = dev_get_priv(dev);
508 tpm_tis_spi_cancel(dev);
510 * The TPM needs some time to clean up here,
511 * so we sleep rather than keeping the bus busy
514 tpm_tis_spi_release_locality(dev, chip->locality, false);
519 static int tpm_tis_spi_open(struct udevice *dev)
521 struct tpm_chip *chip = dev_get_priv(dev);
522 struct tpm_chip_priv *priv = dev_get_uclass_priv(dev);
532 static int tpm_tis_spi_close(struct udevice *dev)
534 struct tpm_chip *chip = dev_get_priv(dev);
537 tpm_tis_spi_release_locality(dev, chip->locality, true);
544 static int tpm_tis_get_desc(struct udevice *dev, char *buf, int size)
546 struct tpm_chip *chip = dev_get_priv(dev);
551 return snprintf(buf, size,
552 "%s v2.0: VendorID 0x%04x, DeviceID 0x%04x, RevisionID 0x%02x [%s]",
553 dev->name, chip->vend_dev & 0xFFFF,
554 chip->vend_dev >> 16, chip->rid,
555 (chip->is_open ? "open" : "closed"));
558 static int tpm_tis_wait_init(struct udevice *dev, int loc)
560 struct tpm_chip *chip = dev_get_priv(dev);
561 unsigned long start, stop;
565 start = get_timer(0);
566 stop = chip->timeout_b;
568 mdelay(TPM_TIMEOUT_MS);
570 ret = tpm_tis_spi_read(dev, TPM_ACCESS(loc), &status, 1);
574 if (status & TPM_ACCESS_VALID)
576 } while (get_timer(start) < stop);
581 static int tpm_tis_spi_probe(struct udevice *dev)
583 struct tpm_tis_chip_data *drv_data = (void *)dev_get_driver_data(dev);
584 struct tpm_chip_priv *priv = dev_get_uclass_priv(dev);
585 struct tpm_chip *chip = dev_get_priv(dev);
588 /* Use the TPM v2 stack */
589 priv->version = TPM_V2;
591 if (CONFIG_IS_ENABLED(DM_GPIO)) {
592 struct gpio_desc reset_gpio;
594 ret = gpio_request_by_name(dev, "gpio-reset", 0,
595 &reset_gpio, GPIOD_IS_OUT);
597 log(LOGC_NONE, LOGL_NOTICE, "%s: missing reset GPIO\n",
600 dm_gpio_set_value(&reset_gpio, 1);
602 dm_gpio_set_value(&reset_gpio, 0);
606 /* Ensure a minimum amount of time elapsed since reset of the TPM */
607 mdelay(drv_data->time_before_first_cmd_ms);
610 chip->timeout_a = TIS_SHORT_TIMEOUT_MS;
611 chip->timeout_b = TIS_LONG_TIMEOUT_MS;
612 chip->timeout_c = TIS_SHORT_TIMEOUT_MS;
613 chip->timeout_d = TIS_SHORT_TIMEOUT_MS;
614 priv->pcr_count = drv_data->pcr_count;
615 priv->pcr_select_min = drv_data->pcr_select_min;
617 ret = tpm_tis_wait_init(dev, chip->locality);
619 log(LOGC_DM, LOGL_ERR, "%s: no device found\n", __func__);
623 ret = tpm_tis_spi_request_locality(dev, chip->locality);
625 log(LOGC_NONE, LOGL_ERR, "%s: could not request locality %d\n",
626 __func__, chip->locality);
630 ret = tpm_tis_spi_read32(dev, TPM_DID_VID(chip->locality),
633 log(LOGC_NONE, LOGL_ERR,
634 "%s: could not retrieve VendorID/DeviceID\n", __func__);
638 ret = tpm_tis_spi_read(dev, TPM_RID(chip->locality), &chip->rid, 1);
640 log(LOGC_NONE, LOGL_ERR, "%s: could not retrieve RevisionID\n",
645 log(LOGC_NONE, LOGL_ERR,
646 "SPI TPMv2.0 found (vid:%04x, did:%04x, rid:%02x)\n",
647 chip->vend_dev & 0xFFFF, chip->vend_dev >> 16, chip->rid);
652 static int tpm_tis_spi_remove(struct udevice *dev)
654 struct tpm_chip *chip = dev_get_priv(dev);
656 tpm_tis_spi_release_locality(dev, chip->locality, true);
661 static const struct tpm_ops tpm_tis_spi_ops = {
662 .open = tpm_tis_spi_open,
663 .close = tpm_tis_spi_close,
664 .get_desc = tpm_tis_get_desc,
665 .send = tpm_tis_spi_send,
666 .recv = tpm_tis_spi_recv,
667 .cleanup = tpm_tis_spi_cleanup,
670 static const struct tpm_tis_chip_data tpm_tis_std_chip_data = {
673 .time_before_first_cmd_ms = 30,
676 static const struct udevice_id tpm_tis_spi_ids[] = {
678 .compatible = "tis,tpm2-spi",
679 .data = (ulong)&tpm_tis_std_chip_data,
684 U_BOOT_DRIVER(tpm_tis_spi) = {
685 .name = "tpm_tis_spi",
687 .of_match = tpm_tis_spi_ids,
688 .ops = &tpm_tis_spi_ops,
689 .probe = tpm_tis_spi_probe,
690 .remove = tpm_tis_spi_remove,
691 .priv_auto_alloc_size = sizeof(struct tpm_chip),