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/errno.h>
23 #include <linux/compiler.h>
24 #include <linux/types.h>
25 #include <linux/unaligned/be_byteshift.h>
26 #include <asm-generic/gpio.h>
29 #include "tpm_internal.h"
31 DECLARE_GLOBAL_DATA_PTR;
33 #define TPM_ACCESS(l) (0x0000 | ((l) << 12))
34 #define TPM_INT_ENABLE(l) (0x0008 | ((l) << 12))
35 #define TPM_STS(l) (0x0018 | ((l) << 12))
36 #define TPM_DATA_FIFO(l) (0x0024 | ((l) << 12))
37 #define TPM_DID_VID(l) (0x0F00 | ((l) << 12))
38 #define TPM_RID(l) (0x0F04 | ((l) << 12))
40 #define MAX_SPI_FRAMESIZE 64
42 /* Number of wait states to wait for */
43 #define TPM_WAIT_STATES 100
46 * struct tpm_tis_chip_data - Non-discoverable TPM information
48 * @pcr_count: Number of PCR per bank
49 * @pcr_select_min: Size in octets of the pcrSelect array
51 struct tpm_tis_chip_data {
52 unsigned int pcr_count;
53 unsigned int pcr_select_min;
54 unsigned int time_before_first_cmd_ms;
58 * tpm_tis_spi_read() - Read from TPM register
60 * @addr: register address to read from
61 * @buffer: provided by caller
62 * @len: number of bytes to read
64 * Read len bytes from TPM register and put them into
65 * buffer (little-endian format, i.e. first byte is put into buffer[0]).
67 * NOTE: TPM is big-endian for multi-byte values. Multi-byte
68 * values have to be swapped.
70 * @return -EIO on error, 0 on success.
72 static int tpm_tis_spi_xfer(struct udevice *dev, u32 addr, const u8 *out,
75 struct spi_slave *slave = dev_get_parent_priv(dev);
76 int transfer_len, ret;
77 u8 tx_buf[MAX_SPI_FRAMESIZE];
78 u8 rx_buf[MAX_SPI_FRAMESIZE];
81 log(LOGC_NONE, LOGL_ERR, "%s: can't do full duplex\n",
86 ret = spi_claim_bus(slave);
88 log(LOGC_NONE, LOGL_ERR, "%s: could not claim bus\n", __func__);
94 transfer_len = min_t(u16, len, MAX_SPI_FRAMESIZE);
95 tx_buf[0] = (in ? BIT(7) : 0) | (transfer_len - 1);
97 tx_buf[2] = addr >> 8;
100 ret = spi_xfer(slave, 4 * 8, tx_buf, rx_buf, SPI_XFER_BEGIN);
102 log(LOGC_NONE, LOGL_ERR,
103 "%s: spi request transfer failed (err: %d)\n",
109 if (!(rx_buf[3] & 0x1)) {
112 for (i = 0; i < TPM_WAIT_STATES; i++) {
113 ret = spi_xfer(slave, 1 * 8, NULL, rx_buf, 0);
115 log(LOGC_NONE, LOGL_ERR,
116 "%s: wait state failed: %d\n",
125 if (i == TPM_WAIT_STATES) {
126 log(LOGC_NONE, LOGL_ERR,
127 "%s: timeout on wait state\n", __func__);
135 memcpy(tx_buf, out, transfer_len);
139 ret = spi_xfer(slave, transfer_len * 8,
144 log(LOGC_NONE, LOGL_ERR,
145 "%s: spi read transfer failed (err: %d)\n",
151 memcpy(in, rx_buf, transfer_len);
159 /* If an error occurred, release the chip by deasserting the CS */
161 spi_xfer(slave, 0, NULL, NULL, SPI_XFER_END);
163 spi_release_bus(slave);
168 static int tpm_tis_spi_read(struct udevice *dev, u16 addr, u8 *in, u16 len)
170 return tpm_tis_spi_xfer(dev, addr, NULL, in, len);
173 static int tpm_tis_spi_read32(struct udevice *dev, u32 addr, u32 *result)
178 ret = tpm_tis_spi_read(dev, addr, (u8 *)&result_le, sizeof(u32));
180 *result = le32_to_cpu(result_le);
185 static int tpm_tis_spi_write(struct udevice *dev, u16 addr, const u8 *out,
188 return tpm_tis_spi_xfer(dev, addr, out, NULL, len);
191 static int tpm_tis_spi_check_locality(struct udevice *dev, int loc)
193 const u8 mask = TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID;
194 struct tpm_chip *chip = dev_get_priv(dev);
198 ret = tpm_tis_spi_read(dev, TPM_ACCESS(loc), &buf, 1);
202 if ((buf & mask) == mask) {
203 chip->locality = loc;
210 static void tpm_tis_spi_release_locality(struct udevice *dev, int loc,
213 const u8 mask = TPM_ACCESS_REQUEST_PENDING | TPM_ACCESS_VALID;
216 if (tpm_tis_spi_read(dev, TPM_ACCESS(loc), &buf, 1) < 0)
219 if (force || (buf & mask) == mask) {
220 buf = TPM_ACCESS_ACTIVE_LOCALITY;
221 tpm_tis_spi_write(dev, TPM_ACCESS(loc), &buf, 1);
225 static int tpm_tis_spi_request_locality(struct udevice *dev, int loc)
227 struct tpm_chip *chip = dev_get_priv(dev);
228 unsigned long start, stop;
229 u8 buf = TPM_ACCESS_REQUEST_USE;
232 ret = tpm_tis_spi_check_locality(dev, loc);
236 if (ret != -ENOENT) {
237 log(LOGC_NONE, LOGL_ERR, "%s: Failed to get locality: %d\n",
242 ret = tpm_tis_spi_write(dev, TPM_ACCESS(loc), &buf, 1);
244 log(LOGC_NONE, LOGL_ERR, "%s: Failed to write to TPM: %d\n",
249 start = get_timer(0);
250 stop = chip->timeout_a;
252 ret = tpm_tis_spi_check_locality(dev, loc);
256 if (ret != -ENOENT) {
257 log(LOGC_NONE, LOGL_ERR,
258 "%s: Failed to get locality: %d\n", __func__, ret);
262 mdelay(TPM_TIMEOUT_MS);
263 } while (get_timer(start) < stop);
265 log(LOGC_NONE, LOGL_ERR, "%s: Timeout getting locality: %d\n", __func__,
271 static u8 tpm_tis_spi_status(struct udevice *dev, u8 *status)
273 struct tpm_chip *chip = dev_get_priv(dev);
275 return tpm_tis_spi_read(dev, TPM_STS(chip->locality), status, 1);
278 static int tpm_tis_spi_wait_for_stat(struct udevice *dev, u8 mask,
279 unsigned long timeout, u8 *status)
281 unsigned long start = get_timer(0);
282 unsigned long stop = timeout;
286 mdelay(TPM_TIMEOUT_MS);
287 ret = tpm_tis_spi_status(dev, status);
291 if ((*status & mask) == mask)
293 } while (get_timer(start) < stop);
298 static int tpm_tis_spi_get_burstcount(struct udevice *dev)
300 struct tpm_chip *chip = dev_get_priv(dev);
301 unsigned long start, stop;
304 /* wait for burstcount */
305 start = get_timer(0);
306 stop = chip->timeout_d;
308 ret = tpm_tis_spi_read32(dev, TPM_STS(chip->locality),
313 burstcount = (burstcount >> 8) & 0xFFFF;
317 mdelay(TPM_TIMEOUT_MS);
318 } while (get_timer(start) < stop);
323 static int tpm_tis_spi_cancel(struct udevice *dev)
325 struct tpm_chip *chip = dev_get_priv(dev);
326 u8 data = TPM_STS_COMMAND_READY;
328 return tpm_tis_spi_write(dev, TPM_STS(chip->locality), &data, 1);
331 static int tpm_tis_spi_recv_data(struct udevice *dev, u8 *buf, size_t count)
333 struct tpm_chip *chip = dev_get_priv(dev);
334 int size = 0, burstcnt, len, ret;
337 while (size < count &&
338 tpm_tis_spi_wait_for_stat(dev,
339 TPM_STS_DATA_AVAIL | TPM_STS_VALID,
340 chip->timeout_c, &status) == 0) {
341 burstcnt = tpm_tis_spi_get_burstcount(dev);
345 len = min_t(int, burstcnt, count - size);
346 ret = tpm_tis_spi_read(dev, TPM_DATA_FIFO(chip->locality),
357 static int tpm_tis_spi_recv(struct udevice *dev, u8 *buf, size_t count)
359 struct tpm_chip *chip = dev_get_priv(dev);
365 if (count < TPM_HEADER_SIZE) {
370 size = tpm_tis_spi_recv_data(dev, buf, TPM_HEADER_SIZE);
371 if (size < TPM_HEADER_SIZE) {
372 log(LOGC_NONE, LOGL_ERR, "TPM error, unable to read header\n");
376 expected = get_unaligned_be32(buf + 2);
377 if (expected > count) {
382 size += tpm_tis_spi_recv_data(dev, &buf[TPM_HEADER_SIZE],
383 expected - TPM_HEADER_SIZE);
384 if (size < expected) {
385 log(LOGC_NONE, LOGL_ERR,
386 "TPM error, unable to read remaining bytes of result\n");
392 tpm_tis_spi_cancel(dev);
393 tpm_tis_spi_release_locality(dev, chip->locality, false);
398 static int tpm_tis_spi_send(struct udevice *dev, const u8 *buf, size_t len)
400 struct tpm_chip *chip = dev_get_priv(dev);
409 if (len > TPM_DEV_BUFSIZE)
410 return -E2BIG; /* Command is too long for our tpm, sorry */
412 ret = tpm_tis_spi_request_locality(dev, 0);
417 * Check if the TPM is ready. If not, if not, cancel the pending command
418 * and poll on the status to be finally ready.
420 ret = tpm_tis_spi_status(dev, &status);
424 if (!(status & TPM_STS_COMMAND_READY)) {
425 /* Force the transition, usually this will be done at startup */
426 ret = tpm_tis_spi_cancel(dev);
428 log(LOGC_NONE, LOGL_ERR,
429 "%s: Could not cancel previous operation\n",
434 ret = tpm_tis_spi_wait_for_stat(dev, TPM_STS_COMMAND_READY,
435 chip->timeout_b, &status);
436 if (ret < 0 || !(status & TPM_STS_COMMAND_READY)) {
437 log(LOGC_NONE, LOGL_ERR,
438 "status %d after wait for stat returned %d\n",
444 for (i = 0; i < len - 1;) {
445 burstcnt = tpm_tis_spi_get_burstcount(dev);
449 size = min_t(int, len - i - 1, burstcnt);
450 ret = tpm_tis_spi_write(dev, TPM_DATA_FIFO(chip->locality),
458 ret = tpm_tis_spi_status(dev, &status);
462 if ((status & TPM_STS_DATA_EXPECT) == 0) {
467 ret = tpm_tis_spi_write(dev, TPM_DATA_FIFO(chip->locality),
472 ret = tpm_tis_spi_status(dev, &status);
476 if ((status & TPM_STS_DATA_EXPECT) != 0) {
482 ret = tpm_tis_spi_write(dev, TPM_STS(chip->locality), &data, 1);
489 tpm_tis_spi_cancel(dev);
490 tpm_tis_spi_release_locality(dev, chip->locality, false);
495 static int tpm_tis_spi_cleanup(struct udevice *dev)
497 struct tpm_chip *chip = dev_get_priv(dev);
499 tpm_tis_spi_cancel(dev);
501 * The TPM needs some time to clean up here,
502 * so we sleep rather than keeping the bus busy
505 tpm_tis_spi_release_locality(dev, chip->locality, false);
510 static int tpm_tis_spi_open(struct udevice *dev)
512 struct tpm_chip *chip = dev_get_priv(dev);
522 static int tpm_tis_spi_close(struct udevice *dev)
524 struct tpm_chip *chip = dev_get_priv(dev);
527 tpm_tis_spi_release_locality(dev, chip->locality, true);
534 static int tpm_tis_get_desc(struct udevice *dev, char *buf, int size)
536 struct tpm_chip *chip = dev_get_priv(dev);
541 return snprintf(buf, size,
542 "%s v2.0: VendorID 0x%04x, DeviceID 0x%04x, RevisionID 0x%02x [%s]",
543 dev->name, chip->vend_dev & 0xFFFF,
544 chip->vend_dev >> 16, chip->rid,
545 (chip->is_open ? "open" : "closed"));
548 static int tpm_tis_wait_init(struct udevice *dev, int loc)
550 struct tpm_chip *chip = dev_get_priv(dev);
551 unsigned long start, stop;
555 start = get_timer(0);
556 stop = chip->timeout_b;
558 mdelay(TPM_TIMEOUT_MS);
560 ret = tpm_tis_spi_read(dev, TPM_ACCESS(loc), &status, 1);
564 if (status & TPM_ACCESS_VALID)
566 } while (get_timer(start) < stop);
571 static int tpm_tis_spi_probe(struct udevice *dev)
573 struct tpm_tis_chip_data *drv_data = (void *)dev_get_driver_data(dev);
574 struct tpm_chip_priv *priv = dev_get_uclass_priv(dev);
575 struct tpm_chip *chip = dev_get_priv(dev);
578 if (IS_ENABLED(CONFIG_DM_GPIO)) {
579 struct gpio_desc reset_gpio;
581 ret = gpio_request_by_name(dev, "gpio-reset", 0,
582 &reset_gpio, GPIOD_IS_OUT);
584 log(LOGC_NONE, LOGL_NOTICE, "%s: missing reset GPIO\n",
587 dm_gpio_set_value(&reset_gpio, 0);
589 dm_gpio_set_value(&reset_gpio, 1);
593 /* Ensure a minimum amount of time elapsed since reset of the TPM */
594 mdelay(drv_data->time_before_first_cmd_ms);
597 chip->timeout_a = TIS_SHORT_TIMEOUT_MS;
598 chip->timeout_b = TIS_LONG_TIMEOUT_MS;
599 chip->timeout_c = TIS_SHORT_TIMEOUT_MS;
600 chip->timeout_d = TIS_SHORT_TIMEOUT_MS;
601 priv->pcr_count = drv_data->pcr_count;
602 priv->pcr_select_min = drv_data->pcr_select_min;
604 ret = tpm_tis_wait_init(dev, chip->locality);
606 log(LOGC_DM, LOGL_ERR, "%s: no device found\n", __func__);
610 ret = tpm_tis_spi_request_locality(dev, chip->locality);
612 log(LOGC_NONE, LOGL_ERR, "%s: could not request locality %d\n",
613 __func__, chip->locality);
617 ret = tpm_tis_spi_read32(dev, TPM_DID_VID(chip->locality),
620 log(LOGC_NONE, LOGL_ERR,
621 "%s: could not retrieve VendorID/DeviceID\n", __func__);
625 ret = tpm_tis_spi_read(dev, TPM_RID(chip->locality), &chip->rid, 1);
627 log(LOGC_NONE, LOGL_ERR, "%s: could not retrieve RevisionID\n",
632 log(LOGC_NONE, LOGL_ERR,
633 "SPI TPMv2.0 found (vid:%04x, did:%04x, rid:%02x)\n",
634 chip->vend_dev & 0xFFFF, chip->vend_dev >> 16, chip->rid);
639 static int tpm_tis_spi_remove(struct udevice *dev)
641 struct tpm_chip *chip = dev_get_priv(dev);
643 tpm_tis_spi_release_locality(dev, chip->locality, true);
648 static const struct tpm_ops tpm_tis_spi_ops = {
649 .open = tpm_tis_spi_open,
650 .close = tpm_tis_spi_close,
651 .get_desc = tpm_tis_get_desc,
652 .send = tpm_tis_spi_send,
653 .recv = tpm_tis_spi_recv,
654 .cleanup = tpm_tis_spi_cleanup,
657 static const struct tpm_tis_chip_data tpm_tis_std_chip_data = {
660 .time_before_first_cmd_ms = 30,
663 static const struct udevice_id tpm_tis_spi_ids[] = {
665 .compatible = "tis,tpm2-spi",
666 .data = (ulong)&tpm_tis_std_chip_data,
671 U_BOOT_DRIVER(tpm_tis_spi) = {
672 .name = "tpm_tis_spi",
674 .of_match = tpm_tis_spi_ids,
675 .ops = &tpm_tis_spi_ops,
676 .probe = tpm_tis_spi_probe,
677 .remove = tpm_tis_spi_remove,
678 .priv_auto_alloc_size = sizeof(struct tpm_chip),