blob: 37f0881980bf80138e8553df20804c5f5d47ac1b [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 Laurie2ea13c82016-09-19 16:04:39 -070044
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 Laurie2ea13c82016-09-19 16:04:39 -070048#define CR50_DID_VID 0x00281ae0L
49
50struct tpm_inf_dev {
51 int bus;
52 unsigned int addr;
Duncan Laurie3727a8d2016-09-19 16:37:46 -070053 uint8_t buf[CR50_MAX_BUFSIZE + sizeof(uint8_t)];
Duncan Laurie2ea13c82016-09-19 16:04:39 -070054};
55
56static struct tpm_inf_dev g_tpm_dev CAR_GLOBAL;
57
58/*
Duncan Laurie510cb6a2016-09-19 17:05:45 -070059 * cr50_i2c_read() - read from TPM register
Duncan Laurie2ea13c82016-09-19 16:04:39 -070060 *
Duncan Laurie510cb6a2016-09-19 17:05:45 -070061 * @chip: TPM chip information
Duncan Laurie2ea13c82016-09-19 16:04:39 -070062 * @addr: register address to read from
63 * @buffer: provided by caller
64 * @len: number of bytes to read
65 *
66 * 1) send register address byte 'addr' to the TPM
67 * 2) wait for TPM to indicate it is ready
68 * 3) read 'len' bytes of TPM response into the provided 'buffer'
69 *
70 * Return -1 on error, 0 on success.
71 */
Duncan Laurie510cb6a2016-09-19 17:05:45 -070072static int cr50_i2c_read(struct tpm_chip *chip, uint8_t addr,
73 uint8_t *buffer, size_t len)
Duncan Laurie2ea13c82016-09-19 16:04:39 -070074{
75 struct tpm_inf_dev *tpm_dev = car_get_var_ptr(&g_tpm_dev);
76
77 if (tpm_dev->addr == 0)
78 return -1;
79
80 /* Send the register address byte to the TPM */
81 if (i2c_write_raw(tpm_dev->bus, tpm_dev->addr, &addr, 1)) {
82 printk(BIOS_ERR, "%s: Address write failed\n", __func__);
83 return -1;
84 }
85
86 /* Wait for TPM to be ready with response data */
Duncan Laurie1dc036c2016-09-19 16:49:23 -070087 mdelay(CR50_TIMEOUT_SHORT_MS);
Duncan Laurie2ea13c82016-09-19 16:04:39 -070088
89 /* Read response data from the TPM */
90 if (i2c_read_raw(tpm_dev->bus, tpm_dev->addr, buffer, len)) {
91 printk(BIOS_ERR, "%s: Read response failed\n", __func__);
92 return -1;
93 }
94
95 return 0;
96}
97
98/*
Duncan Laurie510cb6a2016-09-19 17:05:45 -070099 * cr50_i2c_write() - write to TPM register
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700100 *
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700101 * @chip: TPM chip information
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700102 * @addr: register address to write to
103 * @buffer: data to write
104 * @len: number of bytes to write
105 *
106 * 1) prepend the provided address to the provided data
107 * 2) send the address+data to the TPM
108 * 3) wait for TPM to indicate it is done writing
109 *
110 * Returns -1 on error, 0 on success.
111 */
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700112static int cr50_i2c_write(struct tpm_chip *chip,
113 uint8_t addr, uint8_t *buffer, size_t len)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700114{
115 struct tpm_inf_dev *tpm_dev = car_get_var_ptr(&g_tpm_dev);
116
117 if (tpm_dev->addr == 0)
118 return -1;
Duncan Laurie3727a8d2016-09-19 16:37:46 -0700119 if (len > CR50_MAX_BUFSIZE)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700120 return -1;
121
122 /* Prepend the 'register address' to the buffer */
123 tpm_dev->buf[0] = addr;
124 memcpy(tpm_dev->buf + 1, buffer, len);
125
126 /* Send write request buffer with address */
127 if (i2c_write_raw(tpm_dev->bus, tpm_dev->addr, tpm_dev->buf, len + 1)) {
128 printk(BIOS_ERR, "%s: Error writing to TPM\n", __func__);
129 return -1;
130 }
131
132 /* Wait for TPM to be ready */
Duncan Laurie1dc036c2016-09-19 16:49:23 -0700133 mdelay(CR50_TIMEOUT_SHORT_MS);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700134
135 return 0;
136}
137
138static int check_locality(struct tpm_chip *chip, int loc)
139{
Duncan Lauried3920e72016-09-19 17:09:01 -0700140 uint8_t mask = TPM_ACCESS_VALID | TPM_ACCESS_ACTIVE_LOCALITY;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700141 uint8_t buf;
142
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700143 if (cr50_i2c_read(chip, TPM_ACCESS(loc), &buf, 1) < 0)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700144 return -1;
145
Duncan Lauried3920e72016-09-19 17:09:01 -0700146 if ((buf & mask) == mask) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700147 chip->vendor.locality = loc;
148 return loc;
149 }
150
151 return -1;
152}
153
Duncan Lauried3920e72016-09-19 17:09:01 -0700154static void release_locality(struct tpm_chip *chip, int force)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700155{
Duncan Lauried3920e72016-09-19 17:09:01 -0700156 uint8_t mask = TPM_ACCESS_VALID | TPM_ACCESS_REQUEST_PENDING;
157 uint8_t addr = TPM_ACCESS(chip->vendor.locality);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700158 uint8_t buf;
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700159
Duncan Lauried3920e72016-09-19 17:09:01 -0700160 if (cr50_i2c_read(chip, addr, &buf, 1) < 0)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700161 return;
162
Duncan Lauried3920e72016-09-19 17:09:01 -0700163 if (force || (buf & mask) == mask) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700164 buf = TPM_ACCESS_ACTIVE_LOCALITY;
Duncan Lauried3920e72016-09-19 17:09:01 -0700165 cr50_i2c_write(chip, addr, &buf, 1);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700166 }
Duncan Lauried3920e72016-09-19 17:09:01 -0700167
168 chip->vendor.locality = 0;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700169}
170
171static int request_locality(struct tpm_chip *chip, int loc)
172{
173 uint8_t buf = TPM_ACCESS_REQUEST_USE;
Duncan Lauried3920e72016-09-19 17:09:01 -0700174 struct stopwatch sw;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700175
176 if (check_locality(chip, loc) >= 0)
Duncan Lauried3920e72016-09-19 17:09:01 -0700177 return loc;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700178
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700179 if (cr50_i2c_write(chip, TPM_ACCESS(loc), &buf, 1) < 0)
180 return -1;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700181
Duncan Lauried3920e72016-09-19 17:09:01 -0700182 stopwatch_init_msecs_expire(&sw, CR50_TIMEOUT_LONG_MS);
183 while (check_locality(chip, loc) < 0) {
184 if (stopwatch_expired(&sw))
185 return -1;
186 mdelay(CR50_TIMEOUT_SHORT_MS);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700187 }
Duncan Lauried3920e72016-09-19 17:09:01 -0700188 return loc;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700189}
190
191/* cr50 requires all 4 bytes of status register to be read */
Duncan Laurief235a9b2016-09-19 17:19:10 -0700192static uint8_t cr50_i2c_tis_status(struct tpm_chip *chip)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700193{
194 uint8_t buf[4];
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700195 if (cr50_i2c_read(chip, TPM_STS(chip->vendor.locality),
196 buf, sizeof(buf)) < 0) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700197 printk(BIOS_ERR, "%s: Failed to read status\n", __func__);
198 return 0;
199 }
200 return buf[0];
201}
202
203/* cr50 requires all 4 bytes of status register to be written */
Duncan Laurief235a9b2016-09-19 17:19:10 -0700204static void cr50_i2c_tis_ready(struct tpm_chip *chip)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700205{
206 uint8_t buf[4] = { TPM_STS_COMMAND_READY };
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700207 cr50_i2c_write(chip, TPM_STS(chip->vendor.locality), buf, sizeof(buf));
Duncan Laurie1dc036c2016-09-19 16:49:23 -0700208 mdelay(CR50_TIMEOUT_SHORT_MS);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700209}
210
211/* cr50 uses bytes 3:2 of status register for burst count and
212 * all 4 bytes must be read */
Duncan Laurief235a9b2016-09-19 17:19:10 -0700213static int cr50_i2c_wait_burststs(struct tpm_chip *chip, uint8_t mask,
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700214 size_t *burst, int *status)
215{
216 uint8_t buf[4];
217 struct stopwatch sw;
218
Duncan Laurie1dc036c2016-09-19 16:49:23 -0700219 stopwatch_init_msecs_expire(&sw, CR50_TIMEOUT_LONG_MS);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700220
221 while (!stopwatch_expired(&sw)) {
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700222 if (cr50_i2c_read(chip, TPM_STS(chip->vendor.locality),
223 buf, sizeof(buf)) != 0) {
Duncan Laurie1dc036c2016-09-19 16:49:23 -0700224 mdelay(CR50_TIMEOUT_SHORT_MS);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700225 continue;
226 }
227
228 *status = buf[0];
229 *burst = read_le16(&buf[1]);
230
231 /* Check if mask matches and burst is valid */
232 if ((*status & mask) == mask &&
Duncan Laurie3727a8d2016-09-19 16:37:46 -0700233 *burst > 0 && *burst <= CR50_MAX_BUFSIZE)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700234 return 0;
235
Duncan Laurie1dc036c2016-09-19 16:49:23 -0700236 mdelay(CR50_TIMEOUT_SHORT_MS);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700237 }
238
239 printk(BIOS_ERR, "%s: Timeout reading burst and status\n", __func__);
240 return -1;
241}
242
Duncan Laurief235a9b2016-09-19 17:19:10 -0700243static int cr50_i2c_tis_recv(struct tpm_chip *chip, uint8_t *buf,
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700244 size_t buf_len)
245{
246 size_t burstcnt, current, len, expected;
247 uint8_t addr = TPM_DATA_FIFO(chip->vendor.locality);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700248 uint8_t mask = TPM_STS_VALID | TPM_STS_DATA_AVAIL;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700249 int status;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700250
251 if (buf_len < TPM_HEADER_SIZE)
Duncan Laurief235a9b2016-09-19 17:19:10 -0700252 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700253
Duncan Laurief235a9b2016-09-19 17:19:10 -0700254 if (cr50_i2c_wait_burststs(chip, mask, &burstcnt, &status) < 0) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700255 printk(BIOS_ERR, "%s: First chunk not available\n", __func__);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700256 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700257 }
258
259 /* Read first chunk of burstcnt bytes */
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700260 if (cr50_i2c_read(chip, addr, buf, burstcnt) != 0) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700261 printk(BIOS_ERR, "%s: Read failed\n", __func__);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700262 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700263 }
264
265 /* Determine expected data in the return buffer */
266 expected = read_be32(buf + TPM_RSP_SIZE_BYTE);
267 if (expected > buf_len) {
268 printk(BIOS_ERR, "%s: Too much data: %zu > %zu\n",
269 __func__, expected, buf_len);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700270 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700271 }
272
273 /* Now read the rest of the data */
274 current = burstcnt;
275 while (current < expected) {
276 /* Read updated burst count and check status */
Duncan Laurief235a9b2016-09-19 17:19:10 -0700277 if (cr50_i2c_wait_burststs(chip, mask, &burstcnt, &status) < 0)
278 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700279
280 len = min(burstcnt, expected - current);
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700281 if (cr50_i2c_read(chip, addr, buf + current, len) != 0) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700282 printk(BIOS_ERR, "%s: Read failed\n", __func__);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700283 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700284 }
285
286 current += len;
287 }
288
289 /* Ensure TPM is done reading data */
Duncan Laurief235a9b2016-09-19 17:19:10 -0700290 if (cr50_i2c_wait_burststs(chip, TPM_STS_VALID, &burstcnt, &status) < 0)
291 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700292 if (status & TPM_STS_DATA_AVAIL) {
293 printk(BIOS_ERR, "%s: Data still available\n", __func__);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700294 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700295 }
296
Duncan Laurief235a9b2016-09-19 17:19:10 -0700297 return current;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700298
Duncan Laurief235a9b2016-09-19 17:19:10 -0700299out_err:
300 /* Abort current transaction if still pending */
301 if (cr50_i2c_tis_status(chip) & TPM_STS_COMMAND_READY)
302 cr50_i2c_tis_ready(chip);
303 return -1;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700304}
305
Duncan Laurief235a9b2016-09-19 17:19:10 -0700306static int cr50_i2c_tis_send(struct tpm_chip *chip, uint8_t *buf, size_t len)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700307{
308 int status;
309 size_t burstcnt, limit, sent = 0;
310 uint8_t tpm_go[4] = { TPM_STS_GO };
311 struct stopwatch sw;
312
Duncan Laurie1dc036c2016-09-19 16:49:23 -0700313 stopwatch_init_msecs_expire(&sw, CR50_TIMEOUT_LONG_MS);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700314
315 /* Wait until TPM is ready for a command */
Duncan Laurief235a9b2016-09-19 17:19:10 -0700316 while (!(cr50_i2c_tis_status(chip) & TPM_STS_COMMAND_READY)) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700317 if (stopwatch_expired(&sw)) {
318 printk(BIOS_ERR, "%s: Command ready timeout\n",
319 __func__);
320 return -1;
321 }
322
Duncan Laurief235a9b2016-09-19 17:19:10 -0700323 cr50_i2c_tis_ready(chip);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700324 }
325
326 while (len > 0) {
Duncan Laurief235a9b2016-09-19 17:19:10 -0700327 uint8_t mask = TPM_STS_VALID;
328
329 /* Wait for data if this is not the first chunk */
330 if (sent > 0)
331 mask |= TPM_STS_DATA_EXPECT;
332
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700333 /* Read burst count and check status */
Duncan Laurief235a9b2016-09-19 17:19:10 -0700334 if (cr50_i2c_wait_burststs(chip, mask, &burstcnt, &status) < 0)
335 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700336
337 /* Use burstcnt - 1 to account for the address byte
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700338 * that is inserted by cr50_i2c_write() */
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700339 limit = min(burstcnt - 1, len);
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700340 if (cr50_i2c_write(chip, TPM_DATA_FIFO(chip->vendor.locality),
341 &buf[sent], limit) != 0) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700342 printk(BIOS_ERR, "%s: Write failed\n", __func__);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700343 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700344 }
345
346 sent += limit;
347 len -= limit;
348 }
349
350 /* Ensure TPM is not expecting more data */
Duncan Laurief235a9b2016-09-19 17:19:10 -0700351 if (cr50_i2c_wait_burststs(chip, TPM_STS_VALID, &burstcnt, &status) < 0)
352 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700353 if (status & TPM_STS_DATA_EXPECT) {
354 printk(BIOS_ERR, "%s: Data still expected\n", __func__);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700355 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700356 }
357
358 /* Start the TPM command */
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700359 if (cr50_i2c_write(chip, TPM_STS(chip->vendor.locality), tpm_go,
360 sizeof(tpm_go)) < 0) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700361 printk(BIOS_ERR, "%s: Start command failed\n", __func__);
Duncan Laurief235a9b2016-09-19 17:19:10 -0700362 goto out_err;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700363 }
364 return sent;
365
Duncan Laurief235a9b2016-09-19 17:19:10 -0700366out_err:
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700367 /* Abort current transaction if still pending */
Duncan Laurief235a9b2016-09-19 17:19:10 -0700368 if (cr50_i2c_tis_status(chip) & TPM_STS_COMMAND_READY)
369 cr50_i2c_tis_ready(chip);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700370 return -1;
371}
372
373static void cr50_vendor_init(struct tpm_chip *chip)
374{
375 memset(&chip->vendor, 0, sizeof(struct tpm_vendor_specific));
376 chip->vendor.req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID;
377 chip->vendor.req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID;
378 chip->vendor.req_canceled = TPM_STS_COMMAND_READY;
Duncan Laurief235a9b2016-09-19 17:19:10 -0700379 chip->vendor.status = &cr50_i2c_tis_status;
380 chip->vendor.recv = &cr50_i2c_tis_recv;
381 chip->vendor.send = &cr50_i2c_tis_send;
382 chip->vendor.cancel = &cr50_i2c_tis_ready;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700383}
384
385int tpm_vendor_probe(unsigned bus, uint32_t addr)
386{
387 struct tpm_inf_dev *tpm_dev = car_get_var_ptr(&g_tpm_dev);
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700388 struct tpm_chip probe_chip;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700389 struct stopwatch sw;
390 uint8_t buf = 0;
391 int ret;
Duncan Laurie1dc036c2016-09-19 16:49:23 -0700392 long sw_run_duration = CR50_TIMEOUT_LONG_MS;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700393
394 tpm_dev->bus = bus;
395 tpm_dev->addr = addr;
396
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700397 cr50_vendor_init(&probe_chip);
398
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700399 /* Wait for TPM_ACCESS register ValidSts bit to be set */
400 stopwatch_init_msecs_expire(&sw, sw_run_duration);
401 do {
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700402 ret = cr50_i2c_read(&probe_chip, TPM_ACCESS(0), &buf, 1);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700403 if (!ret && (buf & TPM_STS_VALID)) {
404 sw_run_duration = stopwatch_duration_msecs(&sw);
405 break;
406 }
Duncan Laurie1dc036c2016-09-19 16:49:23 -0700407 mdelay(CR50_TIMEOUT_SHORT_MS);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700408 } while (!stopwatch_expired(&sw));
409
410 printk(BIOS_INFO,
411 "%s: ValidSts bit %s(%d) in TPM_ACCESS register after %ld ms\n",
412 __func__, (buf & TPM_STS_VALID) ? "set" : "clear",
413 (buf & TPM_STS_VALID) >> 7, sw_run_duration);
414
415 /* Claim failure if the ValidSts (bit 7) is clear */
416 if (!(buf & TPM_STS_VALID))
417 return -1;
418
419 return 0;
420}
421
422int tpm_vendor_init(struct tpm_chip *chip, unsigned bus, uint32_t dev_addr)
423{
424 struct tpm_inf_dev *tpm_dev = car_get_var_ptr(&g_tpm_dev);
425 uint32_t vendor;
426
427 if (dev_addr == 0) {
428 printk(BIOS_ERR, "%s: missing device address\n", __func__);
429 return -1;
430 }
431
432 tpm_dev->bus = bus;
433 tpm_dev->addr = dev_addr;
434
435 cr50_vendor_init(chip);
436
437 /* Disable interrupts (not supported) */
438 chip->vendor.irq = 0;
439
440 if (request_locality(chip, 0) != 0)
441 return -1;
442
443 /* Read four bytes from DID_VID register */
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700444 if (cr50_i2c_read(chip, TPM_DID_VID(0), (uint8_t *)&vendor, 4) < 0)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700445 goto out_err;
446
447 if (vendor != CR50_DID_VID) {
448 printk(BIOS_DEBUG, "Vendor ID 0x%08x not recognized\n", vendor);
449 goto out_err;
450 }
451
452 printk(BIOS_DEBUG, "cr50 TPM %u:%02x (device-id 0x%X)\n",
453 tpm_dev->bus, tpm_dev->addr, vendor >> 16);
454
455 chip->is_open = 1;
456 return 0;
457
458out_err:
Duncan Lauried3920e72016-09-19 17:09:01 -0700459 release_locality(chip, 1);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700460 return -1;
461}
462
463void tpm_vendor_cleanup(struct tpm_chip *chip)
464{
Duncan Lauried3920e72016-09-19 17:09:01 -0700465 release_locality(chip, 1);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700466}