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,
41 #include <linux/types.h>
43 #include "compatibility.h"
46 /* max. buffer size supported by our tpm */
50 #define TPM_BUFSIZE 1260
51 /* Address of the TPM on the I2C bus */
52 #define TPM_I2C_ADDR 0x20
53 /* max. number of iterations after i2c NAK */
56 #define SLEEP_DURATION 60 /*in usec*/
58 /* max. number of iterations after i2c NAK for 'long' commands
59 * we need this especially for sending TPM_READY, since the cleanup after the
60 * transtion to the ready state may take some time, but it is unpredictable
61 * how long it will take.
63 #define MAX_COUNT_LONG 50
65 #define SLEEP_DURATION_LONG 210 /* in usec */
67 /* expected value for DIDVID register */
68 #define TPM_TIS_I2C_DID_VID 0x000b15d1L
70 /* Structure to store I2C TPM specific stuff */
73 u8 buf[TPM_BUFSIZE + sizeof(u8)]; /* max. buffer size + addr */
76 static struct tpm_inf_dev tpm_dev = {
81 * iic_tpm_read() - read from TPM register
82 * @addr: register address to read from
83 * @buffer: provided by caller
84 * @len: number of bytes to read
86 * Read len bytes from TPM register and put them into
87 * buffer (little-endian format, i.e. first byte is put into buffer[0]).
89 * NOTE: TPM is big-endian for multi-byte values. Multi-byte
90 * values have to be swapped.
92 * Return -EIO on error, 0 on success.
94 int iic_tpm_read(u8 addr, u8 *buffer, size_t len)
99 /* we have to use uint here, uchar hangs the board */
101 for (count = 0; count < MAX_COUNT; count++) {
102 rc = i2c_write(tpm_dev.addr, 0, 0, (uchar *)&myaddr, 1);
104 break; /*success, break to skip sleep*/
106 udelay(SLEEP_DURATION);
112 /* After the TPM has successfully received the register address it needs
113 * some time, thus we're sleeping here again, before retrieving the data
115 for (count = 0; count < MAX_COUNT; count++) {
116 udelay(SLEEP_DURATION);
117 rc = i2c_read(tpm_dev.addr, 0, 0, buffer, len);
119 break; /*success, break to skip sleep*/
128 static int iic_tpm_write_generic(u8 addr, u8 *buffer, size_t len,
129 unsigned int sleep_time,
135 /* prepare send buffer */
136 tpm_dev.buf[0] = addr;
137 memcpy(&(tpm_dev.buf[1]), buffer, len);
139 for (count = 0; count < max_count; count++) {
140 rc = i2c_write(tpm_dev.addr, 0, 0, tpm_dev.buf, len + 1);
142 break; /*success, break to skip sleep*/
154 * iic_tpm_write() - write to TPM register
155 * @addr: register address to write to
156 * @buffer: containing data to be written
157 * @len: number of bytes to write
159 * Write len bytes from provided buffer to TPM register (little
160 * endian format, i.e. buffer[0] is written as first byte).
162 * NOTE: TPM is big-endian for multi-byte values. Multi-byte
163 * values have to be swapped.
165 * NOTE: use this function instead of the iic_tpm_write_generic function.
167 * Return -EIO on error, 0 on success
169 static int iic_tpm_write(u8 addr, u8 *buffer, size_t len)
171 return iic_tpm_write_generic(addr, buffer, len, SLEEP_DURATION,
176 * This function is needed especially for the cleanup situation after
179 static int iic_tpm_write_long(u8 addr, u8 *buffer, size_t len)
181 return iic_tpm_write_generic(addr, buffer, len, SLEEP_DURATION_LONG,
185 #define TPM_HEADER_SIZE 10
188 TPM_ACCESS_VALID = 0x80,
189 TPM_ACCESS_ACTIVE_LOCALITY = 0x20,
190 TPM_ACCESS_REQUEST_PENDING = 0x04,
191 TPM_ACCESS_REQUEST_USE = 0x02,
195 TPM_STS_VALID = 0x80,
196 TPM_STS_COMMAND_READY = 0x40,
198 TPM_STS_DATA_AVAIL = 0x10,
199 TPM_STS_DATA_EXPECT = 0x08,
203 TIS_SHORT_TIMEOUT = 750, /* ms */
204 TIS_LONG_TIMEOUT = 2000, /* 2 sec */
207 #define TPM_ACCESS(l) (0x0000 | ((l) << 4))
208 #define TPM_STS(l) (0x0001 | ((l) << 4))
209 #define TPM_DATA_FIFO(l) (0x0005 | ((l) << 4))
210 #define TPM_DID_VID(l) (0x0006 | ((l) << 4))
212 static int check_locality(struct tpm_chip *chip, int loc)
217 rc = iic_tpm_read(TPM_ACCESS(loc), &buf, 1);
221 if ((buf & (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID)) ==
222 (TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID)) {
223 chip->vendor.locality = loc;
230 static void release_locality(struct tpm_chip *chip, int loc, int force)
233 if (iic_tpm_read(TPM_ACCESS(loc), &buf, 1) < 0)
236 if (force || (buf & (TPM_ACCESS_REQUEST_PENDING | TPM_ACCESS_VALID)) ==
237 (TPM_ACCESS_REQUEST_PENDING | TPM_ACCESS_VALID)) {
238 buf = TPM_ACCESS_ACTIVE_LOCALITY;
239 iic_tpm_write(TPM_ACCESS(loc), &buf, 1);
243 static int request_locality(struct tpm_chip *chip, int loc)
245 unsigned long start, stop;
246 u8 buf = TPM_ACCESS_REQUEST_USE;
248 if (check_locality(chip, loc) >= 0)
249 return loc; /* we already have the locality */
251 iic_tpm_write(TPM_ACCESS(loc), &buf, 1);
253 /* wait for burstcount */
254 start = get_timer(0);
255 stop = chip->vendor.timeout_a;
257 if (check_locality(chip, loc) >= 0)
260 } while (get_timer(start) < stop);
265 static u8 tpm_tis_i2c_status(struct tpm_chip *chip)
267 /* NOTE: since i2c read may fail, return 0 in this case --> time-out */
269 if (iic_tpm_read(TPM_STS(chip->vendor.locality), &buf, 1) < 0)
275 static void tpm_tis_i2c_ready(struct tpm_chip *chip)
277 /* this causes the current command to be aborted */
278 u8 buf = TPM_STS_COMMAND_READY;
279 iic_tpm_write_long(TPM_STS(chip->vendor.locality), &buf, 1);
282 static ssize_t get_burstcount(struct tpm_chip *chip)
284 unsigned long start, stop;
288 /* wait for burstcount */
289 /* which timeout value, spec has 2 answers (c & d) */
290 start = get_timer(0);
291 stop = chip->vendor.timeout_d;
293 /* Note: STS is little endian */
294 if (iic_tpm_read(TPM_STS(chip->vendor.locality) + 1, buf, 3)
298 burstcnt = (buf[2] << 16) + (buf[1] << 8) + buf[0];
303 } while (get_timer(start) < stop);
308 static int wait_for_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout,
311 unsigned long start, stop;
313 /* check current status */
314 *status = tpm_tis_i2c_status(chip);
315 if ((*status & mask) == mask)
318 start = get_timer(0);
322 *status = tpm_tis_i2c_status(chip);
323 if ((*status & mask) == mask)
326 } while (get_timer(start) < stop);
331 static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count)
337 while (size < count) {
338 burstcnt = get_burstcount(chip);
340 /* burstcount < 0 = tpm is busy */
344 /* limit received data to max. left */
345 if (burstcnt > (count - size))
346 burstcnt = count - size;
348 rc = iic_tpm_read(TPM_DATA_FIFO(chip->vendor.locality),
358 static int tpm_tis_i2c_recv(struct tpm_chip *chip, u8 *buf, size_t count)
361 int expected, status;
363 if (count < TPM_HEADER_SIZE) {
368 /* read first 10 bytes, including tag, paramsize, and result */
369 size = recv_data(chip, buf, TPM_HEADER_SIZE);
370 if (size < TPM_HEADER_SIZE) {
371 dev_err(chip->dev, "Unable to read header\n");
375 expected = get_unaligned_be32(buf + TPM_RSP_SIZE_BYTE);
376 if ((size_t)expected > count) {
381 size += recv_data(chip, &buf[TPM_HEADER_SIZE],
382 expected - TPM_HEADER_SIZE);
383 if (size < expected) {
384 dev_err(chip->dev, "Unable to read remainder of result\n");
389 wait_for_stat(chip, TPM_STS_VALID, chip->vendor.timeout_c, &status);
390 if (status & TPM_STS_DATA_AVAIL) { /* retry? */
391 dev_err(chip->dev, "Error left over data\n");
397 tpm_tis_i2c_ready(chip);
398 /* The TPM needs some time to clean up here,
399 * so we sleep rather than keeping the bus busy
402 release_locality(chip, chip->vendor.locality, 0);
407 static int tpm_tis_i2c_send(struct tpm_chip *chip, u8 *buf, size_t len)
414 if (len > TPM_BUFSIZE)
415 return -E2BIG; /* command is too long for our tpm, sorry */
417 if (request_locality(chip, 0) < 0)
420 status = tpm_tis_i2c_status(chip);
421 if ((status & TPM_STS_COMMAND_READY) == 0) {
422 tpm_tis_i2c_ready(chip);
424 (chip, TPM_STS_COMMAND_READY,
425 chip->vendor.timeout_b, &status) < 0) {
431 while (count < len - 1) {
432 burstcnt = get_burstcount(chip);
434 /* burstcount < 0 = tpm is busy */
438 if (burstcnt > (len-1-count))
439 burstcnt = len-1-count;
441 #ifdef CONFIG_TPM_I2C_BURST_LIMITATION
442 if (burstcnt > CONFIG_TPM_I2C_BURST_LIMITATION)
443 burstcnt = CONFIG_TPM_I2C_BURST_LIMITATION;
444 #endif /* CONFIG_TPM_I2C_BURST_LIMITATION */
446 rc = iic_tpm_write(TPM_DATA_FIFO(chip->vendor.locality),
447 &(buf[count]), burstcnt);
451 wait_for_stat(chip, TPM_STS_VALID,
452 chip->vendor.timeout_c, &status);
454 if ((status & TPM_STS_DATA_EXPECT) == 0) {
460 /* write last byte */
461 iic_tpm_write(TPM_DATA_FIFO(chip->vendor.locality), &(buf[count]), 1);
462 wait_for_stat(chip, TPM_STS_VALID, chip->vendor.timeout_c, &status);
463 if ((status & TPM_STS_DATA_EXPECT) != 0) {
469 iic_tpm_write(TPM_STS(chip->vendor.locality), &sts, 1);
473 tpm_tis_i2c_ready(chip);
474 /* The TPM needs some time to clean up here,
475 * so we sleep rather than keeping the bus busy
478 release_locality(chip, chip->vendor.locality, 0);
483 static struct tpm_vendor_specific tpm_tis_i2c = {
484 .status = tpm_tis_i2c_status,
485 .recv = tpm_tis_i2c_recv,
486 .send = tpm_tis_i2c_send,
487 .cancel = tpm_tis_i2c_ready,
488 .req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
489 .req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID,
490 .req_canceled = TPM_STS_COMMAND_READY,
493 /* initialisation of i2c tpm */
496 int tpm_vendor_init(uint32_t dev_addr)
501 struct tpm_chip *chip;
503 old_addr = tpm_dev.addr;
505 tpm_dev.addr = dev_addr;
507 chip = tpm_register_hardware(&tpm_tis_i2c);
513 /* Disable interrupts (not supported) */
514 chip->vendor.irq = 0;
516 /* Default timeouts */
517 chip->vendor.timeout_a = TIS_SHORT_TIMEOUT;
518 chip->vendor.timeout_b = TIS_LONG_TIMEOUT;
519 chip->vendor.timeout_c = TIS_SHORT_TIMEOUT;
520 chip->vendor.timeout_d = TIS_SHORT_TIMEOUT;
522 if (request_locality(chip, 0) != 0) {
527 /* read four bytes from DID_VID register */
528 if (iic_tpm_read(TPM_DID_VID(0), (uchar *)&vendor, 4) < 0) {
533 /* create DID_VID register value, after swapping to little-endian */
534 vendor = be32_to_cpu(vendor);
536 if (vendor != TPM_TIS_I2C_DID_VID) {
541 dev_info(dev, "1.2 TPM (device-id 0x%X)\n", vendor >> 16);
544 * A timeout query to TPM can be placed here.
545 * Standard timeout values are used so far
551 release_locality(chip, 0, 1);
554 tpm_dev.addr = old_addr;
558 void tpm_vendor_cleanup(struct tpm_chip *chip)
560 release_locality(chip, chip->vendor.locality, 1);