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 * See file CREDITS for list of people who contributed to this
23 * This program is free software; you can redistribute it and/or
24 * modify it under the terms of the GNU General Public License as
25 * published by the Free Software Foundation, version 2 of the
28 * This program is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 * GNU General Public License for more details.
33 * You should have received a copy of the GNU General Public License
34 * along with this program; if not, write to the Free Software
35 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
42 #include <linux/compiler.h>
45 #include <asm-generic/errno.h>
46 #include <linux/types.h>
47 #include <linux/unaligned/be_byteshift.h>
49 #include "tpm_private.h"
51 DECLARE_GLOBAL_DATA_PTR;
53 /* Max buffer size supported by our tpm */
54 #define TPM_DEV_BUFSIZE 1260
56 /* Max number of iterations after i2c NAK */
60 * Max number of iterations after i2c NAK for 'long' commands
62 * We need this especially for sending TPM_READY, since the cleanup after the
63 * transtion to the ready state may take some time, but it is unpredictable
64 * how long it will take.
66 #define MAX_COUNT_LONG 50
68 #define SLEEP_DURATION 60 /* in usec */
69 #define SLEEP_DURATION_LONG 210 /* in usec */
71 #define TPM_HEADER_SIZE 10
74 TPM_ACCESS_VALID = 0x80,
75 TPM_ACCESS_ACTIVE_LOCALITY = 0x20,
76 TPM_ACCESS_REQUEST_PENDING = 0x04,
77 TPM_ACCESS_REQUEST_USE = 0x02,
82 TPM_STS_COMMAND_READY = 0x40,
84 TPM_STS_DATA_AVAIL = 0x10,
85 TPM_STS_DATA_EXPECT = 0x08,
89 TIS_SHORT_TIMEOUT = 750, /* ms */
90 TIS_LONG_TIMEOUT = 2000, /* ms */
93 /* expected value for DIDVID register */
94 #define TPM_TIS_I2C_DID_VID_9635 0x000b15d1L
95 #define TPM_TIS_I2C_DID_VID_9645 0x001a15d1L
103 static const char * const chip_name[] = {
104 [SLB9635] = "slb9635tt",
105 [SLB9645] = "slb9645tt",
106 [UNKNOWN] = "unknown/fallback to slb9635",
109 #define TPM_ACCESS(l) (0x0000 | ((l) << 4))
110 #define TPM_STS(l) (0x0001 | ((l) << 4))
111 #define TPM_DATA_FIFO(l) (0x0005 | ((l) << 4))
112 #define TPM_DID_VID(l) (0x0006 | ((l) << 4))
114 /* Structure to store I2C TPM specific stuff */
117 u8 buf[TPM_DEV_BUFSIZE + sizeof(u8)]; /* Max buffer size + addr */
118 enum i2c_chip_type chip_type;
121 static struct tpm_dev tpm_dev;
124 * iic_tpm_read() - read from TPM register
125 * @addr: register address to read from
126 * @buffer: provided by caller
127 * @len: number of bytes to read
129 * Read len bytes from TPM register and put them into
130 * buffer (little-endian format, i.e. first byte is put into buffer[0]).
132 * NOTE: TPM is big-endian for multi-byte values. Multi-byte
133 * values have to be swapped.
135 * Return -EIO on error, 0 on success.
137 static int iic_tpm_read(u8 addr, u8 *buffer, size_t len)
141 uint32_t addrbuf = addr;
143 if ((tpm_dev.chip_type == SLB9635) || (tpm_dev.chip_type == UNKNOWN)) {
144 /* slb9635 protocol should work in both cases */
145 for (count = 0; count < MAX_COUNT; count++) {
146 rc = dm_i2c_write(tpm_dev.dev, 0, (uchar *)&addrbuf, 1);
148 break; /* Success, break to skip sleep */
149 udelay(SLEEP_DURATION);
154 /* After the TPM has successfully received the register address
155 * it needs some time, thus we're sleeping here again, before
156 * retrieving the data
158 for (count = 0; count < MAX_COUNT; count++) {
159 udelay(SLEEP_DURATION);
160 rc = dm_i2c_read(tpm_dev.dev, 0, buffer, len);
162 break; /* success, break to skip sleep */
166 * Use a combined read for newer chips.
167 * Unfortunately the smbus functions are not suitable due to
168 * the 32 byte limit of the smbus.
169 * Retries should usually not be needed, but are kept just to
170 * be safe on the safe side.
172 for (count = 0; count < MAX_COUNT; count++) {
173 rc = dm_i2c_read(tpm_dev.dev, addr, buffer, len);
175 break; /* break here to skip sleep */
176 udelay(SLEEP_DURATION);
180 /* Take care of 'guard time' */
181 udelay(SLEEP_DURATION);
188 static int iic_tpm_write_generic(u8 addr, u8 *buffer, size_t len,
189 unsigned int sleep_time, u8 max_count)
194 for (count = 0; count < max_count; count++) {
195 rc = dm_i2c_write(tpm_dev.dev, addr, buffer, len);
197 break; /* Success, break to skip sleep */
201 /* take care of 'guard time' */
210 * iic_tpm_write() - write to TPM register
211 * @addr: register address to write to
212 * @buffer: containing data to be written
213 * @len: number of bytes to write
215 * Write len bytes from provided buffer to TPM register (little
216 * endian format, i.e. buffer[0] is written as first byte).
218 * NOTE: TPM is big-endian for multi-byte values. Multi-byte
219 * values have to be swapped.
221 * NOTE: use this function instead of the iic_tpm_write_generic function.
223 * Return -EIO on error, 0 on success
225 static int iic_tpm_write(u8 addr, u8 *buffer, size_t len)
227 return iic_tpm_write_generic(addr, buffer, len, SLEEP_DURATION,
232 * This function is needed especially for the cleanup situation after
235 static int iic_tpm_write_long(u8 addr, u8 *buffer, size_t len)
237 return iic_tpm_write_generic(addr, buffer, len, SLEEP_DURATION_LONG,
241 static int check_locality(struct tpm_chip *chip, int loc)
243 const u8 mask = TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID;
247 rc = iic_tpm_read(TPM_ACCESS(loc), &buf, 1);
251 if ((buf & mask) == mask) {
252 chip->vendor.locality = loc;
259 static void release_locality(struct tpm_chip *chip, int loc, int force)
261 const u8 mask = TPM_ACCESS_REQUEST_PENDING | TPM_ACCESS_VALID;
264 if (iic_tpm_read(TPM_ACCESS(loc), &buf, 1) < 0)
267 if (force || (buf & mask) == mask) {
268 buf = TPM_ACCESS_ACTIVE_LOCALITY;
269 iic_tpm_write(TPM_ACCESS(loc), &buf, 1);
273 static int request_locality(struct tpm_chip *chip, int loc)
275 unsigned long start, stop;
276 u8 buf = TPM_ACCESS_REQUEST_USE;
279 if (check_locality(chip, loc) >= 0)
280 return loc; /* We already have the locality */
282 rc = iic_tpm_write(TPM_ACCESS(loc), &buf, 1);
286 /* Wait for burstcount */
287 start = get_timer(0);
288 stop = chip->vendor.timeout_a;
290 if (check_locality(chip, loc) >= 0)
292 udelay(TPM_TIMEOUT * 1000);
293 } while (get_timer(start) < stop);
298 static u8 tpm_tis_i2c_status(struct tpm_chip *chip)
300 /* NOTE: Since i2c read may fail, return 0 in this case --> time-out */
303 if (iic_tpm_read(TPM_STS(chip->vendor.locality), &buf, 1) < 0)
309 static void tpm_tis_i2c_ready(struct tpm_chip *chip)
313 /* This causes the current command to be aborted */
314 u8 buf = TPM_STS_COMMAND_READY;
316 debug("%s\n", __func__);
317 rc = iic_tpm_write_long(TPM_STS(chip->vendor.locality), &buf, 1);
319 debug("%s: rc=%d\n", __func__, rc);
322 static ssize_t get_burstcount(struct tpm_chip *chip)
324 unsigned long start, stop;
328 /* Wait for burstcount */
329 /* XXX: Which timeout value? Spec has 2 answers (c & d) */
330 start = get_timer(0);
331 stop = chip->vendor.timeout_d;
333 /* Note: STS is little endian */
334 addr = TPM_STS(chip->vendor.locality) + 1;
335 if (iic_tpm_read(addr, buf, 3) < 0)
338 burstcnt = (buf[2] << 16) + (buf[1] << 8) + buf[0];
342 udelay(TPM_TIMEOUT * 1000);
343 } while (get_timer(start) < stop);
348 static int wait_for_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout,
351 unsigned long start, stop;
353 /* Check current status */
354 *status = tpm_tis_i2c_status(chip);
355 if ((*status & mask) == mask)
358 start = get_timer(0);
361 udelay(TPM_TIMEOUT * 1000);
362 *status = tpm_tis_i2c_status(chip);
363 if ((*status & mask) == mask)
365 } while (get_timer(start) < stop);
370 static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count)
376 while (size < count) {
377 burstcnt = get_burstcount(chip);
379 /* burstcount < 0 -> tpm is busy */
383 /* Limit received data to max left */
384 if (burstcnt > (count - size))
385 burstcnt = count - size;
387 rc = iic_tpm_read(TPM_DATA_FIFO(chip->vendor.locality),
388 &(buf[size]), burstcnt);
396 static int tpm_tis_i2c_recv(struct tpm_chip *chip, u8 *buf, size_t count)
399 int expected, status;
401 if (count < TPM_HEADER_SIZE) {
406 /* Read first 10 bytes, including tag, paramsize, and result */
407 size = recv_data(chip, buf, TPM_HEADER_SIZE);
408 if (size < TPM_HEADER_SIZE) {
409 error("Unable to read header\n");
413 expected = get_unaligned_be32(buf + TPM_RSP_SIZE_BYTE);
414 if ((size_t)expected > count) {
415 error("Error size=%x, expected=%x, count=%x\n", size, expected,
421 size += recv_data(chip, &buf[TPM_HEADER_SIZE],
422 expected - TPM_HEADER_SIZE);
423 if (size < expected) {
424 error("Unable to read remainder of result\n");
429 wait_for_stat(chip, TPM_STS_VALID, chip->vendor.timeout_c, &status);
430 if (status & TPM_STS_DATA_AVAIL) { /* Retry? */
431 error("Error left over data\n");
437 tpm_tis_i2c_ready(chip);
439 * The TPM needs some time to clean up here,
440 * so we sleep rather than keeping the bus busy
443 release_locality(chip, chip->vendor.locality, 0);
448 static int tpm_tis_i2c_send(struct tpm_chip *chip, u8 *buf, size_t len)
456 debug("%s: len=%d\n", __func__, len);
457 if (len > TPM_DEV_BUFSIZE)
458 return -E2BIG; /* Command is too long for our tpm, sorry */
460 if (request_locality(chip, 0) < 0)
463 status = tpm_tis_i2c_status(chip);
464 if ((status & TPM_STS_COMMAND_READY) == 0) {
465 tpm_tis_i2c_ready(chip);
466 if (wait_for_stat(chip, TPM_STS_COMMAND_READY,
467 chip->vendor.timeout_b, &status) < 0) {
473 burstcnt = get_burstcount(chip);
475 /* burstcount < 0 -> tpm is busy */
479 while (count < len) {
481 if (burstcnt > len - count)
482 burstcnt = len - count;
484 #ifdef CONFIG_TPM_TIS_I2C_BURST_LIMITATION
485 if (retry && burstcnt > CONFIG_TPM_TIS_I2C_BURST_LIMITATION)
486 burstcnt = CONFIG_TPM_TIS_I2C_BURST_LIMITATION;
487 #endif /* CONFIG_TPM_TIS_I2C_BURST_LIMITATION */
489 rc = iic_tpm_write(TPM_DATA_FIFO(chip->vendor.locality),
490 &(buf[count]), burstcnt);
494 debug("%s: error\n", __func__);
499 rc = wait_for_stat(chip, TPM_STS_VALID,
500 chip->vendor.timeout_c, &status);
504 if ((status & TPM_STS_DATA_EXPECT) == 0) {
512 iic_tpm_write(TPM_STS(chip->vendor.locality), &sts, 1);
518 debug("%s: out_err\n", __func__);
519 tpm_tis_i2c_ready(chip);
521 * The TPM needs some time to clean up here,
522 * so we sleep rather than keeping the bus busy
525 release_locality(chip, chip->vendor.locality, 0);
530 static struct tpm_vendor_specific tpm_tis_i2c = {
531 .status = tpm_tis_i2c_status,
532 .recv = tpm_tis_i2c_recv,
533 .send = tpm_tis_i2c_send,
534 .cancel = tpm_tis_i2c_ready,
535 .req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
536 .req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
537 .req_canceled = TPM_STS_COMMAND_READY,
541 static enum i2c_chip_type tpm_vendor_chip_type(void)
543 #if CONFIG_IS_ENABLED(OF_CONTROL)
544 const void *blob = gd->fdt_blob;
546 if (fdtdec_next_compatible(blob, 0, COMPAT_INFINEON_SLB9645_TPM) >= 0)
549 if (fdtdec_next_compatible(blob, 0, COMPAT_INFINEON_SLB9635_TPM) >= 0)
555 int tpm_vendor_init(struct udevice *dev)
557 struct tpm_chip *chip;
559 u32 expected_did_vid;
562 tpm_dev.chip_type = tpm_vendor_chip_type();
564 chip = tpm_register_hardware(&tpm_tis_i2c);
568 /* Disable interrupts (not supported) */
569 chip->vendor.irq = 0;
571 /* Default timeouts */
572 chip->vendor.timeout_a = TIS_SHORT_TIMEOUT;
573 chip->vendor.timeout_b = TIS_LONG_TIMEOUT;
574 chip->vendor.timeout_c = TIS_SHORT_TIMEOUT;
575 chip->vendor.timeout_d = TIS_SHORT_TIMEOUT;
577 if (request_locality(chip, 0) < 0)
580 /* Read four bytes from DID_VID register */
581 if (iic_tpm_read(TPM_DID_VID(0), (uchar *)&vendor, 4) < 0) {
582 release_locality(chip, 0, 1);
586 if (tpm_dev.chip_type == SLB9635) {
587 vendor = be32_to_cpu(vendor);
588 expected_did_vid = TPM_TIS_I2C_DID_VID_9635;
590 /* device id and byte order has changed for newer i2c tpms */
591 expected_did_vid = TPM_TIS_I2C_DID_VID_9645;
594 if (tpm_dev.chip_type != UNKNOWN && vendor != expected_did_vid) {
595 error("Vendor id did not match! ID was %08x\n", vendor);
599 debug("1.2 TPM (chip type %s device-id 0x%X)\n",
600 chip_name[tpm_dev.chip_type], vendor >> 16);
603 * A timeout query to TPM can be placed here.
604 * Standard timeout values are used so far
610 void tpm_vendor_cleanup(struct tpm_chip *chip)
612 release_locality(chip, chip->vendor.locality, 1);