blob: 929628407dee7ff4c73e364f179024dc642e2250 [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 *
59 * Return -1 on error, 0 on success.
60 */
Tim Wawrzynczakeb1891a2022-02-08 12:49:31 -070061static int 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)
Duncan Laurie2ea13c82016-09-19 16:04:39 -070064 return -1;
65
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__);
72 return -1;
73 }
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)
Duncan Laurie94cc4852016-09-19 17:22:10 -070077 return -1;
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__);
82 return -1;
83 }
84
85 return 0;
86}
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 *
99 * Returns -1 on error, 0 on success.
100 */
Tim Wawrzynczak1e50dfb2022-02-16 13:48:07 -0700101static int 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)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700104 return -1;
Duncan Laurie3727a8d2016-09-19 16:37:46 -0700105 if (len > CR50_MAX_BUFSIZE)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700106 return -1;
107
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__);
118 return -1;
119 }
120
121 /* Wait for TPM to be ready */
Yu-Ping Wuae1e7022022-05-17 09:33:18 +0800122 return cr50_wait_tpm_ready() == CB_SUCCESS ? 0 : -1;
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.
131 */
Tim Wawrzynczakeb1891a2022-02-08 12:49:31 -0700132static int process_reset(void)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700133{
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800134 struct stopwatch sw;
Jon Murphy24604812023-09-05 10:37:05 -0600135 int rc = 0;
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800136 uint8_t access;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700137
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800138 /*
139 * Locality is released by TPM reset.
140 *
141 * If locality is taken at this point, this could be due to the fact
142 * that the TPM is performing a long operation and has not processed
143 * reset request yet. We'll wait up to CR50_TIMEOUT_INIT_MS and see if
144 * it releases locality when reset is processed.
145 */
146 stopwatch_init_msecs_expire(&sw, CR50_TIMEOUT_INIT_MS);
147 do {
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800148 const uint8_t mask =
149 TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700150
Jon Murphy24604812023-09-05 10:37:05 -0600151 rc = cr50_i2c_read(TPM_ACCESS(0),
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800152 &access, sizeof(access));
Jon Murphy24604812023-09-05 10:37:05 -0600153 if (rc || ((access & mask) == mask)) {
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800154 /*
155 * Don't bombard the chip with traffic, let it keep
156 * processing the command.
157 */
158 mdelay(2);
159 continue;
160 }
161
Rob Barnesd522f382022-09-12 06:31:47 -0600162 printk(BIOS_INFO, "TPM ready after %lld ms\n",
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800163 stopwatch_duration_msecs(&sw));
164
165 return 0;
166 } while (!stopwatch_expired(&sw));
167
Jon Murphy24604812023-09-05 10:37:05 -0600168 if (rc)
Richard Spiegel7c1e9592018-08-09 14:41:17 -0700169 printk(BIOS_ERR, "Failed to read TPM\n");
170 else
171 printk(BIOS_ERR,
Rob Barnesd522f382022-09-12 06:31:47 -0600172 "TPM failed to reset after %lld ms, status: %#x\n",
Richard Spiegel7c1e9592018-08-09 14:41:17 -0700173 stopwatch_duration_msecs(&sw), access);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700174
175 return -1;
176}
177
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800178/*
179 * Locality could be already claimed (if this is a later coreboot stage and
180 * the RO did not release it), or not yet claimed, if this is verstage or the
181 * older RO did release it.
182 */
Tim Wawrzynczakeb1891a2022-02-08 12:49:31 -0700183static int claim_locality(void)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700184{
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800185 uint8_t access;
186 const uint8_t mask = TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY;
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700187
Tim Wawrzynczakeb1891a2022-02-08 12:49:31 -0700188 if (cr50_i2c_read(TPM_ACCESS(0), &access, sizeof(access)))
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700189 return -1;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700190
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800191 if ((access & mask) == mask) {
192 printk(BIOS_INFO, "Locality already claimed\n");
193 return 0;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700194 }
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800195
196 access = TPM_ACCESS_REQUEST_USE;
Tim Wawrzynczakeb1891a2022-02-08 12:49:31 -0700197 if (cr50_i2c_write(TPM_ACCESS(0),
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800198 &access, sizeof(access)))
199 return -1;
200
Tim Wawrzynczakeb1891a2022-02-08 12:49:31 -0700201 if (cr50_i2c_read(TPM_ACCESS(0), &access, sizeof(access)))
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800202 return -1;
203
204 if ((access & mask) != mask) {
205 printk(BIOS_INFO, "Failed to claim locality.\n");
206 return -1;
207 }
208
209 return 0;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700210}
211
212/* cr50 requires all 4 bytes of status register to be read */
Sergii Dmytruk86f845a2022-10-29 18:55:24 +0300213static uint8_t cr50_i2c_tis_status(void)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700214{
215 uint8_t buf[4];
Sergii Dmytruk86f845a2022-10-29 18:55:24 +0300216 if (cr50_i2c_read(TPM_STS(tpm_dev.locality), buf, sizeof(buf)) < 0) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700217 printk(BIOS_ERR, "%s: Failed to read status\n", __func__);
218 return 0;
219 }
220 return buf[0];
221}
222
223/* cr50 requires all 4 bytes of status register to be written */
Sergii Dmytruk86f845a2022-10-29 18:55:24 +0300224static void cr50_i2c_tis_ready(void)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700225{
226 uint8_t buf[4] = { TPM_STS_COMMAND_READY };
Sergii Dmytruk86f845a2022-10-29 18:55:24 +0300227 cr50_i2c_write(TPM_STS(tpm_dev.locality), buf, sizeof(buf));
Duncan Laurie1dc036c2016-09-19 16:49:23 -0700228 mdelay(CR50_TIMEOUT_SHORT_MS);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700229}
230
231/* cr50 uses bytes 3:2 of status register for burst count and
232 * all 4 bytes must be read */
Sergii Dmytruk86f845a2022-10-29 18:55:24 +0300233static int cr50_i2c_wait_burststs(uint8_t mask, size_t *burst, int *status)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700234{
235 uint8_t buf[4];
236 struct stopwatch sw;
237
Duncan Laurie1dc036c2016-09-19 16:49:23 -0700238 stopwatch_init_msecs_expire(&sw, CR50_TIMEOUT_LONG_MS);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700239
240 while (!stopwatch_expired(&sw)) {
Sergii Dmytruk86f845a2022-10-29 18:55:24 +0300241 if (cr50_i2c_read(TPM_STS(tpm_dev.locality), buf, sizeof(buf)) != 0) {
Duncan Laurie1dc036c2016-09-19 16:49:23 -0700242 mdelay(CR50_TIMEOUT_SHORT_MS);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700243 continue;
244 }
245
246 *status = buf[0];
247 *burst = read_le16(&buf[1]);
248
249 /* Check if mask matches and burst is valid */
250 if ((*status & mask) == mask &&
Duncan Laurie3727a8d2016-09-19 16:37:46 -0700251 *burst > 0 && *burst <= CR50_MAX_BUFSIZE)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700252 return 0;
253
Duncan Laurie1dc036c2016-09-19 16:49:23 -0700254 mdelay(CR50_TIMEOUT_SHORT_MS);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700255 }
256
257 printk(BIOS_ERR, "%s: Timeout reading burst and status\n", __func__);
258 return -1;
259}
260
Sergii Dmytruk86f845a2022-10-29 18:55:24 +0300261static int cr50_i2c_tis_recv(uint8_t *buf, size_t buf_len)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700262{
263 size_t burstcnt, current, len, expected;
Sergii Dmytruk86f845a2022-10-29 18:55:24 +0300264 uint8_t addr = TPM_DATA_FIFO(tpm_dev.locality);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700265 uint8_t mask = TPM_STS_VALID | TPM_STS_DATA_AVAIL;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700266 int status;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700267
268 if (buf_len < TPM_HEADER_SIZE)
Duncan Laurief235a9b2016-09-19 17:19:10 -0700269 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700270
Sergii Dmytruk86f845a2022-10-29 18:55:24 +0300271 if (cr50_i2c_wait_burststs(mask, &burstcnt, &status) < 0) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700272 printk(BIOS_ERR, "%s: First chunk not available\n", __func__);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700273 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700274 }
275
276 /* Read first chunk of burstcnt bytes */
Tim Wawrzynczakeb1891a2022-02-08 12:49:31 -0700277 if (cr50_i2c_read(addr, buf, burstcnt) != 0) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700278 printk(BIOS_ERR, "%s: Read failed\n", __func__);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700279 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700280 }
281
282 /* Determine expected data in the return buffer */
283 expected = read_be32(buf + TPM_RSP_SIZE_BYTE);
284 if (expected > buf_len) {
285 printk(BIOS_ERR, "%s: Too much data: %zu > %zu\n",
286 __func__, expected, buf_len);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700287 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700288 }
289
290 /* Now read the rest of the data */
291 current = burstcnt;
292 while (current < expected) {
293 /* Read updated burst count and check status */
Sergii Dmytruk86f845a2022-10-29 18:55:24 +0300294 if (cr50_i2c_wait_burststs(mask, &burstcnt, &status) < 0)
Duncan Laurief235a9b2016-09-19 17:19:10 -0700295 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700296
Elyes HAOUAS361a9352019-12-18 21:26:33 +0100297 len = MIN(burstcnt, expected - current);
Tim Wawrzynczakeb1891a2022-02-08 12:49:31 -0700298 if (cr50_i2c_read(addr, buf + current, len) != 0) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700299 printk(BIOS_ERR, "%s: Read failed\n", __func__);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700300 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700301 }
302
303 current += len;
304 }
305
306 /* Ensure TPM is done reading data */
Sergii Dmytruk86f845a2022-10-29 18:55:24 +0300307 if (cr50_i2c_wait_burststs(TPM_STS_VALID, &burstcnt, &status) < 0)
Duncan Laurief235a9b2016-09-19 17:19:10 -0700308 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700309 if (status & TPM_STS_DATA_AVAIL) {
310 printk(BIOS_ERR, "%s: Data still available\n", __func__);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700311 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700312 }
313
Duncan Laurief235a9b2016-09-19 17:19:10 -0700314 return current;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700315
Duncan Laurief235a9b2016-09-19 17:19:10 -0700316out_err:
317 /* Abort current transaction if still pending */
Sergii Dmytruk86f845a2022-10-29 18:55:24 +0300318 if (cr50_i2c_tis_status() & TPM_STS_COMMAND_READY)
319 cr50_i2c_tis_ready();
Duncan Laurief235a9b2016-09-19 17:19:10 -0700320 return -1;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700321}
322
Sergii Dmytruk86f845a2022-10-29 18:55:24 +0300323static int cr50_i2c_tis_send(uint8_t *buf, size_t len)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700324{
325 int status;
326 size_t burstcnt, limit, sent = 0;
327 uint8_t tpm_go[4] = { TPM_STS_GO };
328 struct stopwatch sw;
329
Duncan Laurie1dc036c2016-09-19 16:49:23 -0700330 stopwatch_init_msecs_expire(&sw, CR50_TIMEOUT_LONG_MS);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700331
332 /* Wait until TPM is ready for a command */
Sergii Dmytruk86f845a2022-10-29 18:55:24 +0300333 while (!(cr50_i2c_tis_status() & TPM_STS_COMMAND_READY)) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700334 if (stopwatch_expired(&sw)) {
335 printk(BIOS_ERR, "%s: Command ready timeout\n",
336 __func__);
337 return -1;
338 }
339
Sergii Dmytruk86f845a2022-10-29 18:55:24 +0300340 cr50_i2c_tis_ready();
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700341 }
342
343 while (len > 0) {
Duncan Laurief235a9b2016-09-19 17:19:10 -0700344 uint8_t mask = TPM_STS_VALID;
345
346 /* Wait for data if this is not the first chunk */
347 if (sent > 0)
348 mask |= TPM_STS_DATA_EXPECT;
349
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700350 /* Read burst count and check status */
Sergii Dmytruk86f845a2022-10-29 18:55:24 +0300351 if (cr50_i2c_wait_burststs(mask, &burstcnt, &status) < 0)
Duncan Laurief235a9b2016-09-19 17:19:10 -0700352 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700353
354 /* Use burstcnt - 1 to account for the address byte
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700355 * that is inserted by cr50_i2c_write() */
Elyes HAOUAS361a9352019-12-18 21:26:33 +0100356 limit = MIN(burstcnt - 1, len);
Sergii Dmytruk86f845a2022-10-29 18:55:24 +0300357 if (cr50_i2c_write(TPM_DATA_FIFO(tpm_dev.locality), &buf[sent], limit) != 0) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700358 printk(BIOS_ERR, "%s: Write failed\n", __func__);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700359 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700360 }
361
362 sent += limit;
363 len -= limit;
364 }
365
366 /* Ensure TPM is not expecting more data */
Sergii Dmytruk86f845a2022-10-29 18:55:24 +0300367 if (cr50_i2c_wait_burststs(TPM_STS_VALID, &burstcnt, &status) < 0)
Duncan Laurief235a9b2016-09-19 17:19:10 -0700368 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700369 if (status & TPM_STS_DATA_EXPECT) {
370 printk(BIOS_ERR, "%s: Data still expected\n", __func__);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700371 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700372 }
373
374 /* Start the TPM command */
Sergii Dmytruk86f845a2022-10-29 18:55:24 +0300375 if (cr50_i2c_write(TPM_STS(tpm_dev.locality), tpm_go, sizeof(tpm_go)) < 0) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700376 printk(BIOS_ERR, "%s: Start command failed\n", __func__);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700377 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700378 }
379 return sent;
380
Duncan Laurief235a9b2016-09-19 17:19:10 -0700381out_err:
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700382 /* Abort current transaction if still pending */
Sergii Dmytruk86f845a2022-10-29 18:55:24 +0300383 if (cr50_i2c_tis_status() & TPM_STS_COMMAND_READY)
384 cr50_i2c_tis_ready();
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700385 return -1;
386}
387
388static void cr50_vendor_init(struct tpm_chip *chip)
389{
Sergii Dmytruk86f845a2022-10-29 18:55:24 +0300390 chip->req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID;
391 chip->req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID;
392 chip->req_canceled = TPM_STS_COMMAND_READY;
393 chip->status = &cr50_i2c_tis_status;
394 chip->recv = &cr50_i2c_tis_recv;
395 chip->send = &cr50_i2c_tis_send;
396 chip->cancel = &cr50_i2c_tis_ready;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700397}
398
Lee Leahy52ab30b2017-03-15 09:22:11 -0700399int tpm_vendor_probe(unsigned int bus, uint32_t addr)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700400{
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700401 return 0;
402}
403
Sergii Dmytruk86f845a2022-10-29 18:55:24 +0300404static int cr50_i2c_probe(uint32_t *did_vid)
Keith Short51436352018-12-17 14:21:46 -0700405{
406 int retries;
407
408 /*
Rob Barnes22372f42022-02-11 07:59:21 -0700409 * 1s should be enough to synchronize with the TPM even under the
Keith Short51436352018-12-17 14:21:46 -0700410 * worst nested reset request conditions. In vast majority of cases
Rob Barnes22372f42022-02-11 07:59:21 -0700411 * there would be no wait at all. If this probe fails, boot likely
412 * cannot proceed, so an extra long timeout is appropriate.
Keith Short51436352018-12-17 14:21:46 -0700413 */
414 printk(BIOS_INFO, "Probing TPM I2C: ");
415
Rob Barnes22372f42022-02-11 07:59:21 -0700416 for (retries = 100; retries > 0; retries--) {
Keith Short51436352018-12-17 14:21:46 -0700417 int rc;
418
Tim Wawrzynczakeb1891a2022-02-08 12:49:31 -0700419 rc = cr50_i2c_read(TPM_DID_VID(0), (uint8_t *)did_vid, 4);
Keith Short51436352018-12-17 14:21:46 -0700420
421 /* Exit once DID and VID verified */
Jes Klinke1430b042022-03-28 14:22:24 -0700422 if (!rc && (*did_vid == CR50_DID_VID || *did_vid == TI50_DID_VID)) {
Keith Short51436352018-12-17 14:21:46 -0700423 printk(BIOS_INFO, "done! DID_VID 0x%08x\n", *did_vid);
424 return 0;
425 }
426
427 /* TPM might be resetting, let's retry in a bit. */
428 mdelay(10);
429 printk(BIOS_INFO, ".");
430 }
431
432 /*
433 * I2C reads failed, or the DID and VID didn't match
434 */
435 printk(BIOS_ERR, "DID_VID 0x%08x not recognized\n", *did_vid);
436 return -1;
437}
438
Lee Leahy52ab30b2017-03-15 09:22:11 -0700439int tpm_vendor_init(struct tpm_chip *chip, unsigned int bus, uint32_t dev_addr)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700440{
Keith Short51436352018-12-17 14:21:46 -0700441 uint32_t did_vid = 0;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700442
443 if (dev_addr == 0) {
444 printk(BIOS_ERR, "%s: missing device address\n", __func__);
445 return -1;
446 }
447
Patrick Georgic9b13592019-11-29 11:47:47 +0100448 tpm_dev.bus = bus;
449 tpm_dev.addr = dev_addr;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700450
451 cr50_vendor_init(chip);
452
Sergii Dmytruk86f845a2022-10-29 18:55:24 +0300453 if (cr50_i2c_probe(&did_vid))
Keith Short51436352018-12-17 14:21:46 -0700454 return -1;
455
Julius Werner21a40532020-04-21 16:03:53 -0700456 if (ENV_SEPARATE_VERSTAGE || ENV_BOOTBLOCK)
Tim Wawrzynczakeb1891a2022-02-08 12:49:31 -0700457 if (process_reset())
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800458 return -1;
459
Tim Wawrzynczakeb1891a2022-02-08 12:49:31 -0700460 if (claim_locality())
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700461 return -1;
462
Jon Murphy53fc6672023-09-26 21:05:37 -0600463 printk(BIOS_DEBUG, "cr50 TPM 2.0 (i2c %u:0x%02x id %#x)\n",
Keith Short51436352018-12-17 14:21:46 -0700464 bus, dev_addr, did_vid >> 16);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700465
Jes Klinke1430b042022-03-28 14:22:24 -0700466 if (tpm_first_access_this_boot()) {
Tim Wawrzynczak1e50dfb2022-02-16 13:48:07 -0700467 /* This is called for the side-effect of printing the version string. */
Jes Klinke1430b042022-03-28 14:22:24 -0700468 cr50_get_firmware_version(NULL);
469 cr50_set_board_cfg();
Tim Wawrzynczak1e50dfb2022-02-16 13:48:07 -0700470 }
471
Sergii Dmytruk4ee03172022-12-22 19:35:25 +0200472 chip->is_open = 1;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700473 return 0;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700474}
475
Subrata Banik60b2ab82022-03-09 12:55:34 +0530476enum cb_err tis_vendor_write(unsigned int addr, const void *buffer, size_t bytes)
Tim Wawrzynczak1e50dfb2022-02-16 13:48:07 -0700477{
478 return cr50_i2c_write(addr & 0xff, buffer, bytes) ? CB_ERR : CB_SUCCESS;
479}
480
Subrata Banik60b2ab82022-03-09 12:55:34 +0530481enum cb_err tis_vendor_read(unsigned int addr, void *buffer, size_t bytes)
Tim Wawrzynczak1e50dfb2022-02-16 13:48:07 -0700482{
483 return cr50_i2c_read(addr & 0xff, buffer, bytes) ? CB_ERR : CB_SUCCESS;
484}