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 /* TPM configuration */
384 static struct tpm_chip g_chip;
387 * iic_tpm_read() - read from TPM register
388 * @addr: register address to read from
389 * @buffer: provided by caller
390 * @len: number of bytes to read
392 * Read len bytes from TPM register and put them into
393 * buffer (little-endian format, i.e. first byte is put into buffer[0]).
395 * NOTE: TPM is big-endian for multi-byte values. Multi-byte
396 * values have to be swapped.
398 * Return -EIO on error, 0 on success.
400 static int iic_tpm_read(u8 addr, u8 *buffer, size_t len)
404 uint32_t addrbuf = addr;
406 if ((g_chip.chip_type == SLB9635) || (g_chip.chip_type == UNKNOWN)) {
407 /* slb9635 protocol should work in both cases */
408 for (count = 0; count < MAX_COUNT; count++) {
409 rc = dm_i2c_write(g_chip.dev, 0, (uchar *)&addrbuf, 1);
411 break; /* Success, break to skip sleep */
412 udelay(SLEEP_DURATION);
417 /* After the TPM has successfully received the register address
418 * it needs some time, thus we're sleeping here again, before
419 * retrieving the data
421 for (count = 0; count < MAX_COUNT; count++) {
422 udelay(SLEEP_DURATION);
423 rc = dm_i2c_read(g_chip.dev, 0, buffer, len);
425 break; /* success, break to skip sleep */
429 * Use a combined read for newer chips.
430 * Unfortunately the smbus functions are not suitable due to
431 * the 32 byte limit of the smbus.
432 * Retries should usually not be needed, but are kept just to
433 * be safe on the safe side.
435 for (count = 0; count < MAX_COUNT; count++) {
436 rc = dm_i2c_read(g_chip.dev, addr, buffer, len);
438 break; /* break here to skip sleep */
439 udelay(SLEEP_DURATION);
443 /* Take care of 'guard time' */
444 udelay(SLEEP_DURATION);
451 static int iic_tpm_write_generic(u8 addr, u8 *buffer, size_t len,
452 unsigned int sleep_time, u8 max_count)
457 for (count = 0; count < max_count; count++) {
458 rc = dm_i2c_write(g_chip.dev, addr, buffer, len);
460 break; /* Success, break to skip sleep */
464 /* take care of 'guard time' */
473 * iic_tpm_write() - write to TPM register
474 * @addr: register address to write to
475 * @buffer: containing data to be written
476 * @len: number of bytes to write
478 * Write len bytes from provided buffer to TPM register (little
479 * endian format, i.e. buffer[0] is written as first byte).
481 * NOTE: TPM is big-endian for multi-byte values. Multi-byte
482 * values have to be swapped.
484 * NOTE: use this function instead of the iic_tpm_write_generic function.
486 * Return -EIO on error, 0 on success
488 static int iic_tpm_write(u8 addr, u8 *buffer, size_t len)
490 return iic_tpm_write_generic(addr, buffer, len, SLEEP_DURATION,
495 * This function is needed especially for the cleanup situation after
498 static int iic_tpm_write_long(u8 addr, u8 *buffer, size_t len)
500 return iic_tpm_write_generic(addr, buffer, len, SLEEP_DURATION_LONG,
504 static int check_locality(struct tpm_chip *chip, int loc)
506 const u8 mask = TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID;
510 rc = iic_tpm_read(TPM_ACCESS(loc), &buf, 1);
514 if ((buf & mask) == mask) {
515 chip->locality = loc;
522 static void release_locality(struct tpm_chip *chip, int loc, int force)
524 const u8 mask = TPM_ACCESS_REQUEST_PENDING | TPM_ACCESS_VALID;
527 if (iic_tpm_read(TPM_ACCESS(loc), &buf, 1) < 0)
530 if (force || (buf & mask) == mask) {
531 buf = TPM_ACCESS_ACTIVE_LOCALITY;
532 iic_tpm_write(TPM_ACCESS(loc), &buf, 1);
536 static int request_locality(struct tpm_chip *chip, int loc)
538 unsigned long start, stop;
539 u8 buf = TPM_ACCESS_REQUEST_USE;
542 if (check_locality(chip, loc) >= 0)
543 return loc; /* We already have the locality */
545 rc = iic_tpm_write(TPM_ACCESS(loc), &buf, 1);
549 /* Wait for burstcount */
550 start = get_timer(0);
551 stop = chip->timeout_a;
553 if (check_locality(chip, loc) >= 0)
555 udelay(TPM_TIMEOUT * 1000);
556 } while (get_timer(start) < stop);
561 static u8 tpm_tis_i2c_status(struct tpm_chip *chip)
563 /* NOTE: Since i2c read may fail, return 0 in this case --> time-out */
566 if (iic_tpm_read(TPM_STS(chip->locality), &buf, 1) < 0)
572 static void tpm_tis_i2c_ready(struct tpm_chip *chip)
576 /* This causes the current command to be aborted */
577 u8 buf = TPM_STS_COMMAND_READY;
579 debug("%s\n", __func__);
580 rc = iic_tpm_write_long(TPM_STS(chip->locality), &buf, 1);
582 debug("%s: rc=%d\n", __func__, rc);
585 static ssize_t get_burstcount(struct tpm_chip *chip)
587 unsigned long start, stop;
591 /* Wait for burstcount */
592 /* XXX: Which timeout value? Spec has 2 answers (c & d) */
593 start = get_timer(0);
594 stop = chip->timeout_d;
596 /* Note: STS is little endian */
597 addr = TPM_STS(chip->locality) + 1;
598 if (iic_tpm_read(addr, buf, 3) < 0)
601 burstcnt = (buf[2] << 16) + (buf[1] << 8) + buf[0];
605 udelay(TPM_TIMEOUT * 1000);
606 } while (get_timer(start) < stop);
611 static int wait_for_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout,
614 unsigned long start, stop;
616 /* Check current status */
617 *status = tpm_tis_i2c_status(chip);
618 if ((*status & mask) == mask)
621 start = get_timer(0);
624 udelay(TPM_TIMEOUT * 1000);
625 *status = tpm_tis_i2c_status(chip);
626 if ((*status & mask) == mask)
628 } while (get_timer(start) < stop);
633 static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count)
639 while (size < count) {
640 burstcnt = get_burstcount(chip);
642 /* burstcount < 0 -> tpm is busy */
646 /* Limit received data to max left */
647 if (burstcnt > (count - size))
648 burstcnt = count - size;
650 rc = iic_tpm_read(TPM_DATA_FIFO(chip->locality),
651 &(buf[size]), burstcnt);
659 static int tpm_tis_i2c_recv(struct tpm_chip *chip, u8 *buf, size_t count)
662 int expected, status;
664 if (count < TPM_HEADER_SIZE) {
669 /* Read first 10 bytes, including tag, paramsize, and result */
670 size = recv_data(chip, buf, TPM_HEADER_SIZE);
671 if (size < TPM_HEADER_SIZE) {
672 error("Unable to read header\n");
676 expected = get_unaligned_be32(buf + TPM_RSP_SIZE_BYTE);
677 if ((size_t)expected > count) {
678 error("Error size=%x, expected=%x, count=%x\n", size, expected,
684 size += recv_data(chip, &buf[TPM_HEADER_SIZE],
685 expected - TPM_HEADER_SIZE);
686 if (size < expected) {
687 error("Unable to read remainder of result\n");
692 wait_for_stat(chip, TPM_STS_VALID, chip->timeout_c, &status);
693 if (status & TPM_STS_DATA_AVAIL) { /* Retry? */
694 error("Error left over data\n");
700 tpm_tis_i2c_ready(chip);
702 * The TPM needs some time to clean up here,
703 * so we sleep rather than keeping the bus busy
706 release_locality(chip, chip->locality, 0);
711 static int tpm_tis_i2c_send(struct tpm_chip *chip, u8 *buf, size_t len)
719 debug("%s: len=%d\n", __func__, len);
720 if (len > TPM_DEV_BUFSIZE)
721 return -E2BIG; /* Command is too long for our tpm, sorry */
723 if (request_locality(chip, 0) < 0)
726 status = tpm_tis_i2c_status(chip);
727 if ((status & TPM_STS_COMMAND_READY) == 0) {
728 tpm_tis_i2c_ready(chip);
729 if (wait_for_stat(chip, TPM_STS_COMMAND_READY,
730 chip->timeout_b, &status) < 0) {
736 burstcnt = get_burstcount(chip);
738 /* burstcount < 0 -> tpm is busy */
742 while (count < len) {
744 if (burstcnt > len - count)
745 burstcnt = len - count;
747 #ifdef CONFIG_TPM_TIS_I2C_BURST_LIMITATION
748 if (retry && burstcnt > CONFIG_TPM_TIS_I2C_BURST_LIMITATION)
749 burstcnt = CONFIG_TPM_TIS_I2C_BURST_LIMITATION;
750 #endif /* CONFIG_TPM_TIS_I2C_BURST_LIMITATION */
752 rc = iic_tpm_write(TPM_DATA_FIFO(chip->locality),
753 &(buf[count]), burstcnt);
757 debug("%s: error\n", __func__);
762 rc = wait_for_stat(chip, TPM_STS_VALID,
763 chip->timeout_c, &status);
767 if ((status & TPM_STS_DATA_EXPECT) == 0) {
775 iic_tpm_write(TPM_STS(chip->locality), &sts, 1);
781 debug("%s: out_err\n", __func__);
782 tpm_tis_i2c_ready(chip);
784 * The TPM needs some time to clean up here,
785 * so we sleep rather than keeping the bus busy
788 release_locality(chip, chip->locality, 0);
793 static enum i2c_chip_type tpm_tis_i2c_chip_type(void)
795 #if CONFIG_IS_ENABLED(OF_CONTROL)
796 const void *blob = gd->fdt_blob;
798 if (fdtdec_next_compatible(blob, 0, COMPAT_INFINEON_SLB9645_TPM) >= 0)
801 if (fdtdec_next_compatible(blob, 0, COMPAT_INFINEON_SLB9635_TPM) >= 0)
807 static int tpm_tis_i2c_init(struct udevice *dev)
809 struct tpm_chip *chip = &g_chip;
811 u32 expected_did_vid;
814 g_chip.chip_type = tpm_tis_i2c_chip_type();
817 /* Disable interrupts (not supported) */
820 /* Default timeouts */
821 chip->timeout_a = TIS_SHORT_TIMEOUT;
822 chip->timeout_b = TIS_LONG_TIMEOUT;
823 chip->timeout_c = TIS_SHORT_TIMEOUT;
824 chip->timeout_d = TIS_SHORT_TIMEOUT;
825 chip->req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID;
826 chip->req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID;
827 chip->req_canceled = TPM_STS_COMMAND_READY;
829 if (request_locality(chip, 0) < 0)
832 /* Read four bytes from DID_VID register */
833 if (iic_tpm_read(TPM_DID_VID(0), (uchar *)&vendor, 4) < 0) {
834 release_locality(chip, 0, 1);
838 if (g_chip.chip_type == SLB9635) {
839 vendor = be32_to_cpu(vendor);
840 expected_did_vid = TPM_TIS_I2C_DID_VID_9635;
842 /* device id and byte order has changed for newer i2c tpms */
843 expected_did_vid = TPM_TIS_I2C_DID_VID_9645;
846 if (g_chip.chip_type != UNKNOWN && vendor != expected_did_vid) {
847 error("Vendor id did not match! ID was %08x\n", vendor);
851 debug("1.2 TPM (chip type %s device-id 0x%X)\n",
852 chip_name[g_chip.chip_type], vendor >> 16);
855 * A timeout query to TPM can be placed here.
856 * Standard timeout values are used so far
862 /* Returns max number of milliseconds to wait */
863 static unsigned long tpm_calc_ordinal_duration(struct tpm_chip *chip,
866 int duration_idx = TPM_UNDEFINED;
869 if (ordinal < TPM_MAX_ORDINAL) {
870 duration_idx = tpm_ordinal_duration[ordinal];
871 } else if ((ordinal & TPM_PROTECTED_ORDINAL_MASK) <
872 TPM_MAX_PROTECTED_ORDINAL) {
873 duration_idx = tpm_protected_ordinal_duration[
874 ordinal & TPM_PROTECTED_ORDINAL_MASK];
877 if (duration_idx != TPM_UNDEFINED)
878 duration = chip->duration[duration_idx];
881 return 2 * 60 * HZ; /* Two minutes timeout */
886 static ssize_t tpm_transmit(const unsigned char *buf, size_t bufsiz)
890 unsigned long start, stop;
892 struct tpm_chip *chip = &g_chip;
894 /* switch endianess: big->little */
895 count = get_unaligned_be32(buf + TPM_CMD_COUNT_BYTE);
896 ordinal = get_unaligned_be32(buf + TPM_CMD_ORDINAL_BYTE);
902 if (count > bufsiz) {
903 error("invalid count value %x %zx\n", count, bufsiz);
907 debug("Calling send\n");
908 rc = tpm_tis_i2c_send(chip, (u8 *)buf, count);
909 debug(" ... done calling send\n");
911 error("tpm_transmit: tpm_send: error %d\n", rc);
918 start = get_timer(0);
919 stop = tpm_calc_ordinal_duration(chip, ordinal);
921 debug("waiting for status... %ld %ld\n", start, stop);
922 u8 status = tpm_tis_i2c_status(chip);
923 if ((status & chip->req_complete_mask) ==
924 chip->req_complete_val) {
925 debug("...got it;\n");
929 if (status == chip->req_canceled) {
930 error("Operation Canceled\n");
934 udelay(TPM_TIMEOUT * 1000);
935 } while (get_timer(start) < stop);
937 tpm_tis_i2c_ready(chip);
938 error("Operation Timed out\n");
943 debug("out_recv: reading response...\n");
944 rc = tpm_tis_i2c_recv(chip, (u8 *)buf, TPM_BUFSIZE);
946 error("tpm_transmit: tpm_recv: error %d\n", rc);
952 static int tpm_open_dev(struct udevice *dev)
956 debug("%s: start\n", __func__);
959 rc = tpm_tis_i2c_init(dev);
965 static void tpm_close(void)
967 if (g_chip.is_open) {
968 release_locality(&g_chip, g_chip.locality, 1);
974 * Decode TPM configuration.
976 * @param dev Returns a configuration of TPM device
977 * @return 0 if ok, -1 on error
979 static int tpm_decode_config(struct tpm *dev)
981 const void *blob = gd->fdt_blob;
988 node = fdtdec_next_compatible(blob, 0, COMPAT_INFINEON_SLB9635_TPM);
990 node = fdtdec_next_compatible(blob, 0,
991 COMPAT_INFINEON_SLB9645_TPM);
994 debug("%s: Node not found\n", __func__);
997 parent = fdt_parent_offset(blob, node);
999 debug("%s: Cannot find node parent\n", __func__);
1004 * TODO(sjg@chromium.org): Remove this when driver model supports
1007 ret = uclass_get_device_by_of_offset(UCLASS_I2C, parent, &bus);
1009 debug("Cannot find bus for node '%s: ret=%d'\n",
1010 fdt_get_name(blob, parent, NULL), ret);
1014 chip_addr = fdtdec_get_int(blob, node, "reg", -1);
1015 if (chip_addr == -1) {
1016 debug("Cannot find reg property for node '%s: ret=%d'\n",
1017 fdt_get_name(blob, node, NULL), ret);
1021 * TODO(sjg@chromium.org): Older TPMs will need to use the older method
1022 * in iic_tpm_read() so the offset length needs to be 0 here.
1024 ret = i2c_get_chip(bus, chip_addr, 1, &dev->dev);
1026 debug("Cannot find device for node '%s: ret=%d'\n",
1027 fdt_get_name(blob, node, NULL), ret);
1039 if (tpm_decode_config(&tpm))
1042 debug("%s: done\n", __func__);
1056 rc = tpm_open_dev(tpm.dev);
1071 int tis_sendrecv(const uint8_t *sendbuf, size_t sbuf_size,
1072 uint8_t *recvbuf, size_t *rbuf_len)
1080 if (sizeof(buf) < sbuf_size)
1083 memcpy(buf, sendbuf, sbuf_size);
1085 len = tpm_transmit(buf, sbuf_size);
1092 memcpy(recvbuf, buf, len);