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 static const char * const chip_name[] = {
38 [SLB9635] = "slb9635tt",
39 [SLB9645] = "slb9645tt",
40 [UNKNOWN] = "unknown/fallback to slb9635",
43 static struct tpm_chip g_chip;
46 * tpm_tis_i2c_read() - read from TPM register
47 * @addr: register address to read from
48 * @buffer: provided by caller
49 * @len: number of bytes to read
51 * Read len bytes from TPM register and put them into
52 * buffer (little-endian format, i.e. first byte is put into buffer[0]).
54 * NOTE: TPM is big-endian for multi-byte values. Multi-byte
55 * values have to be swapped.
57 * Return -EIO on error, 0 on success.
59 static int tpm_tis_i2c_read(u8 addr, u8 *buffer, size_t len)
63 uint32_t addrbuf = addr;
65 if ((g_chip.chip_type == SLB9635) || (g_chip.chip_type == UNKNOWN)) {
66 /* slb9635 protocol should work in both cases */
67 for (count = 0; count < MAX_COUNT; count++) {
68 rc = dm_i2c_write(g_chip.dev, 0, (uchar *)&addrbuf, 1);
70 break; /* Success, break to skip sleep */
71 udelay(SLEEP_DURATION);
76 /* After the TPM has successfully received the register address
77 * it needs some time, thus we're sleeping here again, before
80 for (count = 0; count < MAX_COUNT; count++) {
81 udelay(SLEEP_DURATION);
82 rc = dm_i2c_read(g_chip.dev, 0, buffer, len);
84 break; /* success, break to skip sleep */
88 * Use a combined read for newer chips.
89 * Unfortunately the smbus functions are not suitable due to
90 * the 32 byte limit of the smbus.
91 * Retries should usually not be needed, but are kept just to
92 * be safe on the safe side.
94 for (count = 0; count < MAX_COUNT; count++) {
95 rc = dm_i2c_read(g_chip.dev, addr, buffer, len);
97 break; /* break here to skip sleep */
98 udelay(SLEEP_DURATION);
102 /* Take care of 'guard time' */
103 udelay(SLEEP_DURATION);
110 static int tpm_tis_i2c_write_generic(u8 addr, u8 *buffer, size_t len,
111 unsigned int sleep_time, u8 max_count)
116 for (count = 0; count < max_count; count++) {
117 rc = dm_i2c_write(g_chip.dev, addr, buffer, len);
119 break; /* Success, break to skip sleep */
123 /* take care of 'guard time' */
132 * tpm_tis_i2c_write() - write to TPM register
133 * @addr: register address to write to
134 * @buffer: containing data to be written
135 * @len: number of bytes to write
137 * Write len bytes from provided buffer to TPM register (little
138 * endian format, i.e. buffer[0] is written as first byte).
140 * NOTE: TPM is big-endian for multi-byte values. Multi-byte
141 * values have to be swapped.
143 * NOTE: use this function instead of the tpm_tis_i2c_write_generic function.
145 * Return -EIO on error, 0 on success
147 static int tpm_tis_i2c_write(u8 addr, u8 *buffer, size_t len)
149 return tpm_tis_i2c_write_generic(addr, buffer, len, SLEEP_DURATION,
154 * This function is needed especially for the cleanup situation after
157 static int tpm_tis_i2c_write_long(u8 addr, u8 *buffer, size_t len)
159 return tpm_tis_i2c_write_generic(addr, buffer, len, SLEEP_DURATION_LONG,
163 static int tpm_tis_i2c_check_locality(struct tpm_chip *chip, int loc)
165 const u8 mask = TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID;
169 rc = tpm_tis_i2c_read(TPM_ACCESS(loc), &buf, 1);
173 if ((buf & mask) == mask) {
174 chip->locality = loc;
181 static void tpm_tis_i2c_release_locality(struct tpm_chip *chip, int loc,
184 const u8 mask = TPM_ACCESS_REQUEST_PENDING | TPM_ACCESS_VALID;
187 if (tpm_tis_i2c_read(TPM_ACCESS(loc), &buf, 1) < 0)
190 if (force || (buf & mask) == mask) {
191 buf = TPM_ACCESS_ACTIVE_LOCALITY;
192 tpm_tis_i2c_write(TPM_ACCESS(loc), &buf, 1);
196 static int tpm_tis_i2c_request_locality(struct tpm_chip *chip, int loc)
198 unsigned long start, stop;
199 u8 buf = TPM_ACCESS_REQUEST_USE;
202 if (tpm_tis_i2c_check_locality(chip, loc) >= 0)
203 return loc; /* We already have the locality */
205 rc = tpm_tis_i2c_write(TPM_ACCESS(loc), &buf, 1);
209 /* Wait for burstcount */
210 start = get_timer(0);
211 stop = chip->timeout_a;
213 if (tpm_tis_i2c_check_locality(chip, loc) >= 0)
215 udelay(TPM_TIMEOUT * 1000);
216 } while (get_timer(start) < stop);
221 static u8 tpm_tis_i2c_status(struct tpm_chip *chip)
223 /* NOTE: Since i2c read may fail, return 0 in this case --> time-out */
226 if (tpm_tis_i2c_read(TPM_STS(chip->locality), &buf, 1) < 0)
232 static void tpm_tis_i2c_ready(struct tpm_chip *chip)
236 /* This causes the current command to be aborted */
237 u8 buf = TPM_STS_COMMAND_READY;
239 debug("%s\n", __func__);
240 rc = tpm_tis_i2c_write_long(TPM_STS(chip->locality), &buf, 1);
242 debug("%s: rc=%d\n", __func__, rc);
245 static ssize_t tpm_tis_i2c_get_burstcount(struct tpm_chip *chip)
247 unsigned long start, stop;
251 /* Wait for burstcount */
252 /* XXX: Which timeout value? Spec has 2 answers (c & d) */
253 start = get_timer(0);
254 stop = chip->timeout_d;
256 /* Note: STS is little endian */
257 addr = TPM_STS(chip->locality) + 1;
258 if (tpm_tis_i2c_read(addr, buf, 3) < 0)
261 burstcnt = (buf[2] << 16) + (buf[1] << 8) + buf[0];
265 udelay(TPM_TIMEOUT * 1000);
266 } while (get_timer(start) < stop);
271 static int tpm_tis_i2c_wait_for_stat(struct tpm_chip *chip, u8 mask,
272 unsigned long timeout, int *status)
274 unsigned long start, stop;
276 /* Check current status */
277 *status = tpm_tis_i2c_status(chip);
278 if ((*status & mask) == mask)
281 start = get_timer(0);
284 udelay(TPM_TIMEOUT * 1000);
285 *status = tpm_tis_i2c_status(chip);
286 if ((*status & mask) == mask)
288 } while (get_timer(start) < stop);
293 static int tpm_tis_i2c_recv_data(struct tpm_chip *chip, u8 *buf, size_t count)
299 while (size < count) {
300 burstcnt = tpm_tis_i2c_get_burstcount(chip);
302 /* burstcount < 0 -> tpm is busy */
306 /* Limit received data to max left */
307 if (burstcnt > (count - size))
308 burstcnt = count - size;
310 rc = tpm_tis_i2c_read(TPM_DATA_FIFO(chip->locality),
311 &(buf[size]), burstcnt);
319 static int tpm_tis_i2c_recv(struct tpm_chip *chip, u8 *buf, size_t count)
322 int expected, status;
324 if (count < TPM_HEADER_SIZE) {
329 /* Read first 10 bytes, including tag, paramsize, and result */
330 size = tpm_tis_i2c_recv_data(chip, buf, TPM_HEADER_SIZE);
331 if (size < TPM_HEADER_SIZE) {
332 error("Unable to read header\n");
336 expected = get_unaligned_be32(buf + TPM_RSP_SIZE_BYTE);
337 if ((size_t)expected > count) {
338 error("Error size=%x, expected=%x, count=%x\n", size, expected,
344 size += tpm_tis_i2c_recv_data(chip, &buf[TPM_HEADER_SIZE],
345 expected - TPM_HEADER_SIZE);
346 if (size < expected) {
347 error("Unable to read remainder of result\n");
352 tpm_tis_i2c_wait_for_stat(chip, TPM_STS_VALID, chip->timeout_c,
354 if (status & TPM_STS_DATA_AVAIL) { /* Retry? */
355 error("Error left over data\n");
361 tpm_tis_i2c_ready(chip);
363 * The TPM needs some time to clean up here,
364 * so we sleep rather than keeping the bus busy
367 tpm_tis_i2c_release_locality(chip, chip->locality, 0);
372 static int tpm_tis_i2c_send(struct tpm_chip *chip, u8 *buf, size_t len)
380 debug("%s: len=%d\n", __func__, len);
381 if (len > TPM_DEV_BUFSIZE)
382 return -E2BIG; /* Command is too long for our tpm, sorry */
384 if (tpm_tis_i2c_request_locality(chip, 0) < 0)
387 status = tpm_tis_i2c_status(chip);
388 if ((status & TPM_STS_COMMAND_READY) == 0) {
389 tpm_tis_i2c_ready(chip);
390 if (tpm_tis_i2c_wait_for_stat(chip, TPM_STS_COMMAND_READY,
391 chip->timeout_b, &status) < 0) {
397 burstcnt = tpm_tis_i2c_get_burstcount(chip);
399 /* burstcount < 0 -> tpm is busy */
403 while (count < len) {
405 if (burstcnt > len - count)
406 burstcnt = len - count;
408 #ifdef CONFIG_TPM_TIS_I2C_BURST_LIMITATION
409 if (retry && burstcnt > CONFIG_TPM_TIS_I2C_BURST_LIMITATION)
410 burstcnt = CONFIG_TPM_TIS_I2C_BURST_LIMITATION;
411 #endif /* CONFIG_TPM_TIS_I2C_BURST_LIMITATION */
413 rc = tpm_tis_i2c_write(TPM_DATA_FIFO(chip->locality),
414 &(buf[count]), burstcnt);
418 debug("%s: error\n", __func__);
423 rc = tpm_tis_i2c_wait_for_stat(chip, TPM_STS_VALID,
429 if ((status & TPM_STS_DATA_EXPECT) == 0) {
437 tpm_tis_i2c_write(TPM_STS(chip->locality), &sts, 1);
443 debug("%s: out_err\n", __func__);
444 tpm_tis_i2c_ready(chip);
446 * The TPM needs some time to clean up here,
447 * so we sleep rather than keeping the bus busy
450 tpm_tis_i2c_release_locality(chip, chip->locality, 0);
455 static enum i2c_chip_type tpm_tis_i2c_chip_type(void)
457 #if CONFIG_IS_ENABLED(OF_CONTROL)
458 const void *blob = gd->fdt_blob;
460 if (fdtdec_next_compatible(blob, 0, COMPAT_INFINEON_SLB9645_TPM) >= 0)
463 if (fdtdec_next_compatible(blob, 0, COMPAT_INFINEON_SLB9635_TPM) >= 0)
469 static int tpm_tis_i2c_init(struct udevice *dev)
471 struct tpm_chip *chip = &g_chip;
473 u32 expected_did_vid;
476 g_chip.chip_type = tpm_tis_i2c_chip_type();
479 /* Disable interrupts (not supported) */
482 /* Default timeouts - these could move to the device tree */
483 chip->timeout_a = TIS_SHORT_TIMEOUT;
484 chip->timeout_b = TIS_LONG_TIMEOUT;
485 chip->timeout_c = TIS_SHORT_TIMEOUT;
486 chip->timeout_d = TIS_SHORT_TIMEOUT;
487 chip->req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID;
488 chip->req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID;
489 chip->req_canceled = TPM_STS_COMMAND_READY;
491 if (tpm_tis_i2c_request_locality(chip, 0) < 0)
494 /* Read four bytes from DID_VID register */
495 if (tpm_tis_i2c_read(TPM_DID_VID(0), (uchar *)&vendor, 4) < 0) {
496 tpm_tis_i2c_release_locality(chip, 0, 1);
500 if (g_chip.chip_type == SLB9635) {
501 vendor = be32_to_cpu(vendor);
502 expected_did_vid = TPM_TIS_I2C_DID_VID_9635;
504 /* device id and byte order has changed for newer i2c tpms */
505 expected_did_vid = TPM_TIS_I2C_DID_VID_9645;
508 if (g_chip.chip_type != UNKNOWN && vendor != expected_did_vid) {
509 error("Vendor id did not match! ID was %08x\n", vendor);
513 debug("1.2 TPM (chip type %s device-id 0x%X)\n",
514 chip_name[g_chip.chip_type], vendor >> 16);
517 * A timeout query to TPM can be placed here.
518 * Standard timeout values are used so far
524 /* Returns max number of milliseconds to wait */
525 static unsigned long tpm_tis_i2c_calc_ordinal_duration(struct tpm_chip *chip,
528 int duration_idx = TPM_UNDEFINED;
531 if (ordinal < TPM_MAX_ORDINAL) {
532 duration_idx = tpm_ordinal_duration[ordinal];
533 } else if ((ordinal & TPM_PROTECTED_ORDINAL_MASK) <
534 TPM_MAX_PROTECTED_ORDINAL) {
535 duration_idx = tpm_protected_ordinal_duration[
536 ordinal & TPM_PROTECTED_ORDINAL_MASK];
539 if (duration_idx != TPM_UNDEFINED)
540 duration = chip->duration[duration_idx];
543 return 2 * 60 * HZ; /* Two minutes timeout */
548 static ssize_t tpm_tis_i2c_transmit(const unsigned char *buf, size_t bufsiz)
552 unsigned long start, stop;
554 struct tpm_chip *chip = &g_chip;
556 /* switch endianess: big->little */
557 count = get_unaligned_be32(buf + TPM_CMD_COUNT_BYTE);
558 ordinal = get_unaligned_be32(buf + TPM_CMD_ORDINAL_BYTE);
564 if (count > bufsiz) {
565 error("invalid count value %x %zx\n", count, bufsiz);
569 debug("Calling send\n");
570 rc = tpm_tis_i2c_send(chip, (u8 *)buf, count);
571 debug(" ... done calling send\n");
573 error("tpm_transmit: tpm_send: error %d\n", rc);
580 start = get_timer(0);
581 stop = tpm_tis_i2c_calc_ordinal_duration(chip, ordinal);
583 debug("waiting for status... %ld %ld\n", start, stop);
584 u8 status = tpm_tis_i2c_status(chip);
585 if ((status & chip->req_complete_mask) ==
586 chip->req_complete_val) {
587 debug("...got it;\n");
591 if (status == chip->req_canceled) {
592 error("Operation Canceled\n");
596 udelay(TPM_TIMEOUT * 1000);
597 } while (get_timer(start) < stop);
599 tpm_tis_i2c_ready(chip);
600 error("Operation Timed out\n");
605 debug("out_recv: reading response...\n");
606 rc = tpm_tis_i2c_recv(chip, (u8 *)buf, TPM_BUFSIZE);
608 error("tpm_transmit: tpm_recv: error %d\n", rc);
615 * Decode TPM configuration.
617 * @param dev Returns a configuration of TPM device
618 * @return 0 if ok, -1 on error
620 static int tpm_tis_i2c_decode_config(struct tpm_chip *chip)
622 const void *blob = gd->fdt_blob;
629 node = fdtdec_next_compatible(blob, 0, COMPAT_INFINEON_SLB9635_TPM);
631 node = fdtdec_next_compatible(blob, 0,
632 COMPAT_INFINEON_SLB9645_TPM);
635 debug("%s: Node not found\n", __func__);
638 parent = fdt_parent_offset(blob, node);
640 debug("%s: Cannot find node parent\n", __func__);
645 * TODO(sjg@chromium.org): Remove this when driver model supports
648 ret = uclass_get_device_by_of_offset(UCLASS_I2C, parent, &bus);
650 debug("Cannot find bus for node '%s: ret=%d'\n",
651 fdt_get_name(blob, parent, NULL), ret);
655 chip_addr = fdtdec_get_int(blob, node, "reg", -1);
656 if (chip_addr == -1) {
657 debug("Cannot find reg property for node '%s: ret=%d'\n",
658 fdt_get_name(blob, node, NULL), ret);
662 * TODO(sjg@chromium.org): Older TPMs will need to use the older method
663 * in tpm_tis_i2c_read() so the offset length needs to be 0 here.
665 ret = i2c_get_chip(bus, chip_addr, 1, &chip->dev);
667 debug("Cannot find device for node '%s: ret=%d'\n",
668 fdt_get_name(blob, node, NULL), ret);
680 if (tpm_tis_i2c_decode_config(&g_chip))
683 debug("%s: done\n", __func__);
697 debug("%s: start\n", __func__);
700 rc = tpm_tis_i2c_init(g_chip.dev);
712 if (g_chip.is_open) {
713 tpm_tis_i2c_release_locality(&g_chip, g_chip.locality, 1);
720 int tis_sendrecv(const uint8_t *sendbuf, size_t sbuf_size,
721 uint8_t *recvbuf, size_t *rbuf_len)
729 if (sizeof(buf) < sbuf_size)
732 memcpy(buf, sendbuf, sbuf_size);
734 len = tpm_tis_i2c_transmit(buf, sbuf_size);
741 memcpy(recvbuf, buf, len);