2 * Copyright (C) 2011 Infineon Technologies
5 * Peter Huewe <huewe.external@infineon.com>
8 * Device driver for TCG/TCPA TPM (trusted platform module).
9 * Specifications at www.trustedcomputinggroup.org
11 * This device driver implements the TPM interface as defined in
12 * the TCG TPM Interface Spec version 1.2, revision 1.0 and the
13 * Infineon I2C Protocol Stack Specification v0.20.
15 * It is based on the Linux kernel driver tpm.c from Leendert van
16 * Dorn, Dave Safford, Reiner Sailer, and Kyleen Hall.
20 * SPDX-License-Identifier: GPL-2.0
26 #include <linux/compiler.h>
29 #include <asm-generic/errno.h>
30 #include <linux/types.h>
31 #include <linux/unaligned/be_byteshift.h>
33 #include "tpm_tis_i2c.h"
35 DECLARE_GLOBAL_DATA_PTR;
37 /* Max number of iterations after i2c NAK */
41 * Max number of iterations after i2c NAK for 'long' commands
43 * We need this especially for sending TPM_READY, since the cleanup after the
44 * transtion to the ready state may take some time, but it is unpredictable
45 * how long it will take.
47 #define MAX_COUNT_LONG 50
49 #define SLEEP_DURATION 60 /* in usec */
50 #define SLEEP_DURATION_LONG 210 /* in usec */
52 #define TPM_HEADER_SIZE 10
55 TPM_ACCESS_VALID = 0x80,
56 TPM_ACCESS_ACTIVE_LOCALITY = 0x20,
57 TPM_ACCESS_REQUEST_PENDING = 0x04,
58 TPM_ACCESS_REQUEST_USE = 0x02,
63 TPM_STS_COMMAND_READY = 0x40,
65 TPM_STS_DATA_AVAIL = 0x10,
66 TPM_STS_DATA_EXPECT = 0x08,
70 TIS_SHORT_TIMEOUT = 750, /* ms */
71 TIS_LONG_TIMEOUT = 2000, /* ms */
74 /* expected value for DIDVID register */
75 #define TPM_TIS_I2C_DID_VID_9635 0x000b15d1L
76 #define TPM_TIS_I2C_DID_VID_9645 0x001a15d1L
78 static const char * const chip_name[] = {
79 [SLB9635] = "slb9635tt",
80 [SLB9645] = "slb9645tt",
81 [UNKNOWN] = "unknown/fallback to slb9635",
84 #define TPM_ACCESS(l) (0x0000 | ((l) << 4))
85 #define TPM_STS(l) (0x0001 | ((l) << 4))
86 #define TPM_DATA_FIFO(l) (0x0005 | ((l) << 4))
87 #define TPM_DID_VID(l) (0x0006 | ((l) << 4))
96 /* Extended error numbers from linux (see errno.h) */
97 #define ECANCELED 125 /* Operation Canceled */
99 /* Timer frequency. Corresponds to msec timer resolution*/
102 #define TPM_MAX_ORDINAL 243
103 #define TPM_MAX_PROTECTED_ORDINAL 12
104 #define TPM_PROTECTED_ORDINAL_MASK 0xFF
106 #define TPM_CMD_COUNT_BYTE 2
107 #define TPM_CMD_ORDINAL_BYTE 6
110 * Array with one entry per ordinal defining the maximum amount
111 * of time the chip could take to return the result. The ordinal
112 * designation of short, medium or long is defined in a table in
113 * TCG Specification TPM Main Part 2 TPM Structures Section 17. The
114 * values of the SHORT, MEDIUM, and LONG durations are retrieved
115 * from the chip during initialization with a call to tpm_get_timeouts.
117 static const u8 tpm_protected_ordinal_duration[TPM_MAX_PROTECTED_ORDINAL] = {
118 TPM_UNDEFINED, /* 0 */
123 TPM_UNDEFINED, /* 5 */
132 static const u8 tpm_ordinal_duration[TPM_MAX_ORDINAL] = {
133 TPM_UNDEFINED, /* 0 */
138 TPM_UNDEFINED, /* 5 */
188 TPM_UNDEFINED, /* 55 */
208 TPM_UNDEFINED, /* 75 */
218 TPM_UNDEFINED, /* 85 */
228 TPM_UNDEFINED, /* 95 */
233 TPM_MEDIUM, /* 100 */
238 TPM_UNDEFINED, /* 105 */
268 TPM_UNDEFINED, /* 135 */
278 TPM_UNDEFINED, /* 145 */
288 TPM_UNDEFINED, /* 155 */
298 TPM_UNDEFINED, /* 165 */
308 TPM_UNDEFINED, /* 175 */
313 TPM_MEDIUM, /* 180 */
318 TPM_MEDIUM, /* 185 */
323 TPM_UNDEFINED, /* 190 */
328 TPM_UNDEFINED, /* 195 */
343 TPM_MEDIUM, /* 210 */
348 TPM_UNDEFINED, /* 215 */
358 TPM_UNDEFINED, /* 225 */
368 TPM_UNDEFINED, /* 235 */
378 static struct tpm_chip g_chip;
381 * iic_tpm_read() - read from TPM register
382 * @addr: register address to read from
383 * @buffer: provided by caller
384 * @len: number of bytes to read
386 * Read len bytes from TPM register and put them into
387 * buffer (little-endian format, i.e. first byte is put into buffer[0]).
389 * NOTE: TPM is big-endian for multi-byte values. Multi-byte
390 * values have to be swapped.
392 * Return -EIO on error, 0 on success.
394 static int iic_tpm_read(u8 addr, u8 *buffer, size_t len)
398 uint32_t addrbuf = addr;
400 if ((g_chip.chip_type == SLB9635) || (g_chip.chip_type == UNKNOWN)) {
401 /* slb9635 protocol should work in both cases */
402 for (count = 0; count < MAX_COUNT; count++) {
403 rc = dm_i2c_write(g_chip.dev, 0, (uchar *)&addrbuf, 1);
405 break; /* Success, break to skip sleep */
406 udelay(SLEEP_DURATION);
411 /* After the TPM has successfully received the register address
412 * it needs some time, thus we're sleeping here again, before
413 * retrieving the data
415 for (count = 0; count < MAX_COUNT; count++) {
416 udelay(SLEEP_DURATION);
417 rc = dm_i2c_read(g_chip.dev, 0, buffer, len);
419 break; /* success, break to skip sleep */
423 * Use a combined read for newer chips.
424 * Unfortunately the smbus functions are not suitable due to
425 * the 32 byte limit of the smbus.
426 * Retries should usually not be needed, but are kept just to
427 * be safe on the safe side.
429 for (count = 0; count < MAX_COUNT; count++) {
430 rc = dm_i2c_read(g_chip.dev, addr, buffer, len);
432 break; /* break here to skip sleep */
433 udelay(SLEEP_DURATION);
437 /* Take care of 'guard time' */
438 udelay(SLEEP_DURATION);
445 static int iic_tpm_write_generic(u8 addr, u8 *buffer, size_t len,
446 unsigned int sleep_time, u8 max_count)
451 for (count = 0; count < max_count; count++) {
452 rc = dm_i2c_write(g_chip.dev, addr, buffer, len);
454 break; /* Success, break to skip sleep */
458 /* take care of 'guard time' */
467 * iic_tpm_write() - write to TPM register
468 * @addr: register address to write to
469 * @buffer: containing data to be written
470 * @len: number of bytes to write
472 * Write len bytes from provided buffer to TPM register (little
473 * endian format, i.e. buffer[0] is written as first byte).
475 * NOTE: TPM is big-endian for multi-byte values. Multi-byte
476 * values have to be swapped.
478 * NOTE: use this function instead of the iic_tpm_write_generic function.
480 * Return -EIO on error, 0 on success
482 static int iic_tpm_write(u8 addr, u8 *buffer, size_t len)
484 return iic_tpm_write_generic(addr, buffer, len, SLEEP_DURATION,
489 * This function is needed especially for the cleanup situation after
492 static int iic_tpm_write_long(u8 addr, u8 *buffer, size_t len)
494 return iic_tpm_write_generic(addr, buffer, len, SLEEP_DURATION_LONG,
498 static int check_locality(struct tpm_chip *chip, int loc)
500 const u8 mask = TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID;
504 rc = iic_tpm_read(TPM_ACCESS(loc), &buf, 1);
508 if ((buf & mask) == mask) {
509 chip->locality = loc;
516 static void release_locality(struct tpm_chip *chip, int loc, int force)
518 const u8 mask = TPM_ACCESS_REQUEST_PENDING | TPM_ACCESS_VALID;
521 if (iic_tpm_read(TPM_ACCESS(loc), &buf, 1) < 0)
524 if (force || (buf & mask) == mask) {
525 buf = TPM_ACCESS_ACTIVE_LOCALITY;
526 iic_tpm_write(TPM_ACCESS(loc), &buf, 1);
530 static int request_locality(struct tpm_chip *chip, int loc)
532 unsigned long start, stop;
533 u8 buf = TPM_ACCESS_REQUEST_USE;
536 if (check_locality(chip, loc) >= 0)
537 return loc; /* We already have the locality */
539 rc = iic_tpm_write(TPM_ACCESS(loc), &buf, 1);
543 /* Wait for burstcount */
544 start = get_timer(0);
545 stop = chip->timeout_a;
547 if (check_locality(chip, loc) >= 0)
549 udelay(TPM_TIMEOUT * 1000);
550 } while (get_timer(start) < stop);
555 static u8 tpm_tis_i2c_status(struct tpm_chip *chip)
557 /* NOTE: Since i2c read may fail, return 0 in this case --> time-out */
560 if (iic_tpm_read(TPM_STS(chip->locality), &buf, 1) < 0)
566 static void tpm_tis_i2c_ready(struct tpm_chip *chip)
570 /* This causes the current command to be aborted */
571 u8 buf = TPM_STS_COMMAND_READY;
573 debug("%s\n", __func__);
574 rc = iic_tpm_write_long(TPM_STS(chip->locality), &buf, 1);
576 debug("%s: rc=%d\n", __func__, rc);
579 static ssize_t get_burstcount(struct tpm_chip *chip)
581 unsigned long start, stop;
585 /* Wait for burstcount */
586 /* XXX: Which timeout value? Spec has 2 answers (c & d) */
587 start = get_timer(0);
588 stop = chip->timeout_d;
590 /* Note: STS is little endian */
591 addr = TPM_STS(chip->locality) + 1;
592 if (iic_tpm_read(addr, buf, 3) < 0)
595 burstcnt = (buf[2] << 16) + (buf[1] << 8) + buf[0];
599 udelay(TPM_TIMEOUT * 1000);
600 } while (get_timer(start) < stop);
605 static int wait_for_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout,
608 unsigned long start, stop;
610 /* Check current status */
611 *status = tpm_tis_i2c_status(chip);
612 if ((*status & mask) == mask)
615 start = get_timer(0);
618 udelay(TPM_TIMEOUT * 1000);
619 *status = tpm_tis_i2c_status(chip);
620 if ((*status & mask) == mask)
622 } while (get_timer(start) < stop);
627 static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count)
633 while (size < count) {
634 burstcnt = get_burstcount(chip);
636 /* burstcount < 0 -> tpm is busy */
640 /* Limit received data to max left */
641 if (burstcnt > (count - size))
642 burstcnt = count - size;
644 rc = iic_tpm_read(TPM_DATA_FIFO(chip->locality),
645 &(buf[size]), burstcnt);
653 static int tpm_tis_i2c_recv(struct tpm_chip *chip, u8 *buf, size_t count)
656 int expected, status;
658 if (count < TPM_HEADER_SIZE) {
663 /* Read first 10 bytes, including tag, paramsize, and result */
664 size = recv_data(chip, buf, TPM_HEADER_SIZE);
665 if (size < TPM_HEADER_SIZE) {
666 error("Unable to read header\n");
670 expected = get_unaligned_be32(buf + TPM_RSP_SIZE_BYTE);
671 if ((size_t)expected > count) {
672 error("Error size=%x, expected=%x, count=%x\n", size, expected,
678 size += recv_data(chip, &buf[TPM_HEADER_SIZE],
679 expected - TPM_HEADER_SIZE);
680 if (size < expected) {
681 error("Unable to read remainder of result\n");
686 wait_for_stat(chip, TPM_STS_VALID, chip->timeout_c, &status);
687 if (status & TPM_STS_DATA_AVAIL) { /* Retry? */
688 error("Error left over data\n");
694 tpm_tis_i2c_ready(chip);
696 * The TPM needs some time to clean up here,
697 * so we sleep rather than keeping the bus busy
700 release_locality(chip, chip->locality, 0);
705 static int tpm_tis_i2c_send(struct tpm_chip *chip, u8 *buf, size_t len)
713 debug("%s: len=%d\n", __func__, len);
714 if (len > TPM_DEV_BUFSIZE)
715 return -E2BIG; /* Command is too long for our tpm, sorry */
717 if (request_locality(chip, 0) < 0)
720 status = tpm_tis_i2c_status(chip);
721 if ((status & TPM_STS_COMMAND_READY) == 0) {
722 tpm_tis_i2c_ready(chip);
723 if (wait_for_stat(chip, TPM_STS_COMMAND_READY,
724 chip->timeout_b, &status) < 0) {
730 burstcnt = get_burstcount(chip);
732 /* burstcount < 0 -> tpm is busy */
736 while (count < len) {
738 if (burstcnt > len - count)
739 burstcnt = len - count;
741 #ifdef CONFIG_TPM_TIS_I2C_BURST_LIMITATION
742 if (retry && burstcnt > CONFIG_TPM_TIS_I2C_BURST_LIMITATION)
743 burstcnt = CONFIG_TPM_TIS_I2C_BURST_LIMITATION;
744 #endif /* CONFIG_TPM_TIS_I2C_BURST_LIMITATION */
746 rc = iic_tpm_write(TPM_DATA_FIFO(chip->locality),
747 &(buf[count]), burstcnt);
751 debug("%s: error\n", __func__);
756 rc = wait_for_stat(chip, TPM_STS_VALID,
757 chip->timeout_c, &status);
761 if ((status & TPM_STS_DATA_EXPECT) == 0) {
769 iic_tpm_write(TPM_STS(chip->locality), &sts, 1);
775 debug("%s: out_err\n", __func__);
776 tpm_tis_i2c_ready(chip);
778 * The TPM needs some time to clean up here,
779 * so we sleep rather than keeping the bus busy
782 release_locality(chip, chip->locality, 0);
787 static enum i2c_chip_type tpm_tis_i2c_chip_type(void)
789 #if CONFIG_IS_ENABLED(OF_CONTROL)
790 const void *blob = gd->fdt_blob;
792 if (fdtdec_next_compatible(blob, 0, COMPAT_INFINEON_SLB9645_TPM) >= 0)
795 if (fdtdec_next_compatible(blob, 0, COMPAT_INFINEON_SLB9635_TPM) >= 0)
801 static int tpm_tis_i2c_init(struct udevice *dev)
803 struct tpm_chip *chip = &g_chip;
805 u32 expected_did_vid;
808 g_chip.chip_type = tpm_tis_i2c_chip_type();
811 /* Disable interrupts (not supported) */
814 /* Default timeouts */
815 chip->timeout_a = TIS_SHORT_TIMEOUT;
816 chip->timeout_b = TIS_LONG_TIMEOUT;
817 chip->timeout_c = TIS_SHORT_TIMEOUT;
818 chip->timeout_d = TIS_SHORT_TIMEOUT;
819 chip->req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID;
820 chip->req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID;
821 chip->req_canceled = TPM_STS_COMMAND_READY;
823 if (request_locality(chip, 0) < 0)
826 /* Read four bytes from DID_VID register */
827 if (iic_tpm_read(TPM_DID_VID(0), (uchar *)&vendor, 4) < 0) {
828 release_locality(chip, 0, 1);
832 if (g_chip.chip_type == SLB9635) {
833 vendor = be32_to_cpu(vendor);
834 expected_did_vid = TPM_TIS_I2C_DID_VID_9635;
836 /* device id and byte order has changed for newer i2c tpms */
837 expected_did_vid = TPM_TIS_I2C_DID_VID_9645;
840 if (g_chip.chip_type != UNKNOWN && vendor != expected_did_vid) {
841 error("Vendor id did not match! ID was %08x\n", vendor);
845 debug("1.2 TPM (chip type %s device-id 0x%X)\n",
846 chip_name[g_chip.chip_type], vendor >> 16);
849 * A timeout query to TPM can be placed here.
850 * Standard timeout values are used so far
856 /* Returns max number of milliseconds to wait */
857 static unsigned long tpm_calc_ordinal_duration(struct tpm_chip *chip,
860 int duration_idx = TPM_UNDEFINED;
863 if (ordinal < TPM_MAX_ORDINAL) {
864 duration_idx = tpm_ordinal_duration[ordinal];
865 } else if ((ordinal & TPM_PROTECTED_ORDINAL_MASK) <
866 TPM_MAX_PROTECTED_ORDINAL) {
867 duration_idx = tpm_protected_ordinal_duration[
868 ordinal & TPM_PROTECTED_ORDINAL_MASK];
871 if (duration_idx != TPM_UNDEFINED)
872 duration = chip->duration[duration_idx];
875 return 2 * 60 * HZ; /* Two minutes timeout */
880 static ssize_t tpm_transmit(const unsigned char *buf, size_t bufsiz)
884 unsigned long start, stop;
886 struct tpm_chip *chip = &g_chip;
888 /* switch endianess: big->little */
889 count = get_unaligned_be32(buf + TPM_CMD_COUNT_BYTE);
890 ordinal = get_unaligned_be32(buf + TPM_CMD_ORDINAL_BYTE);
896 if (count > bufsiz) {
897 error("invalid count value %x %zx\n", count, bufsiz);
901 debug("Calling send\n");
902 rc = tpm_tis_i2c_send(chip, (u8 *)buf, count);
903 debug(" ... done calling send\n");
905 error("tpm_transmit: tpm_send: error %d\n", rc);
912 start = get_timer(0);
913 stop = tpm_calc_ordinal_duration(chip, ordinal);
915 debug("waiting for status... %ld %ld\n", start, stop);
916 u8 status = tpm_tis_i2c_status(chip);
917 if ((status & chip->req_complete_mask) ==
918 chip->req_complete_val) {
919 debug("...got it;\n");
923 if (status == chip->req_canceled) {
924 error("Operation Canceled\n");
928 udelay(TPM_TIMEOUT * 1000);
929 } while (get_timer(start) < stop);
931 tpm_tis_i2c_ready(chip);
932 error("Operation Timed out\n");
937 debug("out_recv: reading response...\n");
938 rc = tpm_tis_i2c_recv(chip, (u8 *)buf, TPM_BUFSIZE);
940 error("tpm_transmit: tpm_recv: error %d\n", rc);
946 static int tpm_open_dev(struct udevice *dev)
950 debug("%s: start\n", __func__);
953 rc = tpm_tis_i2c_init(dev);
959 static void tpm_close(void)
961 if (g_chip.is_open) {
962 release_locality(&g_chip, g_chip.locality, 1);
968 * Decode TPM configuration.
970 * @param dev Returns a configuration of TPM device
971 * @return 0 if ok, -1 on error
973 static int tpm_decode_config(struct tpm_chip *chip)
975 const void *blob = gd->fdt_blob;
982 node = fdtdec_next_compatible(blob, 0, COMPAT_INFINEON_SLB9635_TPM);
984 node = fdtdec_next_compatible(blob, 0,
985 COMPAT_INFINEON_SLB9645_TPM);
988 debug("%s: Node not found\n", __func__);
991 parent = fdt_parent_offset(blob, node);
993 debug("%s: Cannot find node parent\n", __func__);
998 * TODO(sjg@chromium.org): Remove this when driver model supports
1001 ret = uclass_get_device_by_of_offset(UCLASS_I2C, parent, &bus);
1003 debug("Cannot find bus for node '%s: ret=%d'\n",
1004 fdt_get_name(blob, parent, NULL), ret);
1008 chip_addr = fdtdec_get_int(blob, node, "reg", -1);
1009 if (chip_addr == -1) {
1010 debug("Cannot find reg property for node '%s: ret=%d'\n",
1011 fdt_get_name(blob, node, NULL), ret);
1015 * TODO(sjg@chromium.org): Older TPMs will need to use the older method
1016 * in iic_tpm_read() so the offset length needs to be 0 here.
1018 ret = i2c_get_chip(bus, chip_addr, 1, &chip->dev);
1020 debug("Cannot find device for node '%s: ret=%d'\n",
1021 fdt_get_name(blob, node, NULL), ret);
1033 if (tpm_decode_config(&g_chip))
1036 debug("%s: done\n", __func__);
1050 rc = tpm_open_dev(g_chip.dev);
1065 int tis_sendrecv(const uint8_t *sendbuf, size_t sbuf_size,
1066 uint8_t *recvbuf, size_t *rbuf_len)
1074 if (sizeof(buf) < sbuf_size)
1077 memcpy(buf, sendbuf, sbuf_size);
1079 len = tpm_transmit(buf, sbuf_size);
1086 memcpy(recvbuf, buf, len);