blob: 3d9ca593e40a616a96f4ff7f159be53251fa66f9 [file] [log] [blame]
Duncan Laurie2ea13c82016-09-19 16:04:39 -07001/*
Martin Rothcddd6002019-09-23 17:38:27 -06002 * This file is part of the coreboot project.
Duncan Laurie2ea13c82016-09-19 16:04:39 -07003 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation, version 2 of the
7 * License.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 */
14
Martin Rothcddd6002019-09-23 17:38:27 -060015/* Based on Linux Kernel TPM driver */
16
Duncan Laurie2ea13c82016-09-19 16:04:39 -070017/*
18 * cr50 is a TPM 2.0 capable device that requries special
19 * handling for the I2C interface.
20 *
21 * - Use an interrupt for transaction status instead of hardcoded delays
22 * - Must use write+wait+read read protocol
23 * - All 4 bytes of status register must be read/written at once
24 * - Burst count max is 63 bytes, and burst count behaves
25 * slightly differently than other I2C TPMs
26 * - When reading from FIFO the full burstcnt must be read
27 * instead of just reading header and determining the remainder
28 */
29
Duncan Laurie2ea13c82016-09-19 16:04:39 -070030#include <commonlib/endian.h>
Elyes HAOUAS361a9352019-12-18 21:26:33 +010031#include <commonlib/helpers.h>
Duncan Laurie2ea13c82016-09-19 16:04:39 -070032#include <string.h>
33#include <types.h>
34#include <delay.h>
35#include <console/console.h>
Nico Huber0f2dd1e2017-08-01 14:02:40 +020036#include <device/i2c_simple.h>
Duncan Laurie2ea13c82016-09-19 16:04:39 -070037#include <endian.h>
38#include <timer.h>
Philipp Deppenwiesed88fb362017-10-18 20:26:18 +020039#include <security/tpm/tis.h>
Elyes HAOUASede8dd02019-06-23 06:57:53 +020040
Duncan Laurie2ea13c82016-09-19 16:04:39 -070041#include "tpm.h"
42
Duncan Laurie3727a8d2016-09-19 16:37:46 -070043#define CR50_MAX_BUFSIZE 63
Duncan Laurie469af7b2017-11-07 09:13:19 -080044#define CR50_TIMEOUT_INIT_MS 30000 /* Very long timeout for TPM init */
Duncan Laurie1dc036c2016-09-19 16:49:23 -070045#define CR50_TIMEOUT_LONG_MS 2000 /* Long timeout while waiting for TPM */
46#define CR50_TIMEOUT_SHORT_MS 2 /* Short timeout during transactions */
Duncan Laurieed4fa092016-11-01 15:03:13 -070047#define CR50_TIMEOUT_NOIRQ_MS 20 /* Timeout for TPM ready without IRQ */
48#define CR50_TIMEOUT_IRQ_MS 100 /* Timeout for TPM ready with IRQ */
Duncan Laurie2ea13c82016-09-19 16:04:39 -070049#define CR50_DID_VID 0x00281ae0L
50
51struct tpm_inf_dev {
52 int bus;
53 unsigned int addr;
Duncan Laurie3727a8d2016-09-19 16:37:46 -070054 uint8_t buf[CR50_MAX_BUFSIZE + sizeof(uint8_t)];
Duncan Laurie2ea13c82016-09-19 16:04:39 -070055};
56
Patrick Georgic9b13592019-11-29 11:47:47 +010057static struct tpm_inf_dev tpm_dev;
Duncan Laurie2ea13c82016-09-19 16:04:39 -070058
Aaron Durbin64031672018-04-21 14:45:32 -060059__weak int tis_plat_irq_status(void)
Daniel Kurtzc4852e72017-04-21 14:11:51 +080060{
Arthur Heymans0ca944b2019-11-20 19:51:06 +010061 static int warning_displayed;
Daniel Kurtzc4852e72017-04-21 14:11:51 +080062
Arthur Heymans0ca944b2019-11-20 19:51:06 +010063 if (!warning_displayed) {
Daniel Kurtzc4852e72017-04-21 14:11:51 +080064 printk(BIOS_WARNING, "WARNING: tis_plat_irq_status() not implemented, wasting 20ms to wait on Cr50!\n");
Arthur Heymans0ca944b2019-11-20 19:51:06 +010065 warning_displayed = 1;
Daniel Kurtzc4852e72017-04-21 14:11:51 +080066 }
67 mdelay(CR50_TIMEOUT_NOIRQ_MS);
68
69 return 1;
70}
71
Duncan Laurie94cc4852016-09-19 17:22:10 -070072/* Wait for interrupt to indicate the TPM is ready */
73static int cr50_i2c_wait_tpm_ready(struct tpm_chip *chip)
74{
75 struct stopwatch sw;
76
Duncan Laurieed4fa092016-11-01 15:03:13 -070077 stopwatch_init_msecs_expire(&sw, CR50_TIMEOUT_IRQ_MS);
Duncan Laurie94cc4852016-09-19 17:22:10 -070078
Daniel Kurtzc4852e72017-04-21 14:11:51 +080079 while (!tis_plat_irq_status())
Vadim Bendeburyc7fc1992019-11-26 14:08:59 -080080 if (stopwatch_expired(&sw)) {
81 printk(BIOS_ERR, "Cr50 i2c TPM IRQ timeout!\n");
Duncan Laurie94cc4852016-09-19 17:22:10 -070082 return -1;
Vadim Bendeburyc7fc1992019-11-26 14:08:59 -080083 }
Duncan Laurie94cc4852016-09-19 17:22:10 -070084 return 0;
85}
86
Duncan Laurie2ea13c82016-09-19 16:04:39 -070087/*
Duncan Laurie510cb6a2016-09-19 17:05:45 -070088 * cr50_i2c_read() - read from TPM register
Duncan Laurie2ea13c82016-09-19 16:04:39 -070089 *
Duncan Laurie510cb6a2016-09-19 17:05:45 -070090 * @chip: TPM chip information
Duncan Laurie2ea13c82016-09-19 16:04:39 -070091 * @addr: register address to read from
92 * @buffer: provided by caller
93 * @len: number of bytes to read
94 *
95 * 1) send register address byte 'addr' to the TPM
96 * 2) wait for TPM to indicate it is ready
97 * 3) read 'len' bytes of TPM response into the provided 'buffer'
98 *
99 * Return -1 on error, 0 on success.
100 */
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700101static int cr50_i2c_read(struct tpm_chip *chip, uint8_t addr,
102 uint8_t *buffer, size_t len)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700103{
Patrick Georgic9b13592019-11-29 11:47:47 +0100104 if (tpm_dev.addr == 0)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700105 return -1;
106
Duncan Laurie94cc4852016-09-19 17:22:10 -0700107 /* Clear interrupt before starting transaction */
Daniel Kurtzc4852e72017-04-21 14:11:51 +0800108 tis_plat_irq_status();
Duncan Laurie94cc4852016-09-19 17:22:10 -0700109
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700110 /* Send the register address byte to the TPM */
Patrick Georgic9b13592019-11-29 11:47:47 +0100111 if (i2c_write_raw(tpm_dev.bus, tpm_dev.addr, &addr, 1)) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700112 printk(BIOS_ERR, "%s: Address write failed\n", __func__);
113 return -1;
114 }
115
116 /* Wait for TPM to be ready with response data */
Duncan Laurie94cc4852016-09-19 17:22:10 -0700117 if (cr50_i2c_wait_tpm_ready(chip) < 0)
118 return -1;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700119
120 /* Read response data from the TPM */
Patrick Georgic9b13592019-11-29 11:47:47 +0100121 if (i2c_read_raw(tpm_dev.bus, tpm_dev.addr, buffer, len)) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700122 printk(BIOS_ERR, "%s: Read response failed\n", __func__);
123 return -1;
124 }
125
126 return 0;
127}
128
129/*
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700130 * cr50_i2c_write() - write to TPM register
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700131 *
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700132 * @chip: TPM chip information
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700133 * @addr: register address to write to
134 * @buffer: data to write
135 * @len: number of bytes to write
136 *
137 * 1) prepend the provided address to the provided data
138 * 2) send the address+data to the TPM
139 * 3) wait for TPM to indicate it is done writing
140 *
141 * Returns -1 on error, 0 on success.
142 */
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700143static int cr50_i2c_write(struct tpm_chip *chip,
144 uint8_t addr, uint8_t *buffer, size_t len)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700145{
Patrick Georgic9b13592019-11-29 11:47:47 +0100146 if (tpm_dev.addr == 0)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700147 return -1;
Duncan Laurie3727a8d2016-09-19 16:37:46 -0700148 if (len > CR50_MAX_BUFSIZE)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700149 return -1;
150
151 /* Prepend the 'register address' to the buffer */
Patrick Georgic9b13592019-11-29 11:47:47 +0100152 tpm_dev.buf[0] = addr;
153 memcpy(tpm_dev.buf + 1, buffer, len);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700154
Duncan Laurie94cc4852016-09-19 17:22:10 -0700155 /* Clear interrupt before starting transaction */
Daniel Kurtzc4852e72017-04-21 14:11:51 +0800156 tis_plat_irq_status();
Duncan Laurie94cc4852016-09-19 17:22:10 -0700157
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700158 /* Send write request buffer with address */
Patrick Georgic9b13592019-11-29 11:47:47 +0100159 if (i2c_write_raw(tpm_dev.bus, tpm_dev.addr, tpm_dev.buf, len + 1)) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700160 printk(BIOS_ERR, "%s: Error writing to TPM\n", __func__);
161 return -1;
162 }
163
164 /* Wait for TPM to be ready */
Duncan Laurie94cc4852016-09-19 17:22:10 -0700165 return cr50_i2c_wait_tpm_ready(chip);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700166}
167
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800168/*
169 * Cr50 processes reset requests asynchronously and consceivably could be busy
170 * executing a long command and not reacting to the reset pulse for a while.
171 *
172 * This function will make sure that the AP does not proceed with boot until
173 * TPM finished reset processing.
174 */
175static int process_reset(struct tpm_chip *chip)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700176{
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800177 struct stopwatch sw;
Richard Spiegel7c1e9592018-08-09 14:41:17 -0700178 int rv = 0;
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800179 uint8_t access;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700180
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800181 /*
182 * Locality is released by TPM reset.
183 *
184 * If locality is taken at this point, this could be due to the fact
185 * that the TPM is performing a long operation and has not processed
186 * reset request yet. We'll wait up to CR50_TIMEOUT_INIT_MS and see if
187 * it releases locality when reset is processed.
188 */
189 stopwatch_init_msecs_expire(&sw, CR50_TIMEOUT_INIT_MS);
190 do {
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800191 const uint8_t mask =
192 TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700193
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800194 rv = cr50_i2c_read(chip, TPM_ACCESS(0),
195 &access, sizeof(access));
196 if (rv || ((access & mask) == mask)) {
197 /*
198 * Don't bombard the chip with traffic, let it keep
199 * processing the command.
200 */
201 mdelay(2);
202 continue;
203 }
204
205 printk(BIOS_INFO, "TPM ready after %ld ms\n",
206 stopwatch_duration_msecs(&sw));
207
208 return 0;
209 } while (!stopwatch_expired(&sw));
210
Richard Spiegel7c1e9592018-08-09 14:41:17 -0700211 if (rv)
212 printk(BIOS_ERR, "Failed to read TPM\n");
213 else
214 printk(BIOS_ERR,
215 "TPM failed to reset after %ld ms, status: %#x\n",
216 stopwatch_duration_msecs(&sw), access);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700217
218 return -1;
219}
220
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800221/*
222 * Locality could be already claimed (if this is a later coreboot stage and
223 * the RO did not release it), or not yet claimed, if this is verstage or the
224 * older RO did release it.
225 */
226static int claim_locality(struct tpm_chip *chip)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700227{
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800228 uint8_t access;
229 const uint8_t mask = TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY;
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700230
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800231 if (cr50_i2c_read(chip, TPM_ACCESS(0), &access, sizeof(access)))
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700232 return -1;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700233
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800234 if ((access & mask) == mask) {
235 printk(BIOS_INFO, "Locality already claimed\n");
236 return 0;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700237 }
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800238
239 access = TPM_ACCESS_REQUEST_USE;
240 if (cr50_i2c_write(chip, TPM_ACCESS(0),
241 &access, sizeof(access)))
242 return -1;
243
244 if (cr50_i2c_read(chip, TPM_ACCESS(0), &access, sizeof(access)))
245 return -1;
246
247 if ((access & mask) != mask) {
248 printk(BIOS_INFO, "Failed to claim locality.\n");
249 return -1;
250 }
251
252 return 0;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700253}
254
255/* cr50 requires all 4 bytes of status register to be read */
Duncan Laurief235a9b2016-09-19 17:19:10 -0700256static uint8_t cr50_i2c_tis_status(struct tpm_chip *chip)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700257{
258 uint8_t buf[4];
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700259 if (cr50_i2c_read(chip, TPM_STS(chip->vendor.locality),
260 buf, sizeof(buf)) < 0) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700261 printk(BIOS_ERR, "%s: Failed to read status\n", __func__);
262 return 0;
263 }
264 return buf[0];
265}
266
267/* cr50 requires all 4 bytes of status register to be written */
Duncan Laurief235a9b2016-09-19 17:19:10 -0700268static void cr50_i2c_tis_ready(struct tpm_chip *chip)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700269{
270 uint8_t buf[4] = { TPM_STS_COMMAND_READY };
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700271 cr50_i2c_write(chip, TPM_STS(chip->vendor.locality), buf, sizeof(buf));
Duncan Laurie1dc036c2016-09-19 16:49:23 -0700272 mdelay(CR50_TIMEOUT_SHORT_MS);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700273}
274
275/* cr50 uses bytes 3:2 of status register for burst count and
276 * all 4 bytes must be read */
Duncan Laurief235a9b2016-09-19 17:19:10 -0700277static int cr50_i2c_wait_burststs(struct tpm_chip *chip, uint8_t mask,
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700278 size_t *burst, int *status)
279{
280 uint8_t buf[4];
281 struct stopwatch sw;
282
Duncan Laurie1dc036c2016-09-19 16:49:23 -0700283 stopwatch_init_msecs_expire(&sw, CR50_TIMEOUT_LONG_MS);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700284
285 while (!stopwatch_expired(&sw)) {
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700286 if (cr50_i2c_read(chip, TPM_STS(chip->vendor.locality),
287 buf, sizeof(buf)) != 0) {
Duncan Laurie1dc036c2016-09-19 16:49:23 -0700288 mdelay(CR50_TIMEOUT_SHORT_MS);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700289 continue;
290 }
291
292 *status = buf[0];
293 *burst = read_le16(&buf[1]);
294
295 /* Check if mask matches and burst is valid */
296 if ((*status & mask) == mask &&
Duncan Laurie3727a8d2016-09-19 16:37:46 -0700297 *burst > 0 && *burst <= CR50_MAX_BUFSIZE)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700298 return 0;
299
Duncan Laurie1dc036c2016-09-19 16:49:23 -0700300 mdelay(CR50_TIMEOUT_SHORT_MS);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700301 }
302
303 printk(BIOS_ERR, "%s: Timeout reading burst and status\n", __func__);
304 return -1;
305}
306
Duncan Laurief235a9b2016-09-19 17:19:10 -0700307static int cr50_i2c_tis_recv(struct tpm_chip *chip, uint8_t *buf,
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700308 size_t buf_len)
309{
310 size_t burstcnt, current, len, expected;
311 uint8_t addr = TPM_DATA_FIFO(chip->vendor.locality);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700312 uint8_t mask = TPM_STS_VALID | TPM_STS_DATA_AVAIL;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700313 int status;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700314
315 if (buf_len < TPM_HEADER_SIZE)
Duncan Laurief235a9b2016-09-19 17:19:10 -0700316 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700317
Duncan Laurief235a9b2016-09-19 17:19:10 -0700318 if (cr50_i2c_wait_burststs(chip, mask, &burstcnt, &status) < 0) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700319 printk(BIOS_ERR, "%s: First chunk not available\n", __func__);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700320 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700321 }
322
323 /* Read first chunk of burstcnt bytes */
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700324 if (cr50_i2c_read(chip, addr, buf, burstcnt) != 0) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700325 printk(BIOS_ERR, "%s: Read failed\n", __func__);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700326 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700327 }
328
329 /* Determine expected data in the return buffer */
330 expected = read_be32(buf + TPM_RSP_SIZE_BYTE);
331 if (expected > buf_len) {
332 printk(BIOS_ERR, "%s: Too much data: %zu > %zu\n",
333 __func__, expected, buf_len);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700334 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700335 }
336
337 /* Now read the rest of the data */
338 current = burstcnt;
339 while (current < expected) {
340 /* Read updated burst count and check status */
Duncan Laurief235a9b2016-09-19 17:19:10 -0700341 if (cr50_i2c_wait_burststs(chip, mask, &burstcnt, &status) < 0)
342 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700343
Elyes HAOUAS361a9352019-12-18 21:26:33 +0100344 len = MIN(burstcnt, expected - current);
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700345 if (cr50_i2c_read(chip, addr, buf + current, len) != 0) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700346 printk(BIOS_ERR, "%s: Read failed\n", __func__);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700347 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700348 }
349
350 current += len;
351 }
352
353 /* Ensure TPM is done reading data */
Duncan Laurief235a9b2016-09-19 17:19:10 -0700354 if (cr50_i2c_wait_burststs(chip, TPM_STS_VALID, &burstcnt, &status) < 0)
355 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700356 if (status & TPM_STS_DATA_AVAIL) {
357 printk(BIOS_ERR, "%s: Data still available\n", __func__);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700358 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700359 }
360
Duncan Laurief235a9b2016-09-19 17:19:10 -0700361 return current;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700362
Duncan Laurief235a9b2016-09-19 17:19:10 -0700363out_err:
364 /* Abort current transaction if still pending */
365 if (cr50_i2c_tis_status(chip) & TPM_STS_COMMAND_READY)
366 cr50_i2c_tis_ready(chip);
367 return -1;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700368}
369
Duncan Laurief235a9b2016-09-19 17:19:10 -0700370static int cr50_i2c_tis_send(struct tpm_chip *chip, uint8_t *buf, size_t len)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700371{
372 int status;
373 size_t burstcnt, limit, sent = 0;
374 uint8_t tpm_go[4] = { TPM_STS_GO };
375 struct stopwatch sw;
376
Duncan Laurie1dc036c2016-09-19 16:49:23 -0700377 stopwatch_init_msecs_expire(&sw, CR50_TIMEOUT_LONG_MS);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700378
379 /* Wait until TPM is ready for a command */
Duncan Laurief235a9b2016-09-19 17:19:10 -0700380 while (!(cr50_i2c_tis_status(chip) & TPM_STS_COMMAND_READY)) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700381 if (stopwatch_expired(&sw)) {
382 printk(BIOS_ERR, "%s: Command ready timeout\n",
383 __func__);
384 return -1;
385 }
386
Duncan Laurief235a9b2016-09-19 17:19:10 -0700387 cr50_i2c_tis_ready(chip);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700388 }
389
390 while (len > 0) {
Duncan Laurief235a9b2016-09-19 17:19:10 -0700391 uint8_t mask = TPM_STS_VALID;
392
393 /* Wait for data if this is not the first chunk */
394 if (sent > 0)
395 mask |= TPM_STS_DATA_EXPECT;
396
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700397 /* Read burst count and check status */
Duncan Laurief235a9b2016-09-19 17:19:10 -0700398 if (cr50_i2c_wait_burststs(chip, mask, &burstcnt, &status) < 0)
399 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700400
401 /* Use burstcnt - 1 to account for the address byte
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700402 * that is inserted by cr50_i2c_write() */
Elyes HAOUAS361a9352019-12-18 21:26:33 +0100403 limit = MIN(burstcnt - 1, len);
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700404 if (cr50_i2c_write(chip, TPM_DATA_FIFO(chip->vendor.locality),
405 &buf[sent], limit) != 0) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700406 printk(BIOS_ERR, "%s: Write failed\n", __func__);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700407 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700408 }
409
410 sent += limit;
411 len -= limit;
412 }
413
414 /* Ensure TPM is not expecting more data */
Duncan Laurief235a9b2016-09-19 17:19:10 -0700415 if (cr50_i2c_wait_burststs(chip, TPM_STS_VALID, &burstcnt, &status) < 0)
416 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700417 if (status & TPM_STS_DATA_EXPECT) {
418 printk(BIOS_ERR, "%s: Data still expected\n", __func__);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700419 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700420 }
421
422 /* Start the TPM command */
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700423 if (cr50_i2c_write(chip, TPM_STS(chip->vendor.locality), tpm_go,
424 sizeof(tpm_go)) < 0) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700425 printk(BIOS_ERR, "%s: Start command failed\n", __func__);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700426 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700427 }
428 return sent;
429
Duncan Laurief235a9b2016-09-19 17:19:10 -0700430out_err:
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700431 /* Abort current transaction if still pending */
Duncan Laurief235a9b2016-09-19 17:19:10 -0700432 if (cr50_i2c_tis_status(chip) & TPM_STS_COMMAND_READY)
433 cr50_i2c_tis_ready(chip);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700434 return -1;
435}
436
437static void cr50_vendor_init(struct tpm_chip *chip)
438{
439 memset(&chip->vendor, 0, sizeof(struct tpm_vendor_specific));
440 chip->vendor.req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID;
441 chip->vendor.req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID;
442 chip->vendor.req_canceled = TPM_STS_COMMAND_READY;
Duncan Laurief235a9b2016-09-19 17:19:10 -0700443 chip->vendor.status = &cr50_i2c_tis_status;
444 chip->vendor.recv = &cr50_i2c_tis_recv;
445 chip->vendor.send = &cr50_i2c_tis_send;
446 chip->vendor.cancel = &cr50_i2c_tis_ready;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700447}
448
Lee Leahy52ab30b2017-03-15 09:22:11 -0700449int tpm_vendor_probe(unsigned int bus, uint32_t addr)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700450{
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700451 return 0;
452}
453
Keith Short51436352018-12-17 14:21:46 -0700454static int cr50_i2c_probe(struct tpm_chip *chip, uint32_t *did_vid)
455{
456 int retries;
457
458 /*
459 * 150 ms should be enough to synchronize with the TPM even under the
460 * worst nested reset request conditions. In vast majority of cases
461 * there would be no wait at all.
462 */
463 printk(BIOS_INFO, "Probing TPM I2C: ");
464
465 for (retries = 15; retries > 0; retries--) {
466 int rc;
467
468 rc = cr50_i2c_read(chip, TPM_DID_VID(0), (uint8_t *)did_vid, 4);
469
470 /* Exit once DID and VID verified */
471 if (!rc && (*did_vid == CR50_DID_VID)) {
472 printk(BIOS_INFO, "done! DID_VID 0x%08x\n", *did_vid);
473 return 0;
474 }
475
476 /* TPM might be resetting, let's retry in a bit. */
477 mdelay(10);
478 printk(BIOS_INFO, ".");
479 }
480
481 /*
482 * I2C reads failed, or the DID and VID didn't match
483 */
484 printk(BIOS_ERR, "DID_VID 0x%08x not recognized\n", *did_vid);
485 return -1;
486}
487
Lee Leahy52ab30b2017-03-15 09:22:11 -0700488int tpm_vendor_init(struct tpm_chip *chip, unsigned int bus, uint32_t dev_addr)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700489{
Keith Short51436352018-12-17 14:21:46 -0700490 uint32_t did_vid = 0;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700491
492 if (dev_addr == 0) {
493 printk(BIOS_ERR, "%s: missing device address\n", __func__);
494 return -1;
495 }
496
Patrick Georgic9b13592019-11-29 11:47:47 +0100497 tpm_dev.bus = bus;
498 tpm_dev.addr = dev_addr;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700499
500 cr50_vendor_init(chip);
501
Keith Short51436352018-12-17 14:21:46 -0700502 if (cr50_i2c_probe(chip, &did_vid))
503 return -1;
504
Julius Werner21a40532020-04-21 16:03:53 -0700505 if (ENV_SEPARATE_VERSTAGE || ENV_BOOTBLOCK)
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800506 if (process_reset(chip))
507 return -1;
508
509 if (claim_locality(chip))
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700510 return -1;
511
Daniel Kurtzc4852e72017-04-21 14:11:51 +0800512 printk(BIOS_DEBUG, "cr50 TPM 2.0 (i2c %u:0x%02x id 0x%x)\n",
Keith Short51436352018-12-17 14:21:46 -0700513 bus, dev_addr, did_vid >> 16);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700514
515 chip->is_open = 1;
516 return 0;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700517}
518
519void tpm_vendor_cleanup(struct tpm_chip *chip)
520{
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700521}