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 DECLARE_GLOBAL_DATA_PTR;
35 #define TPM_ACCESS(l) (0x0000 | ((l) << 12))
36 #define TPM_INT_ENABLE(l) (0x0008 | ((l) << 12))
37 #define TPM_STS(l) (0x0018 | ((l) << 12))
38 #define TPM_DATA_FIFO(l) (0x0024 | ((l) << 12))
39 #define TPM_DID_VID(l) (0x0F00 | ((l) << 12))
40 #define TPM_RID(l) (0x0F04 | ((l) << 12))
42 #define MAX_SPI_FRAMESIZE 64
44 /* Number of wait states to wait for */
45 #define TPM_WAIT_STATES 100
48 * struct tpm_tis_chip_data - Non-discoverable TPM information
50 * @pcr_count: Number of PCR per bank
51 * @pcr_select_min: Size in octets of the pcrSelect array
53 struct tpm_tis_chip_data {
54 unsigned int pcr_count;
55 unsigned int pcr_select_min;
56 unsigned int time_before_first_cmd_ms;
60 * tpm_tis_spi_read() - Read from TPM register
62 * @addr: register address to read from
63 * @buffer: provided by caller
64 * @len: number of bytes to read
66 * Read len bytes from TPM register and put them into
67 * buffer (little-endian format, i.e. first byte is put into buffer[0]).
69 * NOTE: TPM is big-endian for multi-byte values. Multi-byte
70 * values have to be swapped.
72 * @return -EIO on error, 0 on success.
74 static int tpm_tis_spi_xfer(struct udevice *dev, u32 addr, const u8 *out,
77 struct spi_slave *slave = dev_get_parent_priv(dev);
78 int transfer_len, ret;
79 u8 tx_buf[MAX_SPI_FRAMESIZE];
80 u8 rx_buf[MAX_SPI_FRAMESIZE];
83 log(LOGC_NONE, LOGL_ERR, "%s: can't do full duplex\n",
88 ret = spi_claim_bus(slave);
90 log(LOGC_NONE, LOGL_ERR, "%s: could not claim bus\n", __func__);
96 transfer_len = min_t(u16, len, MAX_SPI_FRAMESIZE);
97 tx_buf[0] = (in ? BIT(7) : 0) | (transfer_len - 1);
99 tx_buf[2] = addr >> 8;
102 ret = spi_xfer(slave, 4 * 8, tx_buf, rx_buf, SPI_XFER_BEGIN);
104 log(LOGC_NONE, LOGL_ERR,
105 "%s: spi request transfer failed (err: %d)\n",
111 if (!(rx_buf[3] & 0x1)) {
114 for (i = 0; i < TPM_WAIT_STATES; i++) {
115 ret = spi_xfer(slave, 1 * 8, NULL, rx_buf, 0);
117 log(LOGC_NONE, LOGL_ERR,
118 "%s: wait state failed: %d\n",
127 if (i == TPM_WAIT_STATES) {
128 log(LOGC_NONE, LOGL_ERR,
129 "%s: timeout on wait state\n", __func__);
137 memcpy(tx_buf, out, transfer_len);
141 ret = spi_xfer(slave, transfer_len * 8,
146 log(LOGC_NONE, LOGL_ERR,
147 "%s: spi read transfer failed (err: %d)\n",
153 memcpy(in, rx_buf, transfer_len);
161 /* If an error occurred, release the chip by deasserting the CS */
163 spi_xfer(slave, 0, NULL, NULL, SPI_XFER_END);
165 spi_release_bus(slave);
170 static int tpm_tis_spi_read(struct udevice *dev, u16 addr, u8 *in, u16 len)
172 return tpm_tis_spi_xfer(dev, addr, NULL, in, len);
175 static int tpm_tis_spi_read32(struct udevice *dev, u32 addr, u32 *result)
180 ret = tpm_tis_spi_read(dev, addr, (u8 *)&result_le, sizeof(u32));
182 *result = le32_to_cpu(result_le);
187 static int tpm_tis_spi_write(struct udevice *dev, u16 addr, const u8 *out,
190 return tpm_tis_spi_xfer(dev, addr, out, NULL, len);
193 static int tpm_tis_spi_check_locality(struct udevice *dev, int loc)
195 const u8 mask = TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID;
196 struct tpm_chip *chip = dev_get_priv(dev);
200 ret = tpm_tis_spi_read(dev, TPM_ACCESS(loc), &buf, 1);
204 if ((buf & mask) == mask) {
205 chip->locality = loc;
212 static void tpm_tis_spi_release_locality(struct udevice *dev, int loc,
215 const u8 mask = TPM_ACCESS_REQUEST_PENDING | TPM_ACCESS_VALID;
218 if (tpm_tis_spi_read(dev, TPM_ACCESS(loc), &buf, 1) < 0)
221 if (force || (buf & mask) == mask) {
222 buf = TPM_ACCESS_ACTIVE_LOCALITY;
223 tpm_tis_spi_write(dev, TPM_ACCESS(loc), &buf, 1);
227 static int tpm_tis_spi_request_locality(struct udevice *dev, int loc)
229 struct tpm_chip *chip = dev_get_priv(dev);
230 unsigned long start, stop;
231 u8 buf = TPM_ACCESS_REQUEST_USE;
234 ret = tpm_tis_spi_check_locality(dev, loc);
238 if (ret != -ENOENT) {
239 log(LOGC_NONE, LOGL_ERR, "%s: Failed to get locality: %d\n",
244 ret = tpm_tis_spi_write(dev, TPM_ACCESS(loc), &buf, 1);
246 log(LOGC_NONE, LOGL_ERR, "%s: Failed to write to TPM: %d\n",
251 start = get_timer(0);
252 stop = chip->timeout_a;
254 ret = tpm_tis_spi_check_locality(dev, loc);
258 if (ret != -ENOENT) {
259 log(LOGC_NONE, LOGL_ERR,
260 "%s: Failed to get locality: %d\n", __func__, ret);
264 mdelay(TPM_TIMEOUT_MS);
265 } while (get_timer(start) < stop);
267 log(LOGC_NONE, LOGL_ERR, "%s: Timeout getting locality: %d\n", __func__,
273 static u8 tpm_tis_spi_status(struct udevice *dev, u8 *status)
275 struct tpm_chip *chip = dev_get_priv(dev);
277 return tpm_tis_spi_read(dev, TPM_STS(chip->locality), status, 1);
280 static int tpm_tis_spi_wait_for_stat(struct udevice *dev, u8 mask,
281 unsigned long timeout, u8 *status)
283 unsigned long start = get_timer(0);
284 unsigned long stop = timeout;
288 mdelay(TPM_TIMEOUT_MS);
289 ret = tpm_tis_spi_status(dev, status);
293 if ((*status & mask) == mask)
295 } while (get_timer(start) < stop);
300 static u8 tpm_tis_spi_valid_status(struct udevice *dev, u8 *status)
302 struct tpm_chip *chip = dev_get_priv(dev);
304 return tpm_tis_spi_wait_for_stat(dev, TPM_STS_VALID,
305 chip->timeout_c, status);
308 static int tpm_tis_spi_get_burstcount(struct udevice *dev)
310 struct tpm_chip *chip = dev_get_priv(dev);
311 unsigned long start, stop;
314 /* wait for burstcount */
315 start = get_timer(0);
316 stop = chip->timeout_d;
318 ret = tpm_tis_spi_read32(dev, TPM_STS(chip->locality),
323 burstcount = (burstcount >> 8) & 0xFFFF;
327 mdelay(TPM_TIMEOUT_MS);
328 } while (get_timer(start) < stop);
333 static int tpm_tis_spi_cancel(struct udevice *dev)
335 struct tpm_chip *chip = dev_get_priv(dev);
336 u8 data = TPM_STS_COMMAND_READY;
338 return tpm_tis_spi_write(dev, TPM_STS(chip->locality), &data, 1);
341 static int tpm_tis_spi_recv_data(struct udevice *dev, u8 *buf, size_t count)
343 struct tpm_chip *chip = dev_get_priv(dev);
344 int size = 0, burstcnt, len, ret;
347 while (size < count &&
348 tpm_tis_spi_wait_for_stat(dev,
349 TPM_STS_DATA_AVAIL | TPM_STS_VALID,
350 chip->timeout_c, &status) == 0) {
351 burstcnt = tpm_tis_spi_get_burstcount(dev);
355 len = min_t(int, burstcnt, count - size);
356 ret = tpm_tis_spi_read(dev, TPM_DATA_FIFO(chip->locality),
367 static int tpm_tis_spi_recv(struct udevice *dev, u8 *buf, size_t count)
369 struct tpm_chip *chip = dev_get_priv(dev);
375 if (count < TPM_HEADER_SIZE) {
380 size = tpm_tis_spi_recv_data(dev, buf, TPM_HEADER_SIZE);
381 if (size < TPM_HEADER_SIZE) {
382 log(LOGC_NONE, LOGL_ERR, "TPM error, unable to read header\n");
386 expected = get_unaligned_be32(buf + 2);
387 if (expected > count) {
392 size += tpm_tis_spi_recv_data(dev, &buf[TPM_HEADER_SIZE],
393 expected - TPM_HEADER_SIZE);
394 if (size < expected) {
395 log(LOGC_NONE, LOGL_ERR,
396 "TPM error, unable to read remaining bytes of result\n");
402 tpm_tis_spi_cancel(dev);
403 tpm_tis_spi_release_locality(dev, chip->locality, false);
408 static int tpm_tis_spi_send(struct udevice *dev, const u8 *buf, size_t len)
410 struct tpm_chip *chip = dev_get_priv(dev);
419 if (len > TPM_DEV_BUFSIZE)
420 return -E2BIG; /* Command is too long for our tpm, sorry */
422 ret = tpm_tis_spi_request_locality(dev, 0);
427 * Check if the TPM is ready. If not, if not, cancel the pending command
428 * and poll on the status to be finally ready.
430 ret = tpm_tis_spi_status(dev, &status);
434 if (!(status & TPM_STS_COMMAND_READY)) {
435 /* Force the transition, usually this will be done at startup */
436 ret = tpm_tis_spi_cancel(dev);
438 log(LOGC_NONE, LOGL_ERR,
439 "%s: Could not cancel previous operation\n",
444 ret = tpm_tis_spi_wait_for_stat(dev, TPM_STS_COMMAND_READY,
445 chip->timeout_b, &status);
446 if (ret < 0 || !(status & TPM_STS_COMMAND_READY)) {
447 log(LOGC_NONE, LOGL_ERR,
448 "status %d after wait for stat returned %d\n",
454 for (i = 0; i < len - 1;) {
455 burstcnt = tpm_tis_spi_get_burstcount(dev);
459 size = min_t(int, len - i - 1, burstcnt);
460 ret = tpm_tis_spi_write(dev, TPM_DATA_FIFO(chip->locality),
468 ret = tpm_tis_spi_valid_status(dev, &status);
472 if ((status & TPM_STS_DATA_EXPECT) == 0) {
477 ret = tpm_tis_spi_write(dev, TPM_DATA_FIFO(chip->locality),
482 ret = tpm_tis_spi_valid_status(dev, &status);
486 if ((status & TPM_STS_DATA_EXPECT) != 0) {
492 ret = tpm_tis_spi_write(dev, TPM_STS(chip->locality), &data, 1);
499 tpm_tis_spi_cancel(dev);
500 tpm_tis_spi_release_locality(dev, chip->locality, false);
505 static int tpm_tis_spi_cleanup(struct udevice *dev)
507 struct tpm_chip *chip = dev_get_priv(dev);
509 tpm_tis_spi_cancel(dev);
511 * The TPM needs some time to clean up here,
512 * so we sleep rather than keeping the bus busy
515 tpm_tis_spi_release_locality(dev, chip->locality, false);
520 static int tpm_tis_spi_open(struct udevice *dev)
522 struct tpm_chip *chip = dev_get_priv(dev);
523 struct tpm_chip_priv *priv = dev_get_uclass_priv(dev);
533 static int tpm_tis_spi_close(struct udevice *dev)
535 struct tpm_chip *chip = dev_get_priv(dev);
538 tpm_tis_spi_release_locality(dev, chip->locality, true);
545 static int tpm_tis_get_desc(struct udevice *dev, char *buf, int size)
547 struct tpm_chip *chip = dev_get_priv(dev);
552 return snprintf(buf, size,
553 "%s v2.0: VendorID 0x%04x, DeviceID 0x%04x, RevisionID 0x%02x [%s]",
554 dev->name, chip->vend_dev & 0xFFFF,
555 chip->vend_dev >> 16, chip->rid,
556 (chip->is_open ? "open" : "closed"));
559 static int tpm_tis_wait_init(struct udevice *dev, int loc)
561 struct tpm_chip *chip = dev_get_priv(dev);
562 unsigned long start, stop;
566 start = get_timer(0);
567 stop = chip->timeout_b;
569 mdelay(TPM_TIMEOUT_MS);
571 ret = tpm_tis_spi_read(dev, TPM_ACCESS(loc), &status, 1);
575 if (status & TPM_ACCESS_VALID)
577 } while (get_timer(start) < stop);
582 static int tpm_tis_spi_probe(struct udevice *dev)
584 struct tpm_tis_chip_data *drv_data = (void *)dev_get_driver_data(dev);
585 struct tpm_chip_priv *priv = dev_get_uclass_priv(dev);
586 struct tpm_chip *chip = dev_get_priv(dev);
589 /* Use the TPM v2 stack */
590 priv->version = TPM_V2;
592 if (CONFIG_IS_ENABLED(DM_GPIO)) {
593 struct gpio_desc reset_gpio;
595 ret = gpio_request_by_name(dev, "gpio-reset", 0,
596 &reset_gpio, GPIOD_IS_OUT);
598 log(LOGC_NONE, LOGL_NOTICE, "%s: missing reset GPIO\n",
601 dm_gpio_set_value(&reset_gpio, 1);
603 dm_gpio_set_value(&reset_gpio, 0);
607 /* Ensure a minimum amount of time elapsed since reset of the TPM */
608 mdelay(drv_data->time_before_first_cmd_ms);
611 chip->timeout_a = TIS_SHORT_TIMEOUT_MS;
612 chip->timeout_b = TIS_LONG_TIMEOUT_MS;
613 chip->timeout_c = TIS_SHORT_TIMEOUT_MS;
614 chip->timeout_d = TIS_SHORT_TIMEOUT_MS;
615 priv->pcr_count = drv_data->pcr_count;
616 priv->pcr_select_min = drv_data->pcr_select_min;
618 ret = tpm_tis_wait_init(dev, chip->locality);
620 log(LOGC_DM, LOGL_ERR, "%s: no device found\n", __func__);
624 ret = tpm_tis_spi_request_locality(dev, chip->locality);
626 log(LOGC_NONE, LOGL_ERR, "%s: could not request locality %d\n",
627 __func__, chip->locality);
631 ret = tpm_tis_spi_read32(dev, TPM_DID_VID(chip->locality),
634 log(LOGC_NONE, LOGL_ERR,
635 "%s: could not retrieve VendorID/DeviceID\n", __func__);
639 ret = tpm_tis_spi_read(dev, TPM_RID(chip->locality), &chip->rid, 1);
641 log(LOGC_NONE, LOGL_ERR, "%s: could not retrieve RevisionID\n",
646 log(LOGC_NONE, LOGL_ERR,
647 "SPI TPMv2.0 found (vid:%04x, did:%04x, rid:%02x)\n",
648 chip->vend_dev & 0xFFFF, chip->vend_dev >> 16, chip->rid);
653 static int tpm_tis_spi_remove(struct udevice *dev)
655 struct tpm_chip *chip = dev_get_priv(dev);
657 tpm_tis_spi_release_locality(dev, chip->locality, true);
662 static const struct tpm_ops tpm_tis_spi_ops = {
663 .open = tpm_tis_spi_open,
664 .close = tpm_tis_spi_close,
665 .get_desc = tpm_tis_get_desc,
666 .send = tpm_tis_spi_send,
667 .recv = tpm_tis_spi_recv,
668 .cleanup = tpm_tis_spi_cleanup,
671 static const struct tpm_tis_chip_data tpm_tis_std_chip_data = {
674 .time_before_first_cmd_ms = 30,
677 static const struct udevice_id tpm_tis_spi_ids[] = {
679 .compatible = "tcg,tpm_tis-spi",
680 .data = (ulong)&tpm_tis_std_chip_data,
685 U_BOOT_DRIVER(tpm_tis_spi) = {
686 .name = "tpm_tis_spi",
688 .of_match = tpm_tis_spi_ids,
689 .ops = &tpm_tis_spi_ops,
690 .probe = tpm_tis_spi_probe,
691 .remove = tpm_tis_spi_remove,
692 .priv_auto_alloc_size = sizeof(struct tpm_chip),