blob: ce30bf1b7cd683bf4f3d8d268cce69fb0e00c700 [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>
Furquan Shaikh260b2972017-04-07 13:26:01 -070042#include <tpm.h>
Duncan Laurie2ea13c82016-09-19 16:04:39 -070043#include "tpm.h"
44
Duncan Laurie94cc4852016-09-19 17:22:10 -070045#if IS_ENABLED(CONFIG_ARCH_X86)
46#include <arch/acpi.h>
47#endif
Duncan Laurie2ea13c82016-09-19 16:04:39 -070048
Duncan Laurie3727a8d2016-09-19 16:37:46 -070049#define CR50_MAX_BUFSIZE 63
Duncan Laurie1dc036c2016-09-19 16:49:23 -070050#define CR50_TIMEOUT_LONG_MS 2000 /* Long timeout while waiting for TPM */
51#define CR50_TIMEOUT_SHORT_MS 2 /* Short timeout during transactions */
Duncan Laurieed4fa092016-11-01 15:03:13 -070052#define CR50_TIMEOUT_NOIRQ_MS 20 /* Timeout for TPM ready without IRQ */
53#define CR50_TIMEOUT_IRQ_MS 100 /* Timeout for TPM ready with IRQ */
Duncan Laurie2ea13c82016-09-19 16:04:39 -070054#define CR50_DID_VID 0x00281ae0L
55
56struct tpm_inf_dev {
57 int bus;
58 unsigned int addr;
Duncan Laurie3727a8d2016-09-19 16:37:46 -070059 uint8_t buf[CR50_MAX_BUFSIZE + sizeof(uint8_t)];
Duncan Laurie2ea13c82016-09-19 16:04:39 -070060};
61
62static struct tpm_inf_dev g_tpm_dev CAR_GLOBAL;
63
Duncan Laurie94cc4852016-09-19 17:22:10 -070064/* Wait for interrupt to indicate the TPM is ready */
65static int cr50_i2c_wait_tpm_ready(struct tpm_chip *chip)
66{
67 struct stopwatch sw;
68
69 if (!chip->vendor.irq_status) {
70 /* Fixed delay if interrupt not supported */
Duncan Laurieed4fa092016-11-01 15:03:13 -070071 mdelay(CR50_TIMEOUT_NOIRQ_MS);
Duncan Laurie94cc4852016-09-19 17:22:10 -070072 return 0;
73 }
74
Duncan Laurieed4fa092016-11-01 15:03:13 -070075 stopwatch_init_msecs_expire(&sw, CR50_TIMEOUT_IRQ_MS);
Duncan Laurie94cc4852016-09-19 17:22:10 -070076
77 while (!chip->vendor.irq_status(chip->vendor.irq))
78 if (stopwatch_expired(&sw))
79 return -1;
80
81 return 0;
82}
83
84/* Clear pending interrupts */
85static void cr50_i2c_clear_tpm_irq(struct tpm_chip *chip)
86{
87 if (chip->vendor.irq_status)
88 chip->vendor.irq_status(chip->vendor.irq);
89}
90
Duncan Laurie2ea13c82016-09-19 16:04:39 -070091/*
Duncan Laurie510cb6a2016-09-19 17:05:45 -070092 * cr50_i2c_read() - read from TPM register
Duncan Laurie2ea13c82016-09-19 16:04:39 -070093 *
Duncan Laurie510cb6a2016-09-19 17:05:45 -070094 * @chip: TPM chip information
Duncan Laurie2ea13c82016-09-19 16:04:39 -070095 * @addr: register address to read from
96 * @buffer: provided by caller
97 * @len: number of bytes to read
98 *
99 * 1) send register address byte 'addr' to the TPM
100 * 2) wait for TPM to indicate it is ready
101 * 3) read 'len' bytes of TPM response into the provided 'buffer'
102 *
103 * Return -1 on error, 0 on success.
104 */
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700105static int cr50_i2c_read(struct tpm_chip *chip, uint8_t addr,
106 uint8_t *buffer, size_t len)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700107{
108 struct tpm_inf_dev *tpm_dev = car_get_var_ptr(&g_tpm_dev);
109
110 if (tpm_dev->addr == 0)
111 return -1;
112
Duncan Laurie94cc4852016-09-19 17:22:10 -0700113 /* Clear interrupt before starting transaction */
114 cr50_i2c_clear_tpm_irq(chip);
115
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700116 /* Send the register address byte to the TPM */
117 if (i2c_write_raw(tpm_dev->bus, tpm_dev->addr, &addr, 1)) {
118 printk(BIOS_ERR, "%s: Address write failed\n", __func__);
119 return -1;
120 }
121
122 /* Wait for TPM to be ready with response data */
Duncan Laurie94cc4852016-09-19 17:22:10 -0700123 if (cr50_i2c_wait_tpm_ready(chip) < 0)
124 return -1;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700125
126 /* Read response data from the TPM */
127 if (i2c_read_raw(tpm_dev->bus, tpm_dev->addr, buffer, len)) {
128 printk(BIOS_ERR, "%s: Read response failed\n", __func__);
129 return -1;
130 }
131
132 return 0;
133}
134
135/*
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700136 * cr50_i2c_write() - write to TPM register
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700137 *
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700138 * @chip: TPM chip information
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700139 * @addr: register address to write to
140 * @buffer: data to write
141 * @len: number of bytes to write
142 *
143 * 1) prepend the provided address to the provided data
144 * 2) send the address+data to the TPM
145 * 3) wait for TPM to indicate it is done writing
146 *
147 * Returns -1 on error, 0 on success.
148 */
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700149static int cr50_i2c_write(struct tpm_chip *chip,
150 uint8_t addr, uint8_t *buffer, size_t len)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700151{
152 struct tpm_inf_dev *tpm_dev = car_get_var_ptr(&g_tpm_dev);
153
154 if (tpm_dev->addr == 0)
155 return -1;
Duncan Laurie3727a8d2016-09-19 16:37:46 -0700156 if (len > CR50_MAX_BUFSIZE)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700157 return -1;
158
159 /* Prepend the 'register address' to the buffer */
160 tpm_dev->buf[0] = addr;
161 memcpy(tpm_dev->buf + 1, buffer, len);
162
Duncan Laurie94cc4852016-09-19 17:22:10 -0700163 /* Clear interrupt before starting transaction */
164 cr50_i2c_clear_tpm_irq(chip);
165
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700166 /* Send write request buffer with address */
167 if (i2c_write_raw(tpm_dev->bus, tpm_dev->addr, tpm_dev->buf, len + 1)) {
168 printk(BIOS_ERR, "%s: Error writing to TPM\n", __func__);
169 return -1;
170 }
171
172 /* Wait for TPM to be ready */
Duncan Laurie94cc4852016-09-19 17:22:10 -0700173 return cr50_i2c_wait_tpm_ready(chip);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700174}
175
176static int check_locality(struct tpm_chip *chip, int loc)
177{
Duncan Lauried3920e72016-09-19 17:09:01 -0700178 uint8_t mask = TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700179 uint8_t buf;
180
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700181 if (cr50_i2c_read(chip, TPM_ACCESS(loc), &buf, 1) < 0)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700182 return -1;
183
Duncan Lauried3920e72016-09-19 17:09:01 -0700184 if ((buf & mask) == mask) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700185 chip->vendor.locality = loc;
186 return loc;
187 }
188
189 return -1;
190}
191
Duncan Lauried3920e72016-09-19 17:09:01 -0700192static void release_locality(struct tpm_chip *chip, int force)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700193{
Duncan Lauried3920e72016-09-19 17:09:01 -0700194 uint8_t mask = TPM_ACCESS_VALID | TPM_ACCESS_REQUEST_PENDING;
195 uint8_t addr = TPM_ACCESS(chip->vendor.locality);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700196 uint8_t buf;
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700197
Duncan Lauried3920e72016-09-19 17:09:01 -0700198 if (cr50_i2c_read(chip, addr, &buf, 1) < 0)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700199 return;
200
Duncan Lauried3920e72016-09-19 17:09:01 -0700201 if (force || (buf & mask) == mask) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700202 buf = TPM_ACCESS_ACTIVE_LOCALITY;
Duncan Lauried3920e72016-09-19 17:09:01 -0700203 cr50_i2c_write(chip, addr, &buf, 1);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700204 }
Duncan Lauried3920e72016-09-19 17:09:01 -0700205
206 chip->vendor.locality = 0;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700207}
208
209static int request_locality(struct tpm_chip *chip, int loc)
210{
211 uint8_t buf = TPM_ACCESS_REQUEST_USE;
Duncan Lauried3920e72016-09-19 17:09:01 -0700212 struct stopwatch sw;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700213
214 if (check_locality(chip, loc) >= 0)
Duncan Lauried3920e72016-09-19 17:09:01 -0700215 return loc;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700216
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700217 if (cr50_i2c_write(chip, TPM_ACCESS(loc), &buf, 1) < 0)
218 return -1;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700219
Duncan Lauried3920e72016-09-19 17:09:01 -0700220 stopwatch_init_msecs_expire(&sw, CR50_TIMEOUT_LONG_MS);
221 while (check_locality(chip, loc) < 0) {
222 if (stopwatch_expired(&sw))
223 return -1;
224 mdelay(CR50_TIMEOUT_SHORT_MS);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700225 }
Duncan Lauried3920e72016-09-19 17:09:01 -0700226 return loc;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700227}
228
229/* cr50 requires all 4 bytes of status register to be read */
Duncan Laurief235a9b2016-09-19 17:19:10 -0700230static uint8_t cr50_i2c_tis_status(struct tpm_chip *chip)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700231{
232 uint8_t buf[4];
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700233 if (cr50_i2c_read(chip, TPM_STS(chip->vendor.locality),
234 buf, sizeof(buf)) < 0) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700235 printk(BIOS_ERR, "%s: Failed to read status\n", __func__);
236 return 0;
237 }
238 return buf[0];
239}
240
241/* cr50 requires all 4 bytes of status register to be written */
Duncan Laurief235a9b2016-09-19 17:19:10 -0700242static void cr50_i2c_tis_ready(struct tpm_chip *chip)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700243{
244 uint8_t buf[4] = { TPM_STS_COMMAND_READY };
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700245 cr50_i2c_write(chip, TPM_STS(chip->vendor.locality), buf, sizeof(buf));
Duncan Laurie1dc036c2016-09-19 16:49:23 -0700246 mdelay(CR50_TIMEOUT_SHORT_MS);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700247}
248
249/* cr50 uses bytes 3:2 of status register for burst count and
250 * all 4 bytes must be read */
Duncan Laurief235a9b2016-09-19 17:19:10 -0700251static int cr50_i2c_wait_burststs(struct tpm_chip *chip, uint8_t mask,
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700252 size_t *burst, int *status)
253{
254 uint8_t buf[4];
255 struct stopwatch sw;
256
Duncan Laurie1dc036c2016-09-19 16:49:23 -0700257 stopwatch_init_msecs_expire(&sw, CR50_TIMEOUT_LONG_MS);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700258
259 while (!stopwatch_expired(&sw)) {
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700260 if (cr50_i2c_read(chip, TPM_STS(chip->vendor.locality),
261 buf, sizeof(buf)) != 0) {
Duncan Laurie1dc036c2016-09-19 16:49:23 -0700262 mdelay(CR50_TIMEOUT_SHORT_MS);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700263 continue;
264 }
265
266 *status = buf[0];
267 *burst = read_le16(&buf[1]);
268
269 /* Check if mask matches and burst is valid */
270 if ((*status & mask) == mask &&
Duncan Laurie3727a8d2016-09-19 16:37:46 -0700271 *burst > 0 && *burst <= CR50_MAX_BUFSIZE)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700272 return 0;
273
Duncan Laurie1dc036c2016-09-19 16:49:23 -0700274 mdelay(CR50_TIMEOUT_SHORT_MS);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700275 }
276
277 printk(BIOS_ERR, "%s: Timeout reading burst and status\n", __func__);
278 return -1;
279}
280
Duncan Laurief235a9b2016-09-19 17:19:10 -0700281static int cr50_i2c_tis_recv(struct tpm_chip *chip, uint8_t *buf,
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700282 size_t buf_len)
283{
284 size_t burstcnt, current, len, expected;
285 uint8_t addr = TPM_DATA_FIFO(chip->vendor.locality);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700286 uint8_t mask = TPM_STS_VALID | TPM_STS_DATA_AVAIL;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700287 int status;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700288
289 if (buf_len < TPM_HEADER_SIZE)
Duncan Laurief235a9b2016-09-19 17:19:10 -0700290 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700291
Duncan Laurief235a9b2016-09-19 17:19:10 -0700292 if (cr50_i2c_wait_burststs(chip, mask, &burstcnt, &status) < 0) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700293 printk(BIOS_ERR, "%s: First chunk not available\n", __func__);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700294 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700295 }
296
297 /* Read first chunk of burstcnt bytes */
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700298 if (cr50_i2c_read(chip, addr, buf, burstcnt) != 0) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700299 printk(BIOS_ERR, "%s: Read failed\n", __func__);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700300 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700301 }
302
303 /* Determine expected data in the return buffer */
304 expected = read_be32(buf + TPM_RSP_SIZE_BYTE);
305 if (expected > buf_len) {
306 printk(BIOS_ERR, "%s: Too much data: %zu > %zu\n",
307 __func__, expected, buf_len);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700308 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700309 }
310
311 /* Now read the rest of the data */
312 current = burstcnt;
313 while (current < expected) {
314 /* Read updated burst count and check status */
Duncan Laurief235a9b2016-09-19 17:19:10 -0700315 if (cr50_i2c_wait_burststs(chip, mask, &burstcnt, &status) < 0)
316 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700317
318 len = min(burstcnt, expected - current);
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700319 if (cr50_i2c_read(chip, addr, buf + current, len) != 0) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700320 printk(BIOS_ERR, "%s: Read failed\n", __func__);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700321 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700322 }
323
324 current += len;
325 }
326
327 /* Ensure TPM is done reading data */
Duncan Laurief235a9b2016-09-19 17:19:10 -0700328 if (cr50_i2c_wait_burststs(chip, TPM_STS_VALID, &burstcnt, &status) < 0)
329 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700330 if (status & TPM_STS_DATA_AVAIL) {
331 printk(BIOS_ERR, "%s: Data still available\n", __func__);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700332 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700333 }
334
Duncan Laurief235a9b2016-09-19 17:19:10 -0700335 return current;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700336
Duncan Laurief235a9b2016-09-19 17:19:10 -0700337out_err:
338 /* Abort current transaction if still pending */
339 if (cr50_i2c_tis_status(chip) & TPM_STS_COMMAND_READY)
340 cr50_i2c_tis_ready(chip);
341 return -1;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700342}
343
Duncan Laurief235a9b2016-09-19 17:19:10 -0700344static int cr50_i2c_tis_send(struct tpm_chip *chip, uint8_t *buf, size_t len)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700345{
346 int status;
347 size_t burstcnt, limit, sent = 0;
348 uint8_t tpm_go[4] = { TPM_STS_GO };
349 struct stopwatch sw;
350
Duncan Laurie1dc036c2016-09-19 16:49:23 -0700351 stopwatch_init_msecs_expire(&sw, CR50_TIMEOUT_LONG_MS);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700352
353 /* Wait until TPM is ready for a command */
Duncan Laurief235a9b2016-09-19 17:19:10 -0700354 while (!(cr50_i2c_tis_status(chip) & TPM_STS_COMMAND_READY)) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700355 if (stopwatch_expired(&sw)) {
356 printk(BIOS_ERR, "%s: Command ready timeout\n",
357 __func__);
358 return -1;
359 }
360
Duncan Laurief235a9b2016-09-19 17:19:10 -0700361 cr50_i2c_tis_ready(chip);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700362 }
363
364 while (len > 0) {
Duncan Laurief235a9b2016-09-19 17:19:10 -0700365 uint8_t mask = TPM_STS_VALID;
366
367 /* Wait for data if this is not the first chunk */
368 if (sent > 0)
369 mask |= TPM_STS_DATA_EXPECT;
370
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700371 /* Read burst count and check status */
Duncan Laurief235a9b2016-09-19 17:19:10 -0700372 if (cr50_i2c_wait_burststs(chip, mask, &burstcnt, &status) < 0)
373 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700374
375 /* Use burstcnt - 1 to account for the address byte
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700376 * that is inserted by cr50_i2c_write() */
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700377 limit = min(burstcnt - 1, len);
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700378 if (cr50_i2c_write(chip, TPM_DATA_FIFO(chip->vendor.locality),
379 &buf[sent], limit) != 0) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700380 printk(BIOS_ERR, "%s: Write failed\n", __func__);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700381 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700382 }
383
384 sent += limit;
385 len -= limit;
386 }
387
388 /* Ensure TPM is not expecting more data */
Duncan Laurief235a9b2016-09-19 17:19:10 -0700389 if (cr50_i2c_wait_burststs(chip, TPM_STS_VALID, &burstcnt, &status) < 0)
390 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700391 if (status & TPM_STS_DATA_EXPECT) {
392 printk(BIOS_ERR, "%s: Data still expected\n", __func__);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700393 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700394 }
395
396 /* Start the TPM command */
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700397 if (cr50_i2c_write(chip, TPM_STS(chip->vendor.locality), tpm_go,
398 sizeof(tpm_go)) < 0) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700399 printk(BIOS_ERR, "%s: Start command failed\n", __func__);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700400 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700401 }
402 return sent;
403
Duncan Laurief235a9b2016-09-19 17:19:10 -0700404out_err:
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700405 /* Abort current transaction if still pending */
Duncan Laurief235a9b2016-09-19 17:19:10 -0700406 if (cr50_i2c_tis_status(chip) & TPM_STS_COMMAND_READY)
407 cr50_i2c_tis_ready(chip);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700408 return -1;
409}
410
411static void cr50_vendor_init(struct tpm_chip *chip)
412{
413 memset(&chip->vendor, 0, sizeof(struct tpm_vendor_specific));
414 chip->vendor.req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID;
415 chip->vendor.req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID;
416 chip->vendor.req_canceled = TPM_STS_COMMAND_READY;
Duncan Laurief235a9b2016-09-19 17:19:10 -0700417 chip->vendor.status = &cr50_i2c_tis_status;
418 chip->vendor.recv = &cr50_i2c_tis_recv;
419 chip->vendor.send = &cr50_i2c_tis_send;
420 chip->vendor.cancel = &cr50_i2c_tis_ready;
Duncan Laurie94cc4852016-09-19 17:22:10 -0700421 chip->vendor.irq = CONFIG_DRIVER_TPM_I2C_IRQ;
Duncan Laurie48f708d2016-10-03 12:43:14 -0700422
423 /*
424 * Interrupts are not supported this early in firmware,
425 * use use an arch-specific method to query for interrupt status.
426 */
427 if (chip->vendor.irq > 0) {
428#if IS_ENABLED(CONFIG_ARCH_X86)
429 /* Query GPE status for interrupt */
430 chip->vendor.irq_status = &acpi_get_gpe;
431#else
432 chip->vendor.irq = -1;
433#endif
434 }
Duncan Laurieed4fa092016-11-01 15:03:13 -0700435
436 if (chip->vendor.irq <= 0)
437 printk(BIOS_WARNING,
438 "%s: No IRQ, will use %ums delay for TPM ready\n",
439 __func__, CR50_TIMEOUT_NOIRQ_MS);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700440}
441
Lee Leahy52ab30b2017-03-15 09:22:11 -0700442int tpm_vendor_probe(unsigned int bus, uint32_t addr)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700443{
444 struct tpm_inf_dev *tpm_dev = car_get_var_ptr(&g_tpm_dev);
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700445 struct tpm_chip probe_chip;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700446 struct stopwatch sw;
447 uint8_t buf = 0;
448 int ret;
Duncan Laurie1dc036c2016-09-19 16:49:23 -0700449 long sw_run_duration = CR50_TIMEOUT_LONG_MS;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700450
451 tpm_dev->bus = bus;
452 tpm_dev->addr = addr;
453
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700454 cr50_vendor_init(&probe_chip);
455
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700456 /* Wait for TPM_ACCESS register ValidSts bit to be set */
457 stopwatch_init_msecs_expire(&sw, sw_run_duration);
458 do {
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700459 ret = cr50_i2c_read(&probe_chip, TPM_ACCESS(0), &buf, 1);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700460 if (!ret && (buf & TPM_STS_VALID)) {
461 sw_run_duration = stopwatch_duration_msecs(&sw);
462 break;
463 }
Duncan Laurie1dc036c2016-09-19 16:49:23 -0700464 mdelay(CR50_TIMEOUT_SHORT_MS);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700465 } while (!stopwatch_expired(&sw));
466
467 printk(BIOS_INFO,
468 "%s: ValidSts bit %s(%d) in TPM_ACCESS register after %ld ms\n",
469 __func__, (buf & TPM_STS_VALID) ? "set" : "clear",
470 (buf & TPM_STS_VALID) >> 7, sw_run_duration);
471
472 /* Claim failure if the ValidSts (bit 7) is clear */
473 if (!(buf & TPM_STS_VALID))
474 return -1;
475
476 return 0;
477}
478
Lee Leahy52ab30b2017-03-15 09:22:11 -0700479int tpm_vendor_init(struct tpm_chip *chip, unsigned int bus, uint32_t dev_addr)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700480{
481 struct tpm_inf_dev *tpm_dev = car_get_var_ptr(&g_tpm_dev);
482 uint32_t vendor;
483
484 if (dev_addr == 0) {
485 printk(BIOS_ERR, "%s: missing device address\n", __func__);
486 return -1;
487 }
488
489 tpm_dev->bus = bus;
490 tpm_dev->addr = dev_addr;
491
492 cr50_vendor_init(chip);
493
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700494 if (request_locality(chip, 0) != 0)
495 return -1;
496
497 /* Read four bytes from DID_VID register */
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700498 if (cr50_i2c_read(chip, TPM_DID_VID(0), (uint8_t *)&vendor, 4) < 0)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700499 goto out_err;
500
501 if (vendor != CR50_DID_VID) {
502 printk(BIOS_DEBUG, "Vendor ID 0x%08x not recognized\n", vendor);
503 goto out_err;
504 }
505
Duncan Laurie94cc4852016-09-19 17:22:10 -0700506 printk(BIOS_DEBUG, "cr50 TPM 2.0 (i2c %u:0x%02x irq %d id 0x%x)\n",
507 bus, dev_addr, chip->vendor.irq, vendor >> 16);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700508
509 chip->is_open = 1;
510 return 0;
511
512out_err:
Duncan Lauried3920e72016-09-19 17:09:01 -0700513 release_locality(chip, 1);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700514 return -1;
515}
516
517void tpm_vendor_cleanup(struct tpm_chip *chip)
518{
Duncan Lauried3920e72016-09-19 17:09:01 -0700519 release_locality(chip, 1);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700520}