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_private.h"
35 DECLARE_GLOBAL_DATA_PTR;
37 /* Max buffer size supported by our tpm */
38 #define TPM_DEV_BUFSIZE 1260
40 /* Max number of iterations after i2c NAK */
44 * Max number of iterations after i2c NAK for 'long' commands
46 * We need this especially for sending TPM_READY, since the cleanup after the
47 * transtion to the ready state may take some time, but it is unpredictable
48 * how long it will take.
50 #define MAX_COUNT_LONG 50
52 #define SLEEP_DURATION 60 /* in usec */
53 #define SLEEP_DURATION_LONG 210 /* in usec */
55 #define TPM_HEADER_SIZE 10
58 TPM_ACCESS_VALID = 0x80,
59 TPM_ACCESS_ACTIVE_LOCALITY = 0x20,
60 TPM_ACCESS_REQUEST_PENDING = 0x04,
61 TPM_ACCESS_REQUEST_USE = 0x02,
66 TPM_STS_COMMAND_READY = 0x40,
68 TPM_STS_DATA_AVAIL = 0x10,
69 TPM_STS_DATA_EXPECT = 0x08,
73 TIS_SHORT_TIMEOUT = 750, /* ms */
74 TIS_LONG_TIMEOUT = 2000, /* ms */
77 /* expected value for DIDVID register */
78 #define TPM_TIS_I2C_DID_VID_9635 0x000b15d1L
79 #define TPM_TIS_I2C_DID_VID_9645 0x001a15d1L
87 static const char * const chip_name[] = {
88 [SLB9635] = "slb9635tt",
89 [SLB9645] = "slb9645tt",
90 [UNKNOWN] = "unknown/fallback to slb9635",
93 #define TPM_ACCESS(l) (0x0000 | ((l) << 4))
94 #define TPM_STS(l) (0x0001 | ((l) << 4))
95 #define TPM_DATA_FIFO(l) (0x0005 | ((l) << 4))
96 #define TPM_DID_VID(l) (0x0006 | ((l) << 4))
98 /* Structure to store I2C TPM specific stuff */
101 u8 buf[TPM_DEV_BUFSIZE + sizeof(u8)]; /* Max buffer size + addr */
102 enum i2c_chip_type chip_type;
105 static struct tpm_dev tpm_dev;
108 * iic_tpm_read() - read from TPM register
109 * @addr: register address to read from
110 * @buffer: provided by caller
111 * @len: number of bytes to read
113 * Read len bytes from TPM register and put them into
114 * buffer (little-endian format, i.e. first byte is put into buffer[0]).
116 * NOTE: TPM is big-endian for multi-byte values. Multi-byte
117 * values have to be swapped.
119 * Return -EIO on error, 0 on success.
121 static int iic_tpm_read(u8 addr, u8 *buffer, size_t len)
125 uint32_t addrbuf = addr;
127 if ((tpm_dev.chip_type == SLB9635) || (tpm_dev.chip_type == UNKNOWN)) {
128 /* slb9635 protocol should work in both cases */
129 for (count = 0; count < MAX_COUNT; count++) {
130 rc = dm_i2c_write(tpm_dev.dev, 0, (uchar *)&addrbuf, 1);
132 break; /* Success, break to skip sleep */
133 udelay(SLEEP_DURATION);
138 /* After the TPM has successfully received the register address
139 * it needs some time, thus we're sleeping here again, before
140 * retrieving the data
142 for (count = 0; count < MAX_COUNT; count++) {
143 udelay(SLEEP_DURATION);
144 rc = dm_i2c_read(tpm_dev.dev, 0, buffer, len);
146 break; /* success, break to skip sleep */
150 * Use a combined read for newer chips.
151 * Unfortunately the smbus functions are not suitable due to
152 * the 32 byte limit of the smbus.
153 * Retries should usually not be needed, but are kept just to
154 * be safe on the safe side.
156 for (count = 0; count < MAX_COUNT; count++) {
157 rc = dm_i2c_read(tpm_dev.dev, addr, buffer, len);
159 break; /* break here to skip sleep */
160 udelay(SLEEP_DURATION);
164 /* Take care of 'guard time' */
165 udelay(SLEEP_DURATION);
172 static int iic_tpm_write_generic(u8 addr, u8 *buffer, size_t len,
173 unsigned int sleep_time, u8 max_count)
178 for (count = 0; count < max_count; count++) {
179 rc = dm_i2c_write(tpm_dev.dev, addr, buffer, len);
181 break; /* Success, break to skip sleep */
185 /* take care of 'guard time' */
194 * iic_tpm_write() - write to TPM register
195 * @addr: register address to write to
196 * @buffer: containing data to be written
197 * @len: number of bytes to write
199 * Write len bytes from provided buffer to TPM register (little
200 * endian format, i.e. buffer[0] is written as first byte).
202 * NOTE: TPM is big-endian for multi-byte values. Multi-byte
203 * values have to be swapped.
205 * NOTE: use this function instead of the iic_tpm_write_generic function.
207 * Return -EIO on error, 0 on success
209 static int iic_tpm_write(u8 addr, u8 *buffer, size_t len)
211 return iic_tpm_write_generic(addr, buffer, len, SLEEP_DURATION,
216 * This function is needed especially for the cleanup situation after
219 static int iic_tpm_write_long(u8 addr, u8 *buffer, size_t len)
221 return iic_tpm_write_generic(addr, buffer, len, SLEEP_DURATION_LONG,
225 static int check_locality(struct tpm_chip *chip, int loc)
227 const u8 mask = TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID;
231 rc = iic_tpm_read(TPM_ACCESS(loc), &buf, 1);
235 if ((buf & mask) == mask) {
236 chip->vendor.locality = loc;
243 static void release_locality(struct tpm_chip *chip, int loc, int force)
245 const u8 mask = TPM_ACCESS_REQUEST_PENDING | TPM_ACCESS_VALID;
248 if (iic_tpm_read(TPM_ACCESS(loc), &buf, 1) < 0)
251 if (force || (buf & mask) == mask) {
252 buf = TPM_ACCESS_ACTIVE_LOCALITY;
253 iic_tpm_write(TPM_ACCESS(loc), &buf, 1);
257 static int request_locality(struct tpm_chip *chip, int loc)
259 unsigned long start, stop;
260 u8 buf = TPM_ACCESS_REQUEST_USE;
263 if (check_locality(chip, loc) >= 0)
264 return loc; /* We already have the locality */
266 rc = iic_tpm_write(TPM_ACCESS(loc), &buf, 1);
270 /* Wait for burstcount */
271 start = get_timer(0);
272 stop = chip->vendor.timeout_a;
274 if (check_locality(chip, loc) >= 0)
276 udelay(TPM_TIMEOUT * 1000);
277 } while (get_timer(start) < stop);
282 static u8 tpm_tis_i2c_status(struct tpm_chip *chip)
284 /* NOTE: Since i2c read may fail, return 0 in this case --> time-out */
287 if (iic_tpm_read(TPM_STS(chip->vendor.locality), &buf, 1) < 0)
293 static void tpm_tis_i2c_ready(struct tpm_chip *chip)
297 /* This causes the current command to be aborted */
298 u8 buf = TPM_STS_COMMAND_READY;
300 debug("%s\n", __func__);
301 rc = iic_tpm_write_long(TPM_STS(chip->vendor.locality), &buf, 1);
303 debug("%s: rc=%d\n", __func__, rc);
306 static ssize_t get_burstcount(struct tpm_chip *chip)
308 unsigned long start, stop;
312 /* Wait for burstcount */
313 /* XXX: Which timeout value? Spec has 2 answers (c & d) */
314 start = get_timer(0);
315 stop = chip->vendor.timeout_d;
317 /* Note: STS is little endian */
318 addr = TPM_STS(chip->vendor.locality) + 1;
319 if (iic_tpm_read(addr, buf, 3) < 0)
322 burstcnt = (buf[2] << 16) + (buf[1] << 8) + buf[0];
326 udelay(TPM_TIMEOUT * 1000);
327 } while (get_timer(start) < stop);
332 static int wait_for_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout,
335 unsigned long start, stop;
337 /* Check current status */
338 *status = tpm_tis_i2c_status(chip);
339 if ((*status & mask) == mask)
342 start = get_timer(0);
345 udelay(TPM_TIMEOUT * 1000);
346 *status = tpm_tis_i2c_status(chip);
347 if ((*status & mask) == mask)
349 } while (get_timer(start) < stop);
354 static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count)
360 while (size < count) {
361 burstcnt = get_burstcount(chip);
363 /* burstcount < 0 -> tpm is busy */
367 /* Limit received data to max left */
368 if (burstcnt > (count - size))
369 burstcnt = count - size;
371 rc = iic_tpm_read(TPM_DATA_FIFO(chip->vendor.locality),
372 &(buf[size]), burstcnt);
380 static int tpm_tis_i2c_recv(struct tpm_chip *chip, u8 *buf, size_t count)
383 int expected, status;
385 if (count < TPM_HEADER_SIZE) {
390 /* Read first 10 bytes, including tag, paramsize, and result */
391 size = recv_data(chip, buf, TPM_HEADER_SIZE);
392 if (size < TPM_HEADER_SIZE) {
393 error("Unable to read header\n");
397 expected = get_unaligned_be32(buf + TPM_RSP_SIZE_BYTE);
398 if ((size_t)expected > count) {
399 error("Error size=%x, expected=%x, count=%x\n", size, expected,
405 size += recv_data(chip, &buf[TPM_HEADER_SIZE],
406 expected - TPM_HEADER_SIZE);
407 if (size < expected) {
408 error("Unable to read remainder of result\n");
413 wait_for_stat(chip, TPM_STS_VALID, chip->vendor.timeout_c, &status);
414 if (status & TPM_STS_DATA_AVAIL) { /* Retry? */
415 error("Error left over data\n");
421 tpm_tis_i2c_ready(chip);
423 * The TPM needs some time to clean up here,
424 * so we sleep rather than keeping the bus busy
427 release_locality(chip, chip->vendor.locality, 0);
432 static int tpm_tis_i2c_send(struct tpm_chip *chip, u8 *buf, size_t len)
440 debug("%s: len=%d\n", __func__, len);
441 if (len > TPM_DEV_BUFSIZE)
442 return -E2BIG; /* Command is too long for our tpm, sorry */
444 if (request_locality(chip, 0) < 0)
447 status = tpm_tis_i2c_status(chip);
448 if ((status & TPM_STS_COMMAND_READY) == 0) {
449 tpm_tis_i2c_ready(chip);
450 if (wait_for_stat(chip, TPM_STS_COMMAND_READY,
451 chip->vendor.timeout_b, &status) < 0) {
457 burstcnt = get_burstcount(chip);
459 /* burstcount < 0 -> tpm is busy */
463 while (count < len) {
465 if (burstcnt > len - count)
466 burstcnt = len - count;
468 #ifdef CONFIG_TPM_TIS_I2C_BURST_LIMITATION
469 if (retry && burstcnt > CONFIG_TPM_TIS_I2C_BURST_LIMITATION)
470 burstcnt = CONFIG_TPM_TIS_I2C_BURST_LIMITATION;
471 #endif /* CONFIG_TPM_TIS_I2C_BURST_LIMITATION */
473 rc = iic_tpm_write(TPM_DATA_FIFO(chip->vendor.locality),
474 &(buf[count]), burstcnt);
478 debug("%s: error\n", __func__);
483 rc = wait_for_stat(chip, TPM_STS_VALID,
484 chip->vendor.timeout_c, &status);
488 if ((status & TPM_STS_DATA_EXPECT) == 0) {
496 iic_tpm_write(TPM_STS(chip->vendor.locality), &sts, 1);
502 debug("%s: out_err\n", __func__);
503 tpm_tis_i2c_ready(chip);
505 * The TPM needs some time to clean up here,
506 * so we sleep rather than keeping the bus busy
509 release_locality(chip, chip->vendor.locality, 0);
514 static struct tpm_vendor_specific tpm_tis_i2c = {
515 .status = tpm_tis_i2c_status,
516 .recv = tpm_tis_i2c_recv,
517 .send = tpm_tis_i2c_send,
518 .cancel = tpm_tis_i2c_ready,
519 .req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
520 .req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
521 .req_canceled = TPM_STS_COMMAND_READY,
525 static enum i2c_chip_type tpm_vendor_chip_type(void)
527 #if CONFIG_IS_ENABLED(OF_CONTROL)
528 const void *blob = gd->fdt_blob;
530 if (fdtdec_next_compatible(blob, 0, COMPAT_INFINEON_SLB9645_TPM) >= 0)
533 if (fdtdec_next_compatible(blob, 0, COMPAT_INFINEON_SLB9635_TPM) >= 0)
539 int tpm_vendor_init(struct udevice *dev)
541 struct tpm_chip *chip;
543 u32 expected_did_vid;
546 tpm_dev.chip_type = tpm_vendor_chip_type();
548 chip = tpm_register_hardware(&tpm_tis_i2c);
552 /* Disable interrupts (not supported) */
553 chip->vendor.irq = 0;
555 /* Default timeouts */
556 chip->vendor.timeout_a = TIS_SHORT_TIMEOUT;
557 chip->vendor.timeout_b = TIS_LONG_TIMEOUT;
558 chip->vendor.timeout_c = TIS_SHORT_TIMEOUT;
559 chip->vendor.timeout_d = TIS_SHORT_TIMEOUT;
561 if (request_locality(chip, 0) < 0)
564 /* Read four bytes from DID_VID register */
565 if (iic_tpm_read(TPM_DID_VID(0), (uchar *)&vendor, 4) < 0) {
566 release_locality(chip, 0, 1);
570 if (tpm_dev.chip_type == SLB9635) {
571 vendor = be32_to_cpu(vendor);
572 expected_did_vid = TPM_TIS_I2C_DID_VID_9635;
574 /* device id and byte order has changed for newer i2c tpms */
575 expected_did_vid = TPM_TIS_I2C_DID_VID_9645;
578 if (tpm_dev.chip_type != UNKNOWN && vendor != expected_did_vid) {
579 error("Vendor id did not match! ID was %08x\n", vendor);
583 debug("1.2 TPM (chip type %s device-id 0x%X)\n",
584 chip_name[tpm_dev.chip_type], vendor >> 16);
587 * A timeout query to TPM can be placed here.
588 * Standard timeout values are used so far
594 void tpm_vendor_cleanup(struct tpm_chip *chip)
596 release_locality(chip, chip->vendor.locality, 1);