blob: 14238412318fbe3e6ff598938d05dc85802afba7 [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 */
62static int cr50_i2c_wait_tpm_ready(struct tpm_chip *chip)
63{
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 *
Duncan Laurie510cb6a2016-09-19 17:05:45 -070079 * @chip: TPM chip information
Duncan Laurie2ea13c82016-09-19 16:04:39 -070080 * @addr: register address to read from
81 * @buffer: provided by caller
82 * @len: number of bytes to read
83 *
84 * 1) send register address byte 'addr' to the TPM
85 * 2) wait for TPM to indicate it is ready
86 * 3) read 'len' bytes of TPM response into the provided 'buffer'
87 *
88 * Return -1 on error, 0 on success.
89 */
Duncan Laurie510cb6a2016-09-19 17:05:45 -070090static int cr50_i2c_read(struct tpm_chip *chip, uint8_t addr,
91 uint8_t *buffer, size_t len)
Duncan Laurie2ea13c82016-09-19 16:04:39 -070092{
Patrick Georgic9b13592019-11-29 11:47:47 +010093 if (tpm_dev.addr == 0)
Duncan Laurie2ea13c82016-09-19 16:04:39 -070094 return -1;
95
Duncan Laurie94cc4852016-09-19 17:22:10 -070096 /* Clear interrupt before starting transaction */
Daniel Kurtzc4852e72017-04-21 14:11:51 +080097 tis_plat_irq_status();
Duncan Laurie94cc4852016-09-19 17:22:10 -070098
Duncan Laurie2ea13c82016-09-19 16:04:39 -070099 /* Send the register address byte to the TPM */
Patrick Georgic9b13592019-11-29 11:47:47 +0100100 if (i2c_write_raw(tpm_dev.bus, tpm_dev.addr, &addr, 1)) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700101 printk(BIOS_ERR, "%s: Address write failed\n", __func__);
102 return -1;
103 }
104
105 /* Wait for TPM to be ready with response data */
Duncan Laurie94cc4852016-09-19 17:22:10 -0700106 if (cr50_i2c_wait_tpm_ready(chip) < 0)
107 return -1;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700108
109 /* Read response data from the TPM */
Patrick Georgic9b13592019-11-29 11:47:47 +0100110 if (i2c_read_raw(tpm_dev.bus, tpm_dev.addr, buffer, len)) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700111 printk(BIOS_ERR, "%s: Read response failed\n", __func__);
112 return -1;
113 }
114
115 return 0;
116}
117
118/*
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700119 * cr50_i2c_write() - write to TPM register
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700120 *
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700121 * @chip: TPM chip information
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700122 * @addr: register address to write to
123 * @buffer: data to write
124 * @len: number of bytes to write
125 *
126 * 1) prepend the provided address to the provided data
127 * 2) send the address+data to the TPM
128 * 3) wait for TPM to indicate it is done writing
129 *
130 * Returns -1 on error, 0 on success.
131 */
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700132static int cr50_i2c_write(struct tpm_chip *chip,
133 uint8_t addr, uint8_t *buffer, size_t len)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700134{
Patrick Georgic9b13592019-11-29 11:47:47 +0100135 if (tpm_dev.addr == 0)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700136 return -1;
Duncan Laurie3727a8d2016-09-19 16:37:46 -0700137 if (len > CR50_MAX_BUFSIZE)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700138 return -1;
139
140 /* Prepend the 'register address' to the buffer */
Patrick Georgic9b13592019-11-29 11:47:47 +0100141 tpm_dev.buf[0] = addr;
142 memcpy(tpm_dev.buf + 1, buffer, len);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700143
Duncan Laurie94cc4852016-09-19 17:22:10 -0700144 /* Clear interrupt before starting transaction */
Daniel Kurtzc4852e72017-04-21 14:11:51 +0800145 tis_plat_irq_status();
Duncan Laurie94cc4852016-09-19 17:22:10 -0700146
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700147 /* Send write request buffer with address */
Patrick Georgic9b13592019-11-29 11:47:47 +0100148 if (i2c_write_raw(tpm_dev.bus, tpm_dev.addr, tpm_dev.buf, len + 1)) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700149 printk(BIOS_ERR, "%s: Error writing to TPM\n", __func__);
150 return -1;
151 }
152
153 /* Wait for TPM to be ready */
Duncan Laurie94cc4852016-09-19 17:22:10 -0700154 return cr50_i2c_wait_tpm_ready(chip);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700155}
156
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800157/*
158 * Cr50 processes reset requests asynchronously and consceivably could be busy
159 * executing a long command and not reacting to the reset pulse for a while.
160 *
161 * This function will make sure that the AP does not proceed with boot until
162 * TPM finished reset processing.
163 */
164static int process_reset(struct tpm_chip *chip)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700165{
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800166 struct stopwatch sw;
Richard Spiegel7c1e9592018-08-09 14:41:17 -0700167 int rv = 0;
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800168 uint8_t access;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700169
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800170 /*
171 * Locality is released by TPM reset.
172 *
173 * If locality is taken at this point, this could be due to the fact
174 * that the TPM is performing a long operation and has not processed
175 * reset request yet. We'll wait up to CR50_TIMEOUT_INIT_MS and see if
176 * it releases locality when reset is processed.
177 */
178 stopwatch_init_msecs_expire(&sw, CR50_TIMEOUT_INIT_MS);
179 do {
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800180 const uint8_t mask =
181 TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700182
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800183 rv = cr50_i2c_read(chip, TPM_ACCESS(0),
184 &access, sizeof(access));
185 if (rv || ((access & mask) == mask)) {
186 /*
187 * Don't bombard the chip with traffic, let it keep
188 * processing the command.
189 */
190 mdelay(2);
191 continue;
192 }
193
194 printk(BIOS_INFO, "TPM ready after %ld ms\n",
195 stopwatch_duration_msecs(&sw));
196
197 return 0;
198 } while (!stopwatch_expired(&sw));
199
Richard Spiegel7c1e9592018-08-09 14:41:17 -0700200 if (rv)
201 printk(BIOS_ERR, "Failed to read TPM\n");
202 else
203 printk(BIOS_ERR,
204 "TPM failed to reset after %ld ms, status: %#x\n",
205 stopwatch_duration_msecs(&sw), access);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700206
207 return -1;
208}
209
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800210/*
211 * Locality could be already claimed (if this is a later coreboot stage and
212 * the RO did not release it), or not yet claimed, if this is verstage or the
213 * older RO did release it.
214 */
215static int claim_locality(struct tpm_chip *chip)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700216{
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800217 uint8_t access;
218 const uint8_t mask = TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY;
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700219
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800220 if (cr50_i2c_read(chip, TPM_ACCESS(0), &access, sizeof(access)))
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700221 return -1;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700222
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800223 if ((access & mask) == mask) {
224 printk(BIOS_INFO, "Locality already claimed\n");
225 return 0;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700226 }
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800227
228 access = TPM_ACCESS_REQUEST_USE;
229 if (cr50_i2c_write(chip, TPM_ACCESS(0),
230 &access, sizeof(access)))
231 return -1;
232
233 if (cr50_i2c_read(chip, TPM_ACCESS(0), &access, sizeof(access)))
234 return -1;
235
236 if ((access & mask) != mask) {
237 printk(BIOS_INFO, "Failed to claim locality.\n");
238 return -1;
239 }
240
241 return 0;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700242}
243
244/* cr50 requires all 4 bytes of status register to be read */
Duncan Laurief235a9b2016-09-19 17:19:10 -0700245static uint8_t cr50_i2c_tis_status(struct tpm_chip *chip)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700246{
247 uint8_t buf[4];
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700248 if (cr50_i2c_read(chip, TPM_STS(chip->vendor.locality),
249 buf, sizeof(buf)) < 0) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700250 printk(BIOS_ERR, "%s: Failed to read status\n", __func__);
251 return 0;
252 }
253 return buf[0];
254}
255
256/* cr50 requires all 4 bytes of status register to be written */
Duncan Laurief235a9b2016-09-19 17:19:10 -0700257static void cr50_i2c_tis_ready(struct tpm_chip *chip)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700258{
259 uint8_t buf[4] = { TPM_STS_COMMAND_READY };
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700260 cr50_i2c_write(chip, TPM_STS(chip->vendor.locality), buf, sizeof(buf));
Duncan Laurie1dc036c2016-09-19 16:49:23 -0700261 mdelay(CR50_TIMEOUT_SHORT_MS);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700262}
263
264/* cr50 uses bytes 3:2 of status register for burst count and
265 * all 4 bytes must be read */
Duncan Laurief235a9b2016-09-19 17:19:10 -0700266static int cr50_i2c_wait_burststs(struct tpm_chip *chip, uint8_t mask,
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700267 size_t *burst, int *status)
268{
269 uint8_t buf[4];
270 struct stopwatch sw;
271
Duncan Laurie1dc036c2016-09-19 16:49:23 -0700272 stopwatch_init_msecs_expire(&sw, CR50_TIMEOUT_LONG_MS);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700273
274 while (!stopwatch_expired(&sw)) {
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700275 if (cr50_i2c_read(chip, TPM_STS(chip->vendor.locality),
276 buf, sizeof(buf)) != 0) {
Duncan Laurie1dc036c2016-09-19 16:49:23 -0700277 mdelay(CR50_TIMEOUT_SHORT_MS);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700278 continue;
279 }
280
281 *status = buf[0];
282 *burst = read_le16(&buf[1]);
283
284 /* Check if mask matches and burst is valid */
285 if ((*status & mask) == mask &&
Duncan Laurie3727a8d2016-09-19 16:37:46 -0700286 *burst > 0 && *burst <= CR50_MAX_BUFSIZE)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700287 return 0;
288
Duncan Laurie1dc036c2016-09-19 16:49:23 -0700289 mdelay(CR50_TIMEOUT_SHORT_MS);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700290 }
291
292 printk(BIOS_ERR, "%s: Timeout reading burst and status\n", __func__);
293 return -1;
294}
295
Duncan Laurief235a9b2016-09-19 17:19:10 -0700296static int cr50_i2c_tis_recv(struct tpm_chip *chip, uint8_t *buf,
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700297 size_t buf_len)
298{
299 size_t burstcnt, current, len, expected;
300 uint8_t addr = TPM_DATA_FIFO(chip->vendor.locality);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700301 uint8_t mask = TPM_STS_VALID | TPM_STS_DATA_AVAIL;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700302 int status;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700303
304 if (buf_len < TPM_HEADER_SIZE)
Duncan Laurief235a9b2016-09-19 17:19:10 -0700305 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700306
Duncan Laurief235a9b2016-09-19 17:19:10 -0700307 if (cr50_i2c_wait_burststs(chip, mask, &burstcnt, &status) < 0) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700308 printk(BIOS_ERR, "%s: First chunk not available\n", __func__);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700309 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700310 }
311
312 /* Read first chunk of burstcnt bytes */
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700313 if (cr50_i2c_read(chip, addr, buf, burstcnt) != 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 /* Determine expected data in the return buffer */
319 expected = read_be32(buf + TPM_RSP_SIZE_BYTE);
320 if (expected > buf_len) {
321 printk(BIOS_ERR, "%s: Too much data: %zu > %zu\n",
322 __func__, expected, buf_len);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700323 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700324 }
325
326 /* Now read the rest of the data */
327 current = burstcnt;
328 while (current < expected) {
329 /* Read updated burst count and check status */
Duncan Laurief235a9b2016-09-19 17:19:10 -0700330 if (cr50_i2c_wait_burststs(chip, mask, &burstcnt, &status) < 0)
331 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700332
Elyes HAOUAS361a9352019-12-18 21:26:33 +0100333 len = MIN(burstcnt, expected - current);
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700334 if (cr50_i2c_read(chip, addr, buf + current, len) != 0) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700335 printk(BIOS_ERR, "%s: Read failed\n", __func__);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700336 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700337 }
338
339 current += len;
340 }
341
342 /* Ensure TPM is done reading data */
Duncan Laurief235a9b2016-09-19 17:19:10 -0700343 if (cr50_i2c_wait_burststs(chip, TPM_STS_VALID, &burstcnt, &status) < 0)
344 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700345 if (status & TPM_STS_DATA_AVAIL) {
346 printk(BIOS_ERR, "%s: Data still available\n", __func__);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700347 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700348 }
349
Duncan Laurief235a9b2016-09-19 17:19:10 -0700350 return current;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700351
Duncan Laurief235a9b2016-09-19 17:19:10 -0700352out_err:
353 /* Abort current transaction if still pending */
354 if (cr50_i2c_tis_status(chip) & TPM_STS_COMMAND_READY)
355 cr50_i2c_tis_ready(chip);
356 return -1;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700357}
358
Duncan Laurief235a9b2016-09-19 17:19:10 -0700359static int cr50_i2c_tis_send(struct tpm_chip *chip, uint8_t *buf, size_t len)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700360{
361 int status;
362 size_t burstcnt, limit, sent = 0;
363 uint8_t tpm_go[4] = { TPM_STS_GO };
364 struct stopwatch sw;
365
Duncan Laurie1dc036c2016-09-19 16:49:23 -0700366 stopwatch_init_msecs_expire(&sw, CR50_TIMEOUT_LONG_MS);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700367
368 /* Wait until TPM is ready for a command */
Duncan Laurief235a9b2016-09-19 17:19:10 -0700369 while (!(cr50_i2c_tis_status(chip) & TPM_STS_COMMAND_READY)) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700370 if (stopwatch_expired(&sw)) {
371 printk(BIOS_ERR, "%s: Command ready timeout\n",
372 __func__);
373 return -1;
374 }
375
Duncan Laurief235a9b2016-09-19 17:19:10 -0700376 cr50_i2c_tis_ready(chip);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700377 }
378
379 while (len > 0) {
Duncan Laurief235a9b2016-09-19 17:19:10 -0700380 uint8_t mask = TPM_STS_VALID;
381
382 /* Wait for data if this is not the first chunk */
383 if (sent > 0)
384 mask |= TPM_STS_DATA_EXPECT;
385
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700386 /* Read burst count and check status */
Duncan Laurief235a9b2016-09-19 17:19:10 -0700387 if (cr50_i2c_wait_burststs(chip, mask, &burstcnt, &status) < 0)
388 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700389
390 /* Use burstcnt - 1 to account for the address byte
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700391 * that is inserted by cr50_i2c_write() */
Elyes HAOUAS361a9352019-12-18 21:26:33 +0100392 limit = MIN(burstcnt - 1, len);
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700393 if (cr50_i2c_write(chip, TPM_DATA_FIFO(chip->vendor.locality),
394 &buf[sent], limit) != 0) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700395 printk(BIOS_ERR, "%s: Write failed\n", __func__);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700396 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700397 }
398
399 sent += limit;
400 len -= limit;
401 }
402
403 /* Ensure TPM is not expecting more data */
Duncan Laurief235a9b2016-09-19 17:19:10 -0700404 if (cr50_i2c_wait_burststs(chip, TPM_STS_VALID, &burstcnt, &status) < 0)
405 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700406 if (status & TPM_STS_DATA_EXPECT) {
407 printk(BIOS_ERR, "%s: Data still expected\n", __func__);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700408 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700409 }
410
411 /* Start the TPM command */
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700412 if (cr50_i2c_write(chip, TPM_STS(chip->vendor.locality), tpm_go,
413 sizeof(tpm_go)) < 0) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700414 printk(BIOS_ERR, "%s: Start command failed\n", __func__);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700415 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700416 }
417 return sent;
418
Duncan Laurief235a9b2016-09-19 17:19:10 -0700419out_err:
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700420 /* Abort current transaction if still pending */
Duncan Laurief235a9b2016-09-19 17:19:10 -0700421 if (cr50_i2c_tis_status(chip) & TPM_STS_COMMAND_READY)
422 cr50_i2c_tis_ready(chip);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700423 return -1;
424}
425
426static void cr50_vendor_init(struct tpm_chip *chip)
427{
428 memset(&chip->vendor, 0, sizeof(struct tpm_vendor_specific));
429 chip->vendor.req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID;
430 chip->vendor.req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID;
431 chip->vendor.req_canceled = TPM_STS_COMMAND_READY;
Duncan Laurief235a9b2016-09-19 17:19:10 -0700432 chip->vendor.status = &cr50_i2c_tis_status;
433 chip->vendor.recv = &cr50_i2c_tis_recv;
434 chip->vendor.send = &cr50_i2c_tis_send;
435 chip->vendor.cancel = &cr50_i2c_tis_ready;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700436}
437
Lee Leahy52ab30b2017-03-15 09:22:11 -0700438int tpm_vendor_probe(unsigned int bus, uint32_t addr)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700439{
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700440 return 0;
441}
442
Keith Short51436352018-12-17 14:21:46 -0700443static int cr50_i2c_probe(struct tpm_chip *chip, uint32_t *did_vid)
444{
445 int retries;
446
447 /*
Rob Barnes5a8b9c92022-01-13 20:54:14 -0700448 * 200 ms should be enough to synchronize with the TPM even under the
Keith Short51436352018-12-17 14:21:46 -0700449 * worst nested reset request conditions. In vast majority of cases
450 * there would be no wait at all.
451 */
452 printk(BIOS_INFO, "Probing TPM I2C: ");
453
Rob Barnes5a8b9c92022-01-13 20:54:14 -0700454 for (retries = 20; retries > 0; retries--) {
Keith Short51436352018-12-17 14:21:46 -0700455 int rc;
456
457 rc = cr50_i2c_read(chip, TPM_DID_VID(0), (uint8_t *)did_vid, 4);
458
459 /* Exit once DID and VID verified */
460 if (!rc && (*did_vid == CR50_DID_VID)) {
461 printk(BIOS_INFO, "done! DID_VID 0x%08x\n", *did_vid);
462 return 0;
463 }
464
465 /* TPM might be resetting, let's retry in a bit. */
466 mdelay(10);
467 printk(BIOS_INFO, ".");
468 }
469
470 /*
471 * I2C reads failed, or the DID and VID didn't match
472 */
473 printk(BIOS_ERR, "DID_VID 0x%08x not recognized\n", *did_vid);
474 return -1;
475}
476
Lee Leahy52ab30b2017-03-15 09:22:11 -0700477int tpm_vendor_init(struct tpm_chip *chip, unsigned int bus, uint32_t dev_addr)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700478{
Keith Short51436352018-12-17 14:21:46 -0700479 uint32_t did_vid = 0;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700480
481 if (dev_addr == 0) {
482 printk(BIOS_ERR, "%s: missing device address\n", __func__);
483 return -1;
484 }
485
Patrick Georgic9b13592019-11-29 11:47:47 +0100486 tpm_dev.bus = bus;
487 tpm_dev.addr = dev_addr;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700488
489 cr50_vendor_init(chip);
490
Keith Short51436352018-12-17 14:21:46 -0700491 if (cr50_i2c_probe(chip, &did_vid))
492 return -1;
493
Julius Werner21a40532020-04-21 16:03:53 -0700494 if (ENV_SEPARATE_VERSTAGE || ENV_BOOTBLOCK)
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800495 if (process_reset(chip))
496 return -1;
497
498 if (claim_locality(chip))
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700499 return -1;
500
Daniel Kurtzc4852e72017-04-21 14:11:51 +0800501 printk(BIOS_DEBUG, "cr50 TPM 2.0 (i2c %u:0x%02x id 0x%x)\n",
Keith Short51436352018-12-17 14:21:46 -0700502 bus, dev_addr, did_vid >> 16);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700503
504 chip->is_open = 1;
505 return 0;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700506}
507
508void tpm_vendor_cleanup(struct tpm_chip *chip)
509{
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700510}