blob: 8b0ea32269b0b56b1ad6fcdc5bce1845ab87f3e7 [file] [log] [blame]
Duncan Laurie2ea13c82016-09-19 16:04:39 -07001/*
2 * Copyright 2016 Google Inc.
3 *
4 * Based on Linux Kernel TPM driver by
5 * Peter Huewe <peter.huewe@infineon.com>
6 * Copyright (C) 2011 Infineon Technologies
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation, version 2 of the
11 * License.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 */
18
19/*
20 * cr50 is a TPM 2.0 capable device that requries special
21 * handling for the I2C interface.
22 *
23 * - Use an interrupt for transaction status instead of hardcoded delays
24 * - Must use write+wait+read read protocol
25 * - All 4 bytes of status register must be read/written at once
26 * - Burst count max is 63 bytes, and burst count behaves
27 * slightly differently than other I2C TPMs
28 * - When reading from FIFO the full burstcnt must be read
29 * instead of just reading header and determining the remainder
30 */
31
32#include <arch/early_variables.h>
33#include <commonlib/endian.h>
34#include <stdint.h>
35#include <string.h>
36#include <types.h>
37#include <delay.h>
38#include <console/console.h>
Nico Huber0f2dd1e2017-08-01 14:02:40 +020039#include <device/i2c_simple.h>
Duncan Laurie2ea13c82016-09-19 16:04:39 -070040#include <endian.h>
41#include <timer.h>
Furquan Shaikh260b2972017-04-07 13:26:01 -070042#include <tpm.h>
Duncan Laurie2ea13c82016-09-19 16:04:39 -070043#include "tpm.h"
44
Duncan Laurie3727a8d2016-09-19 16:37:46 -070045#define CR50_MAX_BUFSIZE 63
Duncan Laurie469af7b2017-11-07 09:13:19 -080046#define CR50_TIMEOUT_INIT_MS 30000 /* Very long timeout for TPM init */
Duncan Laurie1dc036c2016-09-19 16:49:23 -070047#define CR50_TIMEOUT_LONG_MS 2000 /* Long timeout while waiting for TPM */
48#define CR50_TIMEOUT_SHORT_MS 2 /* Short timeout during transactions */
Duncan Laurieed4fa092016-11-01 15:03:13 -070049#define CR50_TIMEOUT_NOIRQ_MS 20 /* Timeout for TPM ready without IRQ */
50#define CR50_TIMEOUT_IRQ_MS 100 /* Timeout for TPM ready with IRQ */
Duncan Laurie2ea13c82016-09-19 16:04:39 -070051#define CR50_DID_VID 0x00281ae0L
52
53struct tpm_inf_dev {
54 int bus;
55 unsigned int addr;
Duncan Laurie3727a8d2016-09-19 16:37:46 -070056 uint8_t buf[CR50_MAX_BUFSIZE + sizeof(uint8_t)];
Duncan Laurie2ea13c82016-09-19 16:04:39 -070057};
58
59static struct tpm_inf_dev g_tpm_dev CAR_GLOBAL;
60
Stefan Reinauer6a001132017-07-13 02:20:27 +020061__attribute__((weak)) int tis_plat_irq_status(void)
Daniel Kurtzc4852e72017-04-21 14:11:51 +080062{
63 static int warning_displayed CAR_GLOBAL;
64
65 if (!car_get_var(warning_displayed)) {
66 printk(BIOS_WARNING, "WARNING: tis_plat_irq_status() not implemented, wasting 20ms to wait on Cr50!\n");
67 car_set_var(warning_displayed, 1);
68 }
69 mdelay(CR50_TIMEOUT_NOIRQ_MS);
70
71 return 1;
72}
73
Duncan Laurie94cc4852016-09-19 17:22:10 -070074/* Wait for interrupt to indicate the TPM is ready */
75static int cr50_i2c_wait_tpm_ready(struct tpm_chip *chip)
76{
77 struct stopwatch sw;
78
Duncan Laurieed4fa092016-11-01 15:03:13 -070079 stopwatch_init_msecs_expire(&sw, CR50_TIMEOUT_IRQ_MS);
Duncan Laurie94cc4852016-09-19 17:22:10 -070080
Daniel Kurtzc4852e72017-04-21 14:11:51 +080081 while (!tis_plat_irq_status())
Duncan Laurie94cc4852016-09-19 17:22:10 -070082 if (stopwatch_expired(&sw))
83 return -1;
84
85 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;
183 uint8_t access;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700184
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800185 /*
186 * Locality is released by TPM reset.
187 *
188 * If locality is taken at this point, this could be due to the fact
189 * that the TPM is performing a long operation and has not processed
190 * reset request yet. We'll wait up to CR50_TIMEOUT_INIT_MS and see if
191 * it releases locality when reset is processed.
192 */
193 stopwatch_init_msecs_expire(&sw, CR50_TIMEOUT_INIT_MS);
194 do {
195 int rv;
196 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
216 printk(BIOS_ERR,
217 "TPM failed to reset after %ld ms, status: %#x\n",
218 stopwatch_duration_msecs(&sw), access);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700219
220 return -1;
221}
222
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800223/*
224 * Locality could be already claimed (if this is a later coreboot stage and
225 * the RO did not release it), or not yet claimed, if this is verstage or the
226 * older RO did release it.
227 */
228static int claim_locality(struct tpm_chip *chip)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700229{
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800230 uint8_t access;
231 const uint8_t mask = TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY;
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700232
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800233 if (cr50_i2c_read(chip, TPM_ACCESS(0), &access, sizeof(access)))
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700234 return -1;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700235
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800236 if ((access & mask) == mask) {
237 printk(BIOS_INFO, "Locality already claimed\n");
238 return 0;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700239 }
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800240
241 access = TPM_ACCESS_REQUEST_USE;
242 if (cr50_i2c_write(chip, TPM_ACCESS(0),
243 &access, sizeof(access)))
244 return -1;
245
246 if (cr50_i2c_read(chip, TPM_ACCESS(0), &access, sizeof(access)))
247 return -1;
248
249 if ((access & mask) != mask) {
250 printk(BIOS_INFO, "Failed to claim locality.\n");
251 return -1;
252 }
253
254 return 0;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700255}
256
257/* cr50 requires all 4 bytes of status register to be read */
Duncan Laurief235a9b2016-09-19 17:19:10 -0700258static uint8_t cr50_i2c_tis_status(struct tpm_chip *chip)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700259{
260 uint8_t buf[4];
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700261 if (cr50_i2c_read(chip, TPM_STS(chip->vendor.locality),
262 buf, sizeof(buf)) < 0) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700263 printk(BIOS_ERR, "%s: Failed to read status\n", __func__);
264 return 0;
265 }
266 return buf[0];
267}
268
269/* cr50 requires all 4 bytes of status register to be written */
Duncan Laurief235a9b2016-09-19 17:19:10 -0700270static void cr50_i2c_tis_ready(struct tpm_chip *chip)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700271{
272 uint8_t buf[4] = { TPM_STS_COMMAND_READY };
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700273 cr50_i2c_write(chip, TPM_STS(chip->vendor.locality), buf, sizeof(buf));
Duncan Laurie1dc036c2016-09-19 16:49:23 -0700274 mdelay(CR50_TIMEOUT_SHORT_MS);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700275}
276
277/* cr50 uses bytes 3:2 of status register for burst count and
278 * all 4 bytes must be read */
Duncan Laurief235a9b2016-09-19 17:19:10 -0700279static int cr50_i2c_wait_burststs(struct tpm_chip *chip, uint8_t mask,
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700280 size_t *burst, int *status)
281{
282 uint8_t buf[4];
283 struct stopwatch sw;
284
Duncan Laurie1dc036c2016-09-19 16:49:23 -0700285 stopwatch_init_msecs_expire(&sw, CR50_TIMEOUT_LONG_MS);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700286
287 while (!stopwatch_expired(&sw)) {
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700288 if (cr50_i2c_read(chip, TPM_STS(chip->vendor.locality),
289 buf, sizeof(buf)) != 0) {
Duncan Laurie1dc036c2016-09-19 16:49:23 -0700290 mdelay(CR50_TIMEOUT_SHORT_MS);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700291 continue;
292 }
293
294 *status = buf[0];
295 *burst = read_le16(&buf[1]);
296
297 /* Check if mask matches and burst is valid */
298 if ((*status & mask) == mask &&
Duncan Laurie3727a8d2016-09-19 16:37:46 -0700299 *burst > 0 && *burst <= CR50_MAX_BUFSIZE)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700300 return 0;
301
Duncan Laurie1dc036c2016-09-19 16:49:23 -0700302 mdelay(CR50_TIMEOUT_SHORT_MS);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700303 }
304
305 printk(BIOS_ERR, "%s: Timeout reading burst and status\n", __func__);
306 return -1;
307}
308
Duncan Laurief235a9b2016-09-19 17:19:10 -0700309static int cr50_i2c_tis_recv(struct tpm_chip *chip, uint8_t *buf,
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700310 size_t buf_len)
311{
312 size_t burstcnt, current, len, expected;
313 uint8_t addr = TPM_DATA_FIFO(chip->vendor.locality);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700314 uint8_t mask = TPM_STS_VALID | TPM_STS_DATA_AVAIL;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700315 int status;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700316
317 if (buf_len < TPM_HEADER_SIZE)
Duncan Laurief235a9b2016-09-19 17:19:10 -0700318 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700319
Duncan Laurief235a9b2016-09-19 17:19:10 -0700320 if (cr50_i2c_wait_burststs(chip, mask, &burstcnt, &status) < 0) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700321 printk(BIOS_ERR, "%s: First chunk not available\n", __func__);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700322 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700323 }
324
325 /* Read first chunk of burstcnt bytes */
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700326 if (cr50_i2c_read(chip, addr, buf, burstcnt) != 0) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700327 printk(BIOS_ERR, "%s: Read failed\n", __func__);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700328 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700329 }
330
331 /* Determine expected data in the return buffer */
332 expected = read_be32(buf + TPM_RSP_SIZE_BYTE);
333 if (expected > buf_len) {
334 printk(BIOS_ERR, "%s: Too much data: %zu > %zu\n",
335 __func__, expected, buf_len);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700336 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700337 }
338
339 /* Now read the rest of the data */
340 current = burstcnt;
341 while (current < expected) {
342 /* Read updated burst count and check status */
Duncan Laurief235a9b2016-09-19 17:19:10 -0700343 if (cr50_i2c_wait_burststs(chip, mask, &burstcnt, &status) < 0)
344 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700345
346 len = min(burstcnt, expected - current);
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700347 if (cr50_i2c_read(chip, addr, buf + current, len) != 0) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700348 printk(BIOS_ERR, "%s: Read failed\n", __func__);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700349 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700350 }
351
352 current += len;
353 }
354
355 /* Ensure TPM is done reading data */
Duncan Laurief235a9b2016-09-19 17:19:10 -0700356 if (cr50_i2c_wait_burststs(chip, TPM_STS_VALID, &burstcnt, &status) < 0)
357 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700358 if (status & TPM_STS_DATA_AVAIL) {
359 printk(BIOS_ERR, "%s: Data still available\n", __func__);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700360 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700361 }
362
Duncan Laurief235a9b2016-09-19 17:19:10 -0700363 return current;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700364
Duncan Laurief235a9b2016-09-19 17:19:10 -0700365out_err:
366 /* Abort current transaction if still pending */
367 if (cr50_i2c_tis_status(chip) & TPM_STS_COMMAND_READY)
368 cr50_i2c_tis_ready(chip);
369 return -1;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700370}
371
Duncan Laurief235a9b2016-09-19 17:19:10 -0700372static int cr50_i2c_tis_send(struct tpm_chip *chip, uint8_t *buf, size_t len)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700373{
374 int status;
375 size_t burstcnt, limit, sent = 0;
376 uint8_t tpm_go[4] = { TPM_STS_GO };
377 struct stopwatch sw;
378
Duncan Laurie1dc036c2016-09-19 16:49:23 -0700379 stopwatch_init_msecs_expire(&sw, CR50_TIMEOUT_LONG_MS);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700380
381 /* Wait until TPM is ready for a command */
Duncan Laurief235a9b2016-09-19 17:19:10 -0700382 while (!(cr50_i2c_tis_status(chip) & TPM_STS_COMMAND_READY)) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700383 if (stopwatch_expired(&sw)) {
384 printk(BIOS_ERR, "%s: Command ready timeout\n",
385 __func__);
386 return -1;
387 }
388
Duncan Laurief235a9b2016-09-19 17:19:10 -0700389 cr50_i2c_tis_ready(chip);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700390 }
391
392 while (len > 0) {
Duncan Laurief235a9b2016-09-19 17:19:10 -0700393 uint8_t mask = TPM_STS_VALID;
394
395 /* Wait for data if this is not the first chunk */
396 if (sent > 0)
397 mask |= TPM_STS_DATA_EXPECT;
398
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700399 /* Read burst count and check status */
Duncan Laurief235a9b2016-09-19 17:19:10 -0700400 if (cr50_i2c_wait_burststs(chip, mask, &burstcnt, &status) < 0)
401 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700402
403 /* Use burstcnt - 1 to account for the address byte
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700404 * that is inserted by cr50_i2c_write() */
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700405 limit = min(burstcnt - 1, len);
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700406 if (cr50_i2c_write(chip, TPM_DATA_FIFO(chip->vendor.locality),
407 &buf[sent], limit) != 0) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700408 printk(BIOS_ERR, "%s: Write failed\n", __func__);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700409 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700410 }
411
412 sent += limit;
413 len -= limit;
414 }
415
416 /* Ensure TPM is not expecting more data */
Duncan Laurief235a9b2016-09-19 17:19:10 -0700417 if (cr50_i2c_wait_burststs(chip, TPM_STS_VALID, &burstcnt, &status) < 0)
418 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700419 if (status & TPM_STS_DATA_EXPECT) {
420 printk(BIOS_ERR, "%s: Data still expected\n", __func__);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700421 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700422 }
423
424 /* Start the TPM command */
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700425 if (cr50_i2c_write(chip, TPM_STS(chip->vendor.locality), tpm_go,
426 sizeof(tpm_go)) < 0) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700427 printk(BIOS_ERR, "%s: Start command failed\n", __func__);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700428 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700429 }
430 return sent;
431
Duncan Laurief235a9b2016-09-19 17:19:10 -0700432out_err:
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700433 /* Abort current transaction if still pending */
Duncan Laurief235a9b2016-09-19 17:19:10 -0700434 if (cr50_i2c_tis_status(chip) & TPM_STS_COMMAND_READY)
435 cr50_i2c_tis_ready(chip);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700436 return -1;
437}
438
439static void cr50_vendor_init(struct tpm_chip *chip)
440{
441 memset(&chip->vendor, 0, sizeof(struct tpm_vendor_specific));
442 chip->vendor.req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID;
443 chip->vendor.req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID;
444 chip->vendor.req_canceled = TPM_STS_COMMAND_READY;
Duncan Laurief235a9b2016-09-19 17:19:10 -0700445 chip->vendor.status = &cr50_i2c_tis_status;
446 chip->vendor.recv = &cr50_i2c_tis_recv;
447 chip->vendor.send = &cr50_i2c_tis_send;
448 chip->vendor.cancel = &cr50_i2c_tis_ready;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700449}
450
Lee Leahy52ab30b2017-03-15 09:22:11 -0700451int tpm_vendor_probe(unsigned int bus, uint32_t addr)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700452{
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700453 return 0;
454}
455
Lee Leahy52ab30b2017-03-15 09:22:11 -0700456int tpm_vendor_init(struct tpm_chip *chip, unsigned int bus, uint32_t dev_addr)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700457{
458 struct tpm_inf_dev *tpm_dev = car_get_var_ptr(&g_tpm_dev);
459 uint32_t vendor;
460
461 if (dev_addr == 0) {
462 printk(BIOS_ERR, "%s: missing device address\n", __func__);
463 return -1;
464 }
465
466 tpm_dev->bus = bus;
467 tpm_dev->addr = dev_addr;
468
469 cr50_vendor_init(chip);
470
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800471 if (ENV_VERSTAGE || ENV_BOOTBLOCK)
472 if (process_reset(chip))
473 return -1;
474
475 if (claim_locality(chip))
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700476 return -1;
477
478 /* Read four bytes from DID_VID register */
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700479 if (cr50_i2c_read(chip, TPM_DID_VID(0), (uint8_t *)&vendor, 4) < 0)
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800480 return -1;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700481
482 if (vendor != CR50_DID_VID) {
483 printk(BIOS_DEBUG, "Vendor ID 0x%08x not recognized\n", vendor);
Duncan Laurie2bc6ad32017-11-07 09:13:19 -0800484 return -1;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700485 }
486
Daniel Kurtzc4852e72017-04-21 14:11:51 +0800487 printk(BIOS_DEBUG, "cr50 TPM 2.0 (i2c %u:0x%02x id 0x%x)\n",
488 bus, dev_addr, vendor >> 16);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700489
490 chip->is_open = 1;
491 return 0;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700492}
493
494void tpm_vendor_cleanup(struct tpm_chip *chip)
495{
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700496}