blob: 5f5ca66d5f11d0fbe7191421cc80ac4cd2c226b9 [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
Jett Rinkd41ad722024-06-10 09:31:14 -060037#define TI50_DT_DID_VID 0x504a6666L
38#define TI50_OT_DID_VID 0x50666666L
Duncan Laurie2ea13c82016-09-19 16:04:39 -070039
40struct tpm_inf_dev {
41 int bus;
Sergii Dmytruk86f845a2022-10-29 18:55:24 +030042 int locality;
Duncan Laurie2ea13c82016-09-19 16:04:39 -070043 unsigned int addr;
Duncan Laurie3727a8d2016-09-19 16:37:46 -070044 uint8_t buf[CR50_MAX_BUFSIZE + sizeof(uint8_t)];
Duncan Laurie2ea13c82016-09-19 16:04:39 -070045};
46
Patrick Georgic9b13592019-11-29 11:47:47 +010047static struct tpm_inf_dev tpm_dev;
Duncan Laurie2ea13c82016-09-19 16:04:39 -070048
49/*
Duncan Laurie510cb6a2016-09-19 17:05:45 -070050 * cr50_i2c_read() - read from TPM register
Duncan Laurie2ea13c82016-09-19 16:04:39 -070051 *
52 * @addr: register address to read from
53 * @buffer: provided by caller
54 * @len: number of bytes to read
55 *
56 * 1) send register address byte 'addr' to the TPM
57 * 2) wait for TPM to indicate it is ready
58 * 3) read 'len' bytes of TPM response into the provided 'buffer'
59 *
Jon Murphyd7b8dc92023-09-05 11:36:43 -060060 * Returns TSS Return Code from TCG TPM Structures. See tss_errors.h
Duncan Laurie2ea13c82016-09-19 16:04:39 -070061 */
Jon Murphyd7b8dc92023-09-05 11:36:43 -060062static tpm_result_t cr50_i2c_read(uint8_t addr, uint8_t *buffer, size_t len)
Duncan Laurie2ea13c82016-09-19 16:04:39 -070063{
Patrick Georgic9b13592019-11-29 11:47:47 +010064 if (tpm_dev.addr == 0)
Jon Murphydb4e93b2023-09-05 11:38:59 -060065 return TPM_CB_INVALID_ARG;
Duncan Laurie2ea13c82016-09-19 16:04:39 -070066
Duncan Laurie94cc4852016-09-19 17:22:10 -070067 /* Clear interrupt before starting transaction */
Grzegorz Bernacki7758b472023-06-14 12:01:32 +000068 cr50_plat_irq_status();
Duncan Laurie94cc4852016-09-19 17:22:10 -070069
Duncan Laurie2ea13c82016-09-19 16:04:39 -070070 /* Send the register address byte to the TPM */
Patrick Georgic9b13592019-11-29 11:47:47 +010071 if (i2c_write_raw(tpm_dev.bus, tpm_dev.addr, &addr, 1)) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -070072 printk(BIOS_ERR, "%s: Address write failed\n", __func__);
Jon Murphydb4e93b2023-09-05 11:38:59 -060073 return TPM_CB_COMMUNICATION_ERROR;
Duncan Laurie2ea13c82016-09-19 16:04:39 -070074 }
75
76 /* Wait for TPM to be ready with response data */
Yu-Ping Wuae1e7022022-05-17 09:33:18 +080077 if (cr50_wait_tpm_ready() != CB_SUCCESS)
Jon Murphydb4e93b2023-09-05 11:38:59 -060078 return TPM_CB_TIMEOUT;
Duncan Laurie2ea13c82016-09-19 16:04:39 -070079
80 /* Read response data from the TPM */
Patrick Georgic9b13592019-11-29 11:47:47 +010081 if (i2c_read_raw(tpm_dev.bus, tpm_dev.addr, buffer, len)) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -070082 printk(BIOS_ERR, "%s: Read response failed\n", __func__);
Jon Murphydb4e93b2023-09-05 11:38:59 -060083 return TPM_CB_COMMUNICATION_ERROR;
Duncan Laurie2ea13c82016-09-19 16:04:39 -070084 }
85
Jon Murphyd7b8dc92023-09-05 11:36:43 -060086 return TPM_SUCCESS;
Duncan Laurie2ea13c82016-09-19 16:04:39 -070087}
88
89/*
Duncan Laurie510cb6a2016-09-19 17:05:45 -070090 * cr50_i2c_write() - write to TPM register
Duncan Laurie2ea13c82016-09-19 16:04:39 -070091 *
92 * @addr: register address to write to
93 * @buffer: data to write
94 * @len: number of bytes to write
95 *
96 * 1) prepend the provided address to the provided data
97 * 2) send the address+data to the TPM
98 * 3) wait for TPM to indicate it is done writing
99 *
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600100 * Returns TSS Return Code from TCG TPM Structures. See tss_errors.h
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700101 */
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600102static tpm_result_t cr50_i2c_write(uint8_t addr, const uint8_t *buffer, size_t len)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700103{
Patrick Georgic9b13592019-11-29 11:47:47 +0100104 if (tpm_dev.addr == 0)
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600105 return TPM_CB_INVALID_ARG;
Duncan Laurie3727a8d2016-09-19 16:37:46 -0700106 if (len > CR50_MAX_BUFSIZE)
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600107 return TPM_CB_INVALID_ARG;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700108
109 /* Prepend the 'register address' to the buffer */
Patrick Georgic9b13592019-11-29 11:47:47 +0100110 tpm_dev.buf[0] = addr;
111 memcpy(tpm_dev.buf + 1, buffer, len);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700112
Duncan Laurie94cc4852016-09-19 17:22:10 -0700113 /* Clear interrupt before starting transaction */
Grzegorz Bernacki7758b472023-06-14 12:01:32 +0000114 cr50_plat_irq_status();
Duncan Laurie94cc4852016-09-19 17:22:10 -0700115
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700116 /* Send write request buffer with address */
Patrick Georgic9b13592019-11-29 11:47:47 +0100117 if (i2c_write_raw(tpm_dev.bus, tpm_dev.addr, tpm_dev.buf, len + 1)) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700118 printk(BIOS_ERR, "%s: Error writing to TPM\n", __func__);
Jon Murphydb4e93b2023-09-05 11:38:59 -0600119 return TPM_CB_COMMUNICATION_ERROR;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700120 }
121
122 /* Wait for TPM to be ready */
Jon Murphydb4e93b2023-09-05 11:38:59 -0600123 return cr50_wait_tpm_ready() == CB_SUCCESS ? TPM_SUCCESS : TPM_CB_TIMEOUT;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700124}
125
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800126/*
Martin Roth74f18772023-09-03 21:38:29 -0600127 * Cr50 processes reset requests asynchronously and conceivably could be busy
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800128 * executing a long command and not reacting to the reset pulse for a while.
129 *
130 * This function will make sure that the AP does not proceed with boot until
131 * TPM finished reset processing.
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600132 *
133 * Returns TSS Return Code from TCG TPM Structures. See tss_errors.h
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800134 */
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600135static tpm_result_t process_reset(void)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700136{
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800137 struct stopwatch sw;
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600138 tpm_result_t rc = TPM_SUCCESS;
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800139 uint8_t access;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700140
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800141 /*
142 * Locality is released by TPM reset.
143 *
144 * If locality is taken at this point, this could be due to the fact
145 * that the TPM is performing a long operation and has not processed
146 * reset request yet. We'll wait up to CR50_TIMEOUT_INIT_MS and see if
147 * it releases locality when reset is processed.
148 */
149 stopwatch_init_msecs_expire(&sw, CR50_TIMEOUT_INIT_MS);
150 do {
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800151 const uint8_t mask =
152 TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700153
Jon Murphy24604812023-09-05 10:37:05 -0600154 rc = cr50_i2c_read(TPM_ACCESS(0),
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800155 &access, sizeof(access));
Jon Murphy24604812023-09-05 10:37:05 -0600156 if (rc || ((access & mask) == mask)) {
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800157 /*
158 * Don't bombard the chip with traffic, let it keep
159 * processing the command.
160 */
161 mdelay(2);
162 continue;
163 }
164
Rob Barnesd522f382022-09-12 06:31:47 -0600165 printk(BIOS_INFO, "TPM ready after %lld ms\n",
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800166 stopwatch_duration_msecs(&sw));
167
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600168 return TPM_SUCCESS;
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800169 } while (!stopwatch_expired(&sw));
170
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600171 if (rc) {
172 printk(BIOS_ERR, "Failed to read TPM with error %d\n", rc);
173 return rc;
174 } else
Richard Spiegel7c1e9592018-08-09 14:41:17 -0700175 printk(BIOS_ERR,
Rob Barnesd522f382022-09-12 06:31:47 -0600176 "TPM failed to reset after %lld ms, status: %#x\n",
Richard Spiegel7c1e9592018-08-09 14:41:17 -0700177 stopwatch_duration_msecs(&sw), access);
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600178 return TPM_CB_FAIL;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700179}
180
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800181/*
182 * Locality could be already claimed (if this is a later coreboot stage and
183 * the RO did not release it), or not yet claimed, if this is verstage or the
184 * older RO did release it.
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600185 *
186 * Returns TSS Return Code from TCG TPM Structures. See tss_errors.h
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800187 */
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600188static tpm_result_t claim_locality(void)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700189{
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800190 uint8_t access;
191 const uint8_t mask = TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY;
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600192 tpm_result_t rc = TPM_SUCCESS;
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700193
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600194 rc = cr50_i2c_read(TPM_ACCESS(0), &access, sizeof(access));
195 if (rc)
196 return rc;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700197
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800198 if ((access & mask) == mask) {
199 printk(BIOS_INFO, "Locality already claimed\n");
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600200 return TPM_SUCCESS;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700201 }
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800202
203 access = TPM_ACCESS_REQUEST_USE;
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600204 rc = cr50_i2c_write(TPM_ACCESS(0),
205 &access, sizeof(access));
206 if (rc)
207 return rc;
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800208
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600209 rc = cr50_i2c_read(TPM_ACCESS(0), &access, sizeof(access));
210 if (rc)
211 return rc;
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800212
213 if ((access & mask) != mask) {
214 printk(BIOS_INFO, "Failed to claim locality.\n");
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600215 return TPM_CB_FAIL;
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800216 }
217
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600218 return TPM_SUCCESS;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700219}
220
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600221/*
222 * cr50 requires all 4 bytes of status register to be read
223 *
224 * Returns lowest 8-bits of the TIS Status register value
225 * see tis_status bit mask enumerated type in tis.h.
226 * Return 0 on error.
227 */
Sergii Dmytruk86f845a2022-10-29 18:55:24 +0300228static uint8_t cr50_i2c_tis_status(void)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700229{
230 uint8_t buf[4];
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600231 tpm_result_t rc = cr50_i2c_read(TPM_STS(tpm_dev.locality), buf, sizeof(buf));
232 if (rc) {
233 printk(BIOS_ERR, "%s: Failed to read status with error %#x\n", __func__, rc);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700234 return 0;
235 }
236 return buf[0];
237}
238
239/* cr50 requires all 4 bytes of status register to be written */
Sergii Dmytruk86f845a2022-10-29 18:55:24 +0300240static void cr50_i2c_tis_ready(void)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700241{
242 uint8_t buf[4] = { TPM_STS_COMMAND_READY };
Sergii Dmytruk86f845a2022-10-29 18:55:24 +0300243 cr50_i2c_write(TPM_STS(tpm_dev.locality), buf, sizeof(buf));
Duncan Laurie1dc036c2016-09-19 16:49:23 -0700244 mdelay(CR50_TIMEOUT_SHORT_MS);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700245}
246
247/* cr50 uses bytes 3:2 of status register for burst count and
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600248 * all 4 bytes must be read
249 *
250 * Returns TSS Return Code from TCG TPM Structures. See tss_errors.h
251 */
252static tpm_result_t cr50_i2c_wait_burststs(uint8_t mask, size_t *burst, int *status)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700253{
254 uint8_t buf[4];
255 struct stopwatch sw;
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600256 tpm_result_t rc = TPM_SUCCESS;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700257
Duncan Laurie1dc036c2016-09-19 16:49:23 -0700258 stopwatch_init_msecs_expire(&sw, CR50_TIMEOUT_LONG_MS);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700259
260 while (!stopwatch_expired(&sw)) {
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600261 rc = cr50_i2c_read(TPM_STS(tpm_dev.locality), buf, sizeof(buf));
262 if (rc) {
Duncan Laurie1dc036c2016-09-19 16:49:23 -0700263 mdelay(CR50_TIMEOUT_SHORT_MS);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700264 continue;
265 }
266
267 *status = buf[0];
268 *burst = read_le16(&buf[1]);
269
270 /* Check if mask matches and burst is valid */
271 if ((*status & mask) == mask &&
Duncan Laurie3727a8d2016-09-19 16:37:46 -0700272 *burst > 0 && *burst <= CR50_MAX_BUFSIZE)
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600273 return TPM_SUCCESS;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700274
Duncan Laurie1dc036c2016-09-19 16:49:23 -0700275 mdelay(CR50_TIMEOUT_SHORT_MS);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700276 }
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600277 printk(BIOS_ERR, "%s: Timeout reading burst and status with error %#x\n", __func__, rc);
278 if (rc)
279 return rc;
Jon Murphydb4e93b2023-09-05 11:38:59 -0600280 return TPM_CB_COMMUNICATION_ERROR;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700281}
282
Sergii Dmytruk86f845a2022-10-29 18:55:24 +0300283static int cr50_i2c_tis_recv(uint8_t *buf, size_t buf_len)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700284{
285 size_t burstcnt, current, len, expected;
Sergii Dmytruk86f845a2022-10-29 18:55:24 +0300286 uint8_t addr = TPM_DATA_FIFO(tpm_dev.locality);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700287 uint8_t mask = TPM_STS_VALID | TPM_STS_DATA_AVAIL;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700288 int status;
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600289 tpm_result_t rc = TPM_SUCCESS;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700290
291 if (buf_len < TPM_HEADER_SIZE)
Duncan Laurief235a9b2016-09-19 17:19:10 -0700292 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700293
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600294 rc = cr50_i2c_wait_burststs(mask, &burstcnt, &status);
295 if (rc) {
296 printk(BIOS_ERR, "%s: First chunk not available with error %#x\n", __func__, rc);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700297 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700298 }
299
300 /* Read first chunk of burstcnt bytes */
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600301 rc = cr50_i2c_read(addr, buf, burstcnt);
302 if (rc) {
303 printk(BIOS_ERR, "%s: Read failed with error %#x\n", __func__, rc);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700304 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700305 }
306
307 /* Determine expected data in the return buffer */
308 expected = read_be32(buf + TPM_RSP_SIZE_BYTE);
309 if (expected > buf_len) {
310 printk(BIOS_ERR, "%s: Too much data: %zu > %zu\n",
311 __func__, expected, buf_len);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700312 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700313 }
314
315 /* Now read the rest of the data */
316 current = burstcnt;
317 while (current < expected) {
318 /* Read updated burst count and check status */
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600319 if (cr50_i2c_wait_burststs(mask, &burstcnt, &status))
Duncan Laurief235a9b2016-09-19 17:19:10 -0700320 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700321
Elyes HAOUAS361a9352019-12-18 21:26:33 +0100322 len = MIN(burstcnt, expected - current);
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600323 rc = cr50_i2c_read(addr, buf + current, len);
324 if (rc) {
325 printk(BIOS_ERR, "%s: Read failed with error %#x\n", __func__, rc);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700326 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700327 }
328
329 current += len;
330 }
331
332 /* Ensure TPM is done reading data */
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600333 if (cr50_i2c_wait_burststs(TPM_STS_VALID, &burstcnt, &status))
Duncan Laurief235a9b2016-09-19 17:19:10 -0700334 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700335 if (status & TPM_STS_DATA_AVAIL) {
336 printk(BIOS_ERR, "%s: Data still available\n", __func__);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700337 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700338 }
339
Duncan Laurief235a9b2016-09-19 17:19:10 -0700340 return current;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700341
Duncan Laurief235a9b2016-09-19 17:19:10 -0700342out_err:
343 /* Abort current transaction if still pending */
Sergii Dmytruk86f845a2022-10-29 18:55:24 +0300344 if (cr50_i2c_tis_status() & TPM_STS_COMMAND_READY)
345 cr50_i2c_tis_ready();
Duncan Laurief235a9b2016-09-19 17:19:10 -0700346 return -1;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700347}
348
Sergii Dmytruk86f845a2022-10-29 18:55:24 +0300349static int cr50_i2c_tis_send(uint8_t *buf, size_t len)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700350{
351 int status;
352 size_t burstcnt, limit, sent = 0;
353 uint8_t tpm_go[4] = { TPM_STS_GO };
354 struct stopwatch sw;
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600355 tpm_result_t rc = TPM_SUCCESS;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700356
Duncan Laurie1dc036c2016-09-19 16:49:23 -0700357 stopwatch_init_msecs_expire(&sw, CR50_TIMEOUT_LONG_MS);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700358
359 /* Wait until TPM is ready for a command */
Sergii Dmytruk86f845a2022-10-29 18:55:24 +0300360 while (!(cr50_i2c_tis_status() & TPM_STS_COMMAND_READY)) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700361 if (stopwatch_expired(&sw)) {
362 printk(BIOS_ERR, "%s: Command ready timeout\n",
363 __func__);
364 return -1;
365 }
366
Sergii Dmytruk86f845a2022-10-29 18:55:24 +0300367 cr50_i2c_tis_ready();
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700368 }
369
370 while (len > 0) {
Duncan Laurief235a9b2016-09-19 17:19:10 -0700371 uint8_t mask = TPM_STS_VALID;
372
373 /* Wait for data if this is not the first chunk */
374 if (sent > 0)
375 mask |= TPM_STS_DATA_EXPECT;
376
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700377 /* Read burst count and check status */
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600378 if (cr50_i2c_wait_burststs(mask, &burstcnt, &status))
Duncan Laurief235a9b2016-09-19 17:19:10 -0700379 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700380
381 /* Use burstcnt - 1 to account for the address byte
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700382 * that is inserted by cr50_i2c_write() */
Elyes HAOUAS361a9352019-12-18 21:26:33 +0100383 limit = MIN(burstcnt - 1, len);
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600384 rc = cr50_i2c_write(TPM_DATA_FIFO(tpm_dev.locality), &buf[sent], limit);
385 if (rc) {
386 printk(BIOS_ERR, "%s: Write failed with error %#x\n", __func__, rc);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700387 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700388 }
389
390 sent += limit;
391 len -= limit;
392 }
393
394 /* Ensure TPM is not expecting more data */
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600395 if (cr50_i2c_wait_burststs(TPM_STS_VALID, &burstcnt, &status))
Duncan Laurief235a9b2016-09-19 17:19:10 -0700396 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700397 if (status & TPM_STS_DATA_EXPECT) {
398 printk(BIOS_ERR, "%s: Data still expected\n", __func__);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700399 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700400 }
401
402 /* Start the TPM command */
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600403 rc = cr50_i2c_write(TPM_STS(tpm_dev.locality), tpm_go, sizeof(tpm_go));
404 if (rc) {
405 printk(BIOS_ERR, "%s: Start command failed with error %#x\n", __func__, rc);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700406 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700407 }
408 return sent;
409
Duncan Laurief235a9b2016-09-19 17:19:10 -0700410out_err:
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700411 /* Abort current transaction if still pending */
Sergii Dmytruk86f845a2022-10-29 18:55:24 +0300412 if (cr50_i2c_tis_status() & TPM_STS_COMMAND_READY)
413 cr50_i2c_tis_ready();
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700414 return -1;
415}
416
417static void cr50_vendor_init(struct tpm_chip *chip)
418{
Sergii Dmytruk86f845a2022-10-29 18:55:24 +0300419 chip->req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID;
420 chip->req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID;
421 chip->req_canceled = TPM_STS_COMMAND_READY;
422 chip->status = &cr50_i2c_tis_status;
423 chip->recv = &cr50_i2c_tis_recv;
424 chip->send = &cr50_i2c_tis_send;
425 chip->cancel = &cr50_i2c_tis_ready;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700426}
427
Sergii Dmytrukfebf9b92022-10-31 15:30:15 +0200428tpm_result_t tpm_vendor_probe(unsigned int bus, uint32_t addr, enum tpm_family *family)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700429{
Sergii Dmytrukfebf9b92022-10-31 15:30:15 +0200430 /* cr50 is TPM2 */
431 if (family != NULL)
432 *family = TPM_2;
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600433 return TPM_SUCCESS;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700434}
435
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600436static tpm_result_t cr50_i2c_probe(uint32_t *did_vid)
Keith Short51436352018-12-17 14:21:46 -0700437{
438 int retries;
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600439 tpm_result_t rc = TPM_SUCCESS;
Keith Short51436352018-12-17 14:21:46 -0700440
441 /*
Rob Barnes22372f42022-02-11 07:59:21 -0700442 * 1s should be enough to synchronize with the TPM even under the
Keith Short51436352018-12-17 14:21:46 -0700443 * worst nested reset request conditions. In vast majority of cases
Rob Barnes22372f42022-02-11 07:59:21 -0700444 * there would be no wait at all. If this probe fails, boot likely
445 * cannot proceed, so an extra long timeout is appropriate.
Keith Short51436352018-12-17 14:21:46 -0700446 */
447 printk(BIOS_INFO, "Probing TPM I2C: ");
448
Rob Barnes22372f42022-02-11 07:59:21 -0700449 for (retries = 100; retries > 0; retries--) {
Tim Wawrzynczakeb1891a2022-02-08 12:49:31 -0700450 rc = cr50_i2c_read(TPM_DID_VID(0), (uint8_t *)did_vid, 4);
Keith Short51436352018-12-17 14:21:46 -0700451
452 /* Exit once DID and VID verified */
Jett Rinkd41ad722024-06-10 09:31:14 -0600453 if (!rc && (*did_vid == CR50_DID_VID || *did_vid == TI50_DT_DID_VID ||
454 *did_vid == TI50_OT_DID_VID)) {
Keith Short51436352018-12-17 14:21:46 -0700455 printk(BIOS_INFO, "done! DID_VID 0x%08x\n", *did_vid);
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600456 return TPM_SUCCESS;
Keith Short51436352018-12-17 14:21:46 -0700457 }
458
459 /* TPM might be resetting, let's retry in a bit. */
460 mdelay(10);
461 printk(BIOS_INFO, ".");
462 }
463
464 /*
465 * I2C reads failed, or the DID and VID didn't match
466 */
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600467 if (!rc) {
468 printk(BIOS_ERR, "DID_VID 0x%08x not recognized\n", *did_vid);
469 return TPM_CB_FAIL;
470 }
Jon Murphydb4e93b2023-09-05 11:38:59 -0600471 return TPM_CB_COMMUNICATION_ERROR;
Keith Short51436352018-12-17 14:21:46 -0700472}
473
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600474tpm_result_t tpm_vendor_init(struct tpm_chip *chip, unsigned int bus, uint32_t dev_addr)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700475{
Keith Short51436352018-12-17 14:21:46 -0700476 uint32_t did_vid = 0;
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600477 tpm_result_t rc = TPM_SUCCESS;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700478
479 if (dev_addr == 0) {
480 printk(BIOS_ERR, "%s: missing device address\n", __func__);
Jon Murphydb4e93b2023-09-05 11:38:59 -0600481 return TPM_CB_INVALID_ARG;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700482 }
483
Patrick Georgic9b13592019-11-29 11:47:47 +0100484 tpm_dev.bus = bus;
485 tpm_dev.addr = dev_addr;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700486
487 cr50_vendor_init(chip);
488
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600489 rc = cr50_i2c_probe(&did_vid);
490 if (rc)
491 return rc;
Keith Short51436352018-12-17 14:21:46 -0700492
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600493 if (ENV_SEPARATE_VERSTAGE || ENV_BOOTBLOCK) {
494 rc = process_reset();
495 if (rc)
496 return rc;
497 }
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800498
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600499 rc = claim_locality();
500 if (rc)
501 return rc;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700502
Tyler Wang557aad12024-05-14 14:07:02 +0800503 printk(BIOS_DEBUG, "GSC TPM 2.0 (i2c %u:0x%02x id %#x)\n",
Keith Short51436352018-12-17 14:21:46 -0700504 bus, dev_addr, did_vid >> 16);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700505
Jes Klinke1430b042022-03-28 14:22:24 -0700506 if (tpm_first_access_this_boot()) {
Tim Wawrzynczak1e50dfb2022-02-16 13:48:07 -0700507 /* This is called for the side-effect of printing the version string. */
Jes Klinke1430b042022-03-28 14:22:24 -0700508 cr50_get_firmware_version(NULL);
509 cr50_set_board_cfg();
Tim Wawrzynczak1e50dfb2022-02-16 13:48:07 -0700510 }
511
Jon Murphyd7b8dc92023-09-05 11:36:43 -0600512 return TPM_SUCCESS;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700513}
514
Subrata Banik60b2ab82022-03-09 12:55:34 +0530515enum cb_err tis_vendor_write(unsigned int addr, const void *buffer, size_t bytes)
Tim Wawrzynczak1e50dfb2022-02-16 13:48:07 -0700516{
517 return cr50_i2c_write(addr & 0xff, buffer, bytes) ? CB_ERR : CB_SUCCESS;
518}
519
Subrata Banik60b2ab82022-03-09 12:55:34 +0530520enum cb_err tis_vendor_read(unsigned int addr, void *buffer, size_t bytes)
Tim Wawrzynczak1e50dfb2022-02-16 13:48:07 -0700521{
522 return cr50_i2c_read(addr & 0xff, buffer, bytes) ? CB_ERR : CB_SUCCESS;
523}