blob: 0130b931693409999122c2032487f0b47454e811 [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 */
37#define CR50_TIMEOUT_IRQ_MS 100 /* Timeout for TPM ready with IRQ */
Duncan Laurie2ea13c82016-09-19 16:04:39 -070038#define CR50_DID_VID 0x00281ae0L
Jes Klinke1430b042022-03-28 14:22:24 -070039#define TI50_DID_VID 0x504a6666L
Duncan Laurie2ea13c82016-09-19 16:04:39 -070040
41struct tpm_inf_dev {
42 int bus;
43 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 Laurie94cc4852016-09-19 17:22:10 -070063/* Wait for interrupt to indicate the TPM is ready */
Tim Wawrzynczakeb1891a2022-02-08 12:49:31 -070064static int cr50_i2c_wait_tpm_ready(void)
Duncan Laurie94cc4852016-09-19 17:22:10 -070065{
66 struct stopwatch sw;
67
Duncan Laurieed4fa092016-11-01 15:03:13 -070068 stopwatch_init_msecs_expire(&sw, CR50_TIMEOUT_IRQ_MS);
Duncan Laurie94cc4852016-09-19 17:22:10 -070069
Daniel Kurtzc4852e72017-04-21 14:11:51 +080070 while (!tis_plat_irq_status())
Vadim Bendeburyc7fc1992019-11-26 14:08:59 -080071 if (stopwatch_expired(&sw)) {
72 printk(BIOS_ERR, "Cr50 i2c TPM IRQ timeout!\n");
Duncan Laurie94cc4852016-09-19 17:22:10 -070073 return -1;
Vadim Bendeburyc7fc1992019-11-26 14:08:59 -080074 }
Duncan Laurie94cc4852016-09-19 17:22:10 -070075 return 0;
76}
77
Duncan Laurie2ea13c82016-09-19 16:04:39 -070078/*
Duncan Laurie510cb6a2016-09-19 17:05:45 -070079 * cr50_i2c_read() - read from TPM register
Duncan Laurie2ea13c82016-09-19 16:04:39 -070080 *
81 * @addr: register address to read from
82 * @buffer: provided by caller
83 * @len: number of bytes to read
84 *
85 * 1) send register address byte 'addr' to the TPM
86 * 2) wait for TPM to indicate it is ready
87 * 3) read 'len' bytes of TPM response into the provided 'buffer'
88 *
89 * Return -1 on error, 0 on success.
90 */
Tim Wawrzynczakeb1891a2022-02-08 12:49:31 -070091static int cr50_i2c_read(uint8_t addr, 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 */
Tim Wawrzynczakeb1891a2022-02-08 12:49:31 -0700106 if (cr50_i2c_wait_tpm_ready() < 0)
Duncan Laurie94cc4852016-09-19 17:22:10 -0700107 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 *
121 * @addr: register address to write to
122 * @buffer: data to write
123 * @len: number of bytes to write
124 *
125 * 1) prepend the provided address to the provided data
126 * 2) send the address+data to the TPM
127 * 3) wait for TPM to indicate it is done writing
128 *
129 * Returns -1 on error, 0 on success.
130 */
Tim Wawrzynczak1e50dfb2022-02-16 13:48:07 -0700131static int cr50_i2c_write(uint8_t addr, const uint8_t *buffer, size_t len)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700132{
Patrick Georgic9b13592019-11-29 11:47:47 +0100133 if (tpm_dev.addr == 0)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700134 return -1;
Duncan Laurie3727a8d2016-09-19 16:37:46 -0700135 if (len > CR50_MAX_BUFSIZE)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700136 return -1;
137
138 /* Prepend the 'register address' to the buffer */
Patrick Georgic9b13592019-11-29 11:47:47 +0100139 tpm_dev.buf[0] = addr;
140 memcpy(tpm_dev.buf + 1, buffer, len);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700141
Duncan Laurie94cc4852016-09-19 17:22:10 -0700142 /* Clear interrupt before starting transaction */
Daniel Kurtzc4852e72017-04-21 14:11:51 +0800143 tis_plat_irq_status();
Duncan Laurie94cc4852016-09-19 17:22:10 -0700144
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700145 /* Send write request buffer with address */
Patrick Georgic9b13592019-11-29 11:47:47 +0100146 if (i2c_write_raw(tpm_dev.bus, tpm_dev.addr, tpm_dev.buf, len + 1)) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700147 printk(BIOS_ERR, "%s: Error writing to TPM\n", __func__);
148 return -1;
149 }
150
151 /* Wait for TPM to be ready */
Tim Wawrzynczakeb1891a2022-02-08 12:49:31 -0700152 return cr50_i2c_wait_tpm_ready();
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700153}
154
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800155/*
156 * Cr50 processes reset requests asynchronously and consceivably could be busy
157 * executing a long command and not reacting to the reset pulse for a while.
158 *
159 * This function will make sure that the AP does not proceed with boot until
160 * TPM finished reset processing.
161 */
Tim Wawrzynczakeb1891a2022-02-08 12:49:31 -0700162static int process_reset(void)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700163{
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800164 struct stopwatch sw;
Richard Spiegel7c1e9592018-08-09 14:41:17 -0700165 int rv = 0;
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800166 uint8_t access;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700167
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800168 /*
169 * Locality is released by TPM reset.
170 *
171 * If locality is taken at this point, this could be due to the fact
172 * that the TPM is performing a long operation and has not processed
173 * reset request yet. We'll wait up to CR50_TIMEOUT_INIT_MS and see if
174 * it releases locality when reset is processed.
175 */
176 stopwatch_init_msecs_expire(&sw, CR50_TIMEOUT_INIT_MS);
177 do {
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800178 const uint8_t mask =
179 TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700180
Tim Wawrzynczakeb1891a2022-02-08 12:49:31 -0700181 rv = cr50_i2c_read(TPM_ACCESS(0),
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800182 &access, sizeof(access));
183 if (rv || ((access & mask) == mask)) {
184 /*
185 * Don't bombard the chip with traffic, let it keep
186 * processing the command.
187 */
188 mdelay(2);
189 continue;
190 }
191
192 printk(BIOS_INFO, "TPM ready after %ld ms\n",
193 stopwatch_duration_msecs(&sw));
194
195 return 0;
196 } while (!stopwatch_expired(&sw));
197
Richard Spiegel7c1e9592018-08-09 14:41:17 -0700198 if (rv)
199 printk(BIOS_ERR, "Failed to read TPM\n");
200 else
201 printk(BIOS_ERR,
202 "TPM failed to reset after %ld ms, status: %#x\n",
203 stopwatch_duration_msecs(&sw), access);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700204
205 return -1;
206}
207
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800208/*
209 * Locality could be already claimed (if this is a later coreboot stage and
210 * the RO did not release it), or not yet claimed, if this is verstage or the
211 * older RO did release it.
212 */
Tim Wawrzynczakeb1891a2022-02-08 12:49:31 -0700213static int claim_locality(void)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700214{
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800215 uint8_t access;
216 const uint8_t mask = TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY;
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700217
Tim Wawrzynczakeb1891a2022-02-08 12:49:31 -0700218 if (cr50_i2c_read(TPM_ACCESS(0), &access, sizeof(access)))
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700219 return -1;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700220
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800221 if ((access & mask) == mask) {
222 printk(BIOS_INFO, "Locality already claimed\n");
223 return 0;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700224 }
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800225
226 access = TPM_ACCESS_REQUEST_USE;
Tim Wawrzynczakeb1891a2022-02-08 12:49:31 -0700227 if (cr50_i2c_write(TPM_ACCESS(0),
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800228 &access, sizeof(access)))
229 return -1;
230
Tim Wawrzynczakeb1891a2022-02-08 12:49:31 -0700231 if (cr50_i2c_read(TPM_ACCESS(0), &access, sizeof(access)))
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800232 return -1;
233
234 if ((access & mask) != mask) {
235 printk(BIOS_INFO, "Failed to claim locality.\n");
236 return -1;
237 }
238
239 return 0;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700240}
241
242/* cr50 requires all 4 bytes of status register to be read */
Duncan Laurief235a9b2016-09-19 17:19:10 -0700243static uint8_t cr50_i2c_tis_status(struct tpm_chip *chip)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700244{
245 uint8_t buf[4];
Tim Wawrzynczakeb1891a2022-02-08 12:49:31 -0700246 if (cr50_i2c_read(TPM_STS(chip->vendor.locality),
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700247 buf, sizeof(buf)) < 0) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700248 printk(BIOS_ERR, "%s: Failed to read status\n", __func__);
249 return 0;
250 }
251 return buf[0];
252}
253
254/* cr50 requires all 4 bytes of status register to be written */
Duncan Laurief235a9b2016-09-19 17:19:10 -0700255static void cr50_i2c_tis_ready(struct tpm_chip *chip)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700256{
257 uint8_t buf[4] = { TPM_STS_COMMAND_READY };
Tim Wawrzynczakeb1891a2022-02-08 12:49:31 -0700258 cr50_i2c_write(TPM_STS(chip->vendor.locality), buf, sizeof(buf));
Duncan Laurie1dc036c2016-09-19 16:49:23 -0700259 mdelay(CR50_TIMEOUT_SHORT_MS);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700260}
261
262/* cr50 uses bytes 3:2 of status register for burst count and
263 * all 4 bytes must be read */
Duncan Laurief235a9b2016-09-19 17:19:10 -0700264static int cr50_i2c_wait_burststs(struct tpm_chip *chip, uint8_t mask,
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700265 size_t *burst, int *status)
266{
267 uint8_t buf[4];
268 struct stopwatch sw;
269
Duncan Laurie1dc036c2016-09-19 16:49:23 -0700270 stopwatch_init_msecs_expire(&sw, CR50_TIMEOUT_LONG_MS);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700271
272 while (!stopwatch_expired(&sw)) {
Tim Wawrzynczakeb1891a2022-02-08 12:49:31 -0700273 if (cr50_i2c_read(TPM_STS(chip->vendor.locality),
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700274 buf, sizeof(buf)) != 0) {
Duncan Laurie1dc036c2016-09-19 16:49:23 -0700275 mdelay(CR50_TIMEOUT_SHORT_MS);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700276 continue;
277 }
278
279 *status = buf[0];
280 *burst = read_le16(&buf[1]);
281
282 /* Check if mask matches and burst is valid */
283 if ((*status & mask) == mask &&
Duncan Laurie3727a8d2016-09-19 16:37:46 -0700284 *burst > 0 && *burst <= CR50_MAX_BUFSIZE)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700285 return 0;
286
Duncan Laurie1dc036c2016-09-19 16:49:23 -0700287 mdelay(CR50_TIMEOUT_SHORT_MS);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700288 }
289
290 printk(BIOS_ERR, "%s: Timeout reading burst and status\n", __func__);
291 return -1;
292}
293
Duncan Laurief235a9b2016-09-19 17:19:10 -0700294static int cr50_i2c_tis_recv(struct tpm_chip *chip, uint8_t *buf,
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700295 size_t buf_len)
296{
297 size_t burstcnt, current, len, expected;
298 uint8_t addr = TPM_DATA_FIFO(chip->vendor.locality);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700299 uint8_t mask = TPM_STS_VALID | TPM_STS_DATA_AVAIL;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700300 int status;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700301
302 if (buf_len < TPM_HEADER_SIZE)
Duncan Laurief235a9b2016-09-19 17:19:10 -0700303 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700304
Duncan Laurief235a9b2016-09-19 17:19:10 -0700305 if (cr50_i2c_wait_burststs(chip, mask, &burstcnt, &status) < 0) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700306 printk(BIOS_ERR, "%s: First chunk not available\n", __func__);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700307 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700308 }
309
310 /* Read first chunk of burstcnt bytes */
Tim Wawrzynczakeb1891a2022-02-08 12:49:31 -0700311 if (cr50_i2c_read(addr, buf, burstcnt) != 0) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700312 printk(BIOS_ERR, "%s: Read failed\n", __func__);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700313 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700314 }
315
316 /* Determine expected data in the return buffer */
317 expected = read_be32(buf + TPM_RSP_SIZE_BYTE);
318 if (expected > buf_len) {
319 printk(BIOS_ERR, "%s: Too much data: %zu > %zu\n",
320 __func__, expected, buf_len);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700321 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700322 }
323
324 /* Now read the rest of the data */
325 current = burstcnt;
326 while (current < expected) {
327 /* Read updated burst count and check status */
Duncan Laurief235a9b2016-09-19 17:19:10 -0700328 if (cr50_i2c_wait_burststs(chip, mask, &burstcnt, &status) < 0)
329 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700330
Elyes HAOUAS361a9352019-12-18 21:26:33 +0100331 len = MIN(burstcnt, expected - current);
Tim Wawrzynczakeb1891a2022-02-08 12:49:31 -0700332 if (cr50_i2c_read(addr, buf + current, len) != 0) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700333 printk(BIOS_ERR, "%s: Read failed\n", __func__);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700334 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700335 }
336
337 current += len;
338 }
339
340 /* Ensure TPM is done reading data */
Duncan Laurief235a9b2016-09-19 17:19:10 -0700341 if (cr50_i2c_wait_burststs(chip, TPM_STS_VALID, &burstcnt, &status) < 0)
342 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700343 if (status & TPM_STS_DATA_AVAIL) {
344 printk(BIOS_ERR, "%s: Data still available\n", __func__);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700345 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700346 }
347
Duncan Laurief235a9b2016-09-19 17:19:10 -0700348 return current;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700349
Duncan Laurief235a9b2016-09-19 17:19:10 -0700350out_err:
351 /* Abort current transaction if still pending */
352 if (cr50_i2c_tis_status(chip) & TPM_STS_COMMAND_READY)
353 cr50_i2c_tis_ready(chip);
354 return -1;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700355}
356
Duncan Laurief235a9b2016-09-19 17:19:10 -0700357static int cr50_i2c_tis_send(struct tpm_chip *chip, uint8_t *buf, size_t len)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700358{
359 int status;
360 size_t burstcnt, limit, sent = 0;
361 uint8_t tpm_go[4] = { TPM_STS_GO };
362 struct stopwatch sw;
363
Duncan Laurie1dc036c2016-09-19 16:49:23 -0700364 stopwatch_init_msecs_expire(&sw, CR50_TIMEOUT_LONG_MS);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700365
366 /* Wait until TPM is ready for a command */
Duncan Laurief235a9b2016-09-19 17:19:10 -0700367 while (!(cr50_i2c_tis_status(chip) & TPM_STS_COMMAND_READY)) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700368 if (stopwatch_expired(&sw)) {
369 printk(BIOS_ERR, "%s: Command ready timeout\n",
370 __func__);
371 return -1;
372 }
373
Duncan Laurief235a9b2016-09-19 17:19:10 -0700374 cr50_i2c_tis_ready(chip);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700375 }
376
377 while (len > 0) {
Duncan Laurief235a9b2016-09-19 17:19:10 -0700378 uint8_t mask = TPM_STS_VALID;
379
380 /* Wait for data if this is not the first chunk */
381 if (sent > 0)
382 mask |= TPM_STS_DATA_EXPECT;
383
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700384 /* Read burst count and check status */
Duncan Laurief235a9b2016-09-19 17:19:10 -0700385 if (cr50_i2c_wait_burststs(chip, mask, &burstcnt, &status) < 0)
386 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700387
388 /* Use burstcnt - 1 to account for the address byte
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700389 * that is inserted by cr50_i2c_write() */
Elyes HAOUAS361a9352019-12-18 21:26:33 +0100390 limit = MIN(burstcnt - 1, len);
Tim Wawrzynczakeb1891a2022-02-08 12:49:31 -0700391 if (cr50_i2c_write(TPM_DATA_FIFO(chip->vendor.locality),
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700392 &buf[sent], limit) != 0) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700393 printk(BIOS_ERR, "%s: Write failed\n", __func__);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700394 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700395 }
396
397 sent += limit;
398 len -= limit;
399 }
400
401 /* Ensure TPM is not expecting more data */
Duncan Laurief235a9b2016-09-19 17:19:10 -0700402 if (cr50_i2c_wait_burststs(chip, TPM_STS_VALID, &burstcnt, &status) < 0)
403 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700404 if (status & TPM_STS_DATA_EXPECT) {
405 printk(BIOS_ERR, "%s: Data still expected\n", __func__);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700406 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700407 }
408
409 /* Start the TPM command */
Tim Wawrzynczakeb1891a2022-02-08 12:49:31 -0700410 if (cr50_i2c_write(TPM_STS(chip->vendor.locality), tpm_go,
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700411 sizeof(tpm_go)) < 0) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700412 printk(BIOS_ERR, "%s: Start command failed\n", __func__);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700413 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700414 }
415 return sent;
416
Duncan Laurief235a9b2016-09-19 17:19:10 -0700417out_err:
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700418 /* Abort current transaction if still pending */
Duncan Laurief235a9b2016-09-19 17:19:10 -0700419 if (cr50_i2c_tis_status(chip) & TPM_STS_COMMAND_READY)
420 cr50_i2c_tis_ready(chip);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700421 return -1;
422}
423
424static void cr50_vendor_init(struct tpm_chip *chip)
425{
426 memset(&chip->vendor, 0, sizeof(struct tpm_vendor_specific));
427 chip->vendor.req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID;
428 chip->vendor.req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID;
429 chip->vendor.req_canceled = TPM_STS_COMMAND_READY;
Duncan Laurief235a9b2016-09-19 17:19:10 -0700430 chip->vendor.status = &cr50_i2c_tis_status;
431 chip->vendor.recv = &cr50_i2c_tis_recv;
432 chip->vendor.send = &cr50_i2c_tis_send;
433 chip->vendor.cancel = &cr50_i2c_tis_ready;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700434}
435
Lee Leahy52ab30b2017-03-15 09:22:11 -0700436int tpm_vendor_probe(unsigned int bus, uint32_t addr)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700437{
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700438 return 0;
439}
440
Keith Short51436352018-12-17 14:21:46 -0700441static int cr50_i2c_probe(struct tpm_chip *chip, uint32_t *did_vid)
442{
443 int retries;
444
445 /*
Rob Barnes22372f42022-02-11 07:59:21 -0700446 * 1s should be enough to synchronize with the TPM even under the
Keith Short51436352018-12-17 14:21:46 -0700447 * worst nested reset request conditions. In vast majority of cases
Rob Barnes22372f42022-02-11 07:59:21 -0700448 * there would be no wait at all. If this probe fails, boot likely
449 * cannot proceed, so an extra long timeout is appropriate.
Keith Short51436352018-12-17 14:21:46 -0700450 */
451 printk(BIOS_INFO, "Probing TPM I2C: ");
452
Rob Barnes22372f42022-02-11 07:59:21 -0700453 for (retries = 100; retries > 0; retries--) {
Keith Short51436352018-12-17 14:21:46 -0700454 int rc;
455
Tim Wawrzynczakeb1891a2022-02-08 12:49:31 -0700456 rc = cr50_i2c_read(TPM_DID_VID(0), (uint8_t *)did_vid, 4);
Keith Short51436352018-12-17 14:21:46 -0700457
458 /* Exit once DID and VID verified */
Jes Klinke1430b042022-03-28 14:22:24 -0700459 if (!rc && (*did_vid == CR50_DID_VID || *did_vid == TI50_DID_VID)) {
Keith Short51436352018-12-17 14:21:46 -0700460 printk(BIOS_INFO, "done! DID_VID 0x%08x\n", *did_vid);
461 return 0;
462 }
463
464 /* TPM might be resetting, let's retry in a bit. */
465 mdelay(10);
466 printk(BIOS_INFO, ".");
467 }
468
469 /*
470 * I2C reads failed, or the DID and VID didn't match
471 */
472 printk(BIOS_ERR, "DID_VID 0x%08x not recognized\n", *did_vid);
473 return -1;
474}
475
Lee Leahy52ab30b2017-03-15 09:22:11 -0700476int tpm_vendor_init(struct tpm_chip *chip, unsigned int bus, uint32_t dev_addr)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700477{
Keith Short51436352018-12-17 14:21:46 -0700478 uint32_t did_vid = 0;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700479
480 if (dev_addr == 0) {
481 printk(BIOS_ERR, "%s: missing device address\n", __func__);
482 return -1;
483 }
484
Patrick Georgic9b13592019-11-29 11:47:47 +0100485 tpm_dev.bus = bus;
486 tpm_dev.addr = dev_addr;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700487
488 cr50_vendor_init(chip);
489
Keith Short51436352018-12-17 14:21:46 -0700490 if (cr50_i2c_probe(chip, &did_vid))
491 return -1;
492
Julius Werner21a40532020-04-21 16:03:53 -0700493 if (ENV_SEPARATE_VERSTAGE || ENV_BOOTBLOCK)
Tim Wawrzynczakeb1891a2022-02-08 12:49:31 -0700494 if (process_reset())
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800495 return -1;
496
Tim Wawrzynczakeb1891a2022-02-08 12:49:31 -0700497 if (claim_locality())
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700498 return -1;
499
Daniel Kurtzc4852e72017-04-21 14:11:51 +0800500 printk(BIOS_DEBUG, "cr50 TPM 2.0 (i2c %u:0x%02x id 0x%x)\n",
Keith Short51436352018-12-17 14:21:46 -0700501 bus, dev_addr, did_vid >> 16);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700502
Jes Klinke1430b042022-03-28 14:22:24 -0700503 if (tpm_first_access_this_boot()) {
Tim Wawrzynczak1e50dfb2022-02-16 13:48:07 -0700504 /* This is called for the side-effect of printing the version string. */
Jes Klinke1430b042022-03-28 14:22:24 -0700505 cr50_get_firmware_version(NULL);
506 cr50_set_board_cfg();
Tim Wawrzynczak1e50dfb2022-02-16 13:48:07 -0700507 }
508
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700509 chip->is_open = 1;
510 return 0;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700511}
512
513void tpm_vendor_cleanup(struct tpm_chip *chip)
514{
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700515}
Tim Wawrzynczak1e50dfb2022-02-16 13:48:07 -0700516
Subrata Banik60b2ab82022-03-09 12:55:34 +0530517enum cb_err tis_vendor_write(unsigned int addr, const void *buffer, size_t bytes)
Tim Wawrzynczak1e50dfb2022-02-16 13:48:07 -0700518{
519 return cr50_i2c_write(addr & 0xff, buffer, bytes) ? CB_ERR : CB_SUCCESS;
520}
521
Subrata Banik60b2ab82022-03-09 12:55:34 +0530522enum cb_err tis_vendor_read(unsigned int addr, void *buffer, size_t bytes)
Tim Wawrzynczak1e50dfb2022-02-16 13:48:07 -0700523{
524 return cr50_i2c_read(addr & 0xff, buffer, bytes) ? CB_ERR : CB_SUCCESS;
525}