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.
28 #include <linux/delay.h>
29 #include <linux/errno.h>
30 #include <linux/compiler.h>
31 #include <linux/printk.h>
32 #include <linux/types.h>
33 #include <linux/unaligned/be_byteshift.h>
36 #include "tpm_internal.h"
44 /* expected value for DIDVID register */
45 #define TPM_TIS_I2C_DID_VID_9635 0x000b15d1L
46 #define TPM_TIS_I2C_DID_VID_9645 0x001a15d1L
48 static const char * const chip_name[] = {
49 [SLB9635] = "slb9635tt",
50 [SLB9645] = "slb9645tt",
51 [UNKNOWN] = "unknown/fallback to slb9635",
54 #define TPM_INFINEON_ACCESS(l) (0x0000 | ((l) << 4))
55 #define TPM_INFINEON_STS(l) (0x0001 | ((l) << 4))
56 #define TPM_INFINEON_DATA_FIFO(l) (0x0005 | ((l) << 4))
57 #define TPM_INFINEON_DID_VID(l) (0x0006 | ((l) << 4))
60 * tpm_tis_i2c_read() - read from TPM register
61 * @addr: register address to read from
62 * @buffer: provided by caller
63 * @len: number of bytes to read
65 * Read len bytes from TPM register and put them into
66 * buffer (little-endian format, i.e. first byte is put into buffer[0]).
68 * NOTE: TPM is big-endian for multi-byte values. Multi-byte
69 * values have to be swapped.
71 * Return -EIO on error, 0 on success.
73 static int tpm_tis_i2c_read(struct udevice *dev, u8 addr, u8 *buffer,
76 struct tpm_chip *chip = dev_get_priv(dev);
79 uint32_t addrbuf = addr;
81 if ((chip->chip_type == SLB9635) || (chip->chip_type == UNKNOWN)) {
82 /* slb9635 protocol should work in both cases */
83 for (count = 0; count < MAX_COUNT; count++) {
84 rc = dm_i2c_write(dev, 0, (uchar *)&addrbuf, 1);
86 break; /* Success, break to skip sleep */
87 udelay(SLEEP_DURATION_US);
92 /* After the TPM has successfully received the register address
93 * it needs some time, thus we're sleeping here again, before
96 for (count = 0; count < MAX_COUNT; count++) {
97 udelay(SLEEP_DURATION_US);
98 rc = dm_i2c_read(dev, 0, buffer, len);
100 break; /* success, break to skip sleep */
104 * Use a combined read for newer chips.
105 * Unfortunately the smbus functions are not suitable due to
106 * the 32 byte limit of the smbus.
107 * Retries should usually not be needed, but are kept just to
108 * be safe on the safe side.
110 for (count = 0; count < MAX_COUNT; count++) {
111 rc = dm_i2c_read(dev, addr, buffer, len);
113 break; /* break here to skip sleep */
114 udelay(SLEEP_DURATION_US);
118 /* Take care of 'guard time' */
119 udelay(SLEEP_DURATION_US);
126 static int tpm_tis_i2c_write_generic(struct udevice *dev, u8 addr,
127 const u8 *buffer, size_t len,
128 unsigned int sleep_time_us, u8 max_count)
130 struct tpm_chip_priv *priv = dev_get_uclass_priv(dev);
131 struct tpm_chip *chip = dev_get_priv(dev);
135 if (chip->chip_type == SLB9635) {
136 /* Prepare send buffer to include the address */
138 memcpy(&(priv->buf[1]), buffer, len);
144 for (count = 0; count < max_count; count++) {
145 rc = dm_i2c_write(dev, addr, buffer, len);
147 break; /* Success, break to skip sleep */
148 udelay(sleep_time_us);
151 /* take care of 'guard time' */
152 udelay(sleep_time_us);
160 * tpm_tis_i2c_write() - write to TPM register
161 * @addr: register address to write to
162 * @buffer: containing data to be written
163 * @len: number of bytes to write
165 * Write len bytes from provided buffer to TPM register (little
166 * endian format, i.e. buffer[0] is written as first byte).
168 * NOTE: TPM is big-endian for multi-byte values. Multi-byte
169 * values have to be swapped.
171 * NOTE: use this function instead of the tpm_tis_i2c_write_generic function.
173 * Return -EIO on error, 0 on success
175 static int tpm_tis_i2c_write(struct udevice *dev, u8 addr, const u8 *buffer,
178 return tpm_tis_i2c_write_generic(dev, addr, buffer, len,
179 SLEEP_DURATION_US, MAX_COUNT);
183 * This function is needed especially for the cleanup situation after
186 static int tpm_tis_i2c_write_long(struct udevice *dev, u8 addr, u8 *buffer,
189 return tpm_tis_i2c_write_generic(dev, addr, buffer, len,
190 SLEEP_DURATION_LONG_US,
194 static int tpm_tis_i2c_check_locality(struct udevice *dev, int loc)
196 const u8 mask = TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID;
197 struct tpm_chip *chip = dev_get_priv(dev);
201 rc = tpm_tis_i2c_read(dev, TPM_INFINEON_ACCESS(loc), &buf, 1);
205 if ((buf & mask) == mask) {
206 chip->locality = loc;
213 static void tpm_tis_i2c_release_locality(struct udevice *dev, int loc,
216 const u8 mask = TPM_ACCESS_REQUEST_PENDING | TPM_ACCESS_VALID;
219 if (tpm_tis_i2c_read(dev, TPM_INFINEON_ACCESS(loc), &buf, 1) < 0)
222 if (force || (buf & mask) == mask) {
223 buf = TPM_ACCESS_ACTIVE_LOCALITY;
224 tpm_tis_i2c_write(dev, TPM_INFINEON_ACCESS(loc), &buf, 1);
228 static int tpm_tis_i2c_request_locality(struct udevice *dev, int loc)
230 struct tpm_chip *chip = dev_get_priv(dev);
231 unsigned long start, stop;
232 u8 buf = TPM_ACCESS_REQUEST_USE;
235 rc = tpm_tis_i2c_check_locality(dev, loc);
237 debug("%s: Already have locality\n", __func__);
238 return loc; /* We already have the locality */
239 } else if (rc != -ENOENT) {
240 debug("%s: Failed to get locality: %d\n", __func__, rc);
244 rc = tpm_tis_i2c_write(dev, TPM_INFINEON_ACCESS(loc), &buf, 1);
246 debug("%s: Failed to write to TPM: %d\n", __func__, rc);
250 /* Wait for burstcount */
251 start = get_timer(0);
252 stop = chip->timeout_a;
254 rc = tpm_tis_i2c_check_locality(dev, loc);
256 debug("%s: Have locality\n", __func__);
258 } else if (rc != -ENOENT) {
259 debug("%s: Failed to get locality: %d\n", __func__, rc);
262 mdelay(TPM_TIMEOUT_MS);
263 } while (get_timer(start) < stop);
264 debug("%s: Timeout getting locality: %d\n", __func__, rc);
269 static u8 tpm_tis_i2c_status(struct udevice *dev)
271 struct tpm_chip *chip = dev_get_priv(dev);
272 /* NOTE: Since i2c read may fail, return 0 in this case --> time-out */
275 if (tpm_tis_i2c_read(dev, TPM_INFINEON_STS(chip->locality), &buf, 1) < 0)
281 static int tpm_tis_i2c_ready(struct udevice *dev)
283 struct tpm_chip *chip = dev_get_priv(dev);
286 /* This causes the current command to be aborted */
287 u8 buf = TPM_STS_COMMAND_READY;
289 debug("%s\n", __func__);
290 rc = tpm_tis_i2c_write_long(dev, TPM_INFINEON_STS(chip->locality), &buf, 1);
292 debug("%s: rc=%d\n", __func__, rc);
297 static ssize_t tpm_tis_i2c_get_burstcount(struct udevice *dev)
299 struct tpm_chip *chip = dev_get_priv(dev);
300 unsigned long start, stop;
304 /* Wait for burstcount */
305 /* XXX: Which timeout value? Spec has 2 answers (c & d) */
306 start = get_timer(0);
307 stop = chip->timeout_d;
309 /* Note: STS is little endian */
310 addr = TPM_INFINEON_STS(chip->locality) + 1;
311 if (tpm_tis_i2c_read(dev, addr, buf, 3) < 0)
314 burstcnt = (buf[2] << 16) + (buf[1] << 8) + buf[0];
318 mdelay(TPM_TIMEOUT_MS);
319 } while (get_timer(start) < stop);
324 static int tpm_tis_i2c_wait_for_stat(struct udevice *dev, u8 mask,
325 unsigned long timeout, int *status)
327 unsigned long start, stop;
329 /* Check current status */
330 *status = tpm_tis_i2c_status(dev);
331 if ((*status & mask) == mask)
334 start = get_timer(0);
337 mdelay(TPM_TIMEOUT_MS);
338 *status = tpm_tis_i2c_status(dev);
339 if ((*status & mask) == mask)
341 } while (get_timer(start) < stop);
346 static int tpm_tis_i2c_recv_data(struct udevice *dev, u8 *buf, size_t count)
348 struct tpm_chip *chip = dev_get_priv(dev);
353 while (size < count) {
354 burstcnt = tpm_tis_i2c_get_burstcount(dev);
356 /* burstcount < 0 -> tpm is busy */
360 /* Limit received data to max left */
361 if (burstcnt > (count - size))
362 burstcnt = count - size;
364 rc = tpm_tis_i2c_read(dev, TPM_INFINEON_DATA_FIFO(chip->locality),
365 &(buf[size]), burstcnt);
373 static int tpm_tis_i2c_recv(struct udevice *dev, u8 *buf, size_t count)
375 struct tpm_chip *chip = dev_get_priv(dev);
378 unsigned int expected;
381 status = tpm_tis_i2c_status(dev);
382 if (status == TPM_STS_COMMAND_READY)
384 if ((status & (TPM_STS_DATA_AVAIL | TPM_STS_VALID)) !=
385 (TPM_STS_DATA_AVAIL | TPM_STS_VALID))
388 debug("...got it;\n");
390 /* Read first 10 bytes, including tag, paramsize, and result */
391 size = tpm_tis_i2c_recv_data(dev, buf, TPM_HEADER_SIZE);
392 if (size < TPM_HEADER_SIZE) {
393 debug("Unable to read header\n");
394 return size < 0 ? size : -EIO;
397 expected = get_unaligned_be32(buf + TPM_RSP_SIZE_BYTE);
398 if ((size_t)expected > count || (size_t)expected < TPM_HEADER_SIZE) {
399 debug("Error size=%x, expected=%x, count=%x\n", size, expected,
404 size += tpm_tis_i2c_recv_data(dev, &buf[TPM_HEADER_SIZE],
405 expected - TPM_HEADER_SIZE);
406 if (size < expected) {
407 debug("Unable to read remainder of result\n");
411 rc = tpm_tis_i2c_wait_for_stat(dev, TPM_STS_VALID, chip->timeout_c,
415 if (status & TPM_STS_DATA_AVAIL) { /* Retry? */
416 debug("Error left over data\n");
423 static int tpm_tis_i2c_send(struct udevice *dev, const u8 *buf, size_t len)
425 struct tpm_chip *chip = dev_get_priv(dev);
432 debug("%s: len=%d\n", __func__, len);
433 if (len > TPM_DEV_BUFSIZE)
434 return -E2BIG; /* Command is too long for our tpm, sorry */
436 if (tpm_tis_i2c_request_locality(dev, 0) < 0)
439 status = tpm_tis_i2c_status(dev);
440 if ((status & TPM_STS_COMMAND_READY) == 0) {
441 rc = tpm_tis_i2c_ready(dev);
444 rc = tpm_tis_i2c_wait_for_stat(dev, TPM_STS_COMMAND_READY,
445 chip->timeout_b, &status);
450 burstcnt = tpm_tis_i2c_get_burstcount(dev);
452 /* burstcount < 0 -> tpm is busy */
456 while (count < len) {
458 if (burstcnt > len - count)
459 burstcnt = len - count;
461 #ifdef CONFIG_TPM_TIS_I2C_BURST_LIMITATION
462 if (retry && burstcnt > CONFIG_TPM_TIS_I2C_BURST_LIMITATION_LEN)
463 burstcnt = CONFIG_TPM_TIS_I2C_BURST_LIMITATION_LEN;
464 #endif /* CONFIG_TPM_TIS_I2C_BURST_LIMITATION */
466 rc = tpm_tis_i2c_write(dev, TPM_INFINEON_DATA_FIFO(chip->locality),
467 &(buf[count]), burstcnt);
471 debug("%s: error\n", __func__);
474 rc = tpm_tis_i2c_wait_for_stat(dev, TPM_STS_VALID,
480 if ((status & TPM_STS_DATA_EXPECT) == 0)
486 rc = tpm_tis_i2c_write(dev, TPM_INFINEON_STS(chip->locality), &sts, 1);
489 debug("%s: done, rc=%d\n", __func__, rc);
494 static int tpm_tis_i2c_cleanup(struct udevice *dev)
496 struct tpm_chip *chip = dev_get_priv(dev);
498 tpm_tis_i2c_ready(dev);
500 * The TPM needs some time to clean up here,
501 * so we sleep rather than keeping the bus busy
504 tpm_tis_i2c_release_locality(dev, chip->locality, 0);
509 static int tpm_tis_i2c_init(struct udevice *dev)
511 struct tpm_chip *chip = dev_get_priv(dev);
513 u32 expected_did_vid;
518 /* Default timeouts - these could move to the device tree */
519 chip->timeout_a = TIS_SHORT_TIMEOUT_MS;
520 chip->timeout_b = TIS_LONG_TIMEOUT_MS;
521 chip->timeout_c = TIS_SHORT_TIMEOUT_MS;
522 chip->timeout_d = TIS_SHORT_TIMEOUT_MS;
524 rc = tpm_tis_i2c_request_locality(dev, 0);
528 /* Read four bytes from DID_VID register */
529 if (tpm_tis_i2c_read(dev, TPM_INFINEON_DID_VID(0), (uchar *)&vendor, 4) < 0) {
530 tpm_tis_i2c_release_locality(dev, 0, 1);
534 if (chip->chip_type == SLB9635) {
535 vendor = be32_to_cpu(vendor);
536 expected_did_vid = TPM_TIS_I2C_DID_VID_9635;
538 /* device id and byte order has changed for newer i2c tpms */
539 expected_did_vid = TPM_TIS_I2C_DID_VID_9645;
542 if (chip->chip_type != UNKNOWN && vendor != expected_did_vid) {
543 pr_err("Vendor id did not match! ID was %08x\n", vendor);
547 chip->vend_dev = vendor;
548 debug("1.2 TPM (chip type %s device-id 0x%X)\n",
549 chip_name[chip->chip_type], vendor >> 16);
552 * A timeout query to TPM can be placed here.
553 * Standard timeout values are used so far
559 static int tpm_tis_i2c_open(struct udevice *dev)
561 struct tpm_chip *chip = dev_get_priv(dev);
564 debug("%s: start\n", __func__);
567 rc = tpm_tis_i2c_init(dev);
574 static int tpm_tis_i2c_close(struct udevice *dev)
576 struct tpm_chip *chip = dev_get_priv(dev);
579 tpm_tis_i2c_release_locality(dev, chip->locality, 1);
587 static int tpm_tis_i2c_get_desc(struct udevice *dev, char *buf, int size)
589 struct tpm_chip *chip = dev_get_priv(dev);
594 return snprintf(buf, size, "1.2 TPM (%s, chip type %s device-id 0x%x)",
595 chip->is_open ? "open" : "closed",
596 chip_name[chip->chip_type],
597 chip->vend_dev >> 16);
600 static int tpm_tis_i2c_probe(struct udevice *dev)
602 struct tpm_chip_priv *uc_priv = dev_get_uclass_priv(dev);
603 struct tpm_chip *chip = dev_get_priv(dev);
605 chip->chip_type = dev_get_driver_data(dev);
607 /* TODO: These need to be checked and tuned */
608 uc_priv->duration_ms[TPM_SHORT] = TIS_SHORT_TIMEOUT_MS;
609 uc_priv->duration_ms[TPM_MEDIUM] = TIS_LONG_TIMEOUT_MS;
610 uc_priv->duration_ms[TPM_LONG] = TIS_LONG_TIMEOUT_MS;
611 uc_priv->retry_time_ms = TPM_TIMEOUT_MS;
616 static const struct tpm_ops tpm_tis_i2c_ops = {
617 .open = tpm_tis_i2c_open,
618 .close = tpm_tis_i2c_close,
619 .get_desc = tpm_tis_i2c_get_desc,
620 .send = tpm_tis_i2c_send,
621 .recv = tpm_tis_i2c_recv,
622 .cleanup = tpm_tis_i2c_cleanup,
625 static const struct udevice_id tpm_tis_i2c_ids[] = {
626 { .compatible = "infineon,slb9635tt", .data = SLB9635 },
627 { .compatible = "infineon,slb9645tt", .data = SLB9645 },
631 U_BOOT_DRIVER(tpm_tis_i2c) = {
632 .name = "tpm_tis_infineon",
634 .of_match = tpm_tis_i2c_ids,
635 .ops = &tpm_tis_i2c_ops,
636 .probe = tpm_tis_i2c_probe,
637 .priv_auto = sizeof(struct tpm_chip),