1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2011 Infineon Technologies
6 * Peter Huewe <huewe.external@infineon.com>
9 * Device driver for TCG/TCPA TPM (trusted platform module).
10 * Specifications at www.trustedcomputinggroup.org
12 * This device driver implements the TPM interface as defined in
13 * the TCG TPM Interface Spec version 1.2, revision 1.0 and the
14 * Infineon I2C Protocol Stack Specification v0.20.
16 * It is based on the Linux kernel driver tpm.c from Leendert van
17 * Dorn, Dave Safford, Reiner Sailer, and Kyleen Hall.
27 #include <linux/errno.h>
28 #include <linux/compiler.h>
29 #include <linux/types.h>
30 #include <linux/unaligned/be_byteshift.h>
33 #include "tpm_internal.h"
41 /* expected value for DIDVID register */
42 #define TPM_TIS_I2C_DID_VID_9635 0x000b15d1L
43 #define TPM_TIS_I2C_DID_VID_9645 0x001a15d1L
45 static const char * const chip_name[] = {
46 [SLB9635] = "slb9635tt",
47 [SLB9645] = "slb9645tt",
48 [UNKNOWN] = "unknown/fallback to slb9635",
51 #define TPM_ACCESS(l) (0x0000 | ((l) << 4))
52 #define TPM_STS(l) (0x0001 | ((l) << 4))
53 #define TPM_DATA_FIFO(l) (0x0005 | ((l) << 4))
54 #define TPM_DID_VID(l) (0x0006 | ((l) << 4))
57 * tpm_tis_i2c_read() - read from TPM register
58 * @addr: register address to read from
59 * @buffer: provided by caller
60 * @len: number of bytes to read
62 * Read len bytes from TPM register and put them into
63 * buffer (little-endian format, i.e. first byte is put into buffer[0]).
65 * NOTE: TPM is big-endian for multi-byte values. Multi-byte
66 * values have to be swapped.
68 * Return -EIO on error, 0 on success.
70 static int tpm_tis_i2c_read(struct udevice *dev, u8 addr, u8 *buffer,
73 struct tpm_chip *chip = dev_get_priv(dev);
76 uint32_t addrbuf = addr;
78 if ((chip->chip_type == SLB9635) || (chip->chip_type == UNKNOWN)) {
79 /* slb9635 protocol should work in both cases */
80 for (count = 0; count < MAX_COUNT; count++) {
81 rc = dm_i2c_write(dev, 0, (uchar *)&addrbuf, 1);
83 break; /* Success, break to skip sleep */
84 udelay(SLEEP_DURATION_US);
89 /* After the TPM has successfully received the register address
90 * it needs some time, thus we're sleeping here again, before
93 for (count = 0; count < MAX_COUNT; count++) {
94 udelay(SLEEP_DURATION_US);
95 rc = dm_i2c_read(dev, 0, buffer, len);
97 break; /* success, break to skip sleep */
101 * Use a combined read for newer chips.
102 * Unfortunately the smbus functions are not suitable due to
103 * the 32 byte limit of the smbus.
104 * Retries should usually not be needed, but are kept just to
105 * be safe on the safe side.
107 for (count = 0; count < MAX_COUNT; count++) {
108 rc = dm_i2c_read(dev, addr, buffer, len);
110 break; /* break here to skip sleep */
111 udelay(SLEEP_DURATION_US);
115 /* Take care of 'guard time' */
116 udelay(SLEEP_DURATION_US);
123 static int tpm_tis_i2c_write_generic(struct udevice *dev, u8 addr,
124 const u8 *buffer, size_t len,
125 unsigned int sleep_time_us, u8 max_count)
127 struct tpm_chip_priv *priv = dev_get_uclass_priv(dev);
128 struct tpm_chip *chip = dev_get_priv(dev);
132 if (chip->chip_type == SLB9635) {
133 /* Prepare send buffer to include the address */
135 memcpy(&(priv->buf[1]), buffer, len);
141 for (count = 0; count < max_count; count++) {
142 rc = dm_i2c_write(dev, addr, buffer, len);
144 break; /* Success, break to skip sleep */
145 udelay(sleep_time_us);
148 /* take care of 'guard time' */
149 udelay(sleep_time_us);
157 * tpm_tis_i2c_write() - write to TPM register
158 * @addr: register address to write to
159 * @buffer: containing data to be written
160 * @len: number of bytes to write
162 * Write len bytes from provided buffer to TPM register (little
163 * endian format, i.e. buffer[0] is written as first byte).
165 * NOTE: TPM is big-endian for multi-byte values. Multi-byte
166 * values have to be swapped.
168 * NOTE: use this function instead of the tpm_tis_i2c_write_generic function.
170 * Return -EIO on error, 0 on success
172 static int tpm_tis_i2c_write(struct udevice *dev, u8 addr, const u8 *buffer,
175 return tpm_tis_i2c_write_generic(dev, addr, buffer, len,
176 SLEEP_DURATION_US, MAX_COUNT);
180 * This function is needed especially for the cleanup situation after
183 static int tpm_tis_i2c_write_long(struct udevice *dev, u8 addr, u8 *buffer,
186 return tpm_tis_i2c_write_generic(dev, addr, buffer, len,
187 SLEEP_DURATION_LONG_US,
191 static int tpm_tis_i2c_check_locality(struct udevice *dev, int loc)
193 const u8 mask = TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID;
194 struct tpm_chip *chip = dev_get_priv(dev);
198 rc = tpm_tis_i2c_read(dev, TPM_ACCESS(loc), &buf, 1);
202 if ((buf & mask) == mask) {
203 chip->locality = loc;
210 static void tpm_tis_i2c_release_locality(struct udevice *dev, int loc,
213 const u8 mask = TPM_ACCESS_REQUEST_PENDING | TPM_ACCESS_VALID;
216 if (tpm_tis_i2c_read(dev, TPM_ACCESS(loc), &buf, 1) < 0)
219 if (force || (buf & mask) == mask) {
220 buf = TPM_ACCESS_ACTIVE_LOCALITY;
221 tpm_tis_i2c_write(dev, TPM_ACCESS(loc), &buf, 1);
225 static int tpm_tis_i2c_request_locality(struct udevice *dev, int loc)
227 struct tpm_chip *chip = dev_get_priv(dev);
228 unsigned long start, stop;
229 u8 buf = TPM_ACCESS_REQUEST_USE;
232 rc = tpm_tis_i2c_check_locality(dev, loc);
234 debug("%s: Already have locality\n", __func__);
235 return loc; /* We already have the locality */
236 } else if (rc != -ENOENT) {
237 debug("%s: Failed to get locality: %d\n", __func__, rc);
241 rc = tpm_tis_i2c_write(dev, TPM_ACCESS(loc), &buf, 1);
243 debug("%s: Failed to write to TPM: %d\n", __func__, rc);
247 /* Wait for burstcount */
248 start = get_timer(0);
249 stop = chip->timeout_a;
251 rc = tpm_tis_i2c_check_locality(dev, loc);
253 debug("%s: Have locality\n", __func__);
255 } else if (rc != -ENOENT) {
256 debug("%s: Failed to get locality: %d\n", __func__, rc);
259 mdelay(TPM_TIMEOUT_MS);
260 } while (get_timer(start) < stop);
261 debug("%s: Timeout getting locality: %d\n", __func__, rc);
266 static u8 tpm_tis_i2c_status(struct udevice *dev)
268 struct tpm_chip *chip = dev_get_priv(dev);
269 /* NOTE: Since i2c read may fail, return 0 in this case --> time-out */
272 if (tpm_tis_i2c_read(dev, TPM_STS(chip->locality), &buf, 1) < 0)
278 static int tpm_tis_i2c_ready(struct udevice *dev)
280 struct tpm_chip *chip = dev_get_priv(dev);
283 /* This causes the current command to be aborted */
284 u8 buf = TPM_STS_COMMAND_READY;
286 debug("%s\n", __func__);
287 rc = tpm_tis_i2c_write_long(dev, TPM_STS(chip->locality), &buf, 1);
289 debug("%s: rc=%d\n", __func__, rc);
294 static ssize_t tpm_tis_i2c_get_burstcount(struct udevice *dev)
296 struct tpm_chip *chip = dev_get_priv(dev);
297 unsigned long start, stop;
301 /* Wait for burstcount */
302 /* XXX: Which timeout value? Spec has 2 answers (c & d) */
303 start = get_timer(0);
304 stop = chip->timeout_d;
306 /* Note: STS is little endian */
307 addr = TPM_STS(chip->locality) + 1;
308 if (tpm_tis_i2c_read(dev, addr, buf, 3) < 0)
311 burstcnt = (buf[2] << 16) + (buf[1] << 8) + buf[0];
315 mdelay(TPM_TIMEOUT_MS);
316 } while (get_timer(start) < stop);
321 static int tpm_tis_i2c_wait_for_stat(struct udevice *dev, u8 mask,
322 unsigned long timeout, int *status)
324 unsigned long start, stop;
326 /* Check current status */
327 *status = tpm_tis_i2c_status(dev);
328 if ((*status & mask) == mask)
331 start = get_timer(0);
334 mdelay(TPM_TIMEOUT_MS);
335 *status = tpm_tis_i2c_status(dev);
336 if ((*status & mask) == mask)
338 } while (get_timer(start) < stop);
343 static int tpm_tis_i2c_recv_data(struct udevice *dev, u8 *buf, size_t count)
345 struct tpm_chip *chip = dev_get_priv(dev);
350 while (size < count) {
351 burstcnt = tpm_tis_i2c_get_burstcount(dev);
353 /* burstcount < 0 -> tpm is busy */
357 /* Limit received data to max left */
358 if (burstcnt > (count - size))
359 burstcnt = count - size;
361 rc = tpm_tis_i2c_read(dev, TPM_DATA_FIFO(chip->locality),
362 &(buf[size]), burstcnt);
370 static int tpm_tis_i2c_recv(struct udevice *dev, u8 *buf, size_t count)
372 struct tpm_chip *chip = dev_get_priv(dev);
375 unsigned int expected;
378 status = tpm_tis_i2c_status(dev);
379 if (status == TPM_STS_COMMAND_READY)
381 if ((status & (TPM_STS_DATA_AVAIL | TPM_STS_VALID)) !=
382 (TPM_STS_DATA_AVAIL | TPM_STS_VALID))
385 debug("...got it;\n");
387 /* Read first 10 bytes, including tag, paramsize, and result */
388 size = tpm_tis_i2c_recv_data(dev, buf, TPM_HEADER_SIZE);
389 if (size < TPM_HEADER_SIZE) {
390 debug("Unable to read header\n");
391 return size < 0 ? size : -EIO;
394 expected = get_unaligned_be32(buf + TPM_RSP_SIZE_BYTE);
395 if ((size_t)expected > count || (size_t)expected < TPM_HEADER_SIZE) {
396 debug("Error size=%x, expected=%x, count=%x\n", size, expected,
401 size += tpm_tis_i2c_recv_data(dev, &buf[TPM_HEADER_SIZE],
402 expected - TPM_HEADER_SIZE);
403 if (size < expected) {
404 debug("Unable to read remainder of result\n");
408 rc = tpm_tis_i2c_wait_for_stat(dev, TPM_STS_VALID, chip->timeout_c,
412 if (status & TPM_STS_DATA_AVAIL) { /* Retry? */
413 debug("Error left over data\n");
420 static int tpm_tis_i2c_send(struct udevice *dev, const u8 *buf, size_t len)
422 struct tpm_chip *chip = dev_get_priv(dev);
429 debug("%s: len=%d\n", __func__, len);
430 if (len > TPM_DEV_BUFSIZE)
431 return -E2BIG; /* Command is too long for our tpm, sorry */
433 if (tpm_tis_i2c_request_locality(dev, 0) < 0)
436 status = tpm_tis_i2c_status(dev);
437 if ((status & TPM_STS_COMMAND_READY) == 0) {
438 rc = tpm_tis_i2c_ready(dev);
441 rc = tpm_tis_i2c_wait_for_stat(dev, TPM_STS_COMMAND_READY,
442 chip->timeout_b, &status);
447 burstcnt = tpm_tis_i2c_get_burstcount(dev);
449 /* burstcount < 0 -> tpm is busy */
453 while (count < len) {
455 if (burstcnt > len - count)
456 burstcnt = len - count;
458 #ifdef CONFIG_TPM_TIS_I2C_BURST_LIMITATION
459 if (retry && burstcnt > CONFIG_TPM_TIS_I2C_BURST_LIMITATION_LEN)
460 burstcnt = CONFIG_TPM_TIS_I2C_BURST_LIMITATION_LEN;
461 #endif /* CONFIG_TPM_TIS_I2C_BURST_LIMITATION */
463 rc = tpm_tis_i2c_write(dev, TPM_DATA_FIFO(chip->locality),
464 &(buf[count]), burstcnt);
468 debug("%s: error\n", __func__);
471 rc = tpm_tis_i2c_wait_for_stat(dev, TPM_STS_VALID,
477 if ((status & TPM_STS_DATA_EXPECT) == 0)
483 rc = tpm_tis_i2c_write(dev, TPM_STS(chip->locality), &sts, 1);
486 debug("%s: done, rc=%d\n", __func__, rc);
491 static int tpm_tis_i2c_cleanup(struct udevice *dev)
493 struct tpm_chip *chip = dev_get_priv(dev);
495 tpm_tis_i2c_ready(dev);
497 * The TPM needs some time to clean up here,
498 * so we sleep rather than keeping the bus busy
501 tpm_tis_i2c_release_locality(dev, chip->locality, 0);
506 static int tpm_tis_i2c_init(struct udevice *dev)
508 struct tpm_chip *chip = dev_get_priv(dev);
510 u32 expected_did_vid;
515 /* Default timeouts - these could move to the device tree */
516 chip->timeout_a = TIS_SHORT_TIMEOUT_MS;
517 chip->timeout_b = TIS_LONG_TIMEOUT_MS;
518 chip->timeout_c = TIS_SHORT_TIMEOUT_MS;
519 chip->timeout_d = TIS_SHORT_TIMEOUT_MS;
521 rc = tpm_tis_i2c_request_locality(dev, 0);
525 /* Read four bytes from DID_VID register */
526 if (tpm_tis_i2c_read(dev, TPM_DID_VID(0), (uchar *)&vendor, 4) < 0) {
527 tpm_tis_i2c_release_locality(dev, 0, 1);
531 if (chip->chip_type == SLB9635) {
532 vendor = be32_to_cpu(vendor);
533 expected_did_vid = TPM_TIS_I2C_DID_VID_9635;
535 /* device id and byte order has changed for newer i2c tpms */
536 expected_did_vid = TPM_TIS_I2C_DID_VID_9645;
539 if (chip->chip_type != UNKNOWN && vendor != expected_did_vid) {
540 pr_err("Vendor id did not match! ID was %08x\n", vendor);
544 chip->vend_dev = vendor;
545 debug("1.2 TPM (chip type %s device-id 0x%X)\n",
546 chip_name[chip->chip_type], vendor >> 16);
549 * A timeout query to TPM can be placed here.
550 * Standard timeout values are used so far
556 static int tpm_tis_i2c_open(struct udevice *dev)
558 struct tpm_chip *chip = dev_get_priv(dev);
561 debug("%s: start\n", __func__);
564 rc = tpm_tis_i2c_init(dev);
571 static int tpm_tis_i2c_close(struct udevice *dev)
573 struct tpm_chip *chip = dev_get_priv(dev);
576 tpm_tis_i2c_release_locality(dev, chip->locality, 1);
584 static int tpm_tis_get_desc(struct udevice *dev, char *buf, int size)
586 struct tpm_chip *chip = dev_get_priv(dev);
591 return snprintf(buf, size, "1.2 TPM (%s, chip type %s device-id 0x%x)",
592 chip->is_open ? "open" : "closed",
593 chip_name[chip->chip_type],
594 chip->vend_dev >> 16);
597 static int tpm_tis_i2c_probe(struct udevice *dev)
599 struct tpm_chip_priv *uc_priv = dev_get_uclass_priv(dev);
600 struct tpm_chip *chip = dev_get_priv(dev);
602 chip->chip_type = dev_get_driver_data(dev);
604 /* TODO: These need to be checked and tuned */
605 uc_priv->duration_ms[TPM_SHORT] = TIS_SHORT_TIMEOUT_MS;
606 uc_priv->duration_ms[TPM_MEDIUM] = TIS_LONG_TIMEOUT_MS;
607 uc_priv->duration_ms[TPM_LONG] = TIS_LONG_TIMEOUT_MS;
608 uc_priv->retry_time_ms = TPM_TIMEOUT_MS;
613 static const struct tpm_ops tpm_tis_i2c_ops = {
614 .open = tpm_tis_i2c_open,
615 .close = tpm_tis_i2c_close,
616 .get_desc = tpm_tis_get_desc,
617 .send = tpm_tis_i2c_send,
618 .recv = tpm_tis_i2c_recv,
619 .cleanup = tpm_tis_i2c_cleanup,
622 static const struct udevice_id tpm_tis_i2c_ids[] = {
623 { .compatible = "infineon,slb9635tt", .data = SLB9635 },
624 { .compatible = "infineon,slb9645tt", .data = SLB9645 },
628 U_BOOT_DRIVER(tpm_tis_i2c) = {
629 .name = "tpm_tis_infineon",
631 .of_match = tpm_tis_i2c_ids,
632 .ops = &tpm_tis_i2c_ops,
633 .probe = tpm_tis_i2c_probe,
634 .priv_auto_alloc_size = sizeof(struct tpm_chip),