blob: f9a286241edb06444cf2d14cecdc3f9f41d6abb1 [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
30#include <arch/early_variables.h>
31#include <commonlib/endian.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#include <stdlib.h>
41
Duncan Laurie2ea13c82016-09-19 16:04:39 -070042#include "tpm.h"
43
Duncan Laurie3727a8d2016-09-19 16:37:46 -070044#define CR50_MAX_BUFSIZE 63
Duncan Laurie469af7b2017-11-07 09:13:19 -080045#define CR50_TIMEOUT_INIT_MS 30000 /* Very long timeout for TPM init */
Duncan Laurie1dc036c2016-09-19 16:49:23 -070046#define CR50_TIMEOUT_LONG_MS 2000 /* Long timeout while waiting for TPM */
47#define CR50_TIMEOUT_SHORT_MS 2 /* Short timeout during transactions */
Duncan Laurieed4fa092016-11-01 15:03:13 -070048#define CR50_TIMEOUT_NOIRQ_MS 20 /* Timeout for TPM ready without IRQ */
49#define CR50_TIMEOUT_IRQ_MS 100 /* Timeout for TPM ready with IRQ */
Duncan Laurie2ea13c82016-09-19 16:04:39 -070050#define CR50_DID_VID 0x00281ae0L
51
52struct tpm_inf_dev {
53 int bus;
54 unsigned int addr;
Duncan Laurie3727a8d2016-09-19 16:37:46 -070055 uint8_t buf[CR50_MAX_BUFSIZE + sizeof(uint8_t)];
Duncan Laurie2ea13c82016-09-19 16:04:39 -070056};
57
58static struct tpm_inf_dev g_tpm_dev CAR_GLOBAL;
59
Aaron Durbin64031672018-04-21 14:45:32 -060060__weak int tis_plat_irq_status(void)
Daniel Kurtzc4852e72017-04-21 14:11:51 +080061{
62 static int warning_displayed CAR_GLOBAL;
63
64 if (!car_get_var(warning_displayed)) {
65 printk(BIOS_WARNING, "WARNING: tis_plat_irq_status() not implemented, wasting 20ms to wait on Cr50!\n");
66 car_set_var(warning_displayed, 1);
67 }
68 mdelay(CR50_TIMEOUT_NOIRQ_MS);
69
70 return 1;
71}
72
Duncan Laurie94cc4852016-09-19 17:22:10 -070073/* Wait for interrupt to indicate the TPM is ready */
74static int cr50_i2c_wait_tpm_ready(struct tpm_chip *chip)
75{
76 struct stopwatch sw;
77
Duncan Laurieed4fa092016-11-01 15:03:13 -070078 stopwatch_init_msecs_expire(&sw, CR50_TIMEOUT_IRQ_MS);
Duncan Laurie94cc4852016-09-19 17:22:10 -070079
Daniel Kurtzc4852e72017-04-21 14:11:51 +080080 while (!tis_plat_irq_status())
Vadim Bendeburyc7fc1992019-11-26 14:08:59 -080081 if (stopwatch_expired(&sw)) {
82 printk(BIOS_ERR, "Cr50 i2c TPM IRQ timeout!\n");
Duncan Laurie94cc4852016-09-19 17:22:10 -070083 return -1;
Vadim Bendeburyc7fc1992019-11-26 14:08:59 -080084 }
Duncan Laurie94cc4852016-09-19 17:22:10 -070085 return 0;
86}
87
Duncan Laurie2ea13c82016-09-19 16:04:39 -070088/*
Duncan Laurie510cb6a2016-09-19 17:05:45 -070089 * cr50_i2c_read() - read from TPM register
Duncan Laurie2ea13c82016-09-19 16:04:39 -070090 *
Duncan Laurie510cb6a2016-09-19 17:05:45 -070091 * @chip: TPM chip information
Duncan Laurie2ea13c82016-09-19 16:04:39 -070092 * @addr: register address to read from
93 * @buffer: provided by caller
94 * @len: number of bytes to read
95 *
96 * 1) send register address byte 'addr' to the TPM
97 * 2) wait for TPM to indicate it is ready
98 * 3) read 'len' bytes of TPM response into the provided 'buffer'
99 *
100 * Return -1 on error, 0 on success.
101 */
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700102static int cr50_i2c_read(struct tpm_chip *chip, uint8_t addr,
103 uint8_t *buffer, size_t len)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700104{
105 struct tpm_inf_dev *tpm_dev = car_get_var_ptr(&g_tpm_dev);
106
107 if (tpm_dev->addr == 0)
108 return -1;
109
Duncan Laurie94cc4852016-09-19 17:22:10 -0700110 /* Clear interrupt before starting transaction */
Daniel Kurtzc4852e72017-04-21 14:11:51 +0800111 tis_plat_irq_status();
Duncan Laurie94cc4852016-09-19 17:22:10 -0700112
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700113 /* Send the register address byte to the TPM */
114 if (i2c_write_raw(tpm_dev->bus, tpm_dev->addr, &addr, 1)) {
115 printk(BIOS_ERR, "%s: Address write failed\n", __func__);
116 return -1;
117 }
118
119 /* Wait for TPM to be ready with response data */
Duncan Laurie94cc4852016-09-19 17:22:10 -0700120 if (cr50_i2c_wait_tpm_ready(chip) < 0)
121 return -1;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700122
123 /* Read response data from the TPM */
124 if (i2c_read_raw(tpm_dev->bus, tpm_dev->addr, buffer, len)) {
125 printk(BIOS_ERR, "%s: Read response failed\n", __func__);
126 return -1;
127 }
128
129 return 0;
130}
131
132/*
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700133 * cr50_i2c_write() - write to TPM register
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700134 *
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700135 * @chip: TPM chip information
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700136 * @addr: register address to write to
137 * @buffer: data to write
138 * @len: number of bytes to write
139 *
140 * 1) prepend the provided address to the provided data
141 * 2) send the address+data to the TPM
142 * 3) wait for TPM to indicate it is done writing
143 *
144 * Returns -1 on error, 0 on success.
145 */
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700146static int cr50_i2c_write(struct tpm_chip *chip,
147 uint8_t addr, uint8_t *buffer, size_t len)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700148{
149 struct tpm_inf_dev *tpm_dev = car_get_var_ptr(&g_tpm_dev);
150
151 if (tpm_dev->addr == 0)
152 return -1;
Duncan Laurie3727a8d2016-09-19 16:37:46 -0700153 if (len > CR50_MAX_BUFSIZE)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700154 return -1;
155
156 /* Prepend the 'register address' to the buffer */
157 tpm_dev->buf[0] = addr;
158 memcpy(tpm_dev->buf + 1, buffer, len);
159
Duncan Laurie94cc4852016-09-19 17:22:10 -0700160 /* Clear interrupt before starting transaction */
Daniel Kurtzc4852e72017-04-21 14:11:51 +0800161 tis_plat_irq_status();
Duncan Laurie94cc4852016-09-19 17:22:10 -0700162
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700163 /* Send write request buffer with address */
164 if (i2c_write_raw(tpm_dev->bus, tpm_dev->addr, tpm_dev->buf, len + 1)) {
165 printk(BIOS_ERR, "%s: Error writing to TPM\n", __func__);
166 return -1;
167 }
168
169 /* Wait for TPM to be ready */
Duncan Laurie94cc4852016-09-19 17:22:10 -0700170 return cr50_i2c_wait_tpm_ready(chip);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700171}
172
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800173/*
174 * Cr50 processes reset requests asynchronously and consceivably could be busy
175 * executing a long command and not reacting to the reset pulse for a while.
176 *
177 * This function will make sure that the AP does not proceed with boot until
178 * TPM finished reset processing.
179 */
180static int process_reset(struct tpm_chip *chip)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700181{
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800182 struct stopwatch sw;
Richard Spiegel7c1e9592018-08-09 14:41:17 -0700183 int rv = 0;
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800184 uint8_t access;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700185
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800186 /*
187 * Locality is released by TPM reset.
188 *
189 * If locality is taken at this point, this could be due to the fact
190 * that the TPM is performing a long operation and has not processed
191 * reset request yet. We'll wait up to CR50_TIMEOUT_INIT_MS and see if
192 * it releases locality when reset is processed.
193 */
194 stopwatch_init_msecs_expire(&sw, CR50_TIMEOUT_INIT_MS);
195 do {
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800196 const uint8_t mask =
197 TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700198
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800199 rv = cr50_i2c_read(chip, TPM_ACCESS(0),
200 &access, sizeof(access));
201 if (rv || ((access & mask) == mask)) {
202 /*
203 * Don't bombard the chip with traffic, let it keep
204 * processing the command.
205 */
206 mdelay(2);
207 continue;
208 }
209
210 printk(BIOS_INFO, "TPM ready after %ld ms\n",
211 stopwatch_duration_msecs(&sw));
212
213 return 0;
214 } while (!stopwatch_expired(&sw));
215
Richard Spiegel7c1e9592018-08-09 14:41:17 -0700216 if (rv)
217 printk(BIOS_ERR, "Failed to read TPM\n");
218 else
219 printk(BIOS_ERR,
220 "TPM failed to reset after %ld ms, status: %#x\n",
221 stopwatch_duration_msecs(&sw), access);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700222
223 return -1;
224}
225
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800226/*
227 * Locality could be already claimed (if this is a later coreboot stage and
228 * the RO did not release it), or not yet claimed, if this is verstage or the
229 * older RO did release it.
230 */
231static int claim_locality(struct tpm_chip *chip)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700232{
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800233 uint8_t access;
234 const uint8_t mask = TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY;
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700235
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800236 if (cr50_i2c_read(chip, TPM_ACCESS(0), &access, sizeof(access)))
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700237 return -1;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700238
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800239 if ((access & mask) == mask) {
240 printk(BIOS_INFO, "Locality already claimed\n");
241 return 0;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700242 }
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800243
244 access = TPM_ACCESS_REQUEST_USE;
245 if (cr50_i2c_write(chip, TPM_ACCESS(0),
246 &access, sizeof(access)))
247 return -1;
248
249 if (cr50_i2c_read(chip, TPM_ACCESS(0), &access, sizeof(access)))
250 return -1;
251
252 if ((access & mask) != mask) {
253 printk(BIOS_INFO, "Failed to claim locality.\n");
254 return -1;
255 }
256
257 return 0;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700258}
259
260/* cr50 requires all 4 bytes of status register to be read */
Duncan Laurief235a9b2016-09-19 17:19:10 -0700261static uint8_t cr50_i2c_tis_status(struct tpm_chip *chip)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700262{
263 uint8_t buf[4];
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700264 if (cr50_i2c_read(chip, TPM_STS(chip->vendor.locality),
265 buf, sizeof(buf)) < 0) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700266 printk(BIOS_ERR, "%s: Failed to read status\n", __func__);
267 return 0;
268 }
269 return buf[0];
270}
271
272/* cr50 requires all 4 bytes of status register to be written */
Duncan Laurief235a9b2016-09-19 17:19:10 -0700273static void cr50_i2c_tis_ready(struct tpm_chip *chip)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700274{
275 uint8_t buf[4] = { TPM_STS_COMMAND_READY };
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700276 cr50_i2c_write(chip, TPM_STS(chip->vendor.locality), buf, sizeof(buf));
Duncan Laurie1dc036c2016-09-19 16:49:23 -0700277 mdelay(CR50_TIMEOUT_SHORT_MS);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700278}
279
280/* cr50 uses bytes 3:2 of status register for burst count and
281 * all 4 bytes must be read */
Duncan Laurief235a9b2016-09-19 17:19:10 -0700282static int cr50_i2c_wait_burststs(struct tpm_chip *chip, uint8_t mask,
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700283 size_t *burst, int *status)
284{
285 uint8_t buf[4];
286 struct stopwatch sw;
287
Duncan Laurie1dc036c2016-09-19 16:49:23 -0700288 stopwatch_init_msecs_expire(&sw, CR50_TIMEOUT_LONG_MS);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700289
290 while (!stopwatch_expired(&sw)) {
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700291 if (cr50_i2c_read(chip, TPM_STS(chip->vendor.locality),
292 buf, sizeof(buf)) != 0) {
Duncan Laurie1dc036c2016-09-19 16:49:23 -0700293 mdelay(CR50_TIMEOUT_SHORT_MS);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700294 continue;
295 }
296
297 *status = buf[0];
298 *burst = read_le16(&buf[1]);
299
300 /* Check if mask matches and burst is valid */
301 if ((*status & mask) == mask &&
Duncan Laurie3727a8d2016-09-19 16:37:46 -0700302 *burst > 0 && *burst <= CR50_MAX_BUFSIZE)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700303 return 0;
304
Duncan Laurie1dc036c2016-09-19 16:49:23 -0700305 mdelay(CR50_TIMEOUT_SHORT_MS);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700306 }
307
308 printk(BIOS_ERR, "%s: Timeout reading burst and status\n", __func__);
309 return -1;
310}
311
Duncan Laurief235a9b2016-09-19 17:19:10 -0700312static int cr50_i2c_tis_recv(struct tpm_chip *chip, uint8_t *buf,
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700313 size_t buf_len)
314{
315 size_t burstcnt, current, len, expected;
316 uint8_t addr = TPM_DATA_FIFO(chip->vendor.locality);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700317 uint8_t mask = TPM_STS_VALID | TPM_STS_DATA_AVAIL;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700318 int status;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700319
320 if (buf_len < TPM_HEADER_SIZE)
Duncan Laurief235a9b2016-09-19 17:19:10 -0700321 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700322
Duncan Laurief235a9b2016-09-19 17:19:10 -0700323 if (cr50_i2c_wait_burststs(chip, mask, &burstcnt, &status) < 0) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700324 printk(BIOS_ERR, "%s: First chunk not available\n", __func__);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700325 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700326 }
327
328 /* Read first chunk of burstcnt bytes */
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700329 if (cr50_i2c_read(chip, addr, buf, burstcnt) != 0) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700330 printk(BIOS_ERR, "%s: Read failed\n", __func__);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700331 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700332 }
333
334 /* Determine expected data in the return buffer */
335 expected = read_be32(buf + TPM_RSP_SIZE_BYTE);
336 if (expected > buf_len) {
337 printk(BIOS_ERR, "%s: Too much data: %zu > %zu\n",
338 __func__, expected, buf_len);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700339 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700340 }
341
342 /* Now read the rest of the data */
343 current = burstcnt;
344 while (current < expected) {
345 /* Read updated burst count and check status */
Duncan Laurief235a9b2016-09-19 17:19:10 -0700346 if (cr50_i2c_wait_burststs(chip, mask, &burstcnt, &status) < 0)
347 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700348
349 len = min(burstcnt, expected - current);
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700350 if (cr50_i2c_read(chip, addr, buf + current, len) != 0) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700351 printk(BIOS_ERR, "%s: Read failed\n", __func__);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700352 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700353 }
354
355 current += len;
356 }
357
358 /* Ensure TPM is done reading data */
Duncan Laurief235a9b2016-09-19 17:19:10 -0700359 if (cr50_i2c_wait_burststs(chip, TPM_STS_VALID, &burstcnt, &status) < 0)
360 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700361 if (status & TPM_STS_DATA_AVAIL) {
362 printk(BIOS_ERR, "%s: Data still available\n", __func__);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700363 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700364 }
365
Duncan Laurief235a9b2016-09-19 17:19:10 -0700366 return current;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700367
Duncan Laurief235a9b2016-09-19 17:19:10 -0700368out_err:
369 /* Abort current transaction if still pending */
370 if (cr50_i2c_tis_status(chip) & TPM_STS_COMMAND_READY)
371 cr50_i2c_tis_ready(chip);
372 return -1;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700373}
374
Duncan Laurief235a9b2016-09-19 17:19:10 -0700375static int cr50_i2c_tis_send(struct tpm_chip *chip, uint8_t *buf, size_t len)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700376{
377 int status;
378 size_t burstcnt, limit, sent = 0;
379 uint8_t tpm_go[4] = { TPM_STS_GO };
380 struct stopwatch sw;
381
Duncan Laurie1dc036c2016-09-19 16:49:23 -0700382 stopwatch_init_msecs_expire(&sw, CR50_TIMEOUT_LONG_MS);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700383
384 /* Wait until TPM is ready for a command */
Duncan Laurief235a9b2016-09-19 17:19:10 -0700385 while (!(cr50_i2c_tis_status(chip) & TPM_STS_COMMAND_READY)) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700386 if (stopwatch_expired(&sw)) {
387 printk(BIOS_ERR, "%s: Command ready timeout\n",
388 __func__);
389 return -1;
390 }
391
Duncan Laurief235a9b2016-09-19 17:19:10 -0700392 cr50_i2c_tis_ready(chip);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700393 }
394
395 while (len > 0) {
Duncan Laurief235a9b2016-09-19 17:19:10 -0700396 uint8_t mask = TPM_STS_VALID;
397
398 /* Wait for data if this is not the first chunk */
399 if (sent > 0)
400 mask |= TPM_STS_DATA_EXPECT;
401
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700402 /* Read burst count and check status */
Duncan Laurief235a9b2016-09-19 17:19:10 -0700403 if (cr50_i2c_wait_burststs(chip, mask, &burstcnt, &status) < 0)
404 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700405
406 /* Use burstcnt - 1 to account for the address byte
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700407 * that is inserted by cr50_i2c_write() */
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700408 limit = min(burstcnt - 1, len);
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700409 if (cr50_i2c_write(chip, TPM_DATA_FIFO(chip->vendor.locality),
410 &buf[sent], limit) != 0) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700411 printk(BIOS_ERR, "%s: Write failed\n", __func__);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700412 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700413 }
414
415 sent += limit;
416 len -= limit;
417 }
418
419 /* Ensure TPM is not expecting more data */
Duncan Laurief235a9b2016-09-19 17:19:10 -0700420 if (cr50_i2c_wait_burststs(chip, TPM_STS_VALID, &burstcnt, &status) < 0)
421 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700422 if (status & TPM_STS_DATA_EXPECT) {
423 printk(BIOS_ERR, "%s: Data still expected\n", __func__);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700424 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700425 }
426
427 /* Start the TPM command */
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700428 if (cr50_i2c_write(chip, TPM_STS(chip->vendor.locality), tpm_go,
429 sizeof(tpm_go)) < 0) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700430 printk(BIOS_ERR, "%s: Start command failed\n", __func__);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700431 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700432 }
433 return sent;
434
Duncan Laurief235a9b2016-09-19 17:19:10 -0700435out_err:
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700436 /* Abort current transaction if still pending */
Duncan Laurief235a9b2016-09-19 17:19:10 -0700437 if (cr50_i2c_tis_status(chip) & TPM_STS_COMMAND_READY)
438 cr50_i2c_tis_ready(chip);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700439 return -1;
440}
441
442static void cr50_vendor_init(struct tpm_chip *chip)
443{
444 memset(&chip->vendor, 0, sizeof(struct tpm_vendor_specific));
445 chip->vendor.req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID;
446 chip->vendor.req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID;
447 chip->vendor.req_canceled = TPM_STS_COMMAND_READY;
Duncan Laurief235a9b2016-09-19 17:19:10 -0700448 chip->vendor.status = &cr50_i2c_tis_status;
449 chip->vendor.recv = &cr50_i2c_tis_recv;
450 chip->vendor.send = &cr50_i2c_tis_send;
451 chip->vendor.cancel = &cr50_i2c_tis_ready;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700452}
453
Lee Leahy52ab30b2017-03-15 09:22:11 -0700454int tpm_vendor_probe(unsigned int bus, uint32_t addr)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700455{
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700456 return 0;
457}
458
Keith Short51436352018-12-17 14:21:46 -0700459static int cr50_i2c_probe(struct tpm_chip *chip, uint32_t *did_vid)
460{
461 int retries;
462
463 /*
464 * 150 ms should be enough to synchronize with the TPM even under the
465 * worst nested reset request conditions. In vast majority of cases
466 * there would be no wait at all.
467 */
468 printk(BIOS_INFO, "Probing TPM I2C: ");
469
470 for (retries = 15; retries > 0; retries--) {
471 int rc;
472
473 rc = cr50_i2c_read(chip, TPM_DID_VID(0), (uint8_t *)did_vid, 4);
474
475 /* Exit once DID and VID verified */
476 if (!rc && (*did_vid == CR50_DID_VID)) {
477 printk(BIOS_INFO, "done! DID_VID 0x%08x\n", *did_vid);
478 return 0;
479 }
480
481 /* TPM might be resetting, let's retry in a bit. */
482 mdelay(10);
483 printk(BIOS_INFO, ".");
484 }
485
486 /*
487 * I2C reads failed, or the DID and VID didn't match
488 */
489 printk(BIOS_ERR, "DID_VID 0x%08x not recognized\n", *did_vid);
490 return -1;
491}
492
Lee Leahy52ab30b2017-03-15 09:22:11 -0700493int tpm_vendor_init(struct tpm_chip *chip, unsigned int bus, uint32_t dev_addr)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700494{
495 struct tpm_inf_dev *tpm_dev = car_get_var_ptr(&g_tpm_dev);
Keith Short51436352018-12-17 14:21:46 -0700496 uint32_t did_vid = 0;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700497
498 if (dev_addr == 0) {
499 printk(BIOS_ERR, "%s: missing device address\n", __func__);
500 return -1;
501 }
502
503 tpm_dev->bus = bus;
504 tpm_dev->addr = dev_addr;
505
506 cr50_vendor_init(chip);
507
Keith Short51436352018-12-17 14:21:46 -0700508 if (cr50_i2c_probe(chip, &did_vid))
509 return -1;
510
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800511 if (ENV_VERSTAGE || ENV_BOOTBLOCK)
512 if (process_reset(chip))
513 return -1;
514
515 if (claim_locality(chip))
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700516 return -1;
517
Daniel Kurtzc4852e72017-04-21 14:11:51 +0800518 printk(BIOS_DEBUG, "cr50 TPM 2.0 (i2c %u:0x%02x id 0x%x)\n",
Keith Short51436352018-12-17 14:21:46 -0700519 bus, dev_addr, did_vid >> 16);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700520
521 chip->is_open = 1;
522 return 0;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700523}
524
525void tpm_vendor_cleanup(struct tpm_chip *chip)
526{
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700527}