blob: 6c12ee4e9e2a236cc1e4e663ab276340f8dae332 [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>
Duncan Laurie2ea13c82016-09-19 16:04:39 -070020#include <string.h>
21#include <types.h>
22#include <delay.h>
23#include <console/console.h>
Nico Huber0f2dd1e2017-08-01 14:02:40 +020024#include <device/i2c_simple.h>
Duncan Laurie2ea13c82016-09-19 16:04:39 -070025#include <endian.h>
26#include <timer.h>
Philipp Deppenwiesed88fb362017-10-18 20:26:18 +020027#include <security/tpm/tis.h>
Elyes HAOUASede8dd02019-06-23 06:57:53 +020028
Duncan Laurie2ea13c82016-09-19 16:04:39 -070029#include "tpm.h"
30
Duncan Laurie3727a8d2016-09-19 16:37:46 -070031#define CR50_MAX_BUFSIZE 63
Duncan Laurie469af7b2017-11-07 09:13:19 -080032#define CR50_TIMEOUT_INIT_MS 30000 /* Very long timeout for TPM init */
Duncan Laurie1dc036c2016-09-19 16:49:23 -070033#define CR50_TIMEOUT_LONG_MS 2000 /* Long timeout while waiting for TPM */
34#define CR50_TIMEOUT_SHORT_MS 2 /* Short timeout during transactions */
Duncan Laurieed4fa092016-11-01 15:03:13 -070035#define CR50_TIMEOUT_NOIRQ_MS 20 /* Timeout for TPM ready without IRQ */
36#define CR50_TIMEOUT_IRQ_MS 100 /* Timeout for TPM ready with IRQ */
Duncan Laurie2ea13c82016-09-19 16:04:39 -070037#define CR50_DID_VID 0x00281ae0L
38
39struct tpm_inf_dev {
40 int bus;
41 unsigned int addr;
Duncan Laurie3727a8d2016-09-19 16:37:46 -070042 uint8_t buf[CR50_MAX_BUFSIZE + sizeof(uint8_t)];
Duncan Laurie2ea13c82016-09-19 16:04:39 -070043};
44
Patrick Georgic9b13592019-11-29 11:47:47 +010045static struct tpm_inf_dev tpm_dev;
Duncan Laurie2ea13c82016-09-19 16:04:39 -070046
Aaron Durbin64031672018-04-21 14:45:32 -060047__weak int tis_plat_irq_status(void)
Daniel Kurtzc4852e72017-04-21 14:11:51 +080048{
Arthur Heymans0ca944b2019-11-20 19:51:06 +010049 static int warning_displayed;
Daniel Kurtzc4852e72017-04-21 14:11:51 +080050
Arthur Heymans0ca944b2019-11-20 19:51:06 +010051 if (!warning_displayed) {
Julius Wernere9665952022-01-21 17:06:20 -080052 printk(BIOS_WARNING, "%s() not implemented, wasting 20ms to wait on"
Elyes HAOUAS52016652021-01-16 17:29:49 +010053 " Cr50!\n", __func__);
Arthur Heymans0ca944b2019-11-20 19:51:06 +010054 warning_displayed = 1;
Daniel Kurtzc4852e72017-04-21 14:11:51 +080055 }
56 mdelay(CR50_TIMEOUT_NOIRQ_MS);
57
58 return 1;
59}
60
Duncan Laurie94cc4852016-09-19 17:22:10 -070061/* Wait for interrupt to indicate the TPM is ready */
Tim Wawrzynczakeb1891a2022-02-08 12:49:31 -070062static int cr50_i2c_wait_tpm_ready(void)
Duncan Laurie94cc4852016-09-19 17:22:10 -070063{
64 struct stopwatch sw;
65
Duncan Laurieed4fa092016-11-01 15:03:13 -070066 stopwatch_init_msecs_expire(&sw, CR50_TIMEOUT_IRQ_MS);
Duncan Laurie94cc4852016-09-19 17:22:10 -070067
Daniel Kurtzc4852e72017-04-21 14:11:51 +080068 while (!tis_plat_irq_status())
Vadim Bendeburyc7fc1992019-11-26 14:08:59 -080069 if (stopwatch_expired(&sw)) {
70 printk(BIOS_ERR, "Cr50 i2c TPM IRQ timeout!\n");
Duncan Laurie94cc4852016-09-19 17:22:10 -070071 return -1;
Vadim Bendeburyc7fc1992019-11-26 14:08:59 -080072 }
Duncan Laurie94cc4852016-09-19 17:22:10 -070073 return 0;
74}
75
Duncan Laurie2ea13c82016-09-19 16:04:39 -070076/*
Duncan Laurie510cb6a2016-09-19 17:05:45 -070077 * cr50_i2c_read() - read from TPM register
Duncan Laurie2ea13c82016-09-19 16:04:39 -070078 *
79 * @addr: register address to read from
80 * @buffer: provided by caller
81 * @len: number of bytes to read
82 *
83 * 1) send register address byte 'addr' to the TPM
84 * 2) wait for TPM to indicate it is ready
85 * 3) read 'len' bytes of TPM response into the provided 'buffer'
86 *
87 * Return -1 on error, 0 on success.
88 */
Tim Wawrzynczakeb1891a2022-02-08 12:49:31 -070089static int cr50_i2c_read(uint8_t addr, uint8_t *buffer, size_t len)
Duncan Laurie2ea13c82016-09-19 16:04:39 -070090{
Patrick Georgic9b13592019-11-29 11:47:47 +010091 if (tpm_dev.addr == 0)
Duncan Laurie2ea13c82016-09-19 16:04:39 -070092 return -1;
93
Duncan Laurie94cc4852016-09-19 17:22:10 -070094 /* Clear interrupt before starting transaction */
Daniel Kurtzc4852e72017-04-21 14:11:51 +080095 tis_plat_irq_status();
Duncan Laurie94cc4852016-09-19 17:22:10 -070096
Duncan Laurie2ea13c82016-09-19 16:04:39 -070097 /* Send the register address byte to the TPM */
Patrick Georgic9b13592019-11-29 11:47:47 +010098 if (i2c_write_raw(tpm_dev.bus, tpm_dev.addr, &addr, 1)) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -070099 printk(BIOS_ERR, "%s: Address write failed\n", __func__);
100 return -1;
101 }
102
103 /* Wait for TPM to be ready with response data */
Tim Wawrzynczakeb1891a2022-02-08 12:49:31 -0700104 if (cr50_i2c_wait_tpm_ready() < 0)
Duncan Laurie94cc4852016-09-19 17:22:10 -0700105 return -1;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700106
107 /* Read response data from the TPM */
Patrick Georgic9b13592019-11-29 11:47:47 +0100108 if (i2c_read_raw(tpm_dev.bus, tpm_dev.addr, buffer, len)) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700109 printk(BIOS_ERR, "%s: Read response failed\n", __func__);
110 return -1;
111 }
112
113 return 0;
114}
115
116/*
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700117 * cr50_i2c_write() - write to TPM register
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700118 *
119 * @addr: register address to write to
120 * @buffer: data to write
121 * @len: number of bytes to write
122 *
123 * 1) prepend the provided address to the provided data
124 * 2) send the address+data to the TPM
125 * 3) wait for TPM to indicate it is done writing
126 *
127 * Returns -1 on error, 0 on success.
128 */
Tim Wawrzynczakeb1891a2022-02-08 12:49:31 -0700129static int cr50_i2c_write(uint8_t addr, uint8_t *buffer, size_t len)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700130{
Patrick Georgic9b13592019-11-29 11:47:47 +0100131 if (tpm_dev.addr == 0)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700132 return -1;
Duncan Laurie3727a8d2016-09-19 16:37:46 -0700133 if (len > CR50_MAX_BUFSIZE)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700134 return -1;
135
136 /* Prepend the 'register address' to the buffer */
Patrick Georgic9b13592019-11-29 11:47:47 +0100137 tpm_dev.buf[0] = addr;
138 memcpy(tpm_dev.buf + 1, buffer, len);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700139
Duncan Laurie94cc4852016-09-19 17:22:10 -0700140 /* Clear interrupt before starting transaction */
Daniel Kurtzc4852e72017-04-21 14:11:51 +0800141 tis_plat_irq_status();
Duncan Laurie94cc4852016-09-19 17:22:10 -0700142
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700143 /* Send write request buffer with address */
Patrick Georgic9b13592019-11-29 11:47:47 +0100144 if (i2c_write_raw(tpm_dev.bus, tpm_dev.addr, tpm_dev.buf, len + 1)) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700145 printk(BIOS_ERR, "%s: Error writing to TPM\n", __func__);
146 return -1;
147 }
148
149 /* Wait for TPM to be ready */
Tim Wawrzynczakeb1891a2022-02-08 12:49:31 -0700150 return cr50_i2c_wait_tpm_ready();
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700151}
152
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800153/*
154 * Cr50 processes reset requests asynchronously and consceivably could be busy
155 * executing a long command and not reacting to the reset pulse for a while.
156 *
157 * This function will make sure that the AP does not proceed with boot until
158 * TPM finished reset processing.
159 */
Tim Wawrzynczakeb1891a2022-02-08 12:49:31 -0700160static int process_reset(void)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700161{
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800162 struct stopwatch sw;
Richard Spiegel7c1e9592018-08-09 14:41:17 -0700163 int rv = 0;
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800164 uint8_t access;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700165
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800166 /*
167 * Locality is released by TPM reset.
168 *
169 * If locality is taken at this point, this could be due to the fact
170 * that the TPM is performing a long operation and has not processed
171 * reset request yet. We'll wait up to CR50_TIMEOUT_INIT_MS and see if
172 * it releases locality when reset is processed.
173 */
174 stopwatch_init_msecs_expire(&sw, CR50_TIMEOUT_INIT_MS);
175 do {
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800176 const uint8_t mask =
177 TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700178
Tim Wawrzynczakeb1891a2022-02-08 12:49:31 -0700179 rv = cr50_i2c_read(TPM_ACCESS(0),
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800180 &access, sizeof(access));
181 if (rv || ((access & mask) == mask)) {
182 /*
183 * Don't bombard the chip with traffic, let it keep
184 * processing the command.
185 */
186 mdelay(2);
187 continue;
188 }
189
190 printk(BIOS_INFO, "TPM ready after %ld ms\n",
191 stopwatch_duration_msecs(&sw));
192
193 return 0;
194 } while (!stopwatch_expired(&sw));
195
Richard Spiegel7c1e9592018-08-09 14:41:17 -0700196 if (rv)
197 printk(BIOS_ERR, "Failed to read TPM\n");
198 else
199 printk(BIOS_ERR,
200 "TPM failed to reset after %ld ms, status: %#x\n",
201 stopwatch_duration_msecs(&sw), access);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700202
203 return -1;
204}
205
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800206/*
207 * Locality could be already claimed (if this is a later coreboot stage and
208 * the RO did not release it), or not yet claimed, if this is verstage or the
209 * older RO did release it.
210 */
Tim Wawrzynczakeb1891a2022-02-08 12:49:31 -0700211static int claim_locality(void)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700212{
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800213 uint8_t access;
214 const uint8_t mask = TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY;
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700215
Tim Wawrzynczakeb1891a2022-02-08 12:49:31 -0700216 if (cr50_i2c_read(TPM_ACCESS(0), &access, sizeof(access)))
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700217 return -1;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700218
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800219 if ((access & mask) == mask) {
220 printk(BIOS_INFO, "Locality already claimed\n");
221 return 0;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700222 }
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800223
224 access = TPM_ACCESS_REQUEST_USE;
Tim Wawrzynczakeb1891a2022-02-08 12:49:31 -0700225 if (cr50_i2c_write(TPM_ACCESS(0),
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800226 &access, sizeof(access)))
227 return -1;
228
Tim Wawrzynczakeb1891a2022-02-08 12:49:31 -0700229 if (cr50_i2c_read(TPM_ACCESS(0), &access, sizeof(access)))
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800230 return -1;
231
232 if ((access & mask) != mask) {
233 printk(BIOS_INFO, "Failed to claim locality.\n");
234 return -1;
235 }
236
237 return 0;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700238}
239
240/* cr50 requires all 4 bytes of status register to be read */
Duncan Laurief235a9b2016-09-19 17:19:10 -0700241static uint8_t cr50_i2c_tis_status(struct tpm_chip *chip)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700242{
243 uint8_t buf[4];
Tim Wawrzynczakeb1891a2022-02-08 12:49:31 -0700244 if (cr50_i2c_read(TPM_STS(chip->vendor.locality),
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700245 buf, sizeof(buf)) < 0) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700246 printk(BIOS_ERR, "%s: Failed to read status\n", __func__);
247 return 0;
248 }
249 return buf[0];
250}
251
252/* cr50 requires all 4 bytes of status register to be written */
Duncan Laurief235a9b2016-09-19 17:19:10 -0700253static void cr50_i2c_tis_ready(struct tpm_chip *chip)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700254{
255 uint8_t buf[4] = { TPM_STS_COMMAND_READY };
Tim Wawrzynczakeb1891a2022-02-08 12:49:31 -0700256 cr50_i2c_write(TPM_STS(chip->vendor.locality), buf, sizeof(buf));
Duncan Laurie1dc036c2016-09-19 16:49:23 -0700257 mdelay(CR50_TIMEOUT_SHORT_MS);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700258}
259
260/* cr50 uses bytes 3:2 of status register for burst count and
261 * all 4 bytes must be read */
Duncan Laurief235a9b2016-09-19 17:19:10 -0700262static int cr50_i2c_wait_burststs(struct tpm_chip *chip, uint8_t mask,
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700263 size_t *burst, int *status)
264{
265 uint8_t buf[4];
266 struct stopwatch sw;
267
Duncan Laurie1dc036c2016-09-19 16:49:23 -0700268 stopwatch_init_msecs_expire(&sw, CR50_TIMEOUT_LONG_MS);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700269
270 while (!stopwatch_expired(&sw)) {
Tim Wawrzynczakeb1891a2022-02-08 12:49:31 -0700271 if (cr50_i2c_read(TPM_STS(chip->vendor.locality),
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700272 buf, sizeof(buf)) != 0) {
Duncan Laurie1dc036c2016-09-19 16:49:23 -0700273 mdelay(CR50_TIMEOUT_SHORT_MS);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700274 continue;
275 }
276
277 *status = buf[0];
278 *burst = read_le16(&buf[1]);
279
280 /* Check if mask matches and burst is valid */
281 if ((*status & mask) == mask &&
Duncan Laurie3727a8d2016-09-19 16:37:46 -0700282 *burst > 0 && *burst <= CR50_MAX_BUFSIZE)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700283 return 0;
284
Duncan Laurie1dc036c2016-09-19 16:49:23 -0700285 mdelay(CR50_TIMEOUT_SHORT_MS);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700286 }
287
288 printk(BIOS_ERR, "%s: Timeout reading burst and status\n", __func__);
289 return -1;
290}
291
Duncan Laurief235a9b2016-09-19 17:19:10 -0700292static int cr50_i2c_tis_recv(struct tpm_chip *chip, uint8_t *buf,
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700293 size_t buf_len)
294{
295 size_t burstcnt, current, len, expected;
296 uint8_t addr = TPM_DATA_FIFO(chip->vendor.locality);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700297 uint8_t mask = TPM_STS_VALID | TPM_STS_DATA_AVAIL;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700298 int status;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700299
300 if (buf_len < TPM_HEADER_SIZE)
Duncan Laurief235a9b2016-09-19 17:19:10 -0700301 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700302
Duncan Laurief235a9b2016-09-19 17:19:10 -0700303 if (cr50_i2c_wait_burststs(chip, mask, &burstcnt, &status) < 0) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700304 printk(BIOS_ERR, "%s: First chunk not available\n", __func__);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700305 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700306 }
307
308 /* Read first chunk of burstcnt bytes */
Tim Wawrzynczakeb1891a2022-02-08 12:49:31 -0700309 if (cr50_i2c_read(addr, buf, burstcnt) != 0) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700310 printk(BIOS_ERR, "%s: Read failed\n", __func__);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700311 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700312 }
313
314 /* Determine expected data in the return buffer */
315 expected = read_be32(buf + TPM_RSP_SIZE_BYTE);
316 if (expected > buf_len) {
317 printk(BIOS_ERR, "%s: Too much data: %zu > %zu\n",
318 __func__, expected, buf_len);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700319 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700320 }
321
322 /* Now read the rest of the data */
323 current = burstcnt;
324 while (current < expected) {
325 /* Read updated burst count and check status */
Duncan Laurief235a9b2016-09-19 17:19:10 -0700326 if (cr50_i2c_wait_burststs(chip, mask, &burstcnt, &status) < 0)
327 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700328
Elyes HAOUAS361a9352019-12-18 21:26:33 +0100329 len = MIN(burstcnt, expected - current);
Tim Wawrzynczakeb1891a2022-02-08 12:49:31 -0700330 if (cr50_i2c_read(addr, buf + current, len) != 0) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700331 printk(BIOS_ERR, "%s: Read failed\n", __func__);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700332 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700333 }
334
335 current += len;
336 }
337
338 /* Ensure TPM is done reading data */
Duncan Laurief235a9b2016-09-19 17:19:10 -0700339 if (cr50_i2c_wait_burststs(chip, TPM_STS_VALID, &burstcnt, &status) < 0)
340 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700341 if (status & TPM_STS_DATA_AVAIL) {
342 printk(BIOS_ERR, "%s: Data still available\n", __func__);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700343 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700344 }
345
Duncan Laurief235a9b2016-09-19 17:19:10 -0700346 return current;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700347
Duncan Laurief235a9b2016-09-19 17:19:10 -0700348out_err:
349 /* Abort current transaction if still pending */
350 if (cr50_i2c_tis_status(chip) & TPM_STS_COMMAND_READY)
351 cr50_i2c_tis_ready(chip);
352 return -1;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700353}
354
Duncan Laurief235a9b2016-09-19 17:19:10 -0700355static int cr50_i2c_tis_send(struct tpm_chip *chip, uint8_t *buf, size_t len)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700356{
357 int status;
358 size_t burstcnt, limit, sent = 0;
359 uint8_t tpm_go[4] = { TPM_STS_GO };
360 struct stopwatch sw;
361
Duncan Laurie1dc036c2016-09-19 16:49:23 -0700362 stopwatch_init_msecs_expire(&sw, CR50_TIMEOUT_LONG_MS);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700363
364 /* Wait until TPM is ready for a command */
Duncan Laurief235a9b2016-09-19 17:19:10 -0700365 while (!(cr50_i2c_tis_status(chip) & TPM_STS_COMMAND_READY)) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700366 if (stopwatch_expired(&sw)) {
367 printk(BIOS_ERR, "%s: Command ready timeout\n",
368 __func__);
369 return -1;
370 }
371
Duncan Laurief235a9b2016-09-19 17:19:10 -0700372 cr50_i2c_tis_ready(chip);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700373 }
374
375 while (len > 0) {
Duncan Laurief235a9b2016-09-19 17:19:10 -0700376 uint8_t mask = TPM_STS_VALID;
377
378 /* Wait for data if this is not the first chunk */
379 if (sent > 0)
380 mask |= TPM_STS_DATA_EXPECT;
381
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700382 /* Read burst count and check status */
Duncan Laurief235a9b2016-09-19 17:19:10 -0700383 if (cr50_i2c_wait_burststs(chip, mask, &burstcnt, &status) < 0)
384 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700385
386 /* Use burstcnt - 1 to account for the address byte
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700387 * that is inserted by cr50_i2c_write() */
Elyes HAOUAS361a9352019-12-18 21:26:33 +0100388 limit = MIN(burstcnt - 1, len);
Tim Wawrzynczakeb1891a2022-02-08 12:49:31 -0700389 if (cr50_i2c_write(TPM_DATA_FIFO(chip->vendor.locality),
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700390 &buf[sent], limit) != 0) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700391 printk(BIOS_ERR, "%s: Write failed\n", __func__);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700392 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700393 }
394
395 sent += limit;
396 len -= limit;
397 }
398
399 /* Ensure TPM is not expecting more data */
Duncan Laurief235a9b2016-09-19 17:19:10 -0700400 if (cr50_i2c_wait_burststs(chip, TPM_STS_VALID, &burstcnt, &status) < 0)
401 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700402 if (status & TPM_STS_DATA_EXPECT) {
403 printk(BIOS_ERR, "%s: Data still expected\n", __func__);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700404 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700405 }
406
407 /* Start the TPM command */
Tim Wawrzynczakeb1891a2022-02-08 12:49:31 -0700408 if (cr50_i2c_write(TPM_STS(chip->vendor.locality), tpm_go,
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700409 sizeof(tpm_go)) < 0) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700410 printk(BIOS_ERR, "%s: Start command failed\n", __func__);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700411 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700412 }
413 return sent;
414
Duncan Laurief235a9b2016-09-19 17:19:10 -0700415out_err:
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700416 /* Abort current transaction if still pending */
Duncan Laurief235a9b2016-09-19 17:19:10 -0700417 if (cr50_i2c_tis_status(chip) & TPM_STS_COMMAND_READY)
418 cr50_i2c_tis_ready(chip);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700419 return -1;
420}
421
422static void cr50_vendor_init(struct tpm_chip *chip)
423{
424 memset(&chip->vendor, 0, sizeof(struct tpm_vendor_specific));
425 chip->vendor.req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID;
426 chip->vendor.req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID;
427 chip->vendor.req_canceled = TPM_STS_COMMAND_READY;
Duncan Laurief235a9b2016-09-19 17:19:10 -0700428 chip->vendor.status = &cr50_i2c_tis_status;
429 chip->vendor.recv = &cr50_i2c_tis_recv;
430 chip->vendor.send = &cr50_i2c_tis_send;
431 chip->vendor.cancel = &cr50_i2c_tis_ready;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700432}
433
Lee Leahy52ab30b2017-03-15 09:22:11 -0700434int tpm_vendor_probe(unsigned int bus, uint32_t addr)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700435{
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700436 return 0;
437}
438
Keith Short51436352018-12-17 14:21:46 -0700439static int cr50_i2c_probe(struct tpm_chip *chip, uint32_t *did_vid)
440{
441 int retries;
442
443 /*
Rob Barnes5a8b9c92022-01-13 20:54:14 -0700444 * 200 ms should be enough to synchronize with the TPM even under the
Keith Short51436352018-12-17 14:21:46 -0700445 * worst nested reset request conditions. In vast majority of cases
446 * there would be no wait at all.
447 */
448 printk(BIOS_INFO, "Probing TPM I2C: ");
449
Rob Barnes5a8b9c92022-01-13 20:54:14 -0700450 for (retries = 20; retries > 0; retries--) {
Keith Short51436352018-12-17 14:21:46 -0700451 int rc;
452
Tim Wawrzynczakeb1891a2022-02-08 12:49:31 -0700453 rc = cr50_i2c_read(TPM_DID_VID(0), (uint8_t *)did_vid, 4);
Keith Short51436352018-12-17 14:21:46 -0700454
455 /* Exit once DID and VID verified */
456 if (!rc && (*did_vid == CR50_DID_VID)) {
457 printk(BIOS_INFO, "done! DID_VID 0x%08x\n", *did_vid);
458 return 0;
459 }
460
461 /* TPM might be resetting, let's retry in a bit. */
462 mdelay(10);
463 printk(BIOS_INFO, ".");
464 }
465
466 /*
467 * I2C reads failed, or the DID and VID didn't match
468 */
469 printk(BIOS_ERR, "DID_VID 0x%08x not recognized\n", *did_vid);
470 return -1;
471}
472
Lee Leahy52ab30b2017-03-15 09:22:11 -0700473int tpm_vendor_init(struct tpm_chip *chip, unsigned int bus, uint32_t dev_addr)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700474{
Keith Short51436352018-12-17 14:21:46 -0700475 uint32_t did_vid = 0;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700476
477 if (dev_addr == 0) {
478 printk(BIOS_ERR, "%s: missing device address\n", __func__);
479 return -1;
480 }
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
Keith Short51436352018-12-17 14:21:46 -0700487 if (cr50_i2c_probe(chip, &did_vid))
488 return -1;
489
Julius Werner21a40532020-04-21 16:03:53 -0700490 if (ENV_SEPARATE_VERSTAGE || ENV_BOOTBLOCK)
Tim Wawrzynczakeb1891a2022-02-08 12:49:31 -0700491 if (process_reset())
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800492 return -1;
493
Tim Wawrzynczakeb1891a2022-02-08 12:49:31 -0700494 if (claim_locality())
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700495 return -1;
496
Daniel Kurtzc4852e72017-04-21 14:11:51 +0800497 printk(BIOS_DEBUG, "cr50 TPM 2.0 (i2c %u:0x%02x id 0x%x)\n",
Keith Short51436352018-12-17 14:21:46 -0700498 bus, dev_addr, did_vid >> 16);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700499
500 chip->is_open = 1;
501 return 0;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700502}
503
504void tpm_vendor_cleanup(struct tpm_chip *chip)
505{
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700506}