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
28 #include <asm-generic/errno.h>
29 #include <linux/compiler.h>
30 #include <linux/types.h>
31 #include <linux/unaligned/be_byteshift.h>
33 #include "tpm_tis_infineon.h"
34 #include "tpm_internal.h"
36 DECLARE_GLOBAL_DATA_PTR
;
38 static const char * const chip_name
[] = {
39 [SLB9635
] = "slb9635tt",
40 [SLB9645
] = "slb9645tt",
41 [UNKNOWN
] = "unknown/fallback to slb9635",
45 * tpm_tis_i2c_read() - read from TPM register
46 * @addr: register address to read from
47 * @buffer: provided by caller
48 * @len: number of bytes to read
50 * Read len bytes from TPM register and put them into
51 * buffer (little-endian format, i.e. first byte is put into buffer[0]).
53 * NOTE: TPM is big-endian for multi-byte values. Multi-byte
54 * values have to be swapped.
56 * Return -EIO on error, 0 on success.
58 static int tpm_tis_i2c_read(struct udevice
*dev
, u8 addr
, u8
*buffer
,
61 struct tpm_chip
*chip
= dev_get_priv(dev
);
64 uint32_t addrbuf
= addr
;
66 if ((chip
->chip_type
== SLB9635
) || (chip
->chip_type
== UNKNOWN
)) {
67 /* slb9635 protocol should work in both cases */
68 for (count
= 0; count
< MAX_COUNT
; count
++) {
69 rc
= dm_i2c_write(dev
, 0, (uchar
*)&addrbuf
, 1);
71 break; /* Success, break to skip sleep */
72 udelay(SLEEP_DURATION_US
);
77 /* After the TPM has successfully received the register address
78 * it needs some time, thus we're sleeping here again, before
81 for (count
= 0; count
< MAX_COUNT
; count
++) {
82 udelay(SLEEP_DURATION_US
);
83 rc
= dm_i2c_read(dev
, 0, buffer
, len
);
85 break; /* success, break to skip sleep */
89 * Use a combined read for newer chips.
90 * Unfortunately the smbus functions are not suitable due to
91 * the 32 byte limit of the smbus.
92 * Retries should usually not be needed, but are kept just to
93 * be safe on the safe side.
95 for (count
= 0; count
< MAX_COUNT
; count
++) {
96 rc
= dm_i2c_read(dev
, addr
, buffer
, len
);
98 break; /* break here to skip sleep */
99 udelay(SLEEP_DURATION_US
);
103 /* Take care of 'guard time' */
104 udelay(SLEEP_DURATION_US
);
111 static int tpm_tis_i2c_write_generic(struct udevice
*dev
, u8 addr
,
112 const u8
*buffer
, size_t len
,
113 unsigned int sleep_time_us
, u8 max_count
)
115 struct tpm_chip_priv
*priv
= dev_get_uclass_priv(dev
);
116 struct tpm_chip
*chip
= dev_get_priv(dev
);
120 if (chip
->chip_type
== SLB9635
) {
121 /* Prepare send buffer to include the address */
123 memcpy(&(priv
->buf
[1]), buffer
, len
);
129 for (count
= 0; count
< max_count
; count
++) {
130 rc
= dm_i2c_write(dev
, addr
, buffer
, len
);
132 break; /* Success, break to skip sleep */
133 udelay(sleep_time_us
);
136 /* take care of 'guard time' */
137 udelay(sleep_time_us
);
145 * tpm_tis_i2c_write() - write to TPM register
146 * @addr: register address to write to
147 * @buffer: containing data to be written
148 * @len: number of bytes to write
150 * Write len bytes from provided buffer to TPM register (little
151 * endian format, i.e. buffer[0] is written as first byte).
153 * NOTE: TPM is big-endian for multi-byte values. Multi-byte
154 * values have to be swapped.
156 * NOTE: use this function instead of the tpm_tis_i2c_write_generic function.
158 * Return -EIO on error, 0 on success
160 static int tpm_tis_i2c_write(struct udevice
*dev
, u8 addr
, const u8
*buffer
,
163 return tpm_tis_i2c_write_generic(dev
, addr
, buffer
, len
,
164 SLEEP_DURATION_US
, MAX_COUNT
);
168 * This function is needed especially for the cleanup situation after
171 static int tpm_tis_i2c_write_long(struct udevice
*dev
, u8 addr
, u8
*buffer
,
174 return tpm_tis_i2c_write_generic(dev
, addr
, buffer
, len
,
175 SLEEP_DURATION_LONG_US
,
179 static int tpm_tis_i2c_check_locality(struct udevice
*dev
, int loc
)
181 const u8 mask
= TPM_ACCESS_ACTIVE_LOCALITY
| TPM_ACCESS_VALID
;
182 struct tpm_chip
*chip
= dev_get_priv(dev
);
186 rc
= tpm_tis_i2c_read(dev
, TPM_ACCESS(loc
), &buf
, 1);
190 if ((buf
& mask
) == mask
) {
191 chip
->locality
= loc
;
198 static void tpm_tis_i2c_release_locality(struct udevice
*dev
, int loc
,
201 const u8 mask
= TPM_ACCESS_REQUEST_PENDING
| TPM_ACCESS_VALID
;
204 if (tpm_tis_i2c_read(dev
, TPM_ACCESS(loc
), &buf
, 1) < 0)
207 if (force
|| (buf
& mask
) == mask
) {
208 buf
= TPM_ACCESS_ACTIVE_LOCALITY
;
209 tpm_tis_i2c_write(dev
, TPM_ACCESS(loc
), &buf
, 1);
213 static int tpm_tis_i2c_request_locality(struct udevice
*dev
, int loc
)
215 struct tpm_chip
*chip
= dev_get_priv(dev
);
216 unsigned long start
, stop
;
217 u8 buf
= TPM_ACCESS_REQUEST_USE
;
220 rc
= tpm_tis_i2c_check_locality(dev
, loc
);
222 debug("%s: Already have locality\n", __func__
);
223 return loc
; /* We already have the locality */
224 } else if (rc
!= -ENOENT
) {
225 debug("%s: Failed to get locality: %d\n", __func__
, rc
);
229 rc
= tpm_tis_i2c_write(dev
, TPM_ACCESS(loc
), &buf
, 1);
231 debug("%s: Failed to write to TPM: %d\n", __func__
, rc
);
235 /* Wait for burstcount */
236 start
= get_timer(0);
237 stop
= chip
->timeout_a
;
239 rc
= tpm_tis_i2c_check_locality(dev
, loc
);
241 debug("%s: Have locality\n", __func__
);
243 } else if (rc
!= -ENOENT
) {
244 debug("%s: Failed to get locality: %d\n", __func__
, rc
);
247 mdelay(TPM_TIMEOUT_MS
);
248 } while (get_timer(start
) < stop
);
249 debug("%s: Timeout getting locality: %d\n", __func__
, rc
);
254 static u8
tpm_tis_i2c_status(struct udevice
*dev
)
256 struct tpm_chip
*chip
= dev_get_priv(dev
);
257 /* NOTE: Since i2c read may fail, return 0 in this case --> time-out */
260 if (tpm_tis_i2c_read(dev
, TPM_STS(chip
->locality
), &buf
, 1) < 0)
266 static int tpm_tis_i2c_ready(struct udevice
*dev
)
268 struct tpm_chip
*chip
= dev_get_priv(dev
);
271 /* This causes the current command to be aborted */
272 u8 buf
= TPM_STS_COMMAND_READY
;
274 debug("%s\n", __func__
);
275 rc
= tpm_tis_i2c_write_long(dev
, TPM_STS(chip
->locality
), &buf
, 1);
277 debug("%s: rc=%d\n", __func__
, rc
);
282 static ssize_t
tpm_tis_i2c_get_burstcount(struct udevice
*dev
)
284 struct tpm_chip
*chip
= dev_get_priv(dev
);
285 unsigned long start
, stop
;
289 /* Wait for burstcount */
290 /* XXX: Which timeout value? Spec has 2 answers (c & d) */
291 start
= get_timer(0);
292 stop
= chip
->timeout_d
;
294 /* Note: STS is little endian */
295 addr
= TPM_STS(chip
->locality
) + 1;
296 if (tpm_tis_i2c_read(dev
, addr
, buf
, 3) < 0)
299 burstcnt
= (buf
[2] << 16) + (buf
[1] << 8) + buf
[0];
303 mdelay(TPM_TIMEOUT_MS
);
304 } while (get_timer(start
) < stop
);
309 static int tpm_tis_i2c_wait_for_stat(struct udevice
*dev
, u8 mask
,
310 unsigned long timeout
, int *status
)
312 unsigned long start
, stop
;
314 /* Check current status */
315 *status
= tpm_tis_i2c_status(dev
);
316 if ((*status
& mask
) == mask
)
319 start
= get_timer(0);
322 mdelay(TPM_TIMEOUT_MS
);
323 *status
= tpm_tis_i2c_status(dev
);
324 if ((*status
& mask
) == mask
)
326 } while (get_timer(start
) < stop
);
331 static int tpm_tis_i2c_recv_data(struct udevice
*dev
, u8
*buf
, size_t count
)
333 struct tpm_chip
*chip
= dev_get_priv(dev
);
338 while (size
< count
) {
339 burstcnt
= tpm_tis_i2c_get_burstcount(dev
);
341 /* burstcount < 0 -> tpm is busy */
345 /* Limit received data to max left */
346 if (burstcnt
> (count
- size
))
347 burstcnt
= count
- size
;
349 rc
= tpm_tis_i2c_read(dev
, TPM_DATA_FIFO(chip
->locality
),
350 &(buf
[size
]), burstcnt
);
358 static int tpm_tis_i2c_recv(struct udevice
*dev
, u8
*buf
, size_t count
)
360 struct tpm_chip
*chip
= dev_get_priv(dev
);
362 int expected
, status
;
365 status
= tpm_tis_i2c_status(dev
);
366 if (status
== TPM_STS_COMMAND_READY
)
368 if ((status
& (TPM_STS_DATA_AVAIL
| TPM_STS_VALID
)) !=
369 (TPM_STS_DATA_AVAIL
| TPM_STS_VALID
))
372 debug("...got it;\n");
374 /* Read first 10 bytes, including tag, paramsize, and result */
375 size
= tpm_tis_i2c_recv_data(dev
, buf
, TPM_HEADER_SIZE
);
376 if (size
< TPM_HEADER_SIZE
) {
377 debug("Unable to read header\n");
378 return size
< 0 ? size
: -EIO
;
381 expected
= get_unaligned_be32(buf
+ TPM_RSP_SIZE_BYTE
);
382 if ((size_t)expected
> count
) {
383 debug("Error size=%x, expected=%x, count=%x\n", size
, expected
,
388 size
+= tpm_tis_i2c_recv_data(dev
, &buf
[TPM_HEADER_SIZE
],
389 expected
- TPM_HEADER_SIZE
);
390 if (size
< expected
) {
391 debug("Unable to read remainder of result\n");
395 rc
= tpm_tis_i2c_wait_for_stat(dev
, TPM_STS_VALID
, chip
->timeout_c
,
399 if (status
& TPM_STS_DATA_AVAIL
) { /* Retry? */
400 debug("Error left over data\n");
407 static int tpm_tis_i2c_send(struct udevice
*dev
, const u8
*buf
, size_t len
)
409 struct tpm_chip
*chip
= dev_get_priv(dev
);
416 debug("%s: len=%d\n", __func__
, len
);
417 if (len
> TPM_DEV_BUFSIZE
)
418 return -E2BIG
; /* Command is too long for our tpm, sorry */
420 if (tpm_tis_i2c_request_locality(dev
, 0) < 0)
423 status
= tpm_tis_i2c_status(dev
);
424 if ((status
& TPM_STS_COMMAND_READY
) == 0) {
425 rc
= tpm_tis_i2c_ready(dev
);
428 rc
= tpm_tis_i2c_wait_for_stat(dev
, TPM_STS_COMMAND_READY
,
429 chip
->timeout_b
, &status
);
434 burstcnt
= tpm_tis_i2c_get_burstcount(dev
);
436 /* burstcount < 0 -> tpm is busy */
440 while (count
< len
) {
442 if (burstcnt
> len
- count
)
443 burstcnt
= len
- count
;
445 #ifdef CONFIG_TPM_TIS_I2C_BURST_LIMITATION
446 if (retry
&& burstcnt
> CONFIG_TPM_TIS_I2C_BURST_LIMITATION_LEN
)
447 burstcnt
= CONFIG_TPM_TIS_I2C_BURST_LIMITATION_LEN
;
448 #endif /* CONFIG_TPM_TIS_I2C_BURST_LIMITATION */
450 rc
= tpm_tis_i2c_write(dev
, TPM_DATA_FIFO(chip
->locality
),
451 &(buf
[count
]), burstcnt
);
455 debug("%s: error\n", __func__
);
458 rc
= tpm_tis_i2c_wait_for_stat(dev
, TPM_STS_VALID
,
464 if ((status
& TPM_STS_DATA_EXPECT
) == 0)
470 rc
= tpm_tis_i2c_write(dev
, TPM_STS(chip
->locality
), &sts
, 1);
473 debug("%s: done, rc=%d\n", __func__
, rc
);
478 static int tpm_tis_i2c_cleanup(struct udevice
*dev
)
480 struct tpm_chip
*chip
= dev_get_priv(dev
);
482 tpm_tis_i2c_ready(dev
);
484 * The TPM needs some time to clean up here,
485 * so we sleep rather than keeping the bus busy
488 tpm_tis_i2c_release_locality(dev
, chip
->locality
, 0);
493 static int tpm_tis_i2c_init(struct udevice
*dev
)
495 struct tpm_chip
*chip
= dev_get_priv(dev
);
497 u32 expected_did_vid
;
502 /* Default timeouts - these could move to the device tree */
503 chip
->timeout_a
= TIS_SHORT_TIMEOUT_MS
;
504 chip
->timeout_b
= TIS_LONG_TIMEOUT_MS
;
505 chip
->timeout_c
= TIS_SHORT_TIMEOUT_MS
;
506 chip
->timeout_d
= TIS_SHORT_TIMEOUT_MS
;
508 rc
= tpm_tis_i2c_request_locality(dev
, 0);
512 /* Read four bytes from DID_VID register */
513 if (tpm_tis_i2c_read(dev
, TPM_DID_VID(0), (uchar
*)&vendor
, 4) < 0) {
514 tpm_tis_i2c_release_locality(dev
, 0, 1);
518 if (chip
->chip_type
== SLB9635
) {
519 vendor
= be32_to_cpu(vendor
);
520 expected_did_vid
= TPM_TIS_I2C_DID_VID_9635
;
522 /* device id and byte order has changed for newer i2c tpms */
523 expected_did_vid
= TPM_TIS_I2C_DID_VID_9645
;
526 if (chip
->chip_type
!= UNKNOWN
&& vendor
!= expected_did_vid
) {
527 error("Vendor id did not match! ID was %08x\n", vendor
);
531 chip
->vend_dev
= vendor
;
532 debug("1.2 TPM (chip type %s device-id 0x%X)\n",
533 chip_name
[chip
->chip_type
], vendor
>> 16);
536 * A timeout query to TPM can be placed here.
537 * Standard timeout values are used so far
543 static int tpm_tis_i2c_open(struct udevice
*dev
)
545 struct tpm_chip
*chip
= dev_get_priv(dev
);
548 debug("%s: start\n", __func__
);
551 rc
= tpm_tis_i2c_init(dev
);
558 static int tpm_tis_i2c_close(struct udevice
*dev
)
560 struct tpm_chip
*chip
= dev_get_priv(dev
);
563 tpm_tis_i2c_release_locality(dev
, chip
->locality
, 1);
571 static int tpm_tis_get_desc(struct udevice
*dev
, char *buf
, int size
)
573 struct tpm_chip
*chip
= dev_get_priv(dev
);
578 return snprintf(buf
, size
, "1.2 TPM (%s, chip type %s device-id 0x%x)",
579 chip
->is_open
? "open" : "closed",
580 chip_name
[chip
->chip_type
],
581 chip
->vend_dev
>> 16);
584 static int tpm_tis_i2c_probe(struct udevice
*dev
)
586 struct tpm_chip_priv
*uc_priv
= dev_get_uclass_priv(dev
);
587 struct tpm_chip
*chip
= dev_get_priv(dev
);
589 chip
->chip_type
= dev_get_driver_data(dev
);
591 /* TODO: These need to be checked and tuned */
592 uc_priv
->duration_ms
[TPM_SHORT
] = TIS_SHORT_TIMEOUT_MS
;
593 uc_priv
->duration_ms
[TPM_MEDIUM
] = TIS_LONG_TIMEOUT_MS
;
594 uc_priv
->duration_ms
[TPM_LONG
] = TIS_LONG_TIMEOUT_MS
;
595 uc_priv
->retry_time_ms
= TPM_TIMEOUT_MS
;
600 static const struct tpm_ops tpm_tis_i2c_ops
= {
601 .open
= tpm_tis_i2c_open
,
602 .close
= tpm_tis_i2c_close
,
603 .get_desc
= tpm_tis_get_desc
,
604 .send
= tpm_tis_i2c_send
,
605 .recv
= tpm_tis_i2c_recv
,
606 .cleanup
= tpm_tis_i2c_cleanup
,
609 static const struct udevice_id tpm_tis_i2c_ids
[] = {
610 { .compatible
= "infineon,slb9635tt", .data
= SLB9635
},
611 { .compatible
= "infineon,slb9645tt", .data
= SLB9645
},
615 U_BOOT_DRIVER(tpm_tis_i2c
) = {
616 .name
= "tpm_tis_infineon",
618 .of_match
= tpm_tis_i2c_ids
,
619 .ops
= &tpm_tis_i2c_ops
,
620 .probe
= tpm_tis_i2c_probe
,
621 .priv_auto_alloc_size
= sizeof(struct tpm_chip
),