blob: aa59f1e9178f4b123e567d0ba1947a07f4c8027e [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 Laurie3727a8d2016-09-19 16:37:46 -070045#define CR50_MAX_BUFSIZE 63
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
Daniel Kurtzc4852e72017-04-21 14:11:51 +080060__attribute__ ((weak)) int tis_plat_irq_status(void)
61{
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())
Duncan Laurie94cc4852016-09-19 17:22:10 -070081 if (stopwatch_expired(&sw))
82 return -1;
83
84 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{
104 struct tpm_inf_dev *tpm_dev = car_get_var_ptr(&g_tpm_dev);
105
106 if (tpm_dev->addr == 0)
107 return -1;
108
Duncan Laurie94cc4852016-09-19 17:22:10 -0700109 /* Clear interrupt before starting transaction */
Daniel Kurtzc4852e72017-04-21 14:11:51 +0800110 tis_plat_irq_status();
Duncan Laurie94cc4852016-09-19 17:22:10 -0700111
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700112 /* Send the register address byte to the TPM */
113 if (i2c_write_raw(tpm_dev->bus, tpm_dev->addr, &addr, 1)) {
114 printk(BIOS_ERR, "%s: Address write failed\n", __func__);
115 return -1;
116 }
117
118 /* Wait for TPM to be ready with response data */
Duncan Laurie94cc4852016-09-19 17:22:10 -0700119 if (cr50_i2c_wait_tpm_ready(chip) < 0)
120 return -1;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700121
122 /* Read response data from the TPM */
123 if (i2c_read_raw(tpm_dev->bus, tpm_dev->addr, buffer, len)) {
124 printk(BIOS_ERR, "%s: Read response failed\n", __func__);
125 return -1;
126 }
127
128 return 0;
129}
130
131/*
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700132 * cr50_i2c_write() - write to TPM register
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700133 *
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700134 * @chip: TPM chip information
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700135 * @addr: register address to write to
136 * @buffer: data to write
137 * @len: number of bytes to write
138 *
139 * 1) prepend the provided address to the provided data
140 * 2) send the address+data to the TPM
141 * 3) wait for TPM to indicate it is done writing
142 *
143 * Returns -1 on error, 0 on success.
144 */
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700145static int cr50_i2c_write(struct tpm_chip *chip,
146 uint8_t addr, uint8_t *buffer, size_t len)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700147{
148 struct tpm_inf_dev *tpm_dev = car_get_var_ptr(&g_tpm_dev);
149
150 if (tpm_dev->addr == 0)
151 return -1;
Duncan Laurie3727a8d2016-09-19 16:37:46 -0700152 if (len > CR50_MAX_BUFSIZE)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700153 return -1;
154
155 /* Prepend the 'register address' to the buffer */
156 tpm_dev->buf[0] = addr;
157 memcpy(tpm_dev->buf + 1, buffer, len);
158
Duncan Laurie94cc4852016-09-19 17:22:10 -0700159 /* Clear interrupt before starting transaction */
Daniel Kurtzc4852e72017-04-21 14:11:51 +0800160 tis_plat_irq_status();
Duncan Laurie94cc4852016-09-19 17:22:10 -0700161
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700162 /* Send write request buffer with address */
163 if (i2c_write_raw(tpm_dev->bus, tpm_dev->addr, tpm_dev->buf, len + 1)) {
164 printk(BIOS_ERR, "%s: Error writing to TPM\n", __func__);
165 return -1;
166 }
167
168 /* Wait for TPM to be ready */
Duncan Laurie94cc4852016-09-19 17:22:10 -0700169 return cr50_i2c_wait_tpm_ready(chip);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700170}
171
172static int check_locality(struct tpm_chip *chip, int loc)
173{
Duncan Lauried3920e72016-09-19 17:09:01 -0700174 uint8_t mask = TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700175 uint8_t buf;
176
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700177 if (cr50_i2c_read(chip, TPM_ACCESS(loc), &buf, 1) < 0)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700178 return -1;
179
Duncan Lauried3920e72016-09-19 17:09:01 -0700180 if ((buf & mask) == mask) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700181 chip->vendor.locality = loc;
182 return loc;
183 }
184
185 return -1;
186}
187
Duncan Lauried3920e72016-09-19 17:09:01 -0700188static void release_locality(struct tpm_chip *chip, int force)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700189{
Duncan Lauried3920e72016-09-19 17:09:01 -0700190 uint8_t mask = TPM_ACCESS_VALID | TPM_ACCESS_REQUEST_PENDING;
191 uint8_t addr = TPM_ACCESS(chip->vendor.locality);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700192 uint8_t buf;
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700193
Duncan Lauried3920e72016-09-19 17:09:01 -0700194 if (cr50_i2c_read(chip, addr, &buf, 1) < 0)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700195 return;
196
Duncan Lauried3920e72016-09-19 17:09:01 -0700197 if (force || (buf & mask) == mask) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700198 buf = TPM_ACCESS_ACTIVE_LOCALITY;
Duncan Lauried3920e72016-09-19 17:09:01 -0700199 cr50_i2c_write(chip, addr, &buf, 1);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700200 }
Duncan Lauried3920e72016-09-19 17:09:01 -0700201
202 chip->vendor.locality = 0;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700203}
204
205static int request_locality(struct tpm_chip *chip, int loc)
206{
207 uint8_t buf = TPM_ACCESS_REQUEST_USE;
Duncan Lauried3920e72016-09-19 17:09:01 -0700208 struct stopwatch sw;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700209
210 if (check_locality(chip, loc) >= 0)
Duncan Lauried3920e72016-09-19 17:09:01 -0700211 return loc;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700212
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700213 if (cr50_i2c_write(chip, TPM_ACCESS(loc), &buf, 1) < 0)
214 return -1;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700215
Duncan Lauried3920e72016-09-19 17:09:01 -0700216 stopwatch_init_msecs_expire(&sw, CR50_TIMEOUT_LONG_MS);
217 while (check_locality(chip, loc) < 0) {
218 if (stopwatch_expired(&sw))
219 return -1;
220 mdelay(CR50_TIMEOUT_SHORT_MS);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700221 }
Duncan Lauried3920e72016-09-19 17:09:01 -0700222 return loc;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700223}
224
225/* cr50 requires all 4 bytes of status register to be read */
Duncan Laurief235a9b2016-09-19 17:19:10 -0700226static uint8_t cr50_i2c_tis_status(struct tpm_chip *chip)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700227{
228 uint8_t buf[4];
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700229 if (cr50_i2c_read(chip, TPM_STS(chip->vendor.locality),
230 buf, sizeof(buf)) < 0) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700231 printk(BIOS_ERR, "%s: Failed to read status\n", __func__);
232 return 0;
233 }
234 return buf[0];
235}
236
237/* cr50 requires all 4 bytes of status register to be written */
Duncan Laurief235a9b2016-09-19 17:19:10 -0700238static void cr50_i2c_tis_ready(struct tpm_chip *chip)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700239{
240 uint8_t buf[4] = { TPM_STS_COMMAND_READY };
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700241 cr50_i2c_write(chip, TPM_STS(chip->vendor.locality), buf, sizeof(buf));
Duncan Laurie1dc036c2016-09-19 16:49:23 -0700242 mdelay(CR50_TIMEOUT_SHORT_MS);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700243}
244
245/* cr50 uses bytes 3:2 of status register for burst count and
246 * all 4 bytes must be read */
Duncan Laurief235a9b2016-09-19 17:19:10 -0700247static int cr50_i2c_wait_burststs(struct tpm_chip *chip, uint8_t mask,
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700248 size_t *burst, int *status)
249{
250 uint8_t buf[4];
251 struct stopwatch sw;
252
Duncan Laurie1dc036c2016-09-19 16:49:23 -0700253 stopwatch_init_msecs_expire(&sw, CR50_TIMEOUT_LONG_MS);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700254
255 while (!stopwatch_expired(&sw)) {
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700256 if (cr50_i2c_read(chip, TPM_STS(chip->vendor.locality),
257 buf, sizeof(buf)) != 0) {
Duncan Laurie1dc036c2016-09-19 16:49:23 -0700258 mdelay(CR50_TIMEOUT_SHORT_MS);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700259 continue;
260 }
261
262 *status = buf[0];
263 *burst = read_le16(&buf[1]);
264
265 /* Check if mask matches and burst is valid */
266 if ((*status & mask) == mask &&
Duncan Laurie3727a8d2016-09-19 16:37:46 -0700267 *burst > 0 && *burst <= CR50_MAX_BUFSIZE)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700268 return 0;
269
Duncan Laurie1dc036c2016-09-19 16:49:23 -0700270 mdelay(CR50_TIMEOUT_SHORT_MS);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700271 }
272
273 printk(BIOS_ERR, "%s: Timeout reading burst and status\n", __func__);
274 return -1;
275}
276
Duncan Laurief235a9b2016-09-19 17:19:10 -0700277static int cr50_i2c_tis_recv(struct tpm_chip *chip, uint8_t *buf,
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700278 size_t buf_len)
279{
280 size_t burstcnt, current, len, expected;
281 uint8_t addr = TPM_DATA_FIFO(chip->vendor.locality);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700282 uint8_t mask = TPM_STS_VALID | TPM_STS_DATA_AVAIL;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700283 int status;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700284
285 if (buf_len < TPM_HEADER_SIZE)
Duncan Laurief235a9b2016-09-19 17:19:10 -0700286 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700287
Duncan Laurief235a9b2016-09-19 17:19:10 -0700288 if (cr50_i2c_wait_burststs(chip, mask, &burstcnt, &status) < 0) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700289 printk(BIOS_ERR, "%s: First chunk not available\n", __func__);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700290 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700291 }
292
293 /* Read first chunk of burstcnt bytes */
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700294 if (cr50_i2c_read(chip, addr, buf, burstcnt) != 0) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700295 printk(BIOS_ERR, "%s: Read failed\n", __func__);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700296 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700297 }
298
299 /* Determine expected data in the return buffer */
300 expected = read_be32(buf + TPM_RSP_SIZE_BYTE);
301 if (expected > buf_len) {
302 printk(BIOS_ERR, "%s: Too much data: %zu > %zu\n",
303 __func__, expected, buf_len);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700304 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700305 }
306
307 /* Now read the rest of the data */
308 current = burstcnt;
309 while (current < expected) {
310 /* Read updated burst count and check status */
Duncan Laurief235a9b2016-09-19 17:19:10 -0700311 if (cr50_i2c_wait_burststs(chip, mask, &burstcnt, &status) < 0)
312 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700313
314 len = min(burstcnt, expected - current);
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700315 if (cr50_i2c_read(chip, addr, buf + current, len) != 0) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700316 printk(BIOS_ERR, "%s: Read failed\n", __func__);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700317 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700318 }
319
320 current += len;
321 }
322
323 /* Ensure TPM is done reading data */
Duncan Laurief235a9b2016-09-19 17:19:10 -0700324 if (cr50_i2c_wait_burststs(chip, TPM_STS_VALID, &burstcnt, &status) < 0)
325 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700326 if (status & TPM_STS_DATA_AVAIL) {
327 printk(BIOS_ERR, "%s: Data still available\n", __func__);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700328 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700329 }
330
Duncan Laurief235a9b2016-09-19 17:19:10 -0700331 return current;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700332
Duncan Laurief235a9b2016-09-19 17:19:10 -0700333out_err:
334 /* Abort current transaction if still pending */
335 if (cr50_i2c_tis_status(chip) & TPM_STS_COMMAND_READY)
336 cr50_i2c_tis_ready(chip);
337 return -1;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700338}
339
Duncan Laurief235a9b2016-09-19 17:19:10 -0700340static int cr50_i2c_tis_send(struct tpm_chip *chip, uint8_t *buf, size_t len)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700341{
342 int status;
343 size_t burstcnt, limit, sent = 0;
344 uint8_t tpm_go[4] = { TPM_STS_GO };
345 struct stopwatch sw;
346
Duncan Laurie1dc036c2016-09-19 16:49:23 -0700347 stopwatch_init_msecs_expire(&sw, CR50_TIMEOUT_LONG_MS);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700348
349 /* Wait until TPM is ready for a command */
Duncan Laurief235a9b2016-09-19 17:19:10 -0700350 while (!(cr50_i2c_tis_status(chip) & TPM_STS_COMMAND_READY)) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700351 if (stopwatch_expired(&sw)) {
352 printk(BIOS_ERR, "%s: Command ready timeout\n",
353 __func__);
354 return -1;
355 }
356
Duncan Laurief235a9b2016-09-19 17:19:10 -0700357 cr50_i2c_tis_ready(chip);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700358 }
359
360 while (len > 0) {
Duncan Laurief235a9b2016-09-19 17:19:10 -0700361 uint8_t mask = TPM_STS_VALID;
362
363 /* Wait for data if this is not the first chunk */
364 if (sent > 0)
365 mask |= TPM_STS_DATA_EXPECT;
366
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700367 /* Read burst count and check status */
Duncan Laurief235a9b2016-09-19 17:19:10 -0700368 if (cr50_i2c_wait_burststs(chip, mask, &burstcnt, &status) < 0)
369 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700370
371 /* Use burstcnt - 1 to account for the address byte
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700372 * that is inserted by cr50_i2c_write() */
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700373 limit = min(burstcnt - 1, len);
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700374 if (cr50_i2c_write(chip, TPM_DATA_FIFO(chip->vendor.locality),
375 &buf[sent], limit) != 0) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700376 printk(BIOS_ERR, "%s: Write failed\n", __func__);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700377 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700378 }
379
380 sent += limit;
381 len -= limit;
382 }
383
384 /* Ensure TPM is not expecting more data */
Duncan Laurief235a9b2016-09-19 17:19:10 -0700385 if (cr50_i2c_wait_burststs(chip, TPM_STS_VALID, &burstcnt, &status) < 0)
386 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700387 if (status & TPM_STS_DATA_EXPECT) {
388 printk(BIOS_ERR, "%s: Data still expected\n", __func__);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700389 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700390 }
391
392 /* Start the TPM command */
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700393 if (cr50_i2c_write(chip, TPM_STS(chip->vendor.locality), tpm_go,
394 sizeof(tpm_go)) < 0) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700395 printk(BIOS_ERR, "%s: Start command failed\n", __func__);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700396 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700397 }
398 return sent;
399
Duncan Laurief235a9b2016-09-19 17:19:10 -0700400out_err:
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700401 /* Abort current transaction if still pending */
Duncan Laurief235a9b2016-09-19 17:19:10 -0700402 if (cr50_i2c_tis_status(chip) & TPM_STS_COMMAND_READY)
403 cr50_i2c_tis_ready(chip);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700404 return -1;
405}
406
407static void cr50_vendor_init(struct tpm_chip *chip)
408{
409 memset(&chip->vendor, 0, sizeof(struct tpm_vendor_specific));
410 chip->vendor.req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID;
411 chip->vendor.req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID;
412 chip->vendor.req_canceled = TPM_STS_COMMAND_READY;
Duncan Laurief235a9b2016-09-19 17:19:10 -0700413 chip->vendor.status = &cr50_i2c_tis_status;
414 chip->vendor.recv = &cr50_i2c_tis_recv;
415 chip->vendor.send = &cr50_i2c_tis_send;
416 chip->vendor.cancel = &cr50_i2c_tis_ready;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700417}
418
Lee Leahy52ab30b2017-03-15 09:22:11 -0700419int tpm_vendor_probe(unsigned int bus, uint32_t addr)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700420{
421 struct tpm_inf_dev *tpm_dev = car_get_var_ptr(&g_tpm_dev);
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700422 struct tpm_chip probe_chip;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700423 struct stopwatch sw;
424 uint8_t buf = 0;
425 int ret;
Duncan Laurie1dc036c2016-09-19 16:49:23 -0700426 long sw_run_duration = CR50_TIMEOUT_LONG_MS;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700427
428 tpm_dev->bus = bus;
429 tpm_dev->addr = addr;
430
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700431 cr50_vendor_init(&probe_chip);
432
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700433 /* Wait for TPM_ACCESS register ValidSts bit to be set */
434 stopwatch_init_msecs_expire(&sw, sw_run_duration);
435 do {
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700436 ret = cr50_i2c_read(&probe_chip, TPM_ACCESS(0), &buf, 1);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700437 if (!ret && (buf & TPM_STS_VALID)) {
438 sw_run_duration = stopwatch_duration_msecs(&sw);
439 break;
440 }
Duncan Laurie1dc036c2016-09-19 16:49:23 -0700441 mdelay(CR50_TIMEOUT_SHORT_MS);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700442 } while (!stopwatch_expired(&sw));
443
444 printk(BIOS_INFO,
445 "%s: ValidSts bit %s(%d) in TPM_ACCESS register after %ld ms\n",
446 __func__, (buf & TPM_STS_VALID) ? "set" : "clear",
447 (buf & TPM_STS_VALID) >> 7, sw_run_duration);
448
449 /* Claim failure if the ValidSts (bit 7) is clear */
450 if (!(buf & TPM_STS_VALID))
451 return -1;
452
453 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 Laurie2ea13c82016-09-19 16:04:39 -0700471 if (request_locality(chip, 0) != 0)
472 return -1;
473
474 /* Read four bytes from DID_VID register */
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700475 if (cr50_i2c_read(chip, TPM_DID_VID(0), (uint8_t *)&vendor, 4) < 0)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700476 goto out_err;
477
478 if (vendor != CR50_DID_VID) {
479 printk(BIOS_DEBUG, "Vendor ID 0x%08x not recognized\n", vendor);
480 goto out_err;
481 }
482
Daniel Kurtzc4852e72017-04-21 14:11:51 +0800483 printk(BIOS_DEBUG, "cr50 TPM 2.0 (i2c %u:0x%02x id 0x%x)\n",
484 bus, dev_addr, vendor >> 16);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700485
486 chip->is_open = 1;
487 return 0;
488
489out_err:
Duncan Lauried3920e72016-09-19 17:09:01 -0700490 release_locality(chip, 1);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700491 return -1;
492}
493
494void tpm_vendor_cleanup(struct tpm_chip *chip)
495{
Duncan Lauried3920e72016-09-19 17:09:01 -0700496 release_locality(chip, 1);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700497}