blob: a3ff7814dec09bda80bf08145a74a09b7ded54c0 [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 */
192static uint8_t cr50_tis_i2c_status(struct tpm_chip *chip)
193{
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 */
204static void cr50_tis_i2c_ready(struct tpm_chip *chip)
205{
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 */
213static int cr50_wait_burst_status(struct tpm_chip *chip, uint8_t mask,
214 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
243static int cr50_tis_i2c_recv(struct tpm_chip *chip, uint8_t *buf,
244 size_t buf_len)
245{
246 size_t burstcnt, current, len, expected;
247 uint8_t addr = TPM_DATA_FIFO(chip->vendor.locality);
248 int status;
249 int ret = -1;
250
251 if (buf_len < TPM_HEADER_SIZE)
252 goto out;
253
254 if (cr50_wait_burst_status(chip, TPM_STS_VALID, &burstcnt, &status) < 0)
255 goto out;
256 if (!(status & TPM_STS_DATA_AVAIL)) {
257 printk(BIOS_ERR, "%s: First chunk not available\n", __func__);
258 goto out;
259 }
260
261 /* Read first chunk of burstcnt bytes */
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700262 if (cr50_i2c_read(chip, addr, buf, burstcnt) != 0) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700263 printk(BIOS_ERR, "%s: Read failed\n", __func__);
264 goto out;
265 }
266
267 /* Determine expected data in the return buffer */
268 expected = read_be32(buf + TPM_RSP_SIZE_BYTE);
269 if (expected > buf_len) {
270 printk(BIOS_ERR, "%s: Too much data: %zu > %zu\n",
271 __func__, expected, buf_len);
272 goto out;
273 }
274
275 /* Now read the rest of the data */
276 current = burstcnt;
277 while (current < expected) {
278 /* Read updated burst count and check status */
279 if (cr50_wait_burst_status(chip, TPM_STS_VALID,
280 &burstcnt, &status) < 0)
281 goto out;
282 if (!(status & TPM_STS_DATA_AVAIL)) {
283 printk(BIOS_ERR, "%s: Data not available\n", __func__);
284 goto out;
285 }
286
287 len = min(burstcnt, expected - current);
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700288 if (cr50_i2c_read(chip, addr, buf + current, len) != 0) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700289 printk(BIOS_ERR, "%s: Read failed\n", __func__);
290 goto out;
291 }
292
293 current += len;
294 }
295
296 /* Ensure TPM is done reading data */
297 if (cr50_wait_burst_status(chip, TPM_STS_VALID, &burstcnt, &status) < 0)
298 goto out;
299 if (status & TPM_STS_DATA_AVAIL) {
300 printk(BIOS_ERR, "%s: Data still available\n", __func__);
301 goto out;
302 }
303
304 ret = current;
305
306out:
307 return ret;
308}
309
310static int cr50_tis_i2c_send(struct tpm_chip *chip, uint8_t *buf, size_t len)
311{
312 int status;
313 size_t burstcnt, limit, sent = 0;
314 uint8_t tpm_go[4] = { TPM_STS_GO };
315 struct stopwatch sw;
316
Duncan Laurie1dc036c2016-09-19 16:49:23 -0700317 stopwatch_init_msecs_expire(&sw, CR50_TIMEOUT_LONG_MS);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700318
319 /* Wait until TPM is ready for a command */
320 while (!(cr50_tis_i2c_status(chip) & TPM_STS_COMMAND_READY)) {
321 if (stopwatch_expired(&sw)) {
322 printk(BIOS_ERR, "%s: Command ready timeout\n",
323 __func__);
324 return -1;
325 }
326
327 cr50_tis_i2c_ready(chip);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700328 }
329
330 while (len > 0) {
331 /* Read burst count and check status */
332 if (cr50_wait_burst_status(chip, TPM_STS_VALID,
333 &burstcnt, &status) < 0)
334 goto out;
335 if (sent > 0 && !(status & TPM_STS_DATA_EXPECT)) {
336 printk(BIOS_ERR, "%s: Data not expected\n", __func__);
337 goto out;
338 }
339
340 /* Use burstcnt - 1 to account for the address byte
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700341 * that is inserted by cr50_i2c_write() */
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700342 limit = min(burstcnt - 1, len);
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700343 if (cr50_i2c_write(chip, TPM_DATA_FIFO(chip->vendor.locality),
344 &buf[sent], limit) != 0) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700345 printk(BIOS_ERR, "%s: Write failed\n", __func__);
346 goto out;
347 }
348
349 sent += limit;
350 len -= limit;
351 }
352
353 /* Ensure TPM is not expecting more data */
354 if (cr50_wait_burst_status(chip, TPM_STS_VALID, &burstcnt, &status) < 0)
355 goto out;
356 if (status & TPM_STS_DATA_EXPECT) {
357 printk(BIOS_ERR, "%s: Data still expected\n", __func__);
358 goto out;
359 }
360
361 /* Start the TPM command */
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700362 if (cr50_i2c_write(chip, TPM_STS(chip->vendor.locality), tpm_go,
363 sizeof(tpm_go)) < 0) {
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700364 printk(BIOS_ERR, "%s: Start command failed\n", __func__);
365 goto out;
366 }
367 return sent;
368
369out:
370 /* Abort current transaction if still pending */
371 if (cr50_tis_i2c_status(chip) & TPM_STS_COMMAND_READY)
372 cr50_tis_i2c_ready(chip);
373 return -1;
374}
375
376static void cr50_vendor_init(struct tpm_chip *chip)
377{
378 memset(&chip->vendor, 0, sizeof(struct tpm_vendor_specific));
379 chip->vendor.req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID;
380 chip->vendor.req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID;
381 chip->vendor.req_canceled = TPM_STS_COMMAND_READY;
382 chip->vendor.status = &cr50_tis_i2c_status;
383 chip->vendor.recv = &cr50_tis_i2c_recv;
384 chip->vendor.send = &cr50_tis_i2c_send;
385 chip->vendor.cancel = &cr50_tis_i2c_ready;
386}
387
388int tpm_vendor_probe(unsigned bus, uint32_t addr)
389{
390 struct tpm_inf_dev *tpm_dev = car_get_var_ptr(&g_tpm_dev);
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700391 struct tpm_chip probe_chip;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700392 struct stopwatch sw;
393 uint8_t buf = 0;
394 int ret;
Duncan Laurie1dc036c2016-09-19 16:49:23 -0700395 long sw_run_duration = CR50_TIMEOUT_LONG_MS;
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700396
397 tpm_dev->bus = bus;
398 tpm_dev->addr = addr;
399
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700400 cr50_vendor_init(&probe_chip);
401
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700402 /* Wait for TPM_ACCESS register ValidSts bit to be set */
403 stopwatch_init_msecs_expire(&sw, sw_run_duration);
404 do {
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700405 ret = cr50_i2c_read(&probe_chip, TPM_ACCESS(0), &buf, 1);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700406 if (!ret && (buf & TPM_STS_VALID)) {
407 sw_run_duration = stopwatch_duration_msecs(&sw);
408 break;
409 }
Duncan Laurie1dc036c2016-09-19 16:49:23 -0700410 mdelay(CR50_TIMEOUT_SHORT_MS);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700411 } while (!stopwatch_expired(&sw));
412
413 printk(BIOS_INFO,
414 "%s: ValidSts bit %s(%d) in TPM_ACCESS register after %ld ms\n",
415 __func__, (buf & TPM_STS_VALID) ? "set" : "clear",
416 (buf & TPM_STS_VALID) >> 7, sw_run_duration);
417
418 /* Claim failure if the ValidSts (bit 7) is clear */
419 if (!(buf & TPM_STS_VALID))
420 return -1;
421
422 return 0;
423}
424
425int tpm_vendor_init(struct tpm_chip *chip, unsigned bus, uint32_t dev_addr)
426{
427 struct tpm_inf_dev *tpm_dev = car_get_var_ptr(&g_tpm_dev);
428 uint32_t vendor;
429
430 if (dev_addr == 0) {
431 printk(BIOS_ERR, "%s: missing device address\n", __func__);
432 return -1;
433 }
434
435 tpm_dev->bus = bus;
436 tpm_dev->addr = dev_addr;
437
438 cr50_vendor_init(chip);
439
440 /* Disable interrupts (not supported) */
441 chip->vendor.irq = 0;
442
443 if (request_locality(chip, 0) != 0)
444 return -1;
445
446 /* Read four bytes from DID_VID register */
Duncan Laurie510cb6a2016-09-19 17:05:45 -0700447 if (cr50_i2c_read(chip, TPM_DID_VID(0), (uint8_t *)&vendor, 4) < 0)
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700448 goto out_err;
449
450 if (vendor != CR50_DID_VID) {
451 printk(BIOS_DEBUG, "Vendor ID 0x%08x not recognized\n", vendor);
452 goto out_err;
453 }
454
455 printk(BIOS_DEBUG, "cr50 TPM %u:%02x (device-id 0x%X)\n",
456 tpm_dev->bus, tpm_dev->addr, vendor >> 16);
457
458 chip->is_open = 1;
459 return 0;
460
461out_err:
Duncan Lauried3920e72016-09-19 17:09:01 -0700462 release_locality(chip, 1);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700463 return -1;
464}
465
466void tpm_vendor_cleanup(struct tpm_chip *chip)
467{
Duncan Lauried3920e72016-09-19 17:09:01 -0700468 release_locality(chip, 1);
Duncan Laurie2ea13c82016-09-19 16:04:39 -0700469}