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
29 #include <asm-generic/errno.h>
30 #include <linux/compiler.h>
31 #include <linux/types.h>
32 #include <linux/unaligned/be_byteshift.h>
34 #include "tpm_tis_infineon.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",
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(struct udevice *dev, u8 addr, u8 *buffer,
62 struct tpm_chip *chip = dev_get_priv(dev);
65 uint32_t addrbuf = addr;
67 if ((chip->chip_type == SLB9635) || (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(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(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(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(struct udevice *dev, u8 addr,
113 const u8 *buffer, size_t len,
114 unsigned int sleep_time_us, u8 max_count)
116 struct tpm_chip_priv *priv = dev_get_uclass_priv(dev);
117 struct tpm_chip *chip = dev_get_priv(dev);
121 if (chip->chip_type == SLB9635) {
122 /* Prepare send buffer to include the address */
124 memcpy(&(priv->buf[1]), buffer, len);
130 for (count = 0; count < max_count; count++) {
131 rc = dm_i2c_write(dev, addr, buffer, len);
133 break; /* Success, break to skip sleep */
134 udelay(sleep_time_us);
137 /* take care of 'guard time' */
138 udelay(sleep_time_us);
146 * tpm_tis_i2c_write() - write to TPM register
147 * @addr: register address to write to
148 * @buffer: containing data to be written
149 * @len: number of bytes to write
151 * Write len bytes from provided buffer to TPM register (little
152 * endian format, i.e. buffer[0] is written as first byte).
154 * NOTE: TPM is big-endian for multi-byte values. Multi-byte
155 * values have to be swapped.
157 * NOTE: use this function instead of the tpm_tis_i2c_write_generic function.
159 * Return -EIO on error, 0 on success
161 static int tpm_tis_i2c_write(struct udevice *dev, u8 addr, const u8 *buffer,
164 return tpm_tis_i2c_write_generic(dev, addr, buffer, len,
165 SLEEP_DURATION_US, MAX_COUNT);
169 * This function is needed especially for the cleanup situation after
172 static int tpm_tis_i2c_write_long(struct udevice *dev, u8 addr, u8 *buffer,
175 return tpm_tis_i2c_write_generic(dev, addr, buffer, len,
176 SLEEP_DURATION_LONG_US,
180 static int tpm_tis_i2c_check_locality(struct udevice *dev, int loc)
182 const u8 mask = TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID;
183 struct tpm_chip *chip = dev_get_priv(dev);
187 rc = tpm_tis_i2c_read(dev, TPM_ACCESS(loc), &buf, 1);
191 if ((buf & mask) == mask) {
192 chip->locality = loc;
199 static void tpm_tis_i2c_release_locality(struct udevice *dev, int loc,
202 const u8 mask = TPM_ACCESS_REQUEST_PENDING | TPM_ACCESS_VALID;
205 if (tpm_tis_i2c_read(dev, TPM_ACCESS(loc), &buf, 1) < 0)
208 if (force || (buf & mask) == mask) {
209 buf = TPM_ACCESS_ACTIVE_LOCALITY;
210 tpm_tis_i2c_write(dev, TPM_ACCESS(loc), &buf, 1);
214 static int tpm_tis_i2c_request_locality(struct udevice *dev, int loc)
216 struct tpm_chip *chip = dev_get_priv(dev);
217 unsigned long start, stop;
218 u8 buf = TPM_ACCESS_REQUEST_USE;
221 rc = tpm_tis_i2c_check_locality(dev, loc);
223 debug("%s: Already have locality\n", __func__);
224 return loc; /* We already have the locality */
225 } else if (rc != -ENOENT) {
226 debug("%s: Failed to get locality: %d\n", __func__, rc);
230 rc = tpm_tis_i2c_write(dev, TPM_ACCESS(loc), &buf, 1);
232 debug("%s: Failed to write to TPM: %d\n", __func__, rc);
236 /* Wait for burstcount */
237 start = get_timer(0);
238 stop = chip->timeout_a;
240 rc = tpm_tis_i2c_check_locality(dev, loc);
242 debug("%s: Have locality\n", __func__);
244 } else if (rc != -ENOENT) {
245 debug("%s: Failed to get locality: %d\n", __func__, rc);
248 mdelay(TPM_TIMEOUT_MS);
249 } while (get_timer(start) < stop);
250 debug("%s: Timeout getting locality: %d\n", __func__, rc);
255 static u8 tpm_tis_i2c_status(struct udevice *dev)
257 struct tpm_chip *chip = dev_get_priv(dev);
258 /* NOTE: Since i2c read may fail, return 0 in this case --> time-out */
261 if (tpm_tis_i2c_read(dev, TPM_STS(chip->locality), &buf, 1) < 0)
267 static int tpm_tis_i2c_ready(struct udevice *dev)
269 struct tpm_chip *chip = dev_get_priv(dev);
272 /* This causes the current command to be aborted */
273 u8 buf = TPM_STS_COMMAND_READY;
275 debug("%s\n", __func__);
276 rc = tpm_tis_i2c_write_long(dev, TPM_STS(chip->locality), &buf, 1);
278 debug("%s: rc=%d\n", __func__, rc);
283 static ssize_t tpm_tis_i2c_get_burstcount(struct udevice *dev)
285 struct tpm_chip *chip = dev_get_priv(dev);
286 unsigned long start, stop;
290 /* Wait for burstcount */
291 /* XXX: Which timeout value? Spec has 2 answers (c & d) */
292 start = get_timer(0);
293 stop = chip->timeout_d;
295 /* Note: STS is little endian */
296 addr = TPM_STS(chip->locality) + 1;
297 if (tpm_tis_i2c_read(dev, addr, buf, 3) < 0)
300 burstcnt = (buf[2] << 16) + (buf[1] << 8) + buf[0];
304 mdelay(TPM_TIMEOUT_MS);
305 } while (get_timer(start) < stop);
310 static int tpm_tis_i2c_wait_for_stat(struct udevice *dev, u8 mask,
311 unsigned long timeout, int *status)
313 unsigned long start, stop;
315 /* Check current status */
316 *status = tpm_tis_i2c_status(dev);
317 if ((*status & mask) == mask)
320 start = get_timer(0);
323 mdelay(TPM_TIMEOUT_MS);
324 *status = tpm_tis_i2c_status(dev);
325 if ((*status & mask) == mask)
327 } while (get_timer(start) < stop);
332 static int tpm_tis_i2c_recv_data(struct udevice *dev, u8 *buf, size_t count)
334 struct tpm_chip *chip = dev_get_priv(dev);
339 while (size < count) {
340 burstcnt = tpm_tis_i2c_get_burstcount(dev);
342 /* burstcount < 0 -> tpm is busy */
346 /* Limit received data to max left */
347 if (burstcnt > (count - size))
348 burstcnt = count - size;
350 rc = tpm_tis_i2c_read(dev, TPM_DATA_FIFO(chip->locality),
351 &(buf[size]), burstcnt);
359 static int tpm_tis_i2c_recv(struct udevice *dev, u8 *buf, size_t count)
361 struct tpm_chip *chip = dev_get_priv(dev);
363 int expected, status;
366 status = tpm_tis_i2c_status(dev);
367 if (status == TPM_STS_COMMAND_READY)
369 if ((status & (TPM_STS_DATA_AVAIL | TPM_STS_VALID)) !=
370 (TPM_STS_DATA_AVAIL | TPM_STS_VALID))
373 debug("...got it;\n");
375 /* Read first 10 bytes, including tag, paramsize, and result */
376 size = tpm_tis_i2c_recv_data(dev, buf, TPM_HEADER_SIZE);
377 if (size < TPM_HEADER_SIZE) {
378 debug("Unable to read header\n");
379 return size < 0 ? size : -EIO;
382 expected = get_unaligned_be32(buf + TPM_RSP_SIZE_BYTE);
383 if ((size_t)expected > count) {
384 debug("Error size=%x, expected=%x, count=%x\n", size, expected,
389 size += tpm_tis_i2c_recv_data(dev, &buf[TPM_HEADER_SIZE],
390 expected - TPM_HEADER_SIZE);
391 if (size < expected) {
392 debug("Unable to read remainder of result\n");
396 rc = tpm_tis_i2c_wait_for_stat(dev, TPM_STS_VALID, chip->timeout_c,
400 if (status & TPM_STS_DATA_AVAIL) { /* Retry? */
401 debug("Error left over data\n");
408 static int tpm_tis_i2c_send(struct udevice *dev, const u8 *buf, size_t len)
410 struct tpm_chip *chip = dev_get_priv(dev);
417 debug("%s: len=%d\n", __func__, len);
418 if (len > TPM_DEV_BUFSIZE)
419 return -E2BIG; /* Command is too long for our tpm, sorry */
421 if (tpm_tis_i2c_request_locality(dev, 0) < 0)
424 status = tpm_tis_i2c_status(dev);
425 if ((status & TPM_STS_COMMAND_READY) == 0) {
426 rc = tpm_tis_i2c_ready(dev);
429 rc = tpm_tis_i2c_wait_for_stat(dev, TPM_STS_COMMAND_READY,
430 chip->timeout_b, &status);
435 burstcnt = tpm_tis_i2c_get_burstcount(dev);
437 /* burstcount < 0 -> tpm is busy */
441 while (count < len) {
443 if (burstcnt > len - count)
444 burstcnt = len - count;
446 #ifdef CONFIG_TPM_TIS_I2C_BURST_LIMITATION
447 if (retry && burstcnt > CONFIG_TPM_TIS_I2C_BURST_LIMITATION_LEN)
448 burstcnt = CONFIG_TPM_TIS_I2C_BURST_LIMITATION_LEN;
449 #endif /* CONFIG_TPM_TIS_I2C_BURST_LIMITATION */
451 rc = tpm_tis_i2c_write(dev, TPM_DATA_FIFO(chip->locality),
452 &(buf[count]), burstcnt);
456 debug("%s: error\n", __func__);
459 rc = tpm_tis_i2c_wait_for_stat(dev, TPM_STS_VALID,
465 if ((status & TPM_STS_DATA_EXPECT) == 0)
471 rc = tpm_tis_i2c_write(dev, TPM_STS(chip->locality), &sts, 1);
474 debug("%s: done, rc=%d\n", __func__, rc);
479 static int tpm_tis_i2c_cleanup(struct udevice *dev)
481 struct tpm_chip *chip = dev_get_priv(dev);
483 tpm_tis_i2c_ready(dev);
485 * The TPM needs some time to clean up here,
486 * so we sleep rather than keeping the bus busy
489 tpm_tis_i2c_release_locality(dev, chip->locality, 0);
494 static int tpm_tis_i2c_init(struct udevice *dev)
496 struct tpm_chip *chip = dev_get_priv(dev);
498 u32 expected_did_vid;
503 /* Default timeouts - these could move to the device tree */
504 chip->timeout_a = TIS_SHORT_TIMEOUT_MS;
505 chip->timeout_b = TIS_LONG_TIMEOUT_MS;
506 chip->timeout_c = TIS_SHORT_TIMEOUT_MS;
507 chip->timeout_d = TIS_SHORT_TIMEOUT_MS;
509 rc = tpm_tis_i2c_request_locality(dev, 0);
513 /* Read four bytes from DID_VID register */
514 if (tpm_tis_i2c_read(dev, TPM_DID_VID(0), (uchar *)&vendor, 4) < 0) {
515 tpm_tis_i2c_release_locality(dev, 0, 1);
519 if (chip->chip_type == SLB9635) {
520 vendor = be32_to_cpu(vendor);
521 expected_did_vid = TPM_TIS_I2C_DID_VID_9635;
523 /* device id and byte order has changed for newer i2c tpms */
524 expected_did_vid = TPM_TIS_I2C_DID_VID_9645;
527 if (chip->chip_type != UNKNOWN && vendor != expected_did_vid) {
528 error("Vendor id did not match! ID was %08x\n", vendor);
532 chip->vend_dev = vendor;
533 debug("1.2 TPM (chip type %s device-id 0x%X)\n",
534 chip_name[chip->chip_type], vendor >> 16);
537 * A timeout query to TPM can be placed here.
538 * Standard timeout values are used so far
544 static int tpm_tis_i2c_open(struct udevice *dev)
546 struct tpm_chip *chip = dev_get_priv(dev);
549 debug("%s: start\n", __func__);
552 rc = tpm_tis_i2c_init(dev);
559 static int tpm_tis_i2c_close(struct udevice *dev)
561 struct tpm_chip *chip = dev_get_priv(dev);
564 tpm_tis_i2c_release_locality(dev, chip->locality, 1);
572 static int tpm_tis_get_desc(struct udevice *dev, char *buf, int size)
574 struct tpm_chip *chip = dev_get_priv(dev);
579 return snprintf(buf, size, "1.2 TPM (%s, chip type %s device-id 0x%x)",
580 chip->is_open ? "open" : "closed",
581 chip_name[chip->chip_type],
582 chip->vend_dev >> 16);
585 static int tpm_tis_i2c_probe(struct udevice *dev)
587 struct tpm_chip_priv *uc_priv = dev_get_uclass_priv(dev);
588 struct tpm_chip *chip = dev_get_priv(dev);
590 chip->chip_type = dev_get_driver_data(dev);
592 /* TODO: These need to be checked and tuned */
593 uc_priv->duration_ms[TPM_SHORT] = TIS_SHORT_TIMEOUT_MS;
594 uc_priv->duration_ms[TPM_MEDIUM] = TIS_LONG_TIMEOUT_MS;
595 uc_priv->duration_ms[TPM_LONG] = TIS_LONG_TIMEOUT_MS;
596 uc_priv->retry_time_ms = TPM_TIMEOUT_MS;
601 static const struct tpm_ops tpm_tis_i2c_ops = {
602 .open = tpm_tis_i2c_open,
603 .close = tpm_tis_i2c_close,
604 .get_desc = tpm_tis_get_desc,
605 .send = tpm_tis_i2c_send,
606 .recv = tpm_tis_i2c_recv,
607 .cleanup = tpm_tis_i2c_cleanup,
610 static const struct udevice_id tpm_tis_i2c_ids[] = {
611 { .compatible = "infineon,slb9635tt", .data = SLB9635 },
612 { .compatible = "infineon,slb9645tt", .data = SLB9645 },
616 U_BOOT_DRIVER(tpm_tis_i2c) = {
617 .name = "tpm_tis_infineon",
619 .of_match = tpm_tis_i2c_ids,
620 .ops = &tpm_tis_i2c_ops,
621 .probe = tpm_tis_i2c_probe,
622 .priv_auto_alloc_size = sizeof(struct tpm_chip),