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 /* Address of the TPM on the I2C bus */
54 #define TPM_I2C_ADDR 0x20
56 /* Max buffer size supported by our tpm */
57 #define TPM_DEV_BUFSIZE 1260
59 /* Max number of iterations after i2c NAK */
63 * Max number of iterations after i2c NAK for 'long' commands
65 * We need this especially for sending TPM_READY, since the cleanup after the
66 * transtion to the ready state may take some time, but it is unpredictable
67 * how long it will take.
69 #define MAX_COUNT_LONG 50
71 #define SLEEP_DURATION 60 /* in usec */
72 #define SLEEP_DURATION_LONG 210 /* in usec */
74 #define TPM_HEADER_SIZE 10
77 * Expected value for DIDVID register
79 * The only device the system knows about at this moment is Infineon slb9635.
81 #define TPM_TIS_I2C_DID_VID 0x000b15d1L
84 TPM_ACCESS_VALID
= 0x80,
85 TPM_ACCESS_ACTIVE_LOCALITY
= 0x20,
86 TPM_ACCESS_REQUEST_PENDING
= 0x04,
87 TPM_ACCESS_REQUEST_USE
= 0x02,
92 TPM_STS_COMMAND_READY
= 0x40,
94 TPM_STS_DATA_AVAIL
= 0x10,
95 TPM_STS_DATA_EXPECT
= 0x08,
99 TIS_SHORT_TIMEOUT
= 750, /* ms */
100 TIS_LONG_TIMEOUT
= 2000, /* ms */
103 /* expected value for DIDVID register */
104 #define TPM_TIS_I2C_DID_VID_9635 0x000b15d1L
105 #define TPM_TIS_I2C_DID_VID_9645 0x001a15d1L
113 static const char * const chip_name
[] = {
114 [SLB9635
] = "slb9635tt",
115 [SLB9645
] = "slb9645tt",
116 [UNKNOWN
] = "unknown/fallback to slb9635",
119 #define TPM_ACCESS(l) (0x0000 | ((l) << 4))
120 #define TPM_STS(l) (0x0001 | ((l) << 4))
121 #define TPM_DATA_FIFO(l) (0x0005 | ((l) << 4))
122 #define TPM_DID_VID(l) (0x0006 | ((l) << 4))
124 /* Structure to store I2C TPM specific stuff */
131 u8 buf
[TPM_DEV_BUFSIZE
+ sizeof(u8
)]; /* Max buffer size + addr */
132 enum i2c_chip_type chip_type
;
135 static struct tpm_dev tpm_dev
= {
136 #ifndef CONFIG_DM_I2C
141 static struct tpm_dev tpm_dev
;
144 * iic_tpm_read() - read from TPM register
145 * @addr: register address to read from
146 * @buffer: provided by caller
147 * @len: number of bytes to read
149 * Read len bytes from TPM register and put them into
150 * buffer (little-endian format, i.e. first byte is put into buffer[0]).
152 * NOTE: TPM is big-endian for multi-byte values. Multi-byte
153 * values have to be swapped.
155 * Return -EIO on error, 0 on success.
157 static int iic_tpm_read(u8 addr
, u8
*buffer
, size_t len
)
161 uint32_t addrbuf
= addr
;
163 if ((tpm_dev
.chip_type
== SLB9635
) || (tpm_dev
.chip_type
== UNKNOWN
)) {
164 /* slb9635 protocol should work in both cases */
165 for (count
= 0; count
< MAX_COUNT
; count
++) {
167 rc
= dm_i2c_write(tpm_dev
.dev
, 0, (uchar
*)&addrbuf
, 1);
169 rc
= i2c_write(tpm_dev
.addr
, 0, 0,
170 (uchar
*)&addrbuf
, 1);
173 break; /* Success, break to skip sleep */
174 udelay(SLEEP_DURATION
);
179 /* After the TPM has successfully received the register address
180 * it needs some time, thus we're sleeping here again, before
181 * retrieving the data
183 for (count
= 0; count
< MAX_COUNT
; count
++) {
184 udelay(SLEEP_DURATION
);
186 rc
= dm_i2c_read(tpm_dev
.dev
, 0, buffer
, len
);
188 rc
= i2c_read(tpm_dev
.addr
, 0, 0, buffer
, len
);
191 break; /* success, break to skip sleep */
195 * Use a combined read for newer chips.
196 * Unfortunately the smbus functions are not suitable due to
197 * the 32 byte limit of the smbus.
198 * Retries should usually not be needed, but are kept just to
199 * be safe on the safe side.
201 for (count
= 0; count
< MAX_COUNT
; count
++) {
203 rc
= dm_i2c_read(tpm_dev
.dev
, addr
, buffer
, len
);
205 rc
= i2c_read(tpm_dev
.addr
, addr
, 1, buffer
, len
);
208 break; /* break here to skip sleep */
209 udelay(SLEEP_DURATION
);
213 /* Take care of 'guard time' */
214 udelay(SLEEP_DURATION
);
221 static int iic_tpm_write_generic(u8 addr
, u8
*buffer
, size_t len
,
222 unsigned int sleep_time
, u8 max_count
)
227 /* Prepare send buffer */
228 #ifndef CONFIG_DM_I2C
229 tpm_dev
.buf
[0] = addr
;
230 memcpy(&(tpm_dev
.buf
[1]), buffer
, len
);
231 buffer
= tpm_dev
.buf
;
235 for (count
= 0; count
< max_count
; count
++) {
237 rc
= dm_i2c_write(tpm_dev
.dev
, addr
, buffer
, len
);
239 rc
= i2c_write(tpm_dev
.addr
, 0, 0, buffer
, len
);
242 break; /* Success, break to skip sleep */
246 /* take care of 'guard time' */
255 * iic_tpm_write() - write to TPM register
256 * @addr: register address to write to
257 * @buffer: containing data to be written
258 * @len: number of bytes to write
260 * Write len bytes from provided buffer to TPM register (little
261 * endian format, i.e. buffer[0] is written as first byte).
263 * NOTE: TPM is big-endian for multi-byte values. Multi-byte
264 * values have to be swapped.
266 * NOTE: use this function instead of the iic_tpm_write_generic function.
268 * Return -EIO on error, 0 on success
270 static int iic_tpm_write(u8 addr
, u8
*buffer
, size_t len
)
272 return iic_tpm_write_generic(addr
, buffer
, len
, SLEEP_DURATION
,
277 * This function is needed especially for the cleanup situation after
280 static int iic_tpm_write_long(u8 addr
, u8
*buffer
, size_t len
)
282 return iic_tpm_write_generic(addr
, buffer
, len
, SLEEP_DURATION_LONG
,
286 static int check_locality(struct tpm_chip
*chip
, int loc
)
288 const u8 mask
= TPM_ACCESS_ACTIVE_LOCALITY
| TPM_ACCESS_VALID
;
292 rc
= iic_tpm_read(TPM_ACCESS(loc
), &buf
, 1);
296 if ((buf
& mask
) == mask
) {
297 chip
->vendor
.locality
= loc
;
304 static void release_locality(struct tpm_chip
*chip
, int loc
, int force
)
306 const u8 mask
= TPM_ACCESS_REQUEST_PENDING
| TPM_ACCESS_VALID
;
309 if (iic_tpm_read(TPM_ACCESS(loc
), &buf
, 1) < 0)
312 if (force
|| (buf
& mask
) == mask
) {
313 buf
= TPM_ACCESS_ACTIVE_LOCALITY
;
314 iic_tpm_write(TPM_ACCESS(loc
), &buf
, 1);
318 static int request_locality(struct tpm_chip
*chip
, int loc
)
320 unsigned long start
, stop
;
321 u8 buf
= TPM_ACCESS_REQUEST_USE
;
324 if (check_locality(chip
, loc
) >= 0)
325 return loc
; /* We already have the locality */
327 rc
= iic_tpm_write(TPM_ACCESS(loc
), &buf
, 1);
331 /* Wait for burstcount */
332 start
= get_timer(0);
333 stop
= chip
->vendor
.timeout_a
;
335 if (check_locality(chip
, loc
) >= 0)
337 udelay(TPM_TIMEOUT
* 1000);
338 } while (get_timer(start
) < stop
);
343 static u8
tpm_tis_i2c_status(struct tpm_chip
*chip
)
345 /* NOTE: Since i2c read may fail, return 0 in this case --> time-out */
348 if (iic_tpm_read(TPM_STS(chip
->vendor
.locality
), &buf
, 1) < 0)
354 static void tpm_tis_i2c_ready(struct tpm_chip
*chip
)
358 /* This causes the current command to be aborted */
359 u8 buf
= TPM_STS_COMMAND_READY
;
361 debug("%s\n", __func__
);
362 rc
= iic_tpm_write_long(TPM_STS(chip
->vendor
.locality
), &buf
, 1);
364 debug("%s: rc=%d\n", __func__
, rc
);
367 static ssize_t
get_burstcount(struct tpm_chip
*chip
)
369 unsigned long start
, stop
;
373 /* Wait for burstcount */
374 /* XXX: Which timeout value? Spec has 2 answers (c & d) */
375 start
= get_timer(0);
376 stop
= chip
->vendor
.timeout_d
;
378 /* Note: STS is little endian */
379 addr
= TPM_STS(chip
->vendor
.locality
) + 1;
380 if (iic_tpm_read(addr
, buf
, 3) < 0)
383 burstcnt
= (buf
[2] << 16) + (buf
[1] << 8) + buf
[0];
387 udelay(TPM_TIMEOUT
* 1000);
388 } while (get_timer(start
) < stop
);
393 static int wait_for_stat(struct tpm_chip
*chip
, u8 mask
, unsigned long timeout
,
396 unsigned long start
, stop
;
398 /* Check current status */
399 *status
= tpm_tis_i2c_status(chip
);
400 if ((*status
& mask
) == mask
)
403 start
= get_timer(0);
406 udelay(TPM_TIMEOUT
* 1000);
407 *status
= tpm_tis_i2c_status(chip
);
408 if ((*status
& mask
) == mask
)
410 } while (get_timer(start
) < stop
);
415 static int recv_data(struct tpm_chip
*chip
, u8
*buf
, size_t count
)
421 while (size
< count
) {
422 burstcnt
= get_burstcount(chip
);
424 /* burstcount < 0 -> tpm is busy */
428 /* Limit received data to max left */
429 if (burstcnt
> (count
- size
))
430 burstcnt
= count
- size
;
432 rc
= iic_tpm_read(TPM_DATA_FIFO(chip
->vendor
.locality
),
433 &(buf
[size
]), burstcnt
);
441 static int tpm_tis_i2c_recv(struct tpm_chip
*chip
, u8
*buf
, size_t count
)
444 int expected
, status
;
446 if (count
< TPM_HEADER_SIZE
) {
451 /* Read first 10 bytes, including tag, paramsize, and result */
452 size
= recv_data(chip
, buf
, TPM_HEADER_SIZE
);
453 if (size
< TPM_HEADER_SIZE
) {
454 error("Unable to read header\n");
458 expected
= get_unaligned_be32(buf
+ TPM_RSP_SIZE_BYTE
);
459 if ((size_t)expected
> count
) {
460 error("Error size=%x, expected=%x, count=%x\n", size
, expected
,
466 size
+= recv_data(chip
, &buf
[TPM_HEADER_SIZE
],
467 expected
- TPM_HEADER_SIZE
);
468 if (size
< expected
) {
469 error("Unable to read remainder of result\n");
474 wait_for_stat(chip
, TPM_STS_VALID
, chip
->vendor
.timeout_c
, &status
);
475 if (status
& TPM_STS_DATA_AVAIL
) { /* Retry? */
476 error("Error left over data\n");
482 tpm_tis_i2c_ready(chip
);
484 * The TPM needs some time to clean up here,
485 * so we sleep rather than keeping the bus busy
488 release_locality(chip
, chip
->vendor
.locality
, 0);
493 static int tpm_tis_i2c_send(struct tpm_chip
*chip
, u8
*buf
, size_t len
)
501 debug("%s: len=%d\n", __func__
, len
);
502 if (len
> TPM_DEV_BUFSIZE
)
503 return -E2BIG
; /* Command is too long for our tpm, sorry */
505 if (request_locality(chip
, 0) < 0)
508 status
= tpm_tis_i2c_status(chip
);
509 if ((status
& TPM_STS_COMMAND_READY
) == 0) {
510 tpm_tis_i2c_ready(chip
);
511 if (wait_for_stat(chip
, TPM_STS_COMMAND_READY
,
512 chip
->vendor
.timeout_b
, &status
) < 0) {
518 burstcnt
= get_burstcount(chip
);
520 /* burstcount < 0 -> tpm is busy */
524 while (count
< len
) {
526 if (burstcnt
> len
- count
)
527 burstcnt
= len
- count
;
529 #ifdef CONFIG_TPM_TIS_I2C_BURST_LIMITATION
530 if (retry
&& burstcnt
> CONFIG_TPM_TIS_I2C_BURST_LIMITATION
)
531 burstcnt
= CONFIG_TPM_TIS_I2C_BURST_LIMITATION
;
532 #endif /* CONFIG_TPM_TIS_I2C_BURST_LIMITATION */
534 rc
= iic_tpm_write(TPM_DATA_FIFO(chip
->vendor
.locality
),
535 &(buf
[count
]), burstcnt
);
539 debug("%s: error\n", __func__
);
544 rc
= wait_for_stat(chip
, TPM_STS_VALID
,
545 chip
->vendor
.timeout_c
, &status
);
549 if ((status
& TPM_STS_DATA_EXPECT
) == 0) {
557 iic_tpm_write(TPM_STS(chip
->vendor
.locality
), &sts
, 1);
563 debug("%s: out_err\n", __func__
);
564 tpm_tis_i2c_ready(chip
);
566 * The TPM needs some time to clean up here,
567 * so we sleep rather than keeping the bus busy
570 release_locality(chip
, chip
->vendor
.locality
, 0);
575 static struct tpm_vendor_specific tpm_tis_i2c
= {
576 .status
= tpm_tis_i2c_status
,
577 .recv
= tpm_tis_i2c_recv
,
578 .send
= tpm_tis_i2c_send
,
579 .cancel
= tpm_tis_i2c_ready
,
580 .req_complete_mask
= TPM_STS_DATA_AVAIL
| TPM_STS_VALID
,
581 .req_complete_val
= TPM_STS_DATA_AVAIL
| TPM_STS_VALID
,
582 .req_canceled
= TPM_STS_COMMAND_READY
,
586 static enum i2c_chip_type
tpm_vendor_chip_type(void)
588 #ifdef CONFIG_OF_CONTROL
589 const void *blob
= gd
->fdt_blob
;
591 if (fdtdec_next_compatible(blob
, 0, COMPAT_INFINEON_SLB9645_TPM
) >= 0)
594 if (fdtdec_next_compatible(blob
, 0, COMPAT_INFINEON_SLB9635_TPM
) >= 0)
600 static int tpm_vendor_init_common(void)
602 struct tpm_chip
*chip
;
604 u32 expected_did_vid
;
606 tpm_dev
.chip_type
= tpm_vendor_chip_type();
608 chip
= tpm_register_hardware(&tpm_tis_i2c
);
612 /* Disable interrupts (not supported) */
613 chip
->vendor
.irq
= 0;
615 /* Default timeouts */
616 chip
->vendor
.timeout_a
= TIS_SHORT_TIMEOUT
;
617 chip
->vendor
.timeout_b
= TIS_LONG_TIMEOUT
;
618 chip
->vendor
.timeout_c
= TIS_SHORT_TIMEOUT
;
619 chip
->vendor
.timeout_d
= TIS_SHORT_TIMEOUT
;
621 if (request_locality(chip
, 0) < 0)
624 /* Read four bytes from DID_VID register */
625 if (iic_tpm_read(TPM_DID_VID(0), (uchar
*)&vendor
, 4) < 0) {
626 release_locality(chip
, 0, 1);
630 if (tpm_dev
.chip_type
== SLB9635
) {
631 vendor
= be32_to_cpu(vendor
);
632 expected_did_vid
= TPM_TIS_I2C_DID_VID_9635
;
634 /* device id and byte order has changed for newer i2c tpms */
635 expected_did_vid
= TPM_TIS_I2C_DID_VID_9645
;
638 if (tpm_dev
.chip_type
!= UNKNOWN
&& vendor
!= expected_did_vid
) {
639 error("Vendor id did not match! ID was %08x\n", vendor
);
643 debug("1.2 TPM (chip type %s device-id 0x%X)\n",
644 chip_name
[tpm_dev
.chip_type
], vendor
>> 16);
647 * A timeout query to TPM can be placed here.
648 * Standard timeout values are used so far
655 /* Initialisation of i2c tpm */
656 int tpm_vendor_init_dev(struct udevice
*dev
)
659 return tpm_vendor_init_common();
662 /* Initialisation of i2c tpm */
663 int tpm_vendor_init(uint32_t dev_addr
)
668 old_addr
= tpm_dev
.addr
;
670 tpm_dev
.addr
= dev_addr
;
672 rc
= tpm_vendor_init_common();
674 tpm_dev
.addr
= old_addr
;
680 void tpm_vendor_cleanup(struct tpm_chip
*chip
)
682 release_locality(chip
, chip
->vendor
.locality
, 1);