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>
30 #include <asm-generic/errno.h>
31 #include <linux/types.h>
32 #include <linux/unaligned/be_byteshift.h>
34 #include "tpm_tis_i2c.h"
35 #include "tpm_internal.h"
37 DECLARE_GLOBAL_DATA_PTR;
39 static const char * const chip_name[] = {
40 [SLB9635] = "slb9635tt",
41 [SLB9645] = "slb9645tt",
42 [UNKNOWN] = "unknown/fallback to slb9635",
45 static struct tpm_chip g_chip;
48 * tpm_tis_i2c_read() - read from TPM register
49 * @addr: register address to read from
50 * @buffer: provided by caller
51 * @len: number of bytes to read
53 * Read len bytes from TPM register and put them into
54 * buffer (little-endian format, i.e. first byte is put into buffer[0]).
56 * NOTE: TPM is big-endian for multi-byte values. Multi-byte
57 * values have to be swapped.
59 * Return -EIO on error, 0 on success.
61 static int tpm_tis_i2c_read(u8 addr, u8 *buffer, size_t len)
65 uint32_t addrbuf = addr;
67 if ((g_chip.chip_type == SLB9635) || (g_chip.chip_type == UNKNOWN)) {
68 /* slb9635 protocol should work in both cases */
69 for (count = 0; count < MAX_COUNT; count++) {
70 rc = dm_i2c_write(g_chip.dev, 0, (uchar *)&addrbuf, 1);
72 break; /* Success, break to skip sleep */
73 udelay(SLEEP_DURATION_US);
78 /* After the TPM has successfully received the register address
79 * it needs some time, thus we're sleeping here again, before
82 for (count = 0; count < MAX_COUNT; count++) {
83 udelay(SLEEP_DURATION_US);
84 rc = dm_i2c_read(g_chip.dev, 0, buffer, len);
86 break; /* success, break to skip sleep */
90 * Use a combined read for newer chips.
91 * Unfortunately the smbus functions are not suitable due to
92 * the 32 byte limit of the smbus.
93 * Retries should usually not be needed, but are kept just to
94 * be safe on the safe side.
96 for (count = 0; count < MAX_COUNT; count++) {
97 rc = dm_i2c_read(g_chip.dev, addr, buffer, len);
99 break; /* break here to skip sleep */
100 udelay(SLEEP_DURATION_US);
104 /* Take care of 'guard time' */
105 udelay(SLEEP_DURATION_US);
112 static int tpm_tis_i2c_write_generic(u8 addr, u8 *buffer, size_t len,
113 unsigned int sleep_time_us, u8 max_count)
118 for (count = 0; count < max_count; count++) {
119 rc = dm_i2c_write(g_chip.dev, addr, buffer, len);
121 break; /* Success, break to skip sleep */
122 udelay(sleep_time_us);
125 /* take care of 'guard time' */
126 udelay(sleep_time_us);
134 * tpm_tis_i2c_write() - write to TPM register
135 * @addr: register address to write to
136 * @buffer: containing data to be written
137 * @len: number of bytes to write
139 * Write len bytes from provided buffer to TPM register (little
140 * endian format, i.e. buffer[0] is written as first byte).
142 * NOTE: TPM is big-endian for multi-byte values. Multi-byte
143 * values have to be swapped.
145 * NOTE: use this function instead of the tpm_tis_i2c_write_generic function.
147 * Return -EIO on error, 0 on success
149 static int tpm_tis_i2c_write(u8 addr, u8 *buffer, size_t len)
151 return tpm_tis_i2c_write_generic(addr, buffer, len, SLEEP_DURATION_US,
156 * This function is needed especially for the cleanup situation after
159 static int tpm_tis_i2c_write_long(u8 addr, u8 *buffer, size_t len)
161 return tpm_tis_i2c_write_generic(addr, buffer, len,
162 SLEEP_DURATION_LONG_US,
166 static int tpm_tis_i2c_check_locality(struct tpm_chip *chip, int loc)
168 const u8 mask = TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID;
172 rc = tpm_tis_i2c_read(TPM_ACCESS(loc), &buf, 1);
176 if ((buf & mask) == mask) {
177 chip->locality = loc;
184 static void tpm_tis_i2c_release_locality(struct tpm_chip *chip, int loc,
187 const u8 mask = TPM_ACCESS_REQUEST_PENDING | TPM_ACCESS_VALID;
190 if (tpm_tis_i2c_read(TPM_ACCESS(loc), &buf, 1) < 0)
193 if (force || (buf & mask) == mask) {
194 buf = TPM_ACCESS_ACTIVE_LOCALITY;
195 tpm_tis_i2c_write(TPM_ACCESS(loc), &buf, 1);
199 static int tpm_tis_i2c_request_locality(struct tpm_chip *chip, int loc)
201 unsigned long start, stop;
202 u8 buf = TPM_ACCESS_REQUEST_USE;
205 if (tpm_tis_i2c_check_locality(chip, loc) >= 0)
206 return loc; /* We already have the locality */
208 rc = tpm_tis_i2c_write(TPM_ACCESS(loc), &buf, 1);
212 /* Wait for burstcount */
213 start = get_timer(0);
214 stop = chip->timeout_a;
216 if (tpm_tis_i2c_check_locality(chip, loc) >= 0)
218 mdelay(TPM_TIMEOUT_MS);
219 } while (get_timer(start) < stop);
224 static u8 tpm_tis_i2c_status(struct tpm_chip *chip)
226 /* NOTE: Since i2c read may fail, return 0 in this case --> time-out */
229 if (tpm_tis_i2c_read(TPM_STS(chip->locality), &buf, 1) < 0)
235 static void tpm_tis_i2c_ready(struct tpm_chip *chip)
239 /* This causes the current command to be aborted */
240 u8 buf = TPM_STS_COMMAND_READY;
242 debug("%s\n", __func__);
243 rc = tpm_tis_i2c_write_long(TPM_STS(chip->locality), &buf, 1);
245 debug("%s: rc=%d\n", __func__, rc);
248 static ssize_t tpm_tis_i2c_get_burstcount(struct tpm_chip *chip)
250 unsigned long start, stop;
254 /* Wait for burstcount */
255 /* XXX: Which timeout value? Spec has 2 answers (c & d) */
256 start = get_timer(0);
257 stop = chip->timeout_d;
259 /* Note: STS is little endian */
260 addr = TPM_STS(chip->locality) + 1;
261 if (tpm_tis_i2c_read(addr, buf, 3) < 0)
264 burstcnt = (buf[2] << 16) + (buf[1] << 8) + buf[0];
268 mdelay(TPM_TIMEOUT_MS);
269 } while (get_timer(start) < stop);
274 static int tpm_tis_i2c_wait_for_stat(struct tpm_chip *chip, u8 mask,
275 unsigned long timeout, int *status)
277 unsigned long start, stop;
279 /* Check current status */
280 *status = tpm_tis_i2c_status(chip);
281 if ((*status & mask) == mask)
284 start = get_timer(0);
287 mdelay(TPM_TIMEOUT_MS);
288 *status = tpm_tis_i2c_status(chip);
289 if ((*status & mask) == mask)
291 } while (get_timer(start) < stop);
296 static int tpm_tis_i2c_recv_data(struct tpm_chip *chip, u8 *buf, size_t count)
302 while (size < count) {
303 burstcnt = tpm_tis_i2c_get_burstcount(chip);
305 /* burstcount < 0 -> tpm is busy */
309 /* Limit received data to max left */
310 if (burstcnt > (count - size))
311 burstcnt = count - size;
313 rc = tpm_tis_i2c_read(TPM_DATA_FIFO(chip->locality),
314 &(buf[size]), burstcnt);
322 static int tpm_tis_i2c_recv(struct tpm_chip *chip, u8 *buf, size_t count)
325 int expected, status;
327 if (count < TPM_HEADER_SIZE) {
332 /* Read first 10 bytes, including tag, paramsize, and result */
333 size = tpm_tis_i2c_recv_data(chip, buf, TPM_HEADER_SIZE);
334 if (size < TPM_HEADER_SIZE) {
335 error("Unable to read header\n");
339 expected = get_unaligned_be32(buf + TPM_RSP_SIZE_BYTE);
340 if ((size_t)expected > count) {
341 error("Error size=%x, expected=%x, count=%x\n", size, expected,
347 size += tpm_tis_i2c_recv_data(chip, &buf[TPM_HEADER_SIZE],
348 expected - TPM_HEADER_SIZE);
349 if (size < expected) {
350 error("Unable to read remainder of result\n");
355 tpm_tis_i2c_wait_for_stat(chip, TPM_STS_VALID, chip->timeout_c,
357 if (status & TPM_STS_DATA_AVAIL) { /* Retry? */
358 error("Error left over data\n");
364 tpm_tis_i2c_ready(chip);
366 * The TPM needs some time to clean up here,
367 * so we sleep rather than keeping the bus busy
370 tpm_tis_i2c_release_locality(chip, chip->locality, 0);
375 static int tpm_tis_i2c_send(struct tpm_chip *chip, u8 *buf, size_t len)
383 debug("%s: len=%d\n", __func__, len);
384 if (len > TPM_DEV_BUFSIZE)
385 return -E2BIG; /* Command is too long for our tpm, sorry */
387 if (tpm_tis_i2c_request_locality(chip, 0) < 0)
390 status = tpm_tis_i2c_status(chip);
391 if ((status & TPM_STS_COMMAND_READY) == 0) {
392 tpm_tis_i2c_ready(chip);
393 if (tpm_tis_i2c_wait_for_stat(chip, TPM_STS_COMMAND_READY,
394 chip->timeout_b, &status) < 0) {
400 burstcnt = tpm_tis_i2c_get_burstcount(chip);
402 /* burstcount < 0 -> tpm is busy */
406 while (count < len) {
408 if (burstcnt > len - count)
409 burstcnt = len - count;
411 #ifdef CONFIG_TPM_TIS_I2C_BURST_LIMITATION
412 if (retry && burstcnt > CONFIG_TPM_TIS_I2C_BURST_LIMITATION)
413 burstcnt = CONFIG_TPM_TIS_I2C_BURST_LIMITATION;
414 #endif /* CONFIG_TPM_TIS_I2C_BURST_LIMITATION */
416 rc = tpm_tis_i2c_write(TPM_DATA_FIFO(chip->locality),
417 &(buf[count]), burstcnt);
421 debug("%s: error\n", __func__);
426 rc = tpm_tis_i2c_wait_for_stat(chip, TPM_STS_VALID,
432 if ((status & TPM_STS_DATA_EXPECT) == 0) {
440 tpm_tis_i2c_write(TPM_STS(chip->locality), &sts, 1);
446 debug("%s: out_err\n", __func__);
447 tpm_tis_i2c_ready(chip);
449 * The TPM needs some time to clean up here,
450 * so we sleep rather than keeping the bus busy
453 tpm_tis_i2c_release_locality(chip, chip->locality, 0);
458 static enum i2c_chip_type tpm_tis_i2c_chip_type(void)
460 #if CONFIG_IS_ENABLED(OF_CONTROL)
461 const void *blob = gd->fdt_blob;
463 if (fdtdec_next_compatible(blob, 0, COMPAT_INFINEON_SLB9645_TPM) >= 0)
466 if (fdtdec_next_compatible(blob, 0, COMPAT_INFINEON_SLB9635_TPM) >= 0)
472 static int tpm_tis_i2c_init(struct udevice *dev)
474 struct tpm_chip *chip = &g_chip;
476 u32 expected_did_vid;
479 g_chip.chip_type = tpm_tis_i2c_chip_type();
482 /* Disable interrupts (not supported) */
485 /* Default timeouts - these could move to the device tree */
486 chip->timeout_a = TIS_SHORT_TIMEOUT_MS;
487 chip->timeout_b = TIS_LONG_TIMEOUT_MS;
488 chip->timeout_c = TIS_SHORT_TIMEOUT_MS;
489 chip->timeout_d = TIS_SHORT_TIMEOUT_MS;
490 chip->req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID;
491 chip->req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID;
492 chip->req_canceled = TPM_STS_COMMAND_READY;
494 if (tpm_tis_i2c_request_locality(chip, 0) < 0)
497 /* Read four bytes from DID_VID register */
498 if (tpm_tis_i2c_read(TPM_DID_VID(0), (uchar *)&vendor, 4) < 0) {
499 tpm_tis_i2c_release_locality(chip, 0, 1);
503 if (g_chip.chip_type == SLB9635) {
504 vendor = be32_to_cpu(vendor);
505 expected_did_vid = TPM_TIS_I2C_DID_VID_9635;
507 /* device id and byte order has changed for newer i2c tpms */
508 expected_did_vid = TPM_TIS_I2C_DID_VID_9645;
511 if (g_chip.chip_type != UNKNOWN && vendor != expected_did_vid) {
512 error("Vendor id did not match! ID was %08x\n", vendor);
516 debug("1.2 TPM (chip type %s device-id 0x%X)\n",
517 chip_name[g_chip.chip_type], vendor >> 16);
520 * A timeout query to TPM can be placed here.
521 * Standard timeout values are used so far
527 /* Returns max number of milliseconds to wait */
528 static unsigned long tpm_tis_i2c_calc_ordinal_duration(struct tpm_chip *chip,
531 int duration_idx = TPM_UNDEFINED;
534 if (ordinal < TPM_MAX_ORDINAL) {
535 duration_idx = tpm_ordinal_duration[ordinal];
536 } else if ((ordinal & TPM_PROTECTED_ORDINAL_MASK) <
537 TPM_MAX_PROTECTED_ORDINAL) {
538 duration_idx = tpm_protected_ordinal_duration[
539 ordinal & TPM_PROTECTED_ORDINAL_MASK];
542 if (duration_idx != TPM_UNDEFINED)
543 duration = chip->duration[duration_idx];
546 return 2 * 60 * HZ; /* Two minutes timeout */
551 static ssize_t tpm_tis_i2c_transmit(const unsigned char *buf, size_t bufsiz)
555 unsigned long start, stop;
557 struct tpm_chip *chip = &g_chip;
559 /* switch endianess: big->little */
560 count = get_unaligned_be32(buf + TPM_CMD_COUNT_BYTE);
561 ordinal = get_unaligned_be32(buf + TPM_CMD_ORDINAL_BYTE);
567 if (count > bufsiz) {
568 error("invalid count value %x %zx\n", count, bufsiz);
572 debug("Calling send\n");
573 rc = tpm_tis_i2c_send(chip, (u8 *)buf, count);
574 debug(" ... done calling send\n");
576 error("tpm_transmit: tpm_send: error %d\n", rc);
583 start = get_timer(0);
584 stop = tpm_tis_i2c_calc_ordinal_duration(chip, ordinal);
586 debug("waiting for status... %ld %ld\n", start, stop);
587 u8 status = tpm_tis_i2c_status(chip);
588 if ((status & chip->req_complete_mask) ==
589 chip->req_complete_val) {
590 debug("...got it;\n");
594 if (status == chip->req_canceled) {
595 error("Operation Canceled\n");
599 mdelay(TPM_TIMEOUT_MS);
600 } while (get_timer(start) < stop);
602 tpm_tis_i2c_ready(chip);
603 error("Operation Timed out\n");
608 debug("out_recv: reading response...\n");
609 rc = tpm_tis_i2c_recv(chip, (u8 *)buf, TPM_BUFSIZE);
611 error("tpm_transmit: tpm_recv: error %d\n", rc);
618 * Decode TPM configuration.
620 * @param dev Returns a configuration of TPM device
621 * @return 0 if ok, -1 on error
623 static int tpm_tis_i2c_decode_config(struct tpm_chip *chip)
625 const void *blob = gd->fdt_blob;
632 node = fdtdec_next_compatible(blob, 0, COMPAT_INFINEON_SLB9635_TPM);
634 node = fdtdec_next_compatible(blob, 0,
635 COMPAT_INFINEON_SLB9645_TPM);
638 debug("%s: Node not found\n", __func__);
641 parent = fdt_parent_offset(blob, node);
643 debug("%s: Cannot find node parent\n", __func__);
648 * TODO(sjg@chromium.org): Remove this when driver model supports
651 ret = uclass_get_device_by_of_offset(UCLASS_I2C, parent, &bus);
653 debug("Cannot find bus for node '%s: ret=%d'\n",
654 fdt_get_name(blob, parent, NULL), ret);
658 chip_addr = fdtdec_get_int(blob, node, "reg", -1);
659 if (chip_addr == -1) {
660 debug("Cannot find reg property for node '%s: ret=%d'\n",
661 fdt_get_name(blob, node, NULL), ret);
665 * TODO(sjg@chromium.org): Older TPMs will need to use the older method
666 * in tpm_tis_i2c_read() so the offset length needs to be 0 here.
668 ret = i2c_get_chip(bus, chip_addr, 1, &chip->dev);
670 debug("Cannot find device for node '%s: ret=%d'\n",
671 fdt_get_name(blob, node, NULL), ret);
683 if (tpm_tis_i2c_decode_config(&g_chip))
686 debug("%s: done\n", __func__);
700 debug("%s: start\n", __func__);
703 rc = tpm_tis_i2c_init(g_chip.dev);
715 if (g_chip.is_open) {
716 tpm_tis_i2c_release_locality(&g_chip, g_chip.locality, 1);
723 int tis_sendrecv(const uint8_t *sendbuf, size_t sbuf_size,
724 uint8_t *recvbuf, size_t *rbuf_len)
732 if (sizeof(buf) < sbuf_size)
735 memcpy(buf, sendbuf, sbuf_size);
737 len = tpm_tis_i2c_transmit(buf, sbuf_size);
744 memcpy(recvbuf, buf, len);