2 * Copyright 2016 Google Inc.
4 * Based on Linux Kernel TPM driver by
5 * Peter Huewe <peter.huewe@infineon.com>
6 * Copyright (C) 2011 Infineon Technologies
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation, version 2 of the
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
20 * cr50 is a TPM 2.0 capable device that requries special
21 * handling for the I2C interface.
23 * - Use an interrupt for transaction status instead of hardcoded delays
24 * - Must use write+wait+read read protocol
25 * - All 4 bytes of status register must be read/written at once
26 * - Burst count max is 63 bytes, and burst count behaves
27 * slightly differently than other I2C TPMs
28 * - When reading from FIFO the full burstcnt must be read
29 * instead of just reading header and determining the remainder
32 #include <arch/early_variables.h>
33 #include <commonlib/endian.h>
38 #include <console/console.h>
39 #include <device/i2c.h>
45 #define CR50_MAX_BUFSIZE 63
46 #define CR50_TIMEOUT_LONG_MS 2000 /* Long timeout while waiting for TPM */
47 #define CR50_TIMEOUT_SHORT_MS 2 /* Short timeout during transactions */
48 #define CR50_TIMEOUT_NOIRQ_MS 20 /* Timeout for TPM ready without IRQ */
49 #define CR50_TIMEOUT_IRQ_MS 100 /* Timeout for TPM ready with IRQ */
50 #define CR50_DID_VID 0x00281ae0L
55 uint8_t buf
[CR50_MAX_BUFSIZE
+ sizeof(uint8_t)];
58 static struct tpm_inf_dev g_tpm_dev CAR_GLOBAL
;
60 __attribute__((weak
)) int tis_plat_irq_status(void)
62 static int warning_displayed CAR_GLOBAL
;
64 if (!car_get_var(warning_displayed
)) {
65 printk(BIOS_WARNING
, "WARNING: tis_plat_irq_status() not implemented, wasting 20ms to wait on Cr50!\n");
66 car_set_var(warning_displayed
, 1);
68 mdelay(CR50_TIMEOUT_NOIRQ_MS
);
73 /* Wait for interrupt to indicate the TPM is ready */
74 static int cr50_i2c_wait_tpm_ready(struct tpm_chip
*chip
)
78 stopwatch_init_msecs_expire(&sw
, CR50_TIMEOUT_IRQ_MS
);
80 while (!tis_plat_irq_status())
81 if (stopwatch_expired(&sw
))
88 * cr50_i2c_read() - read from TPM register
90 * @chip: TPM chip information
91 * @addr: register address to read from
92 * @buffer: provided by caller
93 * @len: number of bytes to read
95 * 1) send register address byte 'addr' to the TPM
96 * 2) wait for TPM to indicate it is ready
97 * 3) read 'len' bytes of TPM response into the provided 'buffer'
99 * Return -1 on error, 0 on success.
101 static int cr50_i2c_read(struct tpm_chip
*chip
, uint8_t addr
,
102 uint8_t *buffer
, size_t len
)
104 struct tpm_inf_dev
*tpm_dev
= car_get_var_ptr(&g_tpm_dev
);
106 if (tpm_dev
->addr
== 0)
109 /* Clear interrupt before starting transaction */
110 tis_plat_irq_status();
112 /* Send the register address byte to the TPM */
113 if (i2c_write_raw(tpm_dev
->bus
, tpm_dev
->addr
, &addr
, 1)) {
114 printk(BIOS_ERR
, "%s: Address write failed\n", __func__
);
118 /* Wait for TPM to be ready with response data */
119 if (cr50_i2c_wait_tpm_ready(chip
) < 0)
122 /* Read response data from the TPM */
123 if (i2c_read_raw(tpm_dev
->bus
, tpm_dev
->addr
, buffer
, len
)) {
124 printk(BIOS_ERR
, "%s: Read response failed\n", __func__
);
132 * cr50_i2c_write() - write to TPM register
134 * @chip: TPM chip information
135 * @addr: register address to write to
136 * @buffer: data to write
137 * @len: number of bytes to write
139 * 1) prepend the provided address to the provided data
140 * 2) send the address+data to the TPM
141 * 3) wait for TPM to indicate it is done writing
143 * Returns -1 on error, 0 on success.
145 static int cr50_i2c_write(struct tpm_chip
*chip
,
146 uint8_t addr
, uint8_t *buffer
, size_t len
)
148 struct tpm_inf_dev
*tpm_dev
= car_get_var_ptr(&g_tpm_dev
);
150 if (tpm_dev
->addr
== 0)
152 if (len
> CR50_MAX_BUFSIZE
)
155 /* Prepend the 'register address' to the buffer */
156 tpm_dev
->buf
[0] = addr
;
157 memcpy(tpm_dev
->buf
+ 1, buffer
, len
);
159 /* Clear interrupt before starting transaction */
160 tis_plat_irq_status();
162 /* Send write request buffer with address */
163 if (i2c_write_raw(tpm_dev
->bus
, tpm_dev
->addr
, tpm_dev
->buf
, len
+ 1)) {
164 printk(BIOS_ERR
, "%s: Error writing to TPM\n", __func__
);
168 /* Wait for TPM to be ready */
169 return cr50_i2c_wait_tpm_ready(chip
);
172 static int check_locality(struct tpm_chip
*chip
, int loc
)
174 uint8_t mask
= TPM_ACCESS_VALID
| TPM_ACCESS_ACTIVE_LOCALITY
;
177 if (cr50_i2c_read(chip
, TPM_ACCESS(loc
), &buf
, 1) < 0)
180 if ((buf
& mask
) == mask
) {
181 chip
->vendor
.locality
= loc
;
188 static void release_locality(struct tpm_chip
*chip
, int force
)
190 uint8_t mask
= TPM_ACCESS_VALID
| TPM_ACCESS_REQUEST_PENDING
;
191 uint8_t addr
= TPM_ACCESS(chip
->vendor
.locality
);
194 if (cr50_i2c_read(chip
, addr
, &buf
, 1) < 0)
197 if (force
|| (buf
& mask
) == mask
) {
198 buf
= TPM_ACCESS_ACTIVE_LOCALITY
;
199 cr50_i2c_write(chip
, addr
, &buf
, 1);
202 chip
->vendor
.locality
= 0;
205 static int request_locality(struct tpm_chip
*chip
, int loc
)
207 uint8_t buf
= TPM_ACCESS_REQUEST_USE
;
210 if (check_locality(chip
, loc
) >= 0)
213 if (cr50_i2c_write(chip
, TPM_ACCESS(loc
), &buf
, 1) < 0)
216 stopwatch_init_msecs_expire(&sw
, CR50_TIMEOUT_LONG_MS
);
217 while (check_locality(chip
, loc
) < 0) {
218 if (stopwatch_expired(&sw
))
220 mdelay(CR50_TIMEOUT_SHORT_MS
);
225 /* cr50 requires all 4 bytes of status register to be read */
226 static uint8_t cr50_i2c_tis_status(struct tpm_chip
*chip
)
229 if (cr50_i2c_read(chip
, TPM_STS(chip
->vendor
.locality
),
230 buf
, sizeof(buf
)) < 0) {
231 printk(BIOS_ERR
, "%s: Failed to read status\n", __func__
);
237 /* cr50 requires all 4 bytes of status register to be written */
238 static void cr50_i2c_tis_ready(struct tpm_chip
*chip
)
240 uint8_t buf
[4] = { TPM_STS_COMMAND_READY
};
241 cr50_i2c_write(chip
, TPM_STS(chip
->vendor
.locality
), buf
, sizeof(buf
));
242 mdelay(CR50_TIMEOUT_SHORT_MS
);
245 /* cr50 uses bytes 3:2 of status register for burst count and
246 * all 4 bytes must be read */
247 static int cr50_i2c_wait_burststs(struct tpm_chip
*chip
, uint8_t mask
,
248 size_t *burst
, int *status
)
253 stopwatch_init_msecs_expire(&sw
, CR50_TIMEOUT_LONG_MS
);
255 while (!stopwatch_expired(&sw
)) {
256 if (cr50_i2c_read(chip
, TPM_STS(chip
->vendor
.locality
),
257 buf
, sizeof(buf
)) != 0) {
258 mdelay(CR50_TIMEOUT_SHORT_MS
);
263 *burst
= read_le16(&buf
[1]);
265 /* Check if mask matches and burst is valid */
266 if ((*status
& mask
) == mask
&&
267 *burst
> 0 && *burst
<= CR50_MAX_BUFSIZE
)
270 mdelay(CR50_TIMEOUT_SHORT_MS
);
273 printk(BIOS_ERR
, "%s: Timeout reading burst and status\n", __func__
);
277 static int cr50_i2c_tis_recv(struct tpm_chip
*chip
, uint8_t *buf
,
280 size_t burstcnt
, current
, len
, expected
;
281 uint8_t addr
= TPM_DATA_FIFO(chip
->vendor
.locality
);
282 uint8_t mask
= TPM_STS_VALID
| TPM_STS_DATA_AVAIL
;
285 if (buf_len
< TPM_HEADER_SIZE
)
288 if (cr50_i2c_wait_burststs(chip
, mask
, &burstcnt
, &status
) < 0) {
289 printk(BIOS_ERR
, "%s: First chunk not available\n", __func__
);
293 /* Read first chunk of burstcnt bytes */
294 if (cr50_i2c_read(chip
, addr
, buf
, burstcnt
) != 0) {
295 printk(BIOS_ERR
, "%s: Read failed\n", __func__
);
299 /* Determine expected data in the return buffer */
300 expected
= read_be32(buf
+ TPM_RSP_SIZE_BYTE
);
301 if (expected
> buf_len
) {
302 printk(BIOS_ERR
, "%s: Too much data: %zu > %zu\n",
303 __func__
, expected
, buf_len
);
307 /* Now read the rest of the data */
309 while (current
< expected
) {
310 /* Read updated burst count and check status */
311 if (cr50_i2c_wait_burststs(chip
, mask
, &burstcnt
, &status
) < 0)
314 len
= min(burstcnt
, expected
- current
);
315 if (cr50_i2c_read(chip
, addr
, buf
+ current
, len
) != 0) {
316 printk(BIOS_ERR
, "%s: Read failed\n", __func__
);
323 /* Ensure TPM is done reading data */
324 if (cr50_i2c_wait_burststs(chip
, TPM_STS_VALID
, &burstcnt
, &status
) < 0)
326 if (status
& TPM_STS_DATA_AVAIL
) {
327 printk(BIOS_ERR
, "%s: Data still available\n", __func__
);
334 /* Abort current transaction if still pending */
335 if (cr50_i2c_tis_status(chip
) & TPM_STS_COMMAND_READY
)
336 cr50_i2c_tis_ready(chip
);
340 static int cr50_i2c_tis_send(struct tpm_chip
*chip
, uint8_t *buf
, size_t len
)
343 size_t burstcnt
, limit
, sent
= 0;
344 uint8_t tpm_go
[4] = { TPM_STS_GO
};
347 stopwatch_init_msecs_expire(&sw
, CR50_TIMEOUT_LONG_MS
);
349 /* Wait until TPM is ready for a command */
350 while (!(cr50_i2c_tis_status(chip
) & TPM_STS_COMMAND_READY
)) {
351 if (stopwatch_expired(&sw
)) {
352 printk(BIOS_ERR
, "%s: Command ready timeout\n",
357 cr50_i2c_tis_ready(chip
);
361 uint8_t mask
= TPM_STS_VALID
;
363 /* Wait for data if this is not the first chunk */
365 mask
|= TPM_STS_DATA_EXPECT
;
367 /* Read burst count and check status */
368 if (cr50_i2c_wait_burststs(chip
, mask
, &burstcnt
, &status
) < 0)
371 /* Use burstcnt - 1 to account for the address byte
372 * that is inserted by cr50_i2c_write() */
373 limit
= min(burstcnt
- 1, len
);
374 if (cr50_i2c_write(chip
, TPM_DATA_FIFO(chip
->vendor
.locality
),
375 &buf
[sent
], limit
) != 0) {
376 printk(BIOS_ERR
, "%s: Write failed\n", __func__
);
384 /* Ensure TPM is not expecting more data */
385 if (cr50_i2c_wait_burststs(chip
, TPM_STS_VALID
, &burstcnt
, &status
) < 0)
387 if (status
& TPM_STS_DATA_EXPECT
) {
388 printk(BIOS_ERR
, "%s: Data still expected\n", __func__
);
392 /* Start the TPM command */
393 if (cr50_i2c_write(chip
, TPM_STS(chip
->vendor
.locality
), tpm_go
,
394 sizeof(tpm_go
)) < 0) {
395 printk(BIOS_ERR
, "%s: Start command failed\n", __func__
);
401 /* Abort current transaction if still pending */
402 if (cr50_i2c_tis_status(chip
) & TPM_STS_COMMAND_READY
)
403 cr50_i2c_tis_ready(chip
);
407 static void cr50_vendor_init(struct tpm_chip
*chip
)
409 memset(&chip
->vendor
, 0, sizeof(struct tpm_vendor_specific
));
410 chip
->vendor
.req_complete_mask
= TPM_STS_DATA_AVAIL
| TPM_STS_VALID
;
411 chip
->vendor
.req_complete_val
= TPM_STS_DATA_AVAIL
| TPM_STS_VALID
;
412 chip
->vendor
.req_canceled
= TPM_STS_COMMAND_READY
;
413 chip
->vendor
.status
= &cr50_i2c_tis_status
;
414 chip
->vendor
.recv
= &cr50_i2c_tis_recv
;
415 chip
->vendor
.send
= &cr50_i2c_tis_send
;
416 chip
->vendor
.cancel
= &cr50_i2c_tis_ready
;
419 int tpm_vendor_probe(unsigned int bus
, uint32_t addr
)
421 struct tpm_inf_dev
*tpm_dev
= car_get_var_ptr(&g_tpm_dev
);
422 struct tpm_chip probe_chip
;
426 long sw_run_duration
= CR50_TIMEOUT_LONG_MS
;
429 tpm_dev
->addr
= addr
;
431 cr50_vendor_init(&probe_chip
);
433 /* Wait for TPM_ACCESS register ValidSts bit to be set */
434 stopwatch_init_msecs_expire(&sw
, sw_run_duration
);
436 ret
= cr50_i2c_read(&probe_chip
, TPM_ACCESS(0), &buf
, 1);
437 if (!ret
&& (buf
& TPM_STS_VALID
)) {
438 sw_run_duration
= stopwatch_duration_msecs(&sw
);
441 mdelay(CR50_TIMEOUT_SHORT_MS
);
442 } while (!stopwatch_expired(&sw
));
445 "%s: ValidSts bit %s(%d) in TPM_ACCESS register after %ld ms\n",
446 __func__
, (buf
& TPM_STS_VALID
) ? "set" : "clear",
447 (buf
& TPM_STS_VALID
) >> 7, sw_run_duration
);
449 /* Claim failure if the ValidSts (bit 7) is clear */
450 if (!(buf
& TPM_STS_VALID
))
456 int tpm_vendor_init(struct tpm_chip
*chip
, unsigned int bus
, uint32_t dev_addr
)
458 struct tpm_inf_dev
*tpm_dev
= car_get_var_ptr(&g_tpm_dev
);
462 printk(BIOS_ERR
, "%s: missing device address\n", __func__
);
467 tpm_dev
->addr
= dev_addr
;
469 cr50_vendor_init(chip
);
471 if (request_locality(chip
, 0) != 0)
474 /* Read four bytes from DID_VID register */
475 if (cr50_i2c_read(chip
, TPM_DID_VID(0), (uint8_t *)&vendor
, 4) < 0)
478 if (vendor
!= CR50_DID_VID
) {
479 printk(BIOS_DEBUG
, "Vendor ID 0x%08x not recognized\n", vendor
);
483 printk(BIOS_DEBUG
, "cr50 TPM 2.0 (i2c %u:0x%02x id 0x%x)\n",
484 bus
, dev_addr
, vendor
>> 16);
490 release_locality(chip
, 1);
494 void tpm_vendor_cleanup(struct tpm_chip
*chip
)
496 release_locality(chip
, 1);