blob: 42eeff4b744abf87e3167597c7d54cff491b9b3b [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;
42 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
Aaron Durbin64031672018-04-21 14:45:32 -060048__weak int tis_plat_irq_status(void)
Daniel Kurtzc4852e72017-04-21 14:11:51 +080049{
Arthur Heymans0ca944b2019-11-20 19:51:06 +010050 static int warning_displayed;
Daniel Kurtzc4852e72017-04-21 14:11:51 +080051
Arthur Heymans0ca944b2019-11-20 19:51:06 +010052 if (!warning_displayed) {
Julius Wernere9665952022-01-21 17:06:20 -080053 printk(BIOS_WARNING, "%s() not implemented, wasting 20ms to wait on"
Elyes HAOUAS52016652021-01-16 17:29:49 +010054 " Cr50!\n", __func__);
Arthur Heymans0ca944b2019-11-20 19:51:06 +010055 warning_displayed = 1;
Daniel Kurtzc4852e72017-04-21 14:11:51 +080056 }
57 mdelay(CR50_TIMEOUT_NOIRQ_MS);
58
59 return 1;
60}
61
Duncan Laurie2ea13c82016-09-19 16:04:39 -070062/*
Duncan Laurie510cb6a2016-09-19 17:05:45 -070063 * cr50_i2c_read() - read from TPM register
Duncan Laurie2ea13c82016-09-19 16:04:39 -070064 *
65 * @addr: register address to read from
66 * @buffer: provided by caller
67 * @len: number of bytes to read
68 *
69 * 1) send register address byte 'addr' to the TPM
70 * 2) wait for TPM to indicate it is ready
71 * 3) read 'len' bytes of TPM response into the provided 'buffer'
72 *
73 * Return -1 on error, 0 on success.
74 */
Tim Wawrzynczakeb1891a2022-02-08 12:49:31 -070075static int cr50_i2c_read(uint8_t addr, uint8_t *buffer, size_t len)
Duncan Laurie2ea13c82016-09-19 16:04:39 -070076{
Patrick Georgic9b13592019-11-29 11:47:47 +010077 if (tpm_dev.addr == 0)
Duncan Laurie2ea13c82016-09-19 16:04:39 -070078 return -1;
79
Duncan Laurie94cc4852016-09-19 17:22:10 -070080 /* Clear interrupt before starting transaction */
Daniel Kurtzc4852e72017-04-21 14:11:51 +080081 tis_plat_irq_status();
Duncan Laurie94cc4852016-09-19 17:22:10 -070082
Duncan Laurie2ea13c82016-09-19 16:04:39 -070083 /* Send the register address byte to the TPM */
Patrick Georgic9b13592019-11-29 11:47:47 +010084 if (i2c_write_raw(tpm_dev.bus, tpm_dev.addr, &addr, 1)) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -070085 printk(BIOS_ERR, "%s: Address write failed\n", __func__);
86 return -1;
87 }
88
89 /* Wait for TPM to be ready with response data */
Yu-Ping Wuae1e7022022-05-17 09:33:18 +080090 if (cr50_wait_tpm_ready() != CB_SUCCESS)
Duncan Laurie94cc4852016-09-19 17:22:10 -070091 return -1;
Duncan Laurie2ea13c82016-09-19 16:04:39 -070092
93 /* Read response data from the TPM */
Patrick Georgic9b13592019-11-29 11:47:47 +010094 if (i2c_read_raw(tpm_dev.bus, tpm_dev.addr, buffer, len)) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -070095 printk(BIOS_ERR, "%s: Read response failed\n", __func__);
96 return -1;
97 }
98
99 return 0;
100}
101
102/*
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700103 * cr50_i2c_write() - write to TPM register
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700104 *
105 * @addr: register address to write to
106 * @buffer: data to write
107 * @len: number of bytes to write
108 *
109 * 1) prepend the provided address to the provided data
110 * 2) send the address+data to the TPM
111 * 3) wait for TPM to indicate it is done writing
112 *
113 * Returns -1 on error, 0 on success.
114 */
Tim Wawrzynczak1e50dfb2022-02-16 13:48:07 -0700115static int cr50_i2c_write(uint8_t addr, const uint8_t *buffer, size_t len)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700116{
Patrick Georgic9b13592019-11-29 11:47:47 +0100117 if (tpm_dev.addr == 0)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700118 return -1;
Duncan Laurie3727a8d2016-09-19 16:37:46 -0700119 if (len > CR50_MAX_BUFSIZE)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700120 return -1;
121
122 /* Prepend the 'register address' to the buffer */
Patrick Georgic9b13592019-11-29 11:47:47 +0100123 tpm_dev.buf[0] = addr;
124 memcpy(tpm_dev.buf + 1, buffer, len);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700125
Duncan Laurie94cc4852016-09-19 17:22:10 -0700126 /* Clear interrupt before starting transaction */
Daniel Kurtzc4852e72017-04-21 14:11:51 +0800127 tis_plat_irq_status();
Duncan Laurie94cc4852016-09-19 17:22:10 -0700128
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700129 /* Send write request buffer with address */
Patrick Georgic9b13592019-11-29 11:47:47 +0100130 if (i2c_write_raw(tpm_dev.bus, tpm_dev.addr, tpm_dev.buf, len + 1)) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700131 printk(BIOS_ERR, "%s: Error writing to TPM\n", __func__);
132 return -1;
133 }
134
135 /* Wait for TPM to be ready */
Yu-Ping Wuae1e7022022-05-17 09:33:18 +0800136 return cr50_wait_tpm_ready() == CB_SUCCESS ? 0 : -1;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700137}
138
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800139/*
140 * Cr50 processes reset requests asynchronously and consceivably could be busy
141 * executing a long command and not reacting to the reset pulse for a while.
142 *
143 * This function will make sure that the AP does not proceed with boot until
144 * TPM finished reset processing.
145 */
Tim Wawrzynczakeb1891a2022-02-08 12:49:31 -0700146static int process_reset(void)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700147{
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800148 struct stopwatch sw;
Richard Spiegel7c1e9592018-08-09 14:41:17 -0700149 int rv = 0;
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800150 uint8_t access;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700151
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800152 /*
153 * Locality is released by TPM reset.
154 *
155 * If locality is taken at this point, this could be due to the fact
156 * that the TPM is performing a long operation and has not processed
157 * reset request yet. We'll wait up to CR50_TIMEOUT_INIT_MS and see if
158 * it releases locality when reset is processed.
159 */
160 stopwatch_init_msecs_expire(&sw, CR50_TIMEOUT_INIT_MS);
161 do {
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800162 const uint8_t mask =
163 TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700164
Tim Wawrzynczakeb1891a2022-02-08 12:49:31 -0700165 rv = cr50_i2c_read(TPM_ACCESS(0),
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800166 &access, sizeof(access));
167 if (rv || ((access & mask) == mask)) {
168 /*
169 * Don't bombard the chip with traffic, let it keep
170 * processing the command.
171 */
172 mdelay(2);
173 continue;
174 }
175
Rob Barnesd522f382022-09-12 06:31:47 -0600176 printk(BIOS_INFO, "TPM ready after %lld ms\n",
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800177 stopwatch_duration_msecs(&sw));
178
179 return 0;
180 } while (!stopwatch_expired(&sw));
181
Richard Spiegel7c1e9592018-08-09 14:41:17 -0700182 if (rv)
183 printk(BIOS_ERR, "Failed to read TPM\n");
184 else
185 printk(BIOS_ERR,
Rob Barnesd522f382022-09-12 06:31:47 -0600186 "TPM failed to reset after %lld ms, status: %#x\n",
Richard Spiegel7c1e9592018-08-09 14:41:17 -0700187 stopwatch_duration_msecs(&sw), access);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700188
189 return -1;
190}
191
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800192/*
193 * Locality could be already claimed (if this is a later coreboot stage and
194 * the RO did not release it), or not yet claimed, if this is verstage or the
195 * older RO did release it.
196 */
Tim Wawrzynczakeb1891a2022-02-08 12:49:31 -0700197static int claim_locality(void)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700198{
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800199 uint8_t access;
200 const uint8_t mask = TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY;
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700201
Tim Wawrzynczakeb1891a2022-02-08 12:49:31 -0700202 if (cr50_i2c_read(TPM_ACCESS(0), &access, sizeof(access)))
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700203 return -1;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700204
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800205 if ((access & mask) == mask) {
206 printk(BIOS_INFO, "Locality already claimed\n");
207 return 0;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700208 }
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800209
210 access = TPM_ACCESS_REQUEST_USE;
Tim Wawrzynczakeb1891a2022-02-08 12:49:31 -0700211 if (cr50_i2c_write(TPM_ACCESS(0),
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800212 &access, sizeof(access)))
213 return -1;
214
Tim Wawrzynczakeb1891a2022-02-08 12:49:31 -0700215 if (cr50_i2c_read(TPM_ACCESS(0), &access, sizeof(access)))
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800216 return -1;
217
218 if ((access & mask) != mask) {
219 printk(BIOS_INFO, "Failed to claim locality.\n");
220 return -1;
221 }
222
223 return 0;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700224}
225
226/* cr50 requires all 4 bytes of status register to be read */
Duncan Laurief235a9b2016-09-19 17:19:10 -0700227static uint8_t cr50_i2c_tis_status(struct tpm_chip *chip)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700228{
229 uint8_t buf[4];
Tim Wawrzynczakeb1891a2022-02-08 12:49:31 -0700230 if (cr50_i2c_read(TPM_STS(chip->vendor.locality),
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700231 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 */
Duncan Laurief235a9b2016-09-19 17:19:10 -0700239static void cr50_i2c_tis_ready(struct tpm_chip *chip)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700240{
241 uint8_t buf[4] = { TPM_STS_COMMAND_READY };
Tim Wawrzynczakeb1891a2022-02-08 12:49:31 -0700242 cr50_i2c_write(TPM_STS(chip->vendor.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 */
Duncan Laurief235a9b2016-09-19 17:19:10 -0700248static int cr50_i2c_wait_burststs(struct tpm_chip *chip, uint8_t mask,
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700249 size_t *burst, int *status)
250{
251 uint8_t buf[4];
252 struct stopwatch sw;
253
Duncan Laurie1dc036c2016-09-19 16:49:23 -0700254 stopwatch_init_msecs_expire(&sw, CR50_TIMEOUT_LONG_MS);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700255
256 while (!stopwatch_expired(&sw)) {
Tim Wawrzynczakeb1891a2022-02-08 12:49:31 -0700257 if (cr50_i2c_read(TPM_STS(chip->vendor.locality),
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700258 buf, sizeof(buf)) != 0) {
Duncan Laurie1dc036c2016-09-19 16:49:23 -0700259 mdelay(CR50_TIMEOUT_SHORT_MS);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700260 continue;
261 }
262
263 *status = buf[0];
264 *burst = read_le16(&buf[1]);
265
266 /* Check if mask matches and burst is valid */
267 if ((*status & mask) == mask &&
Duncan Laurie3727a8d2016-09-19 16:37:46 -0700268 *burst > 0 && *burst <= CR50_MAX_BUFSIZE)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700269 return 0;
270
Duncan Laurie1dc036c2016-09-19 16:49:23 -0700271 mdelay(CR50_TIMEOUT_SHORT_MS);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700272 }
273
274 printk(BIOS_ERR, "%s: Timeout reading burst and status\n", __func__);
275 return -1;
276}
277
Duncan Laurief235a9b2016-09-19 17:19:10 -0700278static int cr50_i2c_tis_recv(struct tpm_chip *chip, uint8_t *buf,
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700279 size_t buf_len)
280{
281 size_t burstcnt, current, len, expected;
282 uint8_t addr = TPM_DATA_FIFO(chip->vendor.locality);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700283 uint8_t mask = TPM_STS_VALID | TPM_STS_DATA_AVAIL;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700284 int status;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700285
286 if (buf_len < TPM_HEADER_SIZE)
Duncan Laurief235a9b2016-09-19 17:19:10 -0700287 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700288
Duncan Laurief235a9b2016-09-19 17:19:10 -0700289 if (cr50_i2c_wait_burststs(chip, mask, &burstcnt, &status) < 0) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700290 printk(BIOS_ERR, "%s: First chunk not available\n", __func__);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700291 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700292 }
293
294 /* Read first chunk of burstcnt bytes */
Tim Wawrzynczakeb1891a2022-02-08 12:49:31 -0700295 if (cr50_i2c_read(addr, buf, burstcnt) != 0) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700296 printk(BIOS_ERR, "%s: Read failed\n", __func__);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700297 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700298 }
299
300 /* Determine expected data in the return buffer */
301 expected = read_be32(buf + TPM_RSP_SIZE_BYTE);
302 if (expected > buf_len) {
303 printk(BIOS_ERR, "%s: Too much data: %zu > %zu\n",
304 __func__, expected, buf_len);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700305 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700306 }
307
308 /* Now read the rest of the data */
309 current = burstcnt;
310 while (current < expected) {
311 /* Read updated burst count and check status */
Duncan Laurief235a9b2016-09-19 17:19:10 -0700312 if (cr50_i2c_wait_burststs(chip, mask, &burstcnt, &status) < 0)
313 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700314
Elyes HAOUAS361a9352019-12-18 21:26:33 +0100315 len = MIN(burstcnt, expected - current);
Tim Wawrzynczakeb1891a2022-02-08 12:49:31 -0700316 if (cr50_i2c_read(addr, buf + current, len) != 0) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700317 printk(BIOS_ERR, "%s: Read failed\n", __func__);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700318 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700319 }
320
321 current += len;
322 }
323
324 /* Ensure TPM is done reading data */
Duncan Laurief235a9b2016-09-19 17:19:10 -0700325 if (cr50_i2c_wait_burststs(chip, TPM_STS_VALID, &burstcnt, &status) < 0)
326 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700327 if (status & TPM_STS_DATA_AVAIL) {
328 printk(BIOS_ERR, "%s: Data still available\n", __func__);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700329 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700330 }
331
Duncan Laurief235a9b2016-09-19 17:19:10 -0700332 return current;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700333
Duncan Laurief235a9b2016-09-19 17:19:10 -0700334out_err:
335 /* Abort current transaction if still pending */
336 if (cr50_i2c_tis_status(chip) & TPM_STS_COMMAND_READY)
337 cr50_i2c_tis_ready(chip);
338 return -1;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700339}
340
Duncan Laurief235a9b2016-09-19 17:19:10 -0700341static int cr50_i2c_tis_send(struct tpm_chip *chip, uint8_t *buf, size_t len)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700342{
343 int status;
344 size_t burstcnt, limit, sent = 0;
345 uint8_t tpm_go[4] = { TPM_STS_GO };
346 struct stopwatch sw;
347
Duncan Laurie1dc036c2016-09-19 16:49:23 -0700348 stopwatch_init_msecs_expire(&sw, CR50_TIMEOUT_LONG_MS);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700349
350 /* Wait until TPM is ready for a command */
Duncan Laurief235a9b2016-09-19 17:19:10 -0700351 while (!(cr50_i2c_tis_status(chip) & TPM_STS_COMMAND_READY)) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700352 if (stopwatch_expired(&sw)) {
353 printk(BIOS_ERR, "%s: Command ready timeout\n",
354 __func__);
355 return -1;
356 }
357
Duncan Laurief235a9b2016-09-19 17:19:10 -0700358 cr50_i2c_tis_ready(chip);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700359 }
360
361 while (len > 0) {
Duncan Laurief235a9b2016-09-19 17:19:10 -0700362 uint8_t mask = TPM_STS_VALID;
363
364 /* Wait for data if this is not the first chunk */
365 if (sent > 0)
366 mask |= TPM_STS_DATA_EXPECT;
367
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700368 /* Read burst count and check status */
Duncan Laurief235a9b2016-09-19 17:19:10 -0700369 if (cr50_i2c_wait_burststs(chip, mask, &burstcnt, &status) < 0)
370 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700371
372 /* Use burstcnt - 1 to account for the address byte
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700373 * that is inserted by cr50_i2c_write() */
Elyes HAOUAS361a9352019-12-18 21:26:33 +0100374 limit = MIN(burstcnt - 1, len);
Tim Wawrzynczakeb1891a2022-02-08 12:49:31 -0700375 if (cr50_i2c_write(TPM_DATA_FIFO(chip->vendor.locality),
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700376 &buf[sent], limit) != 0) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700377 printk(BIOS_ERR, "%s: Write failed\n", __func__);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700378 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700379 }
380
381 sent += limit;
382 len -= limit;
383 }
384
385 /* Ensure TPM is not expecting more data */
Duncan Laurief235a9b2016-09-19 17:19:10 -0700386 if (cr50_i2c_wait_burststs(chip, TPM_STS_VALID, &burstcnt, &status) < 0)
387 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700388 if (status & TPM_STS_DATA_EXPECT) {
389 printk(BIOS_ERR, "%s: Data still expected\n", __func__);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700390 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700391 }
392
393 /* Start the TPM command */
Tim Wawrzynczakeb1891a2022-02-08 12:49:31 -0700394 if (cr50_i2c_write(TPM_STS(chip->vendor.locality), tpm_go,
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700395 sizeof(tpm_go)) < 0) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700396 printk(BIOS_ERR, "%s: Start command failed\n", __func__);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700397 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700398 }
399 return sent;
400
Duncan Laurief235a9b2016-09-19 17:19:10 -0700401out_err:
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700402 /* Abort current transaction if still pending */
Duncan Laurief235a9b2016-09-19 17:19:10 -0700403 if (cr50_i2c_tis_status(chip) & TPM_STS_COMMAND_READY)
404 cr50_i2c_tis_ready(chip);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700405 return -1;
406}
407
408static void cr50_vendor_init(struct tpm_chip *chip)
409{
410 memset(&chip->vendor, 0, sizeof(struct tpm_vendor_specific));
411 chip->vendor.req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID;
412 chip->vendor.req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID;
413 chip->vendor.req_canceled = TPM_STS_COMMAND_READY;
Duncan Laurief235a9b2016-09-19 17:19:10 -0700414 chip->vendor.status = &cr50_i2c_tis_status;
415 chip->vendor.recv = &cr50_i2c_tis_recv;
416 chip->vendor.send = &cr50_i2c_tis_send;
417 chip->vendor.cancel = &cr50_i2c_tis_ready;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700418}
419
Lee Leahy52ab30b2017-03-15 09:22:11 -0700420int tpm_vendor_probe(unsigned int bus, uint32_t addr)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700421{
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700422 return 0;
423}
424
Keith Short51436352018-12-17 14:21:46 -0700425static int cr50_i2c_probe(struct tpm_chip *chip, uint32_t *did_vid)
426{
427 int retries;
428
429 /*
Rob Barnes22372f42022-02-11 07:59:21 -0700430 * 1s should be enough to synchronize with the TPM even under the
Keith Short51436352018-12-17 14:21:46 -0700431 * worst nested reset request conditions. In vast majority of cases
Rob Barnes22372f42022-02-11 07:59:21 -0700432 * there would be no wait at all. If this probe fails, boot likely
433 * cannot proceed, so an extra long timeout is appropriate.
Keith Short51436352018-12-17 14:21:46 -0700434 */
435 printk(BIOS_INFO, "Probing TPM I2C: ");
436
Rob Barnes22372f42022-02-11 07:59:21 -0700437 for (retries = 100; retries > 0; retries--) {
Keith Short51436352018-12-17 14:21:46 -0700438 int rc;
439
Tim Wawrzynczakeb1891a2022-02-08 12:49:31 -0700440 rc = cr50_i2c_read(TPM_DID_VID(0), (uint8_t *)did_vid, 4);
Keith Short51436352018-12-17 14:21:46 -0700441
442 /* Exit once DID and VID verified */
Jes Klinke1430b042022-03-28 14:22:24 -0700443 if (!rc && (*did_vid == CR50_DID_VID || *did_vid == TI50_DID_VID)) {
Keith Short51436352018-12-17 14:21:46 -0700444 printk(BIOS_INFO, "done! DID_VID 0x%08x\n", *did_vid);
445 return 0;
446 }
447
448 /* TPM might be resetting, let's retry in a bit. */
449 mdelay(10);
450 printk(BIOS_INFO, ".");
451 }
452
453 /*
454 * I2C reads failed, or the DID and VID didn't match
455 */
456 printk(BIOS_ERR, "DID_VID 0x%08x not recognized\n", *did_vid);
457 return -1;
458}
459
Lee Leahy52ab30b2017-03-15 09:22:11 -0700460int tpm_vendor_init(struct tpm_chip *chip, unsigned int bus, uint32_t dev_addr)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700461{
Keith Short51436352018-12-17 14:21:46 -0700462 uint32_t did_vid = 0;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700463
464 if (dev_addr == 0) {
465 printk(BIOS_ERR, "%s: missing device address\n", __func__);
466 return -1;
467 }
468
Patrick Georgic9b13592019-11-29 11:47:47 +0100469 tpm_dev.bus = bus;
470 tpm_dev.addr = dev_addr;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700471
472 cr50_vendor_init(chip);
473
Keith Short51436352018-12-17 14:21:46 -0700474 if (cr50_i2c_probe(chip, &did_vid))
475 return -1;
476
Julius Werner21a40532020-04-21 16:03:53 -0700477 if (ENV_SEPARATE_VERSTAGE || ENV_BOOTBLOCK)
Tim Wawrzynczakeb1891a2022-02-08 12:49:31 -0700478 if (process_reset())
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800479 return -1;
480
Tim Wawrzynczakeb1891a2022-02-08 12:49:31 -0700481 if (claim_locality())
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700482 return -1;
483
Daniel Kurtzc4852e72017-04-21 14:11:51 +0800484 printk(BIOS_DEBUG, "cr50 TPM 2.0 (i2c %u:0x%02x id 0x%x)\n",
Keith Short51436352018-12-17 14:21:46 -0700485 bus, dev_addr, did_vid >> 16);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700486
Jes Klinke1430b042022-03-28 14:22:24 -0700487 if (tpm_first_access_this_boot()) {
Tim Wawrzynczak1e50dfb2022-02-16 13:48:07 -0700488 /* This is called for the side-effect of printing the version string. */
Jes Klinke1430b042022-03-28 14:22:24 -0700489 cr50_get_firmware_version(NULL);
490 cr50_set_board_cfg();
Tim Wawrzynczak1e50dfb2022-02-16 13:48:07 -0700491 }
492
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700493 chip->is_open = 1;
494 return 0;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700495}
496
Subrata Banik60b2ab82022-03-09 12:55:34 +0530497enum cb_err tis_vendor_write(unsigned int addr, const void *buffer, size_t bytes)
Tim Wawrzynczak1e50dfb2022-02-16 13:48:07 -0700498{
499 return cr50_i2c_write(addr & 0xff, buffer, bytes) ? CB_ERR : CB_SUCCESS;
500}
501
Subrata Banik60b2ab82022-03-09 12:55:34 +0530502enum cb_err tis_vendor_read(unsigned int addr, void *buffer, size_t bytes)
Tim Wawrzynczak1e50dfb2022-02-16 13:48:07 -0700503{
504 return cr50_i2c_read(addr & 0xff, buffer, bytes) ? CB_ERR : CB_SUCCESS;
505}