blob: d9088233c27ba21852199f30b827369fb1c47726 [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 Laurieed4fa092016-11-01 15:03:13 -070036#define CR50_TIMEOUT_NOIRQ_MS 20 /* Timeout for TPM ready without IRQ */
Duncan Laurie2ea13c82016-09-19 16:04:39 -070037#define CR50_DID_VID 0x00281ae0L
Jes Klinke1430b042022-03-28 14:22:24 -070038#define TI50_DID_VID 0x504a6666L
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
Aaron Durbin64031672018-04-21 14:45:32 -060049__weak int tis_plat_irq_status(void)
Daniel Kurtzc4852e72017-04-21 14:11:51 +080050{
Arthur Heymans0ca944b2019-11-20 19:51:06 +010051 static int warning_displayed;
Daniel Kurtzc4852e72017-04-21 14:11:51 +080052
Arthur Heymans0ca944b2019-11-20 19:51:06 +010053 if (!warning_displayed) {
Julius Wernere9665952022-01-21 17:06:20 -080054 printk(BIOS_WARNING, "%s() not implemented, wasting 20ms to wait on"
Elyes HAOUAS52016652021-01-16 17:29:49 +010055 " Cr50!\n", __func__);
Arthur Heymans0ca944b2019-11-20 19:51:06 +010056 warning_displayed = 1;
Daniel Kurtzc4852e72017-04-21 14:11:51 +080057 }
58 mdelay(CR50_TIMEOUT_NOIRQ_MS);
59
60 return 1;
61}
62
Duncan Laurie2ea13c82016-09-19 16:04:39 -070063/*
Duncan Laurie510cb6a2016-09-19 17:05:45 -070064 * cr50_i2c_read() - read from TPM register
Duncan Laurie2ea13c82016-09-19 16:04:39 -070065 *
66 * @addr: register address to read from
67 * @buffer: provided by caller
68 * @len: number of bytes to read
69 *
70 * 1) send register address byte 'addr' to the TPM
71 * 2) wait for TPM to indicate it is ready
72 * 3) read 'len' bytes of TPM response into the provided 'buffer'
73 *
74 * Return -1 on error, 0 on success.
75 */
Tim Wawrzynczakeb1891a2022-02-08 12:49:31 -070076static int cr50_i2c_read(uint8_t addr, uint8_t *buffer, size_t len)
Duncan Laurie2ea13c82016-09-19 16:04:39 -070077{
Patrick Georgic9b13592019-11-29 11:47:47 +010078 if (tpm_dev.addr == 0)
Duncan Laurie2ea13c82016-09-19 16:04:39 -070079 return -1;
80
Duncan Laurie94cc4852016-09-19 17:22:10 -070081 /* Clear interrupt before starting transaction */
Daniel Kurtzc4852e72017-04-21 14:11:51 +080082 tis_plat_irq_status();
Duncan Laurie94cc4852016-09-19 17:22:10 -070083
Duncan Laurie2ea13c82016-09-19 16:04:39 -070084 /* Send the register address byte to the TPM */
Patrick Georgic9b13592019-11-29 11:47:47 +010085 if (i2c_write_raw(tpm_dev.bus, tpm_dev.addr, &addr, 1)) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -070086 printk(BIOS_ERR, "%s: Address write failed\n", __func__);
87 return -1;
88 }
89
90 /* Wait for TPM to be ready with response data */
Yu-Ping Wuae1e7022022-05-17 09:33:18 +080091 if (cr50_wait_tpm_ready() != CB_SUCCESS)
Duncan Laurie94cc4852016-09-19 17:22:10 -070092 return -1;
Duncan Laurie2ea13c82016-09-19 16:04:39 -070093
94 /* Read response data from the TPM */
Patrick Georgic9b13592019-11-29 11:47:47 +010095 if (i2c_read_raw(tpm_dev.bus, tpm_dev.addr, buffer, len)) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -070096 printk(BIOS_ERR, "%s: Read response failed\n", __func__);
97 return -1;
98 }
99
100 return 0;
101}
102
103/*
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700104 * cr50_i2c_write() - write to TPM register
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700105 *
106 * @addr: register address to write to
107 * @buffer: data to write
108 * @len: number of bytes to write
109 *
110 * 1) prepend the provided address to the provided data
111 * 2) send the address+data to the TPM
112 * 3) wait for TPM to indicate it is done writing
113 *
114 * Returns -1 on error, 0 on success.
115 */
Tim Wawrzynczak1e50dfb2022-02-16 13:48:07 -0700116static int cr50_i2c_write(uint8_t addr, const uint8_t *buffer, size_t len)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700117{
Patrick Georgic9b13592019-11-29 11:47:47 +0100118 if (tpm_dev.addr == 0)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700119 return -1;
Duncan Laurie3727a8d2016-09-19 16:37:46 -0700120 if (len > CR50_MAX_BUFSIZE)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700121 return -1;
122
123 /* Prepend the 'register address' to the buffer */
Patrick Georgic9b13592019-11-29 11:47:47 +0100124 tpm_dev.buf[0] = addr;
125 memcpy(tpm_dev.buf + 1, buffer, len);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700126
Duncan Laurie94cc4852016-09-19 17:22:10 -0700127 /* Clear interrupt before starting transaction */
Daniel Kurtzc4852e72017-04-21 14:11:51 +0800128 tis_plat_irq_status();
Duncan Laurie94cc4852016-09-19 17:22:10 -0700129
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700130 /* Send write request buffer with address */
Patrick Georgic9b13592019-11-29 11:47:47 +0100131 if (i2c_write_raw(tpm_dev.bus, tpm_dev.addr, tpm_dev.buf, len + 1)) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700132 printk(BIOS_ERR, "%s: Error writing to TPM\n", __func__);
133 return -1;
134 }
135
136 /* Wait for TPM to be ready */
Yu-Ping Wuae1e7022022-05-17 09:33:18 +0800137 return cr50_wait_tpm_ready() == CB_SUCCESS ? 0 : -1;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700138}
139
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800140/*
141 * Cr50 processes reset requests asynchronously and consceivably could be busy
142 * executing a long command and not reacting to the reset pulse for a while.
143 *
144 * This function will make sure that the AP does not proceed with boot until
145 * TPM finished reset processing.
146 */
Tim Wawrzynczakeb1891a2022-02-08 12:49:31 -0700147static int process_reset(void)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700148{
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800149 struct stopwatch sw;
Richard Spiegel7c1e9592018-08-09 14:41:17 -0700150 int rv = 0;
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800151 uint8_t access;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700152
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800153 /*
154 * Locality is released by TPM reset.
155 *
156 * If locality is taken at this point, this could be due to the fact
157 * that the TPM is performing a long operation and has not processed
158 * reset request yet. We'll wait up to CR50_TIMEOUT_INIT_MS and see if
159 * it releases locality when reset is processed.
160 */
161 stopwatch_init_msecs_expire(&sw, CR50_TIMEOUT_INIT_MS);
162 do {
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800163 const uint8_t mask =
164 TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700165
Tim Wawrzynczakeb1891a2022-02-08 12:49:31 -0700166 rv = cr50_i2c_read(TPM_ACCESS(0),
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800167 &access, sizeof(access));
168 if (rv || ((access & mask) == mask)) {
169 /*
170 * Don't bombard the chip with traffic, let it keep
171 * processing the command.
172 */
173 mdelay(2);
174 continue;
175 }
176
Rob Barnesd522f382022-09-12 06:31:47 -0600177 printk(BIOS_INFO, "TPM ready after %lld ms\n",
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800178 stopwatch_duration_msecs(&sw));
179
180 return 0;
181 } while (!stopwatch_expired(&sw));
182
Richard Spiegel7c1e9592018-08-09 14:41:17 -0700183 if (rv)
184 printk(BIOS_ERR, "Failed to read TPM\n");
185 else
186 printk(BIOS_ERR,
Rob Barnesd522f382022-09-12 06:31:47 -0600187 "TPM failed to reset after %lld ms, status: %#x\n",
Richard Spiegel7c1e9592018-08-09 14:41:17 -0700188 stopwatch_duration_msecs(&sw), access);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700189
190 return -1;
191}
192
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800193/*
194 * Locality could be already claimed (if this is a later coreboot stage and
195 * the RO did not release it), or not yet claimed, if this is verstage or the
196 * older RO did release it.
197 */
Tim Wawrzynczakeb1891a2022-02-08 12:49:31 -0700198static int claim_locality(void)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700199{
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800200 uint8_t access;
201 const uint8_t mask = TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY;
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700202
Tim Wawrzynczakeb1891a2022-02-08 12:49:31 -0700203 if (cr50_i2c_read(TPM_ACCESS(0), &access, sizeof(access)))
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700204 return -1;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700205
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800206 if ((access & mask) == mask) {
207 printk(BIOS_INFO, "Locality already claimed\n");
208 return 0;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700209 }
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800210
211 access = TPM_ACCESS_REQUEST_USE;
Tim Wawrzynczakeb1891a2022-02-08 12:49:31 -0700212 if (cr50_i2c_write(TPM_ACCESS(0),
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800213 &access, sizeof(access)))
214 return -1;
215
Tim Wawrzynczakeb1891a2022-02-08 12:49:31 -0700216 if (cr50_i2c_read(TPM_ACCESS(0), &access, sizeof(access)))
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800217 return -1;
218
219 if ((access & mask) != mask) {
220 printk(BIOS_INFO, "Failed to claim locality.\n");
221 return -1;
222 }
223
224 return 0;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700225}
226
227/* cr50 requires all 4 bytes of status register to be read */
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];
Sergii Dmytruk86f845a2022-10-29 18:55:24 +0300231 if (cr50_i2c_read(TPM_STS(tpm_dev.locality), buf, sizeof(buf)) < 0) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700232 printk(BIOS_ERR, "%s: Failed to read status\n", __func__);
233 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
247 * all 4 bytes must be read */
Sergii Dmytruk86f845a2022-10-29 18:55:24 +0300248static int cr50_i2c_wait_burststs(uint8_t mask, size_t *burst, int *status)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700249{
250 uint8_t buf[4];
251 struct stopwatch sw;
252
Duncan Laurie1dc036c2016-09-19 16:49:23 -0700253 stopwatch_init_msecs_expire(&sw, CR50_TIMEOUT_LONG_MS);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700254
255 while (!stopwatch_expired(&sw)) {
Sergii Dmytruk86f845a2022-10-29 18:55:24 +0300256 if (cr50_i2c_read(TPM_STS(tpm_dev.locality), buf, sizeof(buf)) != 0) {
Duncan Laurie1dc036c2016-09-19 16:49:23 -0700257 mdelay(CR50_TIMEOUT_SHORT_MS);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700258 continue;
259 }
260
261 *status = buf[0];
262 *burst = read_le16(&buf[1]);
263
264 /* Check if mask matches and burst is valid */
265 if ((*status & mask) == mask &&
Duncan Laurie3727a8d2016-09-19 16:37:46 -0700266 *burst > 0 && *burst <= CR50_MAX_BUFSIZE)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700267 return 0;
268
Duncan Laurie1dc036c2016-09-19 16:49:23 -0700269 mdelay(CR50_TIMEOUT_SHORT_MS);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700270 }
271
272 printk(BIOS_ERR, "%s: Timeout reading burst and status\n", __func__);
273 return -1;
274}
275
Sergii Dmytruk86f845a2022-10-29 18:55:24 +0300276static int cr50_i2c_tis_recv(uint8_t *buf, size_t buf_len)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700277{
278 size_t burstcnt, current, len, expected;
Sergii Dmytruk86f845a2022-10-29 18:55:24 +0300279 uint8_t addr = TPM_DATA_FIFO(tpm_dev.locality);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700280 uint8_t mask = TPM_STS_VALID | TPM_STS_DATA_AVAIL;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700281 int status;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700282
283 if (buf_len < TPM_HEADER_SIZE)
Duncan Laurief235a9b2016-09-19 17:19:10 -0700284 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700285
Sergii Dmytruk86f845a2022-10-29 18:55:24 +0300286 if (cr50_i2c_wait_burststs(mask, &burstcnt, &status) < 0) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700287 printk(BIOS_ERR, "%s: First chunk not available\n", __func__);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700288 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700289 }
290
291 /* Read first chunk of burstcnt bytes */
Tim Wawrzynczakeb1891a2022-02-08 12:49:31 -0700292 if (cr50_i2c_read(addr, buf, burstcnt) != 0) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700293 printk(BIOS_ERR, "%s: Read failed\n", __func__);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700294 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700295 }
296
297 /* Determine expected data in the return buffer */
298 expected = read_be32(buf + TPM_RSP_SIZE_BYTE);
299 if (expected > buf_len) {
300 printk(BIOS_ERR, "%s: Too much data: %zu > %zu\n",
301 __func__, expected, buf_len);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700302 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700303 }
304
305 /* Now read the rest of the data */
306 current = burstcnt;
307 while (current < expected) {
308 /* Read updated burst count and check status */
Sergii Dmytruk86f845a2022-10-29 18:55:24 +0300309 if (cr50_i2c_wait_burststs(mask, &burstcnt, &status) < 0)
Duncan Laurief235a9b2016-09-19 17:19:10 -0700310 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700311
Elyes HAOUAS361a9352019-12-18 21:26:33 +0100312 len = MIN(burstcnt, expected - current);
Tim Wawrzynczakeb1891a2022-02-08 12:49:31 -0700313 if (cr50_i2c_read(addr, buf + current, len) != 0) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700314 printk(BIOS_ERR, "%s: Read failed\n", __func__);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700315 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700316 }
317
318 current += len;
319 }
320
321 /* Ensure TPM is done reading data */
Sergii Dmytruk86f845a2022-10-29 18:55:24 +0300322 if (cr50_i2c_wait_burststs(TPM_STS_VALID, &burstcnt, &status) < 0)
Duncan Laurief235a9b2016-09-19 17:19:10 -0700323 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700324 if (status & TPM_STS_DATA_AVAIL) {
325 printk(BIOS_ERR, "%s: Data still available\n", __func__);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700326 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700327 }
328
Duncan Laurief235a9b2016-09-19 17:19:10 -0700329 return current;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700330
Duncan Laurief235a9b2016-09-19 17:19:10 -0700331out_err:
332 /* Abort current transaction if still pending */
Sergii Dmytruk86f845a2022-10-29 18:55:24 +0300333 if (cr50_i2c_tis_status() & TPM_STS_COMMAND_READY)
334 cr50_i2c_tis_ready();
Duncan Laurief235a9b2016-09-19 17:19:10 -0700335 return -1;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700336}
337
Sergii Dmytruk86f845a2022-10-29 18:55:24 +0300338static int cr50_i2c_tis_send(uint8_t *buf, size_t len)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700339{
340 int status;
341 size_t burstcnt, limit, sent = 0;
342 uint8_t tpm_go[4] = { TPM_STS_GO };
343 struct stopwatch sw;
344
Duncan Laurie1dc036c2016-09-19 16:49:23 -0700345 stopwatch_init_msecs_expire(&sw, CR50_TIMEOUT_LONG_MS);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700346
347 /* Wait until TPM is ready for a command */
Sergii Dmytruk86f845a2022-10-29 18:55:24 +0300348 while (!(cr50_i2c_tis_status() & TPM_STS_COMMAND_READY)) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700349 if (stopwatch_expired(&sw)) {
350 printk(BIOS_ERR, "%s: Command ready timeout\n",
351 __func__);
352 return -1;
353 }
354
Sergii Dmytruk86f845a2022-10-29 18:55:24 +0300355 cr50_i2c_tis_ready();
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700356 }
357
358 while (len > 0) {
Duncan Laurief235a9b2016-09-19 17:19:10 -0700359 uint8_t mask = TPM_STS_VALID;
360
361 /* Wait for data if this is not the first chunk */
362 if (sent > 0)
363 mask |= TPM_STS_DATA_EXPECT;
364
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700365 /* Read burst count and check status */
Sergii Dmytruk86f845a2022-10-29 18:55:24 +0300366 if (cr50_i2c_wait_burststs(mask, &burstcnt, &status) < 0)
Duncan Laurief235a9b2016-09-19 17:19:10 -0700367 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700368
369 /* Use burstcnt - 1 to account for the address byte
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700370 * that is inserted by cr50_i2c_write() */
Elyes HAOUAS361a9352019-12-18 21:26:33 +0100371 limit = MIN(burstcnt - 1, len);
Sergii Dmytruk86f845a2022-10-29 18:55:24 +0300372 if (cr50_i2c_write(TPM_DATA_FIFO(tpm_dev.locality), &buf[sent], limit) != 0) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700373 printk(BIOS_ERR, "%s: Write failed\n", __func__);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700374 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700375 }
376
377 sent += limit;
378 len -= limit;
379 }
380
381 /* Ensure TPM is not expecting more data */
Sergii Dmytruk86f845a2022-10-29 18:55:24 +0300382 if (cr50_i2c_wait_burststs(TPM_STS_VALID, &burstcnt, &status) < 0)
Duncan Laurief235a9b2016-09-19 17:19:10 -0700383 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700384 if (status & TPM_STS_DATA_EXPECT) {
385 printk(BIOS_ERR, "%s: Data still expected\n", __func__);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700386 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700387 }
388
389 /* Start the TPM command */
Sergii Dmytruk86f845a2022-10-29 18:55:24 +0300390 if (cr50_i2c_write(TPM_STS(tpm_dev.locality), tpm_go, sizeof(tpm_go)) < 0) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700391 printk(BIOS_ERR, "%s: Start command failed\n", __func__);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700392 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700393 }
394 return sent;
395
Duncan Laurief235a9b2016-09-19 17:19:10 -0700396out_err:
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700397 /* Abort current transaction if still pending */
Sergii Dmytruk86f845a2022-10-29 18:55:24 +0300398 if (cr50_i2c_tis_status() & TPM_STS_COMMAND_READY)
399 cr50_i2c_tis_ready();
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700400 return -1;
401}
402
403static void cr50_vendor_init(struct tpm_chip *chip)
404{
Sergii Dmytruk86f845a2022-10-29 18:55:24 +0300405 chip->req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID;
406 chip->req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID;
407 chip->req_canceled = TPM_STS_COMMAND_READY;
408 chip->status = &cr50_i2c_tis_status;
409 chip->recv = &cr50_i2c_tis_recv;
410 chip->send = &cr50_i2c_tis_send;
411 chip->cancel = &cr50_i2c_tis_ready;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700412}
413
Lee Leahy52ab30b2017-03-15 09:22:11 -0700414int tpm_vendor_probe(unsigned int bus, uint32_t addr)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700415{
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700416 return 0;
417}
418
Sergii Dmytruk86f845a2022-10-29 18:55:24 +0300419static int cr50_i2c_probe(uint32_t *did_vid)
Keith Short51436352018-12-17 14:21:46 -0700420{
421 int retries;
422
423 /*
Rob Barnes22372f42022-02-11 07:59:21 -0700424 * 1s should be enough to synchronize with the TPM even under the
Keith Short51436352018-12-17 14:21:46 -0700425 * worst nested reset request conditions. In vast majority of cases
Rob Barnes22372f42022-02-11 07:59:21 -0700426 * there would be no wait at all. If this probe fails, boot likely
427 * cannot proceed, so an extra long timeout is appropriate.
Keith Short51436352018-12-17 14:21:46 -0700428 */
429 printk(BIOS_INFO, "Probing TPM I2C: ");
430
Rob Barnes22372f42022-02-11 07:59:21 -0700431 for (retries = 100; retries > 0; retries--) {
Keith Short51436352018-12-17 14:21:46 -0700432 int rc;
433
Tim Wawrzynczakeb1891a2022-02-08 12:49:31 -0700434 rc = cr50_i2c_read(TPM_DID_VID(0), (uint8_t *)did_vid, 4);
Keith Short51436352018-12-17 14:21:46 -0700435
436 /* Exit once DID and VID verified */
Jes Klinke1430b042022-03-28 14:22:24 -0700437 if (!rc && (*did_vid == CR50_DID_VID || *did_vid == TI50_DID_VID)) {
Keith Short51436352018-12-17 14:21:46 -0700438 printk(BIOS_INFO, "done! DID_VID 0x%08x\n", *did_vid);
439 return 0;
440 }
441
442 /* TPM might be resetting, let's retry in a bit. */
443 mdelay(10);
444 printk(BIOS_INFO, ".");
445 }
446
447 /*
448 * I2C reads failed, or the DID and VID didn't match
449 */
450 printk(BIOS_ERR, "DID_VID 0x%08x not recognized\n", *did_vid);
451 return -1;
452}
453
Lee Leahy52ab30b2017-03-15 09:22:11 -0700454int tpm_vendor_init(struct tpm_chip *chip, unsigned int bus, uint32_t dev_addr)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700455{
Keith Short51436352018-12-17 14:21:46 -0700456 uint32_t did_vid = 0;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700457
458 if (dev_addr == 0) {
459 printk(BIOS_ERR, "%s: missing device address\n", __func__);
460 return -1;
461 }
462
Patrick Georgic9b13592019-11-29 11:47:47 +0100463 tpm_dev.bus = bus;
464 tpm_dev.addr = dev_addr;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700465
466 cr50_vendor_init(chip);
467
Sergii Dmytruk86f845a2022-10-29 18:55:24 +0300468 if (cr50_i2c_probe(&did_vid))
Keith Short51436352018-12-17 14:21:46 -0700469 return -1;
470
Julius Werner21a40532020-04-21 16:03:53 -0700471 if (ENV_SEPARATE_VERSTAGE || ENV_BOOTBLOCK)
Tim Wawrzynczakeb1891a2022-02-08 12:49:31 -0700472 if (process_reset())
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800473 return -1;
474
Tim Wawrzynczakeb1891a2022-02-08 12:49:31 -0700475 if (claim_locality())
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700476 return -1;
477
Daniel Kurtzc4852e72017-04-21 14:11:51 +0800478 printk(BIOS_DEBUG, "cr50 TPM 2.0 (i2c %u:0x%02x id 0x%x)\n",
Keith Short51436352018-12-17 14:21:46 -0700479 bus, dev_addr, did_vid >> 16);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700480
Jes Klinke1430b042022-03-28 14:22:24 -0700481 if (tpm_first_access_this_boot()) {
Tim Wawrzynczak1e50dfb2022-02-16 13:48:07 -0700482 /* This is called for the side-effect of printing the version string. */
Jes Klinke1430b042022-03-28 14:22:24 -0700483 cr50_get_firmware_version(NULL);
484 cr50_set_board_cfg();
Tim Wawrzynczak1e50dfb2022-02-16 13:48:07 -0700485 }
486
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700487 chip->is_open = 1;
488 return 0;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700489}
490
Subrata Banik60b2ab82022-03-09 12:55:34 +0530491enum cb_err tis_vendor_write(unsigned int addr, const void *buffer, size_t bytes)
Tim Wawrzynczak1e50dfb2022-02-16 13:48:07 -0700492{
493 return cr50_i2c_write(addr & 0xff, buffer, bytes) ? CB_ERR : CB_SUCCESS;
494}
495
Subrata Banik60b2ab82022-03-09 12:55:34 +0530496enum cb_err tis_vendor_read(unsigned int addr, void *buffer, size_t bytes)
Tim Wawrzynczak1e50dfb2022-02-16 13:48:07 -0700497{
498 return cr50_i2c_read(addr & 0xff, buffer, bytes) ? CB_ERR : CB_SUCCESS;
499}