blob: 42c82d6fece54935e1dd714f0d5fb7841717aac2 [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>
39#include <device/i2c.h>
40#include <endian.h>
41#include <timer.h>
42#include "tpm.h"
43
Duncan Laurie94cc4852016-09-19 17:22:10 -070044#if IS_ENABLED(CONFIG_ARCH_X86)
45#include <arch/acpi.h>
46#endif
Duncan Laurie2ea13c82016-09-19 16:04:39 -070047
Duncan Laurie3727a8d2016-09-19 16:37:46 -070048#define CR50_MAX_BUFSIZE 63
Duncan Laurie1dc036c2016-09-19 16:49:23 -070049#define CR50_TIMEOUT_LONG_MS 2000 /* Long timeout while waiting for TPM */
50#define CR50_TIMEOUT_SHORT_MS 2 /* Short timeout during transactions */
Duncan Laurieed4fa092016-11-01 15:03:13 -070051#define CR50_TIMEOUT_NOIRQ_MS 20 /* Timeout for TPM ready without IRQ */
52#define CR50_TIMEOUT_IRQ_MS 100 /* Timeout for TPM ready with IRQ */
Duncan Laurie2ea13c82016-09-19 16:04:39 -070053#define CR50_DID_VID 0x00281ae0L
54
55struct tpm_inf_dev {
56 int bus;
57 unsigned int addr;
Duncan Laurie3727a8d2016-09-19 16:37:46 -070058 uint8_t buf[CR50_MAX_BUFSIZE + sizeof(uint8_t)];
Duncan Laurie2ea13c82016-09-19 16:04:39 -070059};
60
61static struct tpm_inf_dev g_tpm_dev CAR_GLOBAL;
62
Duncan Laurie94cc4852016-09-19 17:22:10 -070063/* Wait for interrupt to indicate the TPM is ready */
64static int cr50_i2c_wait_tpm_ready(struct tpm_chip *chip)
65{
66 struct stopwatch sw;
67
68 if (!chip->vendor.irq_status) {
69 /* Fixed delay if interrupt not supported */
Duncan Laurieed4fa092016-11-01 15:03:13 -070070 mdelay(CR50_TIMEOUT_NOIRQ_MS);
Duncan Laurie94cc4852016-09-19 17:22:10 -070071 return 0;
72 }
73
Duncan Laurieed4fa092016-11-01 15:03:13 -070074 stopwatch_init_msecs_expire(&sw, CR50_TIMEOUT_IRQ_MS);
Duncan Laurie94cc4852016-09-19 17:22:10 -070075
76 while (!chip->vendor.irq_status(chip->vendor.irq))
77 if (stopwatch_expired(&sw))
78 return -1;
79
80 return 0;
81}
82
83/* Clear pending interrupts */
84static void cr50_i2c_clear_tpm_irq(struct tpm_chip *chip)
85{
86 if (chip->vendor.irq_status)
87 chip->vendor.irq_status(chip->vendor.irq);
88}
89
Duncan Laurie2ea13c82016-09-19 16:04:39 -070090/*
Duncan Laurie510cb6a2016-09-19 17:05:45 -070091 * cr50_i2c_read() - read from TPM register
Duncan Laurie2ea13c82016-09-19 16:04:39 -070092 *
Duncan Laurie510cb6a2016-09-19 17:05:45 -070093 * @chip: TPM chip information
Duncan Laurie2ea13c82016-09-19 16:04:39 -070094 * @addr: register address to read from
95 * @buffer: provided by caller
96 * @len: number of bytes to read
97 *
98 * 1) send register address byte 'addr' to the TPM
99 * 2) wait for TPM to indicate it is ready
100 * 3) read 'len' bytes of TPM response into the provided 'buffer'
101 *
102 * Return -1 on error, 0 on success.
103 */
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700104static int cr50_i2c_read(struct tpm_chip *chip, uint8_t addr,
105 uint8_t *buffer, size_t len)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700106{
107 struct tpm_inf_dev *tpm_dev = car_get_var_ptr(&g_tpm_dev);
108
109 if (tpm_dev->addr == 0)
110 return -1;
111
Duncan Laurie94cc4852016-09-19 17:22:10 -0700112 /* Clear interrupt before starting transaction */
113 cr50_i2c_clear_tpm_irq(chip);
114
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700115 /* Send the register address byte to the TPM */
116 if (i2c_write_raw(tpm_dev->bus, tpm_dev->addr, &addr, 1)) {
117 printk(BIOS_ERR, "%s: Address write failed\n", __func__);
118 return -1;
119 }
120
121 /* Wait for TPM to be ready with response data */
Duncan Laurie94cc4852016-09-19 17:22:10 -0700122 if (cr50_i2c_wait_tpm_ready(chip) < 0)
123 return -1;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700124
125 /* Read response data from the TPM */
126 if (i2c_read_raw(tpm_dev->bus, tpm_dev->addr, buffer, len)) {
127 printk(BIOS_ERR, "%s: Read response failed\n", __func__);
128 return -1;
129 }
130
131 return 0;
132}
133
134/*
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700135 * cr50_i2c_write() - write to TPM register
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700136 *
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700137 * @chip: TPM chip information
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700138 * @addr: register address to write to
139 * @buffer: data to write
140 * @len: number of bytes to write
141 *
142 * 1) prepend the provided address to the provided data
143 * 2) send the address+data to the TPM
144 * 3) wait for TPM to indicate it is done writing
145 *
146 * Returns -1 on error, 0 on success.
147 */
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700148static int cr50_i2c_write(struct tpm_chip *chip,
149 uint8_t addr, uint8_t *buffer, size_t len)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700150{
151 struct tpm_inf_dev *tpm_dev = car_get_var_ptr(&g_tpm_dev);
152
153 if (tpm_dev->addr == 0)
154 return -1;
Duncan Laurie3727a8d2016-09-19 16:37:46 -0700155 if (len > CR50_MAX_BUFSIZE)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700156 return -1;
157
158 /* Prepend the 'register address' to the buffer */
159 tpm_dev->buf[0] = addr;
160 memcpy(tpm_dev->buf + 1, buffer, len);
161
Duncan Laurie94cc4852016-09-19 17:22:10 -0700162 /* Clear interrupt before starting transaction */
163 cr50_i2c_clear_tpm_irq(chip);
164
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700165 /* Send write request buffer with address */
166 if (i2c_write_raw(tpm_dev->bus, tpm_dev->addr, tpm_dev->buf, len + 1)) {
167 printk(BIOS_ERR, "%s: Error writing to TPM\n", __func__);
168 return -1;
169 }
170
171 /* Wait for TPM to be ready */
Duncan Laurie94cc4852016-09-19 17:22:10 -0700172 return cr50_i2c_wait_tpm_ready(chip);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700173}
174
175static int check_locality(struct tpm_chip *chip, int loc)
176{
Duncan Lauried3920e72016-09-19 17:09:01 -0700177 uint8_t mask = TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700178 uint8_t buf;
179
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700180 if (cr50_i2c_read(chip, TPM_ACCESS(loc), &buf, 1) < 0)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700181 return -1;
182
Duncan Lauried3920e72016-09-19 17:09:01 -0700183 if ((buf & mask) == mask) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700184 chip->vendor.locality = loc;
185 return loc;
186 }
187
188 return -1;
189}
190
Duncan Lauried3920e72016-09-19 17:09:01 -0700191static void release_locality(struct tpm_chip *chip, int force)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700192{
Duncan Lauried3920e72016-09-19 17:09:01 -0700193 uint8_t mask = TPM_ACCESS_VALID | TPM_ACCESS_REQUEST_PENDING;
194 uint8_t addr = TPM_ACCESS(chip->vendor.locality);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700195 uint8_t buf;
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700196
Duncan Lauried3920e72016-09-19 17:09:01 -0700197 if (cr50_i2c_read(chip, addr, &buf, 1) < 0)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700198 return;
199
Duncan Lauried3920e72016-09-19 17:09:01 -0700200 if (force || (buf & mask) == mask) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700201 buf = TPM_ACCESS_ACTIVE_LOCALITY;
Duncan Lauried3920e72016-09-19 17:09:01 -0700202 cr50_i2c_write(chip, addr, &buf, 1);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700203 }
Duncan Lauried3920e72016-09-19 17:09:01 -0700204
205 chip->vendor.locality = 0;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700206}
207
208static int request_locality(struct tpm_chip *chip, int loc)
209{
210 uint8_t buf = TPM_ACCESS_REQUEST_USE;
Duncan Lauried3920e72016-09-19 17:09:01 -0700211 struct stopwatch sw;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700212
213 if (check_locality(chip, loc) >= 0)
Duncan Lauried3920e72016-09-19 17:09:01 -0700214 return loc;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700215
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700216 if (cr50_i2c_write(chip, TPM_ACCESS(loc), &buf, 1) < 0)
217 return -1;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700218
Duncan Lauried3920e72016-09-19 17:09:01 -0700219 stopwatch_init_msecs_expire(&sw, CR50_TIMEOUT_LONG_MS);
220 while (check_locality(chip, loc) < 0) {
221 if (stopwatch_expired(&sw))
222 return -1;
223 mdelay(CR50_TIMEOUT_SHORT_MS);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700224 }
Duncan Lauried3920e72016-09-19 17:09:01 -0700225 return loc;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700226}
227
228/* cr50 requires all 4 bytes of status register to be read */
Duncan Laurief235a9b2016-09-19 17:19:10 -0700229static uint8_t cr50_i2c_tis_status(struct tpm_chip *chip)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700230{
231 uint8_t buf[4];
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700232 if (cr50_i2c_read(chip, TPM_STS(chip->vendor.locality),
233 buf, sizeof(buf)) < 0) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700234 printk(BIOS_ERR, "%s: Failed to read status\n", __func__);
235 return 0;
236 }
237 return buf[0];
238}
239
240/* cr50 requires all 4 bytes of status register to be written */
Duncan Laurief235a9b2016-09-19 17:19:10 -0700241static void cr50_i2c_tis_ready(struct tpm_chip *chip)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700242{
243 uint8_t buf[4] = { TPM_STS_COMMAND_READY };
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700244 cr50_i2c_write(chip, TPM_STS(chip->vendor.locality), buf, sizeof(buf));
Duncan Laurie1dc036c2016-09-19 16:49:23 -0700245 mdelay(CR50_TIMEOUT_SHORT_MS);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700246}
247
248/* cr50 uses bytes 3:2 of status register for burst count and
249 * all 4 bytes must be read */
Duncan Laurief235a9b2016-09-19 17:19:10 -0700250static int cr50_i2c_wait_burststs(struct tpm_chip *chip, uint8_t mask,
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700251 size_t *burst, int *status)
252{
253 uint8_t buf[4];
254 struct stopwatch sw;
255
Duncan Laurie1dc036c2016-09-19 16:49:23 -0700256 stopwatch_init_msecs_expire(&sw, CR50_TIMEOUT_LONG_MS);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700257
258 while (!stopwatch_expired(&sw)) {
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700259 if (cr50_i2c_read(chip, TPM_STS(chip->vendor.locality),
260 buf, sizeof(buf)) != 0) {
Duncan Laurie1dc036c2016-09-19 16:49:23 -0700261 mdelay(CR50_TIMEOUT_SHORT_MS);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700262 continue;
263 }
264
265 *status = buf[0];
266 *burst = read_le16(&buf[1]);
267
268 /* Check if mask matches and burst is valid */
269 if ((*status & mask) == mask &&
Duncan Laurie3727a8d2016-09-19 16:37:46 -0700270 *burst > 0 && *burst <= CR50_MAX_BUFSIZE)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700271 return 0;
272
Duncan Laurie1dc036c2016-09-19 16:49:23 -0700273 mdelay(CR50_TIMEOUT_SHORT_MS);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700274 }
275
276 printk(BIOS_ERR, "%s: Timeout reading burst and status\n", __func__);
277 return -1;
278}
279
Duncan Laurief235a9b2016-09-19 17:19:10 -0700280static int cr50_i2c_tis_recv(struct tpm_chip *chip, uint8_t *buf,
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700281 size_t buf_len)
282{
283 size_t burstcnt, current, len, expected;
284 uint8_t addr = TPM_DATA_FIFO(chip->vendor.locality);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700285 uint8_t mask = TPM_STS_VALID | TPM_STS_DATA_AVAIL;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700286 int status;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700287
288 if (buf_len < TPM_HEADER_SIZE)
Duncan Laurief235a9b2016-09-19 17:19:10 -0700289 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700290
Duncan Laurief235a9b2016-09-19 17:19:10 -0700291 if (cr50_i2c_wait_burststs(chip, mask, &burstcnt, &status) < 0) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700292 printk(BIOS_ERR, "%s: First chunk not available\n", __func__);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700293 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700294 }
295
296 /* Read first chunk of burstcnt bytes */
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700297 if (cr50_i2c_read(chip, addr, buf, burstcnt) != 0) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700298 printk(BIOS_ERR, "%s: Read failed\n", __func__);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700299 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700300 }
301
302 /* Determine expected data in the return buffer */
303 expected = read_be32(buf + TPM_RSP_SIZE_BYTE);
304 if (expected > buf_len) {
305 printk(BIOS_ERR, "%s: Too much data: %zu > %zu\n",
306 __func__, expected, buf_len);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700307 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700308 }
309
310 /* Now read the rest of the data */
311 current = burstcnt;
312 while (current < expected) {
313 /* Read updated burst count and check status */
Duncan Laurief235a9b2016-09-19 17:19:10 -0700314 if (cr50_i2c_wait_burststs(chip, mask, &burstcnt, &status) < 0)
315 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700316
317 len = min(burstcnt, expected - current);
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700318 if (cr50_i2c_read(chip, addr, buf + current, len) != 0) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700319 printk(BIOS_ERR, "%s: Read failed\n", __func__);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700320 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700321 }
322
323 current += len;
324 }
325
326 /* Ensure TPM is done reading data */
Duncan Laurief235a9b2016-09-19 17:19:10 -0700327 if (cr50_i2c_wait_burststs(chip, TPM_STS_VALID, &burstcnt, &status) < 0)
328 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700329 if (status & TPM_STS_DATA_AVAIL) {
330 printk(BIOS_ERR, "%s: Data still available\n", __func__);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700331 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700332 }
333
Duncan Laurief235a9b2016-09-19 17:19:10 -0700334 return current;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700335
Duncan Laurief235a9b2016-09-19 17:19:10 -0700336out_err:
337 /* Abort current transaction if still pending */
338 if (cr50_i2c_tis_status(chip) & TPM_STS_COMMAND_READY)
339 cr50_i2c_tis_ready(chip);
340 return -1;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700341}
342
Duncan Laurief235a9b2016-09-19 17:19:10 -0700343static int cr50_i2c_tis_send(struct tpm_chip *chip, uint8_t *buf, size_t len)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700344{
345 int status;
346 size_t burstcnt, limit, sent = 0;
347 uint8_t tpm_go[4] = { TPM_STS_GO };
348 struct stopwatch sw;
349
Duncan Laurie1dc036c2016-09-19 16:49:23 -0700350 stopwatch_init_msecs_expire(&sw, CR50_TIMEOUT_LONG_MS);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700351
352 /* Wait until TPM is ready for a command */
Duncan Laurief235a9b2016-09-19 17:19:10 -0700353 while (!(cr50_i2c_tis_status(chip) & TPM_STS_COMMAND_READY)) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700354 if (stopwatch_expired(&sw)) {
355 printk(BIOS_ERR, "%s: Command ready timeout\n",
356 __func__);
357 return -1;
358 }
359
Duncan Laurief235a9b2016-09-19 17:19:10 -0700360 cr50_i2c_tis_ready(chip);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700361 }
362
363 while (len > 0) {
Duncan Laurief235a9b2016-09-19 17:19:10 -0700364 uint8_t mask = TPM_STS_VALID;
365
366 /* Wait for data if this is not the first chunk */
367 if (sent > 0)
368 mask |= TPM_STS_DATA_EXPECT;
369
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700370 /* Read burst count and check status */
Duncan Laurief235a9b2016-09-19 17:19:10 -0700371 if (cr50_i2c_wait_burststs(chip, mask, &burstcnt, &status) < 0)
372 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700373
374 /* Use burstcnt - 1 to account for the address byte
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700375 * that is inserted by cr50_i2c_write() */
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700376 limit = min(burstcnt - 1, len);
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700377 if (cr50_i2c_write(chip, TPM_DATA_FIFO(chip->vendor.locality),
378 &buf[sent], limit) != 0) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700379 printk(BIOS_ERR, "%s: Write failed\n", __func__);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700380 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700381 }
382
383 sent += limit;
384 len -= limit;
385 }
386
387 /* Ensure TPM is not expecting more data */
Duncan Laurief235a9b2016-09-19 17:19:10 -0700388 if (cr50_i2c_wait_burststs(chip, TPM_STS_VALID, &burstcnt, &status) < 0)
389 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700390 if (status & TPM_STS_DATA_EXPECT) {
391 printk(BIOS_ERR, "%s: Data still expected\n", __func__);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700392 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700393 }
394
395 /* Start the TPM command */
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700396 if (cr50_i2c_write(chip, TPM_STS(chip->vendor.locality), tpm_go,
397 sizeof(tpm_go)) < 0) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700398 printk(BIOS_ERR, "%s: Start command failed\n", __func__);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700399 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700400 }
401 return sent;
402
Duncan Laurief235a9b2016-09-19 17:19:10 -0700403out_err:
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700404 /* Abort current transaction if still pending */
Duncan Laurief235a9b2016-09-19 17:19:10 -0700405 if (cr50_i2c_tis_status(chip) & TPM_STS_COMMAND_READY)
406 cr50_i2c_tis_ready(chip);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700407 return -1;
408}
409
410static void cr50_vendor_init(struct tpm_chip *chip)
411{
412 memset(&chip->vendor, 0, sizeof(struct tpm_vendor_specific));
413 chip->vendor.req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID;
414 chip->vendor.req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID;
415 chip->vendor.req_canceled = TPM_STS_COMMAND_READY;
Duncan Laurief235a9b2016-09-19 17:19:10 -0700416 chip->vendor.status = &cr50_i2c_tis_status;
417 chip->vendor.recv = &cr50_i2c_tis_recv;
418 chip->vendor.send = &cr50_i2c_tis_send;
419 chip->vendor.cancel = &cr50_i2c_tis_ready;
Duncan Laurie94cc4852016-09-19 17:22:10 -0700420 chip->vendor.irq = CONFIG_DRIVER_TPM_I2C_IRQ;
Duncan Laurie48f708d2016-10-03 12:43:14 -0700421
422 /*
423 * Interrupts are not supported this early in firmware,
424 * use use an arch-specific method to query for interrupt status.
425 */
426 if (chip->vendor.irq > 0) {
427#if IS_ENABLED(CONFIG_ARCH_X86)
428 /* Query GPE status for interrupt */
429 chip->vendor.irq_status = &acpi_get_gpe;
430#else
431 chip->vendor.irq = -1;
432#endif
433 }
Duncan Laurieed4fa092016-11-01 15:03:13 -0700434
435 if (chip->vendor.irq <= 0)
436 printk(BIOS_WARNING,
437 "%s: No IRQ, will use %ums delay for TPM ready\n",
438 __func__, CR50_TIMEOUT_NOIRQ_MS);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700439}
440
Lee Leahy52ab30b2017-03-15 09:22:11 -0700441int tpm_vendor_probe(unsigned int bus, uint32_t addr)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700442{
443 struct tpm_inf_dev *tpm_dev = car_get_var_ptr(&g_tpm_dev);
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700444 struct tpm_chip probe_chip;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700445 struct stopwatch sw;
446 uint8_t buf = 0;
447 int ret;
Duncan Laurie1dc036c2016-09-19 16:49:23 -0700448 long sw_run_duration = CR50_TIMEOUT_LONG_MS;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700449
450 tpm_dev->bus = bus;
451 tpm_dev->addr = addr;
452
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700453 cr50_vendor_init(&probe_chip);
454
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700455 /* Wait for TPM_ACCESS register ValidSts bit to be set */
456 stopwatch_init_msecs_expire(&sw, sw_run_duration);
457 do {
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700458 ret = cr50_i2c_read(&probe_chip, TPM_ACCESS(0), &buf, 1);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700459 if (!ret && (buf & TPM_STS_VALID)) {
460 sw_run_duration = stopwatch_duration_msecs(&sw);
461 break;
462 }
Duncan Laurie1dc036c2016-09-19 16:49:23 -0700463 mdelay(CR50_TIMEOUT_SHORT_MS);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700464 } while (!stopwatch_expired(&sw));
465
466 printk(BIOS_INFO,
467 "%s: ValidSts bit %s(%d) in TPM_ACCESS register after %ld ms\n",
468 __func__, (buf & TPM_STS_VALID) ? "set" : "clear",
469 (buf & TPM_STS_VALID) >> 7, sw_run_duration);
470
471 /* Claim failure if the ValidSts (bit 7) is clear */
472 if (!(buf & TPM_STS_VALID))
473 return -1;
474
475 return 0;
476}
477
Lee Leahy52ab30b2017-03-15 09:22:11 -0700478int tpm_vendor_init(struct tpm_chip *chip, unsigned int bus, uint32_t dev_addr)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700479{
480 struct tpm_inf_dev *tpm_dev = car_get_var_ptr(&g_tpm_dev);
481 uint32_t vendor;
482
483 if (dev_addr == 0) {
484 printk(BIOS_ERR, "%s: missing device address\n", __func__);
485 return -1;
486 }
487
488 tpm_dev->bus = bus;
489 tpm_dev->addr = dev_addr;
490
491 cr50_vendor_init(chip);
492
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700493 if (request_locality(chip, 0) != 0)
494 return -1;
495
496 /* Read four bytes from DID_VID register */
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700497 if (cr50_i2c_read(chip, TPM_DID_VID(0), (uint8_t *)&vendor, 4) < 0)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700498 goto out_err;
499
500 if (vendor != CR50_DID_VID) {
501 printk(BIOS_DEBUG, "Vendor ID 0x%08x not recognized\n", vendor);
502 goto out_err;
503 }
504
Duncan Laurie94cc4852016-09-19 17:22:10 -0700505 printk(BIOS_DEBUG, "cr50 TPM 2.0 (i2c %u:0x%02x irq %d id 0x%x)\n",
506 bus, dev_addr, chip->vendor.irq, vendor >> 16);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700507
508 chip->is_open = 1;
509 return 0;
510
511out_err:
Duncan Lauried3920e72016-09-19 17:09:01 -0700512 release_locality(chip, 1);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700513 return -1;
514}
515
516void tpm_vendor_cleanup(struct tpm_chip *chip)
517{
Duncan Lauried3920e72016-09-19 17:09:01 -0700518 release_locality(chip, 1);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700519}