blob: 5973d337d0f09d9f0b86975fb25cf92bd2f264e8 [file] [log] [blame]
Patrick Georgiac959032020-05-05 22:49:26 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Duncan Laurie2ea13c82016-09-19 16:04:39 -07002
Martin Rothcddd6002019-09-23 17:38:27 -06003/* Based on Linux Kernel TPM driver */
4
Duncan Laurie2ea13c82016-09-19 16:04:39 -07005/*
Martin Roth0949e732021-10-01 14:28:22 -06006 * cr50 is a TPM 2.0 capable device that requires special
Duncan Laurie2ea13c82016-09-19 16:04:39 -07007 * handling for the I2C interface.
8 *
9 * - Use an interrupt for transaction status instead of hardcoded delays
10 * - Must use write+wait+read read protocol
11 * - All 4 bytes of status register must be read/written at once
12 * - Burst count max is 63 bytes, and burst count behaves
13 * slightly differently than other I2C TPMs
14 * - When reading from FIFO the full burstcnt must be read
15 * instead of just reading header and determining the remainder
16 */
17
Duncan Laurie2ea13c82016-09-19 16:04:39 -070018#include <commonlib/endian.h>
Elyes HAOUAS361a9352019-12-18 21:26:33 +010019#include <commonlib/helpers.h>
Tim Wawrzynczak1e50dfb2022-02-16 13:48:07 -070020#include <console/console.h>
21#include <delay.h>
22#include <device/i2c_simple.h>
23#include <drivers/tpm/cr50.h>
24#include <endian.h>
25#include <security/tpm/tis.h>
Duncan Laurie2ea13c82016-09-19 16:04:39 -070026#include <string.h>
27#include <types.h>
Duncan Laurie2ea13c82016-09-19 16:04:39 -070028#include <timer.h>
Elyes HAOUASede8dd02019-06-23 06:57:53 +020029
Duncan Laurie2ea13c82016-09-19 16:04:39 -070030#include "tpm.h"
31
Duncan Laurie3727a8d2016-09-19 16:37:46 -070032#define CR50_MAX_BUFSIZE 63
Duncan Laurie469af7b2017-11-07 09:13:19 -080033#define CR50_TIMEOUT_INIT_MS 30000 /* Very long timeout for TPM init */
Duncan Laurie1dc036c2016-09-19 16:49:23 -070034#define CR50_TIMEOUT_LONG_MS 2000 /* Long timeout while waiting for TPM */
35#define CR50_TIMEOUT_SHORT_MS 2 /* Short timeout during transactions */
Duncan Laurie2ea13c82016-09-19 16:04:39 -070036#define CR50_DID_VID 0x00281ae0L
Jes Klinke1430b042022-03-28 14:22:24 -070037#define TI50_DID_VID 0x504a6666L
Duncan Laurie2ea13c82016-09-19 16:04:39 -070038
39struct tpm_inf_dev {
40 int bus;
Sergii Dmytruk86f845a2022-10-29 18:55:24 +030041 int locality;
Duncan Laurie2ea13c82016-09-19 16:04:39 -070042 unsigned int addr;
Duncan Laurie3727a8d2016-09-19 16:37:46 -070043 uint8_t buf[CR50_MAX_BUFSIZE + sizeof(uint8_t)];
Duncan Laurie2ea13c82016-09-19 16:04:39 -070044};
45
Patrick Georgic9b13592019-11-29 11:47:47 +010046static struct tpm_inf_dev tpm_dev;
Duncan Laurie2ea13c82016-09-19 16:04:39 -070047
48/*
Duncan Laurie510cb6a2016-09-19 17:05:45 -070049 * cr50_i2c_read() - read from TPM register
Duncan Laurie2ea13c82016-09-19 16:04:39 -070050 *
51 * @addr: register address to read from
52 * @buffer: provided by caller
53 * @len: number of bytes to read
54 *
55 * 1) send register address byte 'addr' to the TPM
56 * 2) wait for TPM to indicate it is ready
57 * 3) read 'len' bytes of TPM response into the provided 'buffer'
58 *
Jon Murphyd7b8dc92023-09-05 11:36:43 -060059 * Returns TSS Return Code from TCG TPM Structures. See tss_errors.h
Duncan Laurie2ea13c82016-09-19 16:04:39 -070060 */
Jon Murphyd7b8dc92023-09-05 11:36:43 -060061static tpm_result_t cr50_i2c_read(uint8_t addr, uint8_t *buffer, size_t len)
Duncan Laurie2ea13c82016-09-19 16:04:39 -070062{
Patrick Georgic9b13592019-11-29 11:47:47 +010063 if (tpm_dev.addr == 0)
Jon Murphydb4e93b2023-09-05 11:38:59 -060064 return TPM_CB_INVALID_ARG;
Duncan Laurie2ea13c82016-09-19 16:04:39 -070065
Duncan Laurie94cc4852016-09-19 17:22:10 -070066 /* Clear interrupt before starting transaction */
Grzegorz Bernacki7758b472023-06-14 12:01:32 +000067 cr50_plat_irq_status();
Duncan Laurie94cc4852016-09-19 17:22:10 -070068
Duncan Laurie2ea13c82016-09-19 16:04:39 -070069 /* Send the register address byte to the TPM */
Patrick Georgic9b13592019-11-29 11:47:47 +010070 if (i2c_write_raw(tpm_dev.bus, tpm_dev.addr, &addr, 1)) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -070071 printk(BIOS_ERR, "%s: Address write failed\n", __func__);
Jon Murphydb4e93b2023-09-05 11:38:59 -060072 return TPM_CB_COMMUNICATION_ERROR;
Duncan Laurie2ea13c82016-09-19 16:04:39 -070073 }
74
75 /* Wait for TPM to be ready with response data */
Yu-Ping Wuae1e7022022-05-17 09:33:18 +080076 if (cr50_wait_tpm_ready() != CB_SUCCESS)
Jon Murphydb4e93b2023-09-05 11:38:59 -060077 return TPM_CB_TIMEOUT;
Duncan Laurie2ea13c82016-09-19 16:04:39 -070078
79 /* Read response data from the TPM */
Patrick Georgic9b13592019-11-29 11:47:47 +010080 if (i2c_read_raw(tpm_dev.bus, tpm_dev.addr, buffer, len)) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -070081 printk(BIOS_ERR, "%s: Read response failed\n", __func__);
Jon Murphydb4e93b2023-09-05 11:38:59 -060082 return TPM_CB_COMMUNICATION_ERROR;
Duncan Laurie2ea13c82016-09-19 16:04:39 -070083 }
84
Jon Murphyd7b8dc92023-09-05 11:36:43 -060085 return TPM_SUCCESS;
Duncan Laurie2ea13c82016-09-19 16:04:39 -070086}
87
88/*
Duncan Laurie510cb6a2016-09-19 17:05:45 -070089 * cr50_i2c_write() - write to TPM register
Duncan Laurie2ea13c82016-09-19 16:04:39 -070090 *
91 * @addr: register address to write to
92 * @buffer: data to write
93 * @len: number of bytes to write
94 *
95 * 1) prepend the provided address to the provided data
96 * 2) send the address+data to the TPM
97 * 3) wait for TPM to indicate it is done writing
98 *
Jon Murphyd7b8dc92023-09-05 11:36:43 -060099 * Returns TSS Return Code from TCG TPM Structures. See tss_errors.h
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700100 */
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600101static tpm_result_t cr50_i2c_write(uint8_t addr, const uint8_t *buffer, size_t len)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700102{
Patrick Georgic9b13592019-11-29 11:47:47 +0100103 if (tpm_dev.addr == 0)
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600104 return TPM_CB_INVALID_ARG;
Duncan Laurie3727a8d2016-09-19 16:37:46 -0700105 if (len > CR50_MAX_BUFSIZE)
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600106 return TPM_CB_INVALID_ARG;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700107
108 /* Prepend the 'register address' to the buffer */
Patrick Georgic9b13592019-11-29 11:47:47 +0100109 tpm_dev.buf[0] = addr;
110 memcpy(tpm_dev.buf + 1, buffer, len);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700111
Duncan Laurie94cc4852016-09-19 17:22:10 -0700112 /* Clear interrupt before starting transaction */
Grzegorz Bernacki7758b472023-06-14 12:01:32 +0000113 cr50_plat_irq_status();
Duncan Laurie94cc4852016-09-19 17:22:10 -0700114
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700115 /* Send write request buffer with address */
Patrick Georgic9b13592019-11-29 11:47:47 +0100116 if (i2c_write_raw(tpm_dev.bus, tpm_dev.addr, tpm_dev.buf, len + 1)) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700117 printk(BIOS_ERR, "%s: Error writing to TPM\n", __func__);
Jon Murphydb4e93b2023-09-05 11:38:59 -0600118 return TPM_CB_COMMUNICATION_ERROR;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700119 }
120
121 /* Wait for TPM to be ready */
Jon Murphydb4e93b2023-09-05 11:38:59 -0600122 return cr50_wait_tpm_ready() == CB_SUCCESS ? TPM_SUCCESS : TPM_CB_TIMEOUT;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700123}
124
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800125/*
Martin Roth74f18772023-09-03 21:38:29 -0600126 * Cr50 processes reset requests asynchronously and conceivably could be busy
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800127 * executing a long command and not reacting to the reset pulse for a while.
128 *
129 * This function will make sure that the AP does not proceed with boot until
130 * TPM finished reset processing.
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600131 *
132 * Returns TSS Return Code from TCG TPM Structures. See tss_errors.h
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800133 */
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600134static tpm_result_t process_reset(void)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700135{
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800136 struct stopwatch sw;
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600137 tpm_result_t rc = TPM_SUCCESS;
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800138 uint8_t access;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700139
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800140 /*
141 * Locality is released by TPM reset.
142 *
143 * If locality is taken at this point, this could be due to the fact
144 * that the TPM is performing a long operation and has not processed
145 * reset request yet. We'll wait up to CR50_TIMEOUT_INIT_MS and see if
146 * it releases locality when reset is processed.
147 */
148 stopwatch_init_msecs_expire(&sw, CR50_TIMEOUT_INIT_MS);
149 do {
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800150 const uint8_t mask =
151 TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700152
Jon Murphy24604812023-09-05 10:37:05 -0600153 rc = cr50_i2c_read(TPM_ACCESS(0),
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800154 &access, sizeof(access));
Jon Murphy24604812023-09-05 10:37:05 -0600155 if (rc || ((access & mask) == mask)) {
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800156 /*
157 * Don't bombard the chip with traffic, let it keep
158 * processing the command.
159 */
160 mdelay(2);
161 continue;
162 }
163
Rob Barnesd522f382022-09-12 06:31:47 -0600164 printk(BIOS_INFO, "TPM ready after %lld ms\n",
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800165 stopwatch_duration_msecs(&sw));
166
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600167 return TPM_SUCCESS;
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800168 } while (!stopwatch_expired(&sw));
169
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600170 if (rc) {
171 printk(BIOS_ERR, "Failed to read TPM with error %d\n", rc);
172 return rc;
173 } else
Richard Spiegel7c1e9592018-08-09 14:41:17 -0700174 printk(BIOS_ERR,
Rob Barnesd522f382022-09-12 06:31:47 -0600175 "TPM failed to reset after %lld ms, status: %#x\n",
Richard Spiegel7c1e9592018-08-09 14:41:17 -0700176 stopwatch_duration_msecs(&sw), access);
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600177 return TPM_CB_FAIL;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700178}
179
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800180/*
181 * Locality could be already claimed (if this is a later coreboot stage and
182 * the RO did not release it), or not yet claimed, if this is verstage or the
183 * older RO did release it.
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600184 *
185 * Returns TSS Return Code from TCG TPM Structures. See tss_errors.h
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800186 */
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600187static tpm_result_t claim_locality(void)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700188{
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800189 uint8_t access;
190 const uint8_t mask = TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY;
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600191 tpm_result_t rc = TPM_SUCCESS;
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700192
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600193 rc = cr50_i2c_read(TPM_ACCESS(0), &access, sizeof(access));
194 if (rc)
195 return rc;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700196
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800197 if ((access & mask) == mask) {
198 printk(BIOS_INFO, "Locality already claimed\n");
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600199 return TPM_SUCCESS;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700200 }
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800201
202 access = TPM_ACCESS_REQUEST_USE;
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600203 rc = cr50_i2c_write(TPM_ACCESS(0),
204 &access, sizeof(access));
205 if (rc)
206 return rc;
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800207
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600208 rc = cr50_i2c_read(TPM_ACCESS(0), &access, sizeof(access));
209 if (rc)
210 return rc;
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800211
212 if ((access & mask) != mask) {
213 printk(BIOS_INFO, "Failed to claim locality.\n");
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600214 return TPM_CB_FAIL;
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800215 }
216
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600217 return TPM_SUCCESS;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700218}
219
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600220/*
221 * cr50 requires all 4 bytes of status register to be read
222 *
223 * Returns lowest 8-bits of the TIS Status register value
224 * see tis_status bit mask enumerated type in tis.h.
225 * Return 0 on error.
226 */
Sergii Dmytruk86f845a2022-10-29 18:55:24 +0300227static uint8_t cr50_i2c_tis_status(void)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700228{
229 uint8_t buf[4];
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600230 tpm_result_t rc = cr50_i2c_read(TPM_STS(tpm_dev.locality), buf, sizeof(buf));
231 if (rc) {
232 printk(BIOS_ERR, "%s: Failed to read status with error %#x\n", __func__, rc);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700233 return 0;
234 }
235 return buf[0];
236}
237
238/* cr50 requires all 4 bytes of status register to be written */
Sergii Dmytruk86f845a2022-10-29 18:55:24 +0300239static void cr50_i2c_tis_ready(void)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700240{
241 uint8_t buf[4] = { TPM_STS_COMMAND_READY };
Sergii Dmytruk86f845a2022-10-29 18:55:24 +0300242 cr50_i2c_write(TPM_STS(tpm_dev.locality), buf, sizeof(buf));
Duncan Laurie1dc036c2016-09-19 16:49:23 -0700243 mdelay(CR50_TIMEOUT_SHORT_MS);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700244}
245
246/* cr50 uses bytes 3:2 of status register for burst count and
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600247 * all 4 bytes must be read
248 *
249 * Returns TSS Return Code from TCG TPM Structures. See tss_errors.h
250 */
251static tpm_result_t cr50_i2c_wait_burststs(uint8_t mask, size_t *burst, int *status)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700252{
253 uint8_t buf[4];
254 struct stopwatch sw;
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600255 tpm_result_t rc = TPM_SUCCESS;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700256
Duncan Laurie1dc036c2016-09-19 16:49:23 -0700257 stopwatch_init_msecs_expire(&sw, CR50_TIMEOUT_LONG_MS);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700258
259 while (!stopwatch_expired(&sw)) {
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600260 rc = cr50_i2c_read(TPM_STS(tpm_dev.locality), buf, sizeof(buf));
261 if (rc) {
Duncan Laurie1dc036c2016-09-19 16:49:23 -0700262 mdelay(CR50_TIMEOUT_SHORT_MS);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700263 continue;
264 }
265
266 *status = buf[0];
267 *burst = read_le16(&buf[1]);
268
269 /* Check if mask matches and burst is valid */
270 if ((*status & mask) == mask &&
Duncan Laurie3727a8d2016-09-19 16:37:46 -0700271 *burst > 0 && *burst <= CR50_MAX_BUFSIZE)
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600272 return TPM_SUCCESS;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700273
Duncan Laurie1dc036c2016-09-19 16:49:23 -0700274 mdelay(CR50_TIMEOUT_SHORT_MS);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700275 }
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600276 printk(BIOS_ERR, "%s: Timeout reading burst and status with error %#x\n", __func__, rc);
277 if (rc)
278 return rc;
Jon Murphydb4e93b2023-09-05 11:38:59 -0600279 return TPM_CB_COMMUNICATION_ERROR;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700280}
281
Sergii Dmytruk86f845a2022-10-29 18:55:24 +0300282static int cr50_i2c_tis_recv(uint8_t *buf, size_t buf_len)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700283{
284 size_t burstcnt, current, len, expected;
Sergii Dmytruk86f845a2022-10-29 18:55:24 +0300285 uint8_t addr = TPM_DATA_FIFO(tpm_dev.locality);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700286 uint8_t mask = TPM_STS_VALID | TPM_STS_DATA_AVAIL;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700287 int status;
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600288 tpm_result_t rc = TPM_SUCCESS;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700289
290 if (buf_len < TPM_HEADER_SIZE)
Duncan Laurief235a9b2016-09-19 17:19:10 -0700291 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700292
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600293 rc = cr50_i2c_wait_burststs(mask, &burstcnt, &status);
294 if (rc) {
295 printk(BIOS_ERR, "%s: First chunk not available with error %#x\n", __func__, rc);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700296 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700297 }
298
299 /* Read first chunk of burstcnt bytes */
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600300 rc = cr50_i2c_read(addr, buf, burstcnt);
301 if (rc) {
302 printk(BIOS_ERR, "%s: Read failed with error %#x\n", __func__, rc);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700303 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700304 }
305
306 /* Determine expected data in the return buffer */
307 expected = read_be32(buf + TPM_RSP_SIZE_BYTE);
308 if (expected > buf_len) {
309 printk(BIOS_ERR, "%s: Too much data: %zu > %zu\n",
310 __func__, expected, buf_len);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700311 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700312 }
313
314 /* Now read the rest of the data */
315 current = burstcnt;
316 while (current < expected) {
317 /* Read updated burst count and check status */
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600318 if (cr50_i2c_wait_burststs(mask, &burstcnt, &status))
Duncan Laurief235a9b2016-09-19 17:19:10 -0700319 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700320
Elyes HAOUAS361a9352019-12-18 21:26:33 +0100321 len = MIN(burstcnt, expected - current);
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600322 rc = cr50_i2c_read(addr, buf + current, len);
323 if (rc) {
324 printk(BIOS_ERR, "%s: Read failed with error %#x\n", __func__, rc);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700325 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700326 }
327
328 current += len;
329 }
330
331 /* Ensure TPM is done reading data */
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600332 if (cr50_i2c_wait_burststs(TPM_STS_VALID, &burstcnt, &status))
Duncan Laurief235a9b2016-09-19 17:19:10 -0700333 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700334 if (status & TPM_STS_DATA_AVAIL) {
335 printk(BIOS_ERR, "%s: Data still available\n", __func__);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700336 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700337 }
338
Duncan Laurief235a9b2016-09-19 17:19:10 -0700339 return current;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700340
Duncan Laurief235a9b2016-09-19 17:19:10 -0700341out_err:
342 /* Abort current transaction if still pending */
Sergii Dmytruk86f845a2022-10-29 18:55:24 +0300343 if (cr50_i2c_tis_status() & TPM_STS_COMMAND_READY)
344 cr50_i2c_tis_ready();
Duncan Laurief235a9b2016-09-19 17:19:10 -0700345 return -1;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700346}
347
Sergii Dmytruk86f845a2022-10-29 18:55:24 +0300348static int cr50_i2c_tis_send(uint8_t *buf, size_t len)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700349{
350 int status;
351 size_t burstcnt, limit, sent = 0;
352 uint8_t tpm_go[4] = { TPM_STS_GO };
353 struct stopwatch sw;
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600354 tpm_result_t rc = TPM_SUCCESS;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700355
Duncan Laurie1dc036c2016-09-19 16:49:23 -0700356 stopwatch_init_msecs_expire(&sw, CR50_TIMEOUT_LONG_MS);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700357
358 /* Wait until TPM is ready for a command */
Sergii Dmytruk86f845a2022-10-29 18:55:24 +0300359 while (!(cr50_i2c_tis_status() & TPM_STS_COMMAND_READY)) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700360 if (stopwatch_expired(&sw)) {
361 printk(BIOS_ERR, "%s: Command ready timeout\n",
362 __func__);
363 return -1;
364 }
365
Sergii Dmytruk86f845a2022-10-29 18:55:24 +0300366 cr50_i2c_tis_ready();
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700367 }
368
369 while (len > 0) {
Duncan Laurief235a9b2016-09-19 17:19:10 -0700370 uint8_t mask = TPM_STS_VALID;
371
372 /* Wait for data if this is not the first chunk */
373 if (sent > 0)
374 mask |= TPM_STS_DATA_EXPECT;
375
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700376 /* Read burst count and check status */
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600377 if (cr50_i2c_wait_burststs(mask, &burstcnt, &status))
Duncan Laurief235a9b2016-09-19 17:19:10 -0700378 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700379
380 /* Use burstcnt - 1 to account for the address byte
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700381 * that is inserted by cr50_i2c_write() */
Elyes HAOUAS361a9352019-12-18 21:26:33 +0100382 limit = MIN(burstcnt - 1, len);
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600383 rc = cr50_i2c_write(TPM_DATA_FIFO(tpm_dev.locality), &buf[sent], limit);
384 if (rc) {
385 printk(BIOS_ERR, "%s: Write failed with error %#x\n", __func__, rc);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700386 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700387 }
388
389 sent += limit;
390 len -= limit;
391 }
392
393 /* Ensure TPM is not expecting more data */
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600394 if (cr50_i2c_wait_burststs(TPM_STS_VALID, &burstcnt, &status))
Duncan Laurief235a9b2016-09-19 17:19:10 -0700395 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700396 if (status & TPM_STS_DATA_EXPECT) {
397 printk(BIOS_ERR, "%s: Data still expected\n", __func__);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700398 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700399 }
400
401 /* Start the TPM command */
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600402 rc = cr50_i2c_write(TPM_STS(tpm_dev.locality), tpm_go, sizeof(tpm_go));
403 if (rc) {
404 printk(BIOS_ERR, "%s: Start command failed with error %#x\n", __func__, rc);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700405 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700406 }
407 return sent;
408
Duncan Laurief235a9b2016-09-19 17:19:10 -0700409out_err:
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700410 /* Abort current transaction if still pending */
Sergii Dmytruk86f845a2022-10-29 18:55:24 +0300411 if (cr50_i2c_tis_status() & TPM_STS_COMMAND_READY)
412 cr50_i2c_tis_ready();
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700413 return -1;
414}
415
416static void cr50_vendor_init(struct tpm_chip *chip)
417{
Sergii Dmytruk86f845a2022-10-29 18:55:24 +0300418 chip->req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID;
419 chip->req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID;
420 chip->req_canceled = TPM_STS_COMMAND_READY;
421 chip->status = &cr50_i2c_tis_status;
422 chip->recv = &cr50_i2c_tis_recv;
423 chip->send = &cr50_i2c_tis_send;
424 chip->cancel = &cr50_i2c_tis_ready;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700425}
426
Sergii Dmytrukfebf9b92022-10-31 15:30:15 +0200427tpm_result_t tpm_vendor_probe(unsigned int bus, uint32_t addr, enum tpm_family *family)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700428{
Sergii Dmytrukfebf9b92022-10-31 15:30:15 +0200429 /* cr50 is TPM2 */
430 if (family != NULL)
431 *family = TPM_2;
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600432 return TPM_SUCCESS;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700433}
434
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600435static tpm_result_t cr50_i2c_probe(uint32_t *did_vid)
Keith Short51436352018-12-17 14:21:46 -0700436{
437 int retries;
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600438 tpm_result_t rc = TPM_SUCCESS;
Keith Short51436352018-12-17 14:21:46 -0700439
440 /*
Rob Barnes22372f42022-02-11 07:59:21 -0700441 * 1s should be enough to synchronize with the TPM even under the
Keith Short51436352018-12-17 14:21:46 -0700442 * worst nested reset request conditions. In vast majority of cases
Rob Barnes22372f42022-02-11 07:59:21 -0700443 * there would be no wait at all. If this probe fails, boot likely
444 * cannot proceed, so an extra long timeout is appropriate.
Keith Short51436352018-12-17 14:21:46 -0700445 */
446 printk(BIOS_INFO, "Probing TPM I2C: ");
447
Rob Barnes22372f42022-02-11 07:59:21 -0700448 for (retries = 100; retries > 0; retries--) {
Tim Wawrzynczakeb1891a2022-02-08 12:49:31 -0700449 rc = cr50_i2c_read(TPM_DID_VID(0), (uint8_t *)did_vid, 4);
Keith Short51436352018-12-17 14:21:46 -0700450
451 /* Exit once DID and VID verified */
Jes Klinke1430b042022-03-28 14:22:24 -0700452 if (!rc && (*did_vid == CR50_DID_VID || *did_vid == TI50_DID_VID)) {
Keith Short51436352018-12-17 14:21:46 -0700453 printk(BIOS_INFO, "done! DID_VID 0x%08x\n", *did_vid);
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600454 return TPM_SUCCESS;
Keith Short51436352018-12-17 14:21:46 -0700455 }
456
457 /* TPM might be resetting, let's retry in a bit. */
458 mdelay(10);
459 printk(BIOS_INFO, ".");
460 }
461
462 /*
463 * I2C reads failed, or the DID and VID didn't match
464 */
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600465 if (!rc) {
466 printk(BIOS_ERR, "DID_VID 0x%08x not recognized\n", *did_vid);
467 return TPM_CB_FAIL;
468 }
Jon Murphydb4e93b2023-09-05 11:38:59 -0600469 return TPM_CB_COMMUNICATION_ERROR;
Keith Short51436352018-12-17 14:21:46 -0700470}
471
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600472tpm_result_t tpm_vendor_init(struct tpm_chip *chip, unsigned int bus, uint32_t dev_addr)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700473{
Keith Short51436352018-12-17 14:21:46 -0700474 uint32_t did_vid = 0;
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600475 tpm_result_t rc = TPM_SUCCESS;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700476
477 if (dev_addr == 0) {
478 printk(BIOS_ERR, "%s: missing device address\n", __func__);
Jon Murphydb4e93b2023-09-05 11:38:59 -0600479 return TPM_CB_INVALID_ARG;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700480 }
481
Patrick Georgic9b13592019-11-29 11:47:47 +0100482 tpm_dev.bus = bus;
483 tpm_dev.addr = dev_addr;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700484
485 cr50_vendor_init(chip);
486
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600487 rc = cr50_i2c_probe(&did_vid);
488 if (rc)
489 return rc;
Keith Short51436352018-12-17 14:21:46 -0700490
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600491 if (ENV_SEPARATE_VERSTAGE || ENV_BOOTBLOCK) {
492 rc = process_reset();
493 if (rc)
494 return rc;
495 }
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800496
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600497 rc = claim_locality();
498 if (rc)
499 return rc;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700500
Jon Murphy53fc6672023-09-26 21:05:37 -0600501 printk(BIOS_DEBUG, "cr50 TPM 2.0 (i2c %u:0x%02x id %#x)\n",
Keith Short51436352018-12-17 14:21:46 -0700502 bus, dev_addr, did_vid >> 16);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700503
Jes Klinke1430b042022-03-28 14:22:24 -0700504 if (tpm_first_access_this_boot()) {
Tim Wawrzynczak1e50dfb2022-02-16 13:48:07 -0700505 /* This is called for the side-effect of printing the version string. */
Jes Klinke1430b042022-03-28 14:22:24 -0700506 cr50_get_firmware_version(NULL);
507 cr50_set_board_cfg();
Tim Wawrzynczak1e50dfb2022-02-16 13:48:07 -0700508 }
509
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600510 return TPM_SUCCESS;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700511}
512
Subrata Banik60b2ab82022-03-09 12:55:34 +0530513enum cb_err tis_vendor_write(unsigned int addr, const void *buffer, size_t bytes)
Tim Wawrzynczak1e50dfb2022-02-16 13:48:07 -0700514{
515 return cr50_i2c_write(addr & 0xff, buffer, bytes) ? CB_ERR : CB_SUCCESS;
516}
517
Subrata Banik60b2ab82022-03-09 12:55:34 +0530518enum cb_err tis_vendor_read(unsigned int addr, void *buffer, size_t bytes)
Tim Wawrzynczak1e50dfb2022-02-16 13:48:07 -0700519{
520 return cr50_i2c_read(addr & 0xff, buffer, bytes) ? CB_ERR : CB_SUCCESS;
521}