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 buffer size supported by our tpm */
38 #define TPM_DEV_BUFSIZE 1260
40 /* Max number of iterations after i2c NAK */
44 * Max number of iterations after i2c NAK for 'long' commands
46 * We need this especially for sending TPM_READY, since the cleanup after the
47 * transtion to the ready state may take some time, but it is unpredictable
48 * how long it will take.
50 #define MAX_COUNT_LONG 50
52 #define SLEEP_DURATION 60 /* in usec */
53 #define SLEEP_DURATION_LONG 210 /* in usec */
55 #define TPM_HEADER_SIZE 10
58 TPM_ACCESS_VALID = 0x80,
59 TPM_ACCESS_ACTIVE_LOCALITY = 0x20,
60 TPM_ACCESS_REQUEST_PENDING = 0x04,
61 TPM_ACCESS_REQUEST_USE = 0x02,
66 TPM_STS_COMMAND_READY = 0x40,
68 TPM_STS_DATA_AVAIL = 0x10,
69 TPM_STS_DATA_EXPECT = 0x08,
73 TIS_SHORT_TIMEOUT = 750, /* ms */
74 TIS_LONG_TIMEOUT = 2000, /* ms */
77 /* expected value for DIDVID register */
78 #define TPM_TIS_I2C_DID_VID_9635 0x000b15d1L
79 #define TPM_TIS_I2C_DID_VID_9645 0x001a15d1L
87 static const char * const chip_name[] = {
88 [SLB9635] = "slb9635tt",
89 [SLB9645] = "slb9645tt",
90 [UNKNOWN] = "unknown/fallback to slb9635",
93 #define TPM_ACCESS(l) (0x0000 | ((l) << 4))
94 #define TPM_STS(l) (0x0001 | ((l) << 4))
95 #define TPM_DATA_FIFO(l) (0x0005 | ((l) << 4))
96 #define TPM_DID_VID(l) (0x0006 | ((l) << 4))
105 /* Extended error numbers from linux (see errno.h) */
106 #define ECANCELED 125 /* Operation Canceled */
108 /* Timer frequency. Corresponds to msec timer resolution*/
111 #define TPM_MAX_ORDINAL 243
112 #define TPM_MAX_PROTECTED_ORDINAL 12
113 #define TPM_PROTECTED_ORDINAL_MASK 0xFF
115 #define TPM_CMD_COUNT_BYTE 2
116 #define TPM_CMD_ORDINAL_BYTE 6
119 * Array with one entry per ordinal defining the maximum amount
120 * of time the chip could take to return the result. The ordinal
121 * designation of short, medium or long is defined in a table in
122 * TCG Specification TPM Main Part 2 TPM Structures Section 17. The
123 * values of the SHORT, MEDIUM, and LONG durations are retrieved
124 * from the chip during initialization with a call to tpm_get_timeouts.
126 static const u8 tpm_protected_ordinal_duration[TPM_MAX_PROTECTED_ORDINAL] = {
127 TPM_UNDEFINED, /* 0 */
132 TPM_UNDEFINED, /* 5 */
141 static const u8 tpm_ordinal_duration[TPM_MAX_ORDINAL] = {
142 TPM_UNDEFINED, /* 0 */
147 TPM_UNDEFINED, /* 5 */
197 TPM_UNDEFINED, /* 55 */
217 TPM_UNDEFINED, /* 75 */
227 TPM_UNDEFINED, /* 85 */
237 TPM_UNDEFINED, /* 95 */
242 TPM_MEDIUM, /* 100 */
247 TPM_UNDEFINED, /* 105 */
277 TPM_UNDEFINED, /* 135 */
287 TPM_UNDEFINED, /* 145 */
297 TPM_UNDEFINED, /* 155 */
307 TPM_UNDEFINED, /* 165 */
317 TPM_UNDEFINED, /* 175 */
322 TPM_MEDIUM, /* 180 */
327 TPM_MEDIUM, /* 185 */
332 TPM_UNDEFINED, /* 190 */
337 TPM_UNDEFINED, /* 195 */
352 TPM_MEDIUM, /* 210 */
357 TPM_UNDEFINED, /* 215 */
367 TPM_UNDEFINED, /* 225 */
377 TPM_UNDEFINED, /* 235 */
387 /* TPM configuration */
393 /* Global structure for tpm chip data */
394 static struct tpm_chip g_chip;
396 /* Structure to store I2C TPM specific stuff */
399 u8 buf[TPM_DEV_BUFSIZE + sizeof(u8)]; /* Max buffer size + addr */
400 enum i2c_chip_type chip_type;
403 static struct tpm_dev tpm_dev;
406 * iic_tpm_read() - read from TPM register
407 * @addr: register address to read from
408 * @buffer: provided by caller
409 * @len: number of bytes to read
411 * Read len bytes from TPM register and put them into
412 * buffer (little-endian format, i.e. first byte is put into buffer[0]).
414 * NOTE: TPM is big-endian for multi-byte values. Multi-byte
415 * values have to be swapped.
417 * Return -EIO on error, 0 on success.
419 static int iic_tpm_read(u8 addr, u8 *buffer, size_t len)
423 uint32_t addrbuf = addr;
425 if ((tpm_dev.chip_type == SLB9635) || (tpm_dev.chip_type == UNKNOWN)) {
426 /* slb9635 protocol should work in both cases */
427 for (count = 0; count < MAX_COUNT; count++) {
428 rc = dm_i2c_write(tpm_dev.dev, 0, (uchar *)&addrbuf, 1);
430 break; /* Success, break to skip sleep */
431 udelay(SLEEP_DURATION);
436 /* After the TPM has successfully received the register address
437 * it needs some time, thus we're sleeping here again, before
438 * retrieving the data
440 for (count = 0; count < MAX_COUNT; count++) {
441 udelay(SLEEP_DURATION);
442 rc = dm_i2c_read(tpm_dev.dev, 0, buffer, len);
444 break; /* success, break to skip sleep */
448 * Use a combined read for newer chips.
449 * Unfortunately the smbus functions are not suitable due to
450 * the 32 byte limit of the smbus.
451 * Retries should usually not be needed, but are kept just to
452 * be safe on the safe side.
454 for (count = 0; count < MAX_COUNT; count++) {
455 rc = dm_i2c_read(tpm_dev.dev, addr, buffer, len);
457 break; /* break here to skip sleep */
458 udelay(SLEEP_DURATION);
462 /* Take care of 'guard time' */
463 udelay(SLEEP_DURATION);
470 static int iic_tpm_write_generic(u8 addr, u8 *buffer, size_t len,
471 unsigned int sleep_time, u8 max_count)
476 for (count = 0; count < max_count; count++) {
477 rc = dm_i2c_write(tpm_dev.dev, addr, buffer, len);
479 break; /* Success, break to skip sleep */
483 /* take care of 'guard time' */
492 * iic_tpm_write() - write to TPM register
493 * @addr: register address to write to
494 * @buffer: containing data to be written
495 * @len: number of bytes to write
497 * Write len bytes from provided buffer to TPM register (little
498 * endian format, i.e. buffer[0] is written as first byte).
500 * NOTE: TPM is big-endian for multi-byte values. Multi-byte
501 * values have to be swapped.
503 * NOTE: use this function instead of the iic_tpm_write_generic function.
505 * Return -EIO on error, 0 on success
507 static int iic_tpm_write(u8 addr, u8 *buffer, size_t len)
509 return iic_tpm_write_generic(addr, buffer, len, SLEEP_DURATION,
514 * This function is needed especially for the cleanup situation after
517 static int iic_tpm_write_long(u8 addr, u8 *buffer, size_t len)
519 return iic_tpm_write_generic(addr, buffer, len, SLEEP_DURATION_LONG,
523 static int check_locality(struct tpm_chip *chip, int loc)
525 const u8 mask = TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID;
529 rc = iic_tpm_read(TPM_ACCESS(loc), &buf, 1);
533 if ((buf & mask) == mask) {
534 chip->vendor.locality = loc;
541 static void release_locality(struct tpm_chip *chip, int loc, int force)
543 const u8 mask = TPM_ACCESS_REQUEST_PENDING | TPM_ACCESS_VALID;
546 if (iic_tpm_read(TPM_ACCESS(loc), &buf, 1) < 0)
549 if (force || (buf & mask) == mask) {
550 buf = TPM_ACCESS_ACTIVE_LOCALITY;
551 iic_tpm_write(TPM_ACCESS(loc), &buf, 1);
555 static int request_locality(struct tpm_chip *chip, int loc)
557 unsigned long start, stop;
558 u8 buf = TPM_ACCESS_REQUEST_USE;
561 if (check_locality(chip, loc) >= 0)
562 return loc; /* We already have the locality */
564 rc = iic_tpm_write(TPM_ACCESS(loc), &buf, 1);
568 /* Wait for burstcount */
569 start = get_timer(0);
570 stop = chip->vendor.timeout_a;
572 if (check_locality(chip, loc) >= 0)
574 udelay(TPM_TIMEOUT * 1000);
575 } while (get_timer(start) < stop);
580 static u8 tpm_tis_i2c_status(struct tpm_chip *chip)
582 /* NOTE: Since i2c read may fail, return 0 in this case --> time-out */
585 if (iic_tpm_read(TPM_STS(chip->vendor.locality), &buf, 1) < 0)
591 static void tpm_tis_i2c_ready(struct tpm_chip *chip)
595 /* This causes the current command to be aborted */
596 u8 buf = TPM_STS_COMMAND_READY;
598 debug("%s\n", __func__);
599 rc = iic_tpm_write_long(TPM_STS(chip->vendor.locality), &buf, 1);
601 debug("%s: rc=%d\n", __func__, rc);
604 static ssize_t get_burstcount(struct tpm_chip *chip)
606 unsigned long start, stop;
610 /* Wait for burstcount */
611 /* XXX: Which timeout value? Spec has 2 answers (c & d) */
612 start = get_timer(0);
613 stop = chip->vendor.timeout_d;
615 /* Note: STS is little endian */
616 addr = TPM_STS(chip->vendor.locality) + 1;
617 if (iic_tpm_read(addr, buf, 3) < 0)
620 burstcnt = (buf[2] << 16) + (buf[1] << 8) + buf[0];
624 udelay(TPM_TIMEOUT * 1000);
625 } while (get_timer(start) < stop);
630 static int wait_for_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout,
633 unsigned long start, stop;
635 /* Check current status */
636 *status = tpm_tis_i2c_status(chip);
637 if ((*status & mask) == mask)
640 start = get_timer(0);
643 udelay(TPM_TIMEOUT * 1000);
644 *status = tpm_tis_i2c_status(chip);
645 if ((*status & mask) == mask)
647 } while (get_timer(start) < stop);
652 static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count)
658 while (size < count) {
659 burstcnt = get_burstcount(chip);
661 /* burstcount < 0 -> tpm is busy */
665 /* Limit received data to max left */
666 if (burstcnt > (count - size))
667 burstcnt = count - size;
669 rc = iic_tpm_read(TPM_DATA_FIFO(chip->vendor.locality),
670 &(buf[size]), burstcnt);
678 static int tpm_tis_i2c_recv(struct tpm_chip *chip, u8 *buf, size_t count)
681 int expected, status;
683 if (count < TPM_HEADER_SIZE) {
688 /* Read first 10 bytes, including tag, paramsize, and result */
689 size = recv_data(chip, buf, TPM_HEADER_SIZE);
690 if (size < TPM_HEADER_SIZE) {
691 error("Unable to read header\n");
695 expected = get_unaligned_be32(buf + TPM_RSP_SIZE_BYTE);
696 if ((size_t)expected > count) {
697 error("Error size=%x, expected=%x, count=%x\n", size, expected,
703 size += recv_data(chip, &buf[TPM_HEADER_SIZE],
704 expected - TPM_HEADER_SIZE);
705 if (size < expected) {
706 error("Unable to read remainder of result\n");
711 wait_for_stat(chip, TPM_STS_VALID, chip->vendor.timeout_c, &status);
712 if (status & TPM_STS_DATA_AVAIL) { /* Retry? */
713 error("Error left over data\n");
719 tpm_tis_i2c_ready(chip);
721 * The TPM needs some time to clean up here,
722 * so we sleep rather than keeping the bus busy
725 release_locality(chip, chip->vendor.locality, 0);
730 static int tpm_tis_i2c_send(struct tpm_chip *chip, u8 *buf, size_t len)
738 debug("%s: len=%d\n", __func__, len);
739 if (len > TPM_DEV_BUFSIZE)
740 return -E2BIG; /* Command is too long for our tpm, sorry */
742 if (request_locality(chip, 0) < 0)
745 status = tpm_tis_i2c_status(chip);
746 if ((status & TPM_STS_COMMAND_READY) == 0) {
747 tpm_tis_i2c_ready(chip);
748 if (wait_for_stat(chip, TPM_STS_COMMAND_READY,
749 chip->vendor.timeout_b, &status) < 0) {
755 burstcnt = get_burstcount(chip);
757 /* burstcount < 0 -> tpm is busy */
761 while (count < len) {
763 if (burstcnt > len - count)
764 burstcnt = len - count;
766 #ifdef CONFIG_TPM_TIS_I2C_BURST_LIMITATION
767 if (retry && burstcnt > CONFIG_TPM_TIS_I2C_BURST_LIMITATION)
768 burstcnt = CONFIG_TPM_TIS_I2C_BURST_LIMITATION;
769 #endif /* CONFIG_TPM_TIS_I2C_BURST_LIMITATION */
771 rc = iic_tpm_write(TPM_DATA_FIFO(chip->vendor.locality),
772 &(buf[count]), burstcnt);
776 debug("%s: error\n", __func__);
781 rc = wait_for_stat(chip, TPM_STS_VALID,
782 chip->vendor.timeout_c, &status);
786 if ((status & TPM_STS_DATA_EXPECT) == 0) {
794 iic_tpm_write(TPM_STS(chip->vendor.locality), &sts, 1);
800 debug("%s: out_err\n", __func__);
801 tpm_tis_i2c_ready(chip);
803 * The TPM needs some time to clean up here,
804 * so we sleep rather than keeping the bus busy
807 release_locality(chip, chip->vendor.locality, 0);
812 static struct tpm_vendor_specific tpm_tis_i2c = {
813 .req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
814 .req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
815 .req_canceled = TPM_STS_COMMAND_READY,
819 static enum i2c_chip_type tpm_vendor_chip_type(void)
821 #if CONFIG_IS_ENABLED(OF_CONTROL)
822 const void *blob = gd->fdt_blob;
824 if (fdtdec_next_compatible(blob, 0, COMPAT_INFINEON_SLB9645_TPM) >= 0)
827 if (fdtdec_next_compatible(blob, 0, COMPAT_INFINEON_SLB9635_TPM) >= 0)
833 int tpm_vendor_init(struct udevice *dev)
835 struct tpm_chip *chip;
837 u32 expected_did_vid;
840 tpm_dev.chip_type = tpm_vendor_chip_type();
842 chip = tpm_register_hardware(&tpm_tis_i2c);
846 /* Disable interrupts (not supported) */
847 chip->vendor.irq = 0;
849 /* Default timeouts */
850 chip->vendor.timeout_a = TIS_SHORT_TIMEOUT;
851 chip->vendor.timeout_b = TIS_LONG_TIMEOUT;
852 chip->vendor.timeout_c = TIS_SHORT_TIMEOUT;
853 chip->vendor.timeout_d = TIS_SHORT_TIMEOUT;
855 if (request_locality(chip, 0) < 0)
858 /* Read four bytes from DID_VID register */
859 if (iic_tpm_read(TPM_DID_VID(0), (uchar *)&vendor, 4) < 0) {
860 release_locality(chip, 0, 1);
864 if (tpm_dev.chip_type == SLB9635) {
865 vendor = be32_to_cpu(vendor);
866 expected_did_vid = TPM_TIS_I2C_DID_VID_9635;
868 /* device id and byte order has changed for newer i2c tpms */
869 expected_did_vid = TPM_TIS_I2C_DID_VID_9645;
872 if (tpm_dev.chip_type != UNKNOWN && vendor != expected_did_vid) {
873 error("Vendor id did not match! ID was %08x\n", vendor);
877 debug("1.2 TPM (chip type %s device-id 0x%X)\n",
878 chip_name[tpm_dev.chip_type], vendor >> 16);
881 * A timeout query to TPM can be placed here.
882 * Standard timeout values are used so far
888 void tpm_vendor_cleanup(struct tpm_chip *chip)
890 release_locality(chip, chip->vendor.locality, 1);
893 /* Returns max number of milliseconds to wait */
894 static unsigned long tpm_calc_ordinal_duration(struct tpm_chip *chip,
897 int duration_idx = TPM_UNDEFINED;
900 if (ordinal < TPM_MAX_ORDINAL) {
901 duration_idx = tpm_ordinal_duration[ordinal];
902 } else if ((ordinal & TPM_PROTECTED_ORDINAL_MASK) <
903 TPM_MAX_PROTECTED_ORDINAL) {
904 duration_idx = tpm_protected_ordinal_duration[
905 ordinal & TPM_PROTECTED_ORDINAL_MASK];
908 if (duration_idx != TPM_UNDEFINED)
909 duration = chip->vendor.duration[duration_idx];
912 return 2 * 60 * HZ; /* Two minutes timeout */
917 static ssize_t tpm_transmit(const unsigned char *buf, size_t bufsiz)
921 unsigned long start, stop;
923 struct tpm_chip *chip = &g_chip;
925 /* switch endianess: big->little */
926 count = get_unaligned_be32(buf + TPM_CMD_COUNT_BYTE);
927 ordinal = get_unaligned_be32(buf + TPM_CMD_ORDINAL_BYTE);
933 if (count > bufsiz) {
934 error("invalid count value %x %zx\n", count, bufsiz);
938 debug("Calling send\n");
939 rc = tpm_tis_i2c_send(chip, (u8 *)buf, count);
940 debug(" ... done calling send\n");
942 error("tpm_transmit: tpm_send: error %d\n", rc);
946 if (chip->vendor.irq)
949 start = get_timer(0);
950 stop = tpm_calc_ordinal_duration(chip, ordinal);
952 debug("waiting for status... %ld %ld\n", start, stop);
953 u8 status = tpm_tis_i2c_status(chip);
954 if ((status & chip->vendor.req_complete_mask) ==
955 chip->vendor.req_complete_val) {
956 debug("...got it;\n");
960 if (status == chip->vendor.req_canceled) {
961 error("Operation Canceled\n");
965 udelay(TPM_TIMEOUT * 1000);
966 } while (get_timer(start) < stop);
968 tpm_tis_i2c_ready(chip);
969 error("Operation Timed out\n");
974 debug("out_recv: reading response...\n");
975 rc = tpm_tis_i2c_recv(chip, (u8 *)buf, TPM_BUFSIZE);
977 error("tpm_transmit: tpm_recv: error %d\n", rc);
983 static int tpm_open_dev(struct udevice *dev)
987 debug("%s: start\n", __func__);
990 rc = tpm_vendor_init(dev);
996 static void tpm_close(void)
998 if (g_chip.is_open) {
999 tpm_vendor_cleanup(&g_chip);
1005 * Decode TPM configuration.
1007 * @param dev Returns a configuration of TPM device
1008 * @return 0 if ok, -1 on error
1010 static int tpm_decode_config(struct tpm *dev)
1012 const void *blob = gd->fdt_blob;
1013 struct udevice *bus;
1019 node = fdtdec_next_compatible(blob, 0, COMPAT_INFINEON_SLB9635_TPM);
1021 node = fdtdec_next_compatible(blob, 0,
1022 COMPAT_INFINEON_SLB9645_TPM);
1025 debug("%s: Node not found\n", __func__);
1028 parent = fdt_parent_offset(blob, node);
1030 debug("%s: Cannot find node parent\n", __func__);
1035 * TODO(sjg@chromium.org): Remove this when driver model supports
1038 ret = uclass_get_device_by_of_offset(UCLASS_I2C, parent, &bus);
1040 debug("Cannot find bus for node '%s: ret=%d'\n",
1041 fdt_get_name(blob, parent, NULL), ret);
1045 chip_addr = fdtdec_get_int(blob, node, "reg", -1);
1046 if (chip_addr == -1) {
1047 debug("Cannot find reg property for node '%s: ret=%d'\n",
1048 fdt_get_name(blob, node, NULL), ret);
1052 * TODO(sjg@chromium.org): Older TPMs will need to use the older method
1053 * in iic_tpm_read() so the offset length needs to be 0 here.
1055 ret = i2c_get_chip(bus, chip_addr, 1, &dev->dev);
1057 debug("Cannot find device for node '%s: ret=%d'\n",
1058 fdt_get_name(blob, node, NULL), ret);
1065 struct tpm_chip *tpm_register_hardware(const struct tpm_vendor_specific *entry)
1067 struct tpm_chip *chip;
1069 /* Driver specific per-device data */
1071 memcpy(&chip->vendor, entry, sizeof(struct tpm_vendor_specific));
1082 if (tpm_decode_config(&tpm))
1085 debug("%s: done\n", __func__);
1099 rc = tpm_open_dev(tpm.dev);
1114 int tis_sendrecv(const uint8_t *sendbuf, size_t sbuf_size,
1115 uint8_t *recvbuf, size_t *rbuf_len)
1123 if (sizeof(buf) < sbuf_size)
1126 memcpy(buf, sendbuf, sbuf_size);
1128 len = tpm_transmit(buf, sbuf_size);
1135 memcpy(recvbuf, buf, len);