1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2011 Infineon Technologies
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/types.h>
32 #include <linux/unaligned/be_byteshift.h>
35 #include "tpm_internal.h"
43 /* expected value for DIDVID register */
44 #define TPM_TIS_I2C_DID_VID_9635 0x000b15d1L
45 #define TPM_TIS_I2C_DID_VID_9645 0x001a15d1L
47 static const char * const chip_name[] = {
48 [SLB9635] = "slb9635tt",
49 [SLB9645] = "slb9645tt",
50 [UNKNOWN] = "unknown/fallback to slb9635",
53 #define TPM_ACCESS(l) (0x0000 | ((l) << 4))
54 #define TPM_STS(l) (0x0001 | ((l) << 4))
55 #define TPM_DATA_FIFO(l) (0x0005 | ((l) << 4))
56 #define TPM_DID_VID(l) (0x0006 | ((l) << 4))
59 * tpm_tis_i2c_read() - read from TPM register
60 * @addr: register address to read from
61 * @buffer: provided by caller
62 * @len: number of bytes to read
64 * Read len bytes from TPM register and put them into
65 * buffer (little-endian format, i.e. first byte is put into buffer[0]).
67 * NOTE: TPM is big-endian for multi-byte values. Multi-byte
68 * values have to be swapped.
70 * Return -EIO on error, 0 on success.
72 static int tpm_tis_i2c_read(struct udevice *dev, u8 addr, u8 *buffer,
75 struct tpm_chip *chip = dev_get_priv(dev);
78 uint32_t addrbuf = addr;
80 if ((chip->chip_type == SLB9635) || (chip->chip_type == UNKNOWN)) {
81 /* slb9635 protocol should work in both cases */
82 for (count = 0; count < MAX_COUNT; count++) {
83 rc = dm_i2c_write(dev, 0, (uchar *)&addrbuf, 1);
85 break; /* Success, break to skip sleep */
86 udelay(SLEEP_DURATION_US);
91 /* After the TPM has successfully received the register address
92 * it needs some time, thus we're sleeping here again, before
95 for (count = 0; count < MAX_COUNT; count++) {
96 udelay(SLEEP_DURATION_US);
97 rc = dm_i2c_read(dev, 0, buffer, len);
99 break; /* success, break to skip sleep */
103 * Use a combined read for newer chips.
104 * Unfortunately the smbus functions are not suitable due to
105 * the 32 byte limit of the smbus.
106 * Retries should usually not be needed, but are kept just to
107 * be safe on the safe side.
109 for (count = 0; count < MAX_COUNT; count++) {
110 rc = dm_i2c_read(dev, addr, buffer, len);
112 break; /* break here to skip sleep */
113 udelay(SLEEP_DURATION_US);
117 /* Take care of 'guard time' */
118 udelay(SLEEP_DURATION_US);
125 static int tpm_tis_i2c_write_generic(struct udevice *dev, u8 addr,
126 const u8 *buffer, size_t len,
127 unsigned int sleep_time_us, u8 max_count)
129 struct tpm_chip_priv *priv = dev_get_uclass_priv(dev);
130 struct tpm_chip *chip = dev_get_priv(dev);
134 if (chip->chip_type == SLB9635) {
135 /* Prepare send buffer to include the address */
137 memcpy(&(priv->buf[1]), buffer, len);
143 for (count = 0; count < max_count; count++) {
144 rc = dm_i2c_write(dev, addr, buffer, len);
146 break; /* Success, break to skip sleep */
147 udelay(sleep_time_us);
150 /* take care of 'guard time' */
151 udelay(sleep_time_us);
159 * tpm_tis_i2c_write() - write to TPM register
160 * @addr: register address to write to
161 * @buffer: containing data to be written
162 * @len: number of bytes to write
164 * Write len bytes from provided buffer to TPM register (little
165 * endian format, i.e. buffer[0] is written as first byte).
167 * NOTE: TPM is big-endian for multi-byte values. Multi-byte
168 * values have to be swapped.
170 * NOTE: use this function instead of the tpm_tis_i2c_write_generic function.
172 * Return -EIO on error, 0 on success
174 static int tpm_tis_i2c_write(struct udevice *dev, u8 addr, const u8 *buffer,
177 return tpm_tis_i2c_write_generic(dev, addr, buffer, len,
178 SLEEP_DURATION_US, MAX_COUNT);
182 * This function is needed especially for the cleanup situation after
185 static int tpm_tis_i2c_write_long(struct udevice *dev, u8 addr, u8 *buffer,
188 return tpm_tis_i2c_write_generic(dev, addr, buffer, len,
189 SLEEP_DURATION_LONG_US,
193 static int tpm_tis_i2c_check_locality(struct udevice *dev, int loc)
195 const u8 mask = TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID;
196 struct tpm_chip *chip = dev_get_priv(dev);
200 rc = tpm_tis_i2c_read(dev, TPM_ACCESS(loc), &buf, 1);
204 if ((buf & mask) == mask) {
205 chip->locality = loc;
212 static void tpm_tis_i2c_release_locality(struct udevice *dev, int loc,
215 const u8 mask = TPM_ACCESS_REQUEST_PENDING | TPM_ACCESS_VALID;
218 if (tpm_tis_i2c_read(dev, TPM_ACCESS(loc), &buf, 1) < 0)
221 if (force || (buf & mask) == mask) {
222 buf = TPM_ACCESS_ACTIVE_LOCALITY;
223 tpm_tis_i2c_write(dev, TPM_ACCESS(loc), &buf, 1);
227 static int tpm_tis_i2c_request_locality(struct udevice *dev, int loc)
229 struct tpm_chip *chip = dev_get_priv(dev);
230 unsigned long start, stop;
231 u8 buf = TPM_ACCESS_REQUEST_USE;
234 rc = tpm_tis_i2c_check_locality(dev, loc);
236 debug("%s: Already have locality\n", __func__);
237 return loc; /* We already have the locality */
238 } else if (rc != -ENOENT) {
239 debug("%s: Failed to get locality: %d\n", __func__, rc);
243 rc = tpm_tis_i2c_write(dev, TPM_ACCESS(loc), &buf, 1);
245 debug("%s: Failed to write to TPM: %d\n", __func__, rc);
249 /* Wait for burstcount */
250 start = get_timer(0);
251 stop = chip->timeout_a;
253 rc = tpm_tis_i2c_check_locality(dev, loc);
255 debug("%s: Have locality\n", __func__);
257 } else if (rc != -ENOENT) {
258 debug("%s: Failed to get locality: %d\n", __func__, rc);
261 mdelay(TPM_TIMEOUT_MS);
262 } while (get_timer(start) < stop);
263 debug("%s: Timeout getting locality: %d\n", __func__, rc);
268 static u8 tpm_tis_i2c_status(struct udevice *dev)
270 struct tpm_chip *chip = dev_get_priv(dev);
271 /* NOTE: Since i2c read may fail, return 0 in this case --> time-out */
274 if (tpm_tis_i2c_read(dev, TPM_STS(chip->locality), &buf, 1) < 0)
280 static int tpm_tis_i2c_ready(struct udevice *dev)
282 struct tpm_chip *chip = dev_get_priv(dev);
285 /* This causes the current command to be aborted */
286 u8 buf = TPM_STS_COMMAND_READY;
288 debug("%s\n", __func__);
289 rc = tpm_tis_i2c_write_long(dev, TPM_STS(chip->locality), &buf, 1);
291 debug("%s: rc=%d\n", __func__, rc);
296 static ssize_t tpm_tis_i2c_get_burstcount(struct udevice *dev)
298 struct tpm_chip *chip = dev_get_priv(dev);
299 unsigned long start, stop;
303 /* Wait for burstcount */
304 /* XXX: Which timeout value? Spec has 2 answers (c & d) */
305 start = get_timer(0);
306 stop = chip->timeout_d;
308 /* Note: STS is little endian */
309 addr = TPM_STS(chip->locality) + 1;
310 if (tpm_tis_i2c_read(dev, addr, buf, 3) < 0)
313 burstcnt = (buf[2] << 16) + (buf[1] << 8) + buf[0];
317 mdelay(TPM_TIMEOUT_MS);
318 } while (get_timer(start) < stop);
323 static int tpm_tis_i2c_wait_for_stat(struct udevice *dev, u8 mask,
324 unsigned long timeout, int *status)
326 unsigned long start, stop;
328 /* Check current status */
329 *status = tpm_tis_i2c_status(dev);
330 if ((*status & mask) == mask)
333 start = get_timer(0);
336 mdelay(TPM_TIMEOUT_MS);
337 *status = tpm_tis_i2c_status(dev);
338 if ((*status & mask) == mask)
340 } while (get_timer(start) < stop);
345 static int tpm_tis_i2c_recv_data(struct udevice *dev, u8 *buf, size_t count)
347 struct tpm_chip *chip = dev_get_priv(dev);
352 while (size < count) {
353 burstcnt = tpm_tis_i2c_get_burstcount(dev);
355 /* burstcount < 0 -> tpm is busy */
359 /* Limit received data to max left */
360 if (burstcnt > (count - size))
361 burstcnt = count - size;
363 rc = tpm_tis_i2c_read(dev, TPM_DATA_FIFO(chip->locality),
364 &(buf[size]), burstcnt);
372 static int tpm_tis_i2c_recv(struct udevice *dev, u8 *buf, size_t count)
374 struct tpm_chip *chip = dev_get_priv(dev);
377 unsigned int expected;
380 status = tpm_tis_i2c_status(dev);
381 if (status == TPM_STS_COMMAND_READY)
383 if ((status & (TPM_STS_DATA_AVAIL | TPM_STS_VALID)) !=
384 (TPM_STS_DATA_AVAIL | TPM_STS_VALID))
387 debug("...got it;\n");
389 /* Read first 10 bytes, including tag, paramsize, and result */
390 size = tpm_tis_i2c_recv_data(dev, buf, TPM_HEADER_SIZE);
391 if (size < TPM_HEADER_SIZE) {
392 debug("Unable to read header\n");
393 return size < 0 ? size : -EIO;
396 expected = get_unaligned_be32(buf + TPM_RSP_SIZE_BYTE);
397 if ((size_t)expected > count || (size_t)expected < TPM_HEADER_SIZE) {
398 debug("Error size=%x, expected=%x, count=%x\n", size, expected,
403 size += tpm_tis_i2c_recv_data(dev, &buf[TPM_HEADER_SIZE],
404 expected - TPM_HEADER_SIZE);
405 if (size < expected) {
406 debug("Unable to read remainder of result\n");
410 rc = tpm_tis_i2c_wait_for_stat(dev, TPM_STS_VALID, chip->timeout_c,
414 if (status & TPM_STS_DATA_AVAIL) { /* Retry? */
415 debug("Error left over data\n");
422 static int tpm_tis_i2c_send(struct udevice *dev, const u8 *buf, size_t len)
424 struct tpm_chip *chip = dev_get_priv(dev);
431 debug("%s: len=%d\n", __func__, len);
432 if (len > TPM_DEV_BUFSIZE)
433 return -E2BIG; /* Command is too long for our tpm, sorry */
435 if (tpm_tis_i2c_request_locality(dev, 0) < 0)
438 status = tpm_tis_i2c_status(dev);
439 if ((status & TPM_STS_COMMAND_READY) == 0) {
440 rc = tpm_tis_i2c_ready(dev);
443 rc = tpm_tis_i2c_wait_for_stat(dev, TPM_STS_COMMAND_READY,
444 chip->timeout_b, &status);
449 burstcnt = tpm_tis_i2c_get_burstcount(dev);
451 /* burstcount < 0 -> tpm is busy */
455 while (count < len) {
457 if (burstcnt > len - count)
458 burstcnt = len - count;
460 #ifdef CONFIG_TPM_TIS_I2C_BURST_LIMITATION
461 if (retry && burstcnt > CONFIG_TPM_TIS_I2C_BURST_LIMITATION_LEN)
462 burstcnt = CONFIG_TPM_TIS_I2C_BURST_LIMITATION_LEN;
463 #endif /* CONFIG_TPM_TIS_I2C_BURST_LIMITATION */
465 rc = tpm_tis_i2c_write(dev, TPM_DATA_FIFO(chip->locality),
466 &(buf[count]), burstcnt);
470 debug("%s: error\n", __func__);
473 rc = tpm_tis_i2c_wait_for_stat(dev, TPM_STS_VALID,
479 if ((status & TPM_STS_DATA_EXPECT) == 0)
485 rc = tpm_tis_i2c_write(dev, TPM_STS(chip->locality), &sts, 1);
488 debug("%s: done, rc=%d\n", __func__, rc);
493 static int tpm_tis_i2c_cleanup(struct udevice *dev)
495 struct tpm_chip *chip = dev_get_priv(dev);
497 tpm_tis_i2c_ready(dev);
499 * The TPM needs some time to clean up here,
500 * so we sleep rather than keeping the bus busy
503 tpm_tis_i2c_release_locality(dev, chip->locality, 0);
508 static int tpm_tis_i2c_init(struct udevice *dev)
510 struct tpm_chip *chip = dev_get_priv(dev);
512 u32 expected_did_vid;
517 /* Default timeouts - these could move to the device tree */
518 chip->timeout_a = TIS_SHORT_TIMEOUT_MS;
519 chip->timeout_b = TIS_LONG_TIMEOUT_MS;
520 chip->timeout_c = TIS_SHORT_TIMEOUT_MS;
521 chip->timeout_d = TIS_SHORT_TIMEOUT_MS;
523 rc = tpm_tis_i2c_request_locality(dev, 0);
527 /* Read four bytes from DID_VID register */
528 if (tpm_tis_i2c_read(dev, TPM_DID_VID(0), (uchar *)&vendor, 4) < 0) {
529 tpm_tis_i2c_release_locality(dev, 0, 1);
533 if (chip->chip_type == SLB9635) {
534 vendor = be32_to_cpu(vendor);
535 expected_did_vid = TPM_TIS_I2C_DID_VID_9635;
537 /* device id and byte order has changed for newer i2c tpms */
538 expected_did_vid = TPM_TIS_I2C_DID_VID_9645;
541 if (chip->chip_type != UNKNOWN && vendor != expected_did_vid) {
542 pr_err("Vendor id did not match! ID was %08x\n", vendor);
546 chip->vend_dev = vendor;
547 debug("1.2 TPM (chip type %s device-id 0x%X)\n",
548 chip_name[chip->chip_type], vendor >> 16);
551 * A timeout query to TPM can be placed here.
552 * Standard timeout values are used so far
558 static int tpm_tis_i2c_open(struct udevice *dev)
560 struct tpm_chip *chip = dev_get_priv(dev);
563 debug("%s: start\n", __func__);
566 rc = tpm_tis_i2c_init(dev);
573 static int tpm_tis_i2c_close(struct udevice *dev)
575 struct tpm_chip *chip = dev_get_priv(dev);
578 tpm_tis_i2c_release_locality(dev, chip->locality, 1);
586 static int tpm_tis_get_desc(struct udevice *dev, char *buf, int size)
588 struct tpm_chip *chip = dev_get_priv(dev);
593 return snprintf(buf, size, "1.2 TPM (%s, chip type %s device-id 0x%x)",
594 chip->is_open ? "open" : "closed",
595 chip_name[chip->chip_type],
596 chip->vend_dev >> 16);
599 static int tpm_tis_i2c_probe(struct udevice *dev)
601 struct tpm_chip_priv *uc_priv = dev_get_uclass_priv(dev);
602 struct tpm_chip *chip = dev_get_priv(dev);
604 chip->chip_type = dev_get_driver_data(dev);
606 /* TODO: These need to be checked and tuned */
607 uc_priv->duration_ms[TPM_SHORT] = TIS_SHORT_TIMEOUT_MS;
608 uc_priv->duration_ms[TPM_MEDIUM] = TIS_LONG_TIMEOUT_MS;
609 uc_priv->duration_ms[TPM_LONG] = TIS_LONG_TIMEOUT_MS;
610 uc_priv->retry_time_ms = TPM_TIMEOUT_MS;
615 static const struct tpm_ops tpm_tis_i2c_ops = {
616 .open = tpm_tis_i2c_open,
617 .close = tpm_tis_i2c_close,
618 .get_desc = tpm_tis_get_desc,
619 .send = tpm_tis_i2c_send,
620 .recv = tpm_tis_i2c_recv,
621 .cleanup = tpm_tis_i2c_cleanup,
624 static const struct udevice_id tpm_tis_i2c_ids[] = {
625 { .compatible = "infineon,slb9635tt", .data = SLB9635 },
626 { .compatible = "infineon,slb9645tt", .data = SLB9645 },
630 U_BOOT_DRIVER(tpm_tis_i2c) = {
631 .name = "tpm_tis_infineon",
633 .of_match = tpm_tis_i2c_ids,
634 .ops = &tpm_tis_i2c_ops,
635 .probe = tpm_tis_i2c_probe,
636 .priv_auto_alloc_size = sizeof(struct tpm_chip),