2013-04-12 10:44:57 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2011 Infineon Technologies
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Peter Huewe <huewe.external@infineon.com>
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Device driver for TCG/TCPA TPM (trusted platform module).
|
|
|
|
* Specifications at www.trustedcomputinggroup.org
|
|
|
|
*
|
|
|
|
* This device driver implements the TPM interface as defined in
|
|
|
|
* the TCG TPM Interface Spec version 1.2, revision 1.0 and the
|
|
|
|
* Infineon I2C Protocol Stack Specification v0.20.
|
|
|
|
*
|
|
|
|
* It is based on the Linux kernel driver tpm.c from Leendert van
|
|
|
|
* Dorn, Dave Safford, Reiner Sailer, and Kyleen Hall.
|
|
|
|
*
|
|
|
|
* Version: 2.1.1
|
|
|
|
*
|
2015-08-23 00:31:21 +00:00
|
|
|
* SPDX-License-Identifier: GPL-2.0
|
2013-04-12 10:44:57 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
2015-05-04 17:30:59 +00:00
|
|
|
#include <dm.h>
|
2013-04-12 11:04:36 +00:00
|
|
|
#include <fdtdec.h>
|
2014-11-26 07:00:58 +00:00
|
|
|
#include <linux/compiler.h>
|
2013-04-12 10:44:57 +00:00
|
|
|
#include <i2c.h>
|
2013-04-12 11:04:37 +00:00
|
|
|
#include <tpm.h>
|
|
|
|
#include <asm-generic/errno.h>
|
2013-04-12 10:44:57 +00:00
|
|
|
#include <linux/types.h>
|
2013-04-12 11:04:37 +00:00
|
|
|
#include <linux/unaligned/be_byteshift.h>
|
2013-04-12 10:44:57 +00:00
|
|
|
|
2015-08-23 00:31:22 +00:00
|
|
|
#include "tpm_tis_i2c.h"
|
2013-04-12 10:44:57 +00:00
|
|
|
|
2013-04-12 11:04:36 +00:00
|
|
|
DECLARE_GLOBAL_DATA_PTR;
|
|
|
|
|
|
|
|
static const char * const chip_name[] = {
|
|
|
|
[SLB9635] = "slb9635tt",
|
|
|
|
[SLB9645] = "slb9645tt",
|
|
|
|
[UNKNOWN] = "unknown/fallback to slb9635",
|
|
|
|
};
|
2013-04-12 10:44:57 +00:00
|
|
|
|
2015-08-23 00:31:22 +00:00
|
|
|
static struct tpm_chip g_chip;
|
|
|
|
|
2013-04-12 10:44:57 +00:00
|
|
|
/*
|
|
|
|
* iic_tpm_read() - read from TPM register
|
|
|
|
* @addr: register address to read from
|
|
|
|
* @buffer: provided by caller
|
|
|
|
* @len: number of bytes to read
|
|
|
|
*
|
|
|
|
* Read len bytes from TPM register and put them into
|
|
|
|
* buffer (little-endian format, i.e. first byte is put into buffer[0]).
|
|
|
|
*
|
|
|
|
* NOTE: TPM is big-endian for multi-byte values. Multi-byte
|
|
|
|
* values have to be swapped.
|
|
|
|
*
|
|
|
|
* Return -EIO on error, 0 on success.
|
|
|
|
*/
|
2013-04-12 11:04:37 +00:00
|
|
|
static int iic_tpm_read(u8 addr, u8 *buffer, size_t len)
|
2013-04-12 10:44:57 +00:00
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
int count;
|
2013-04-12 11:04:37 +00:00
|
|
|
uint32_t addrbuf = addr;
|
2013-04-12 10:44:57 +00:00
|
|
|
|
2015-08-23 00:31:25 +00:00
|
|
|
if ((g_chip.chip_type == SLB9635) || (g_chip.chip_type == UNKNOWN)) {
|
2013-04-12 11:04:36 +00:00
|
|
|
/* slb9635 protocol should work in both cases */
|
|
|
|
for (count = 0; count < MAX_COUNT; count++) {
|
2015-08-23 00:31:25 +00:00
|
|
|
rc = dm_i2c_write(g_chip.dev, 0, (uchar *)&addrbuf, 1);
|
2013-04-12 11:04:36 +00:00
|
|
|
if (rc == 0)
|
2013-04-12 11:04:37 +00:00
|
|
|
break; /* Success, break to skip sleep */
|
2013-04-12 11:04:36 +00:00
|
|
|
udelay(SLEEP_DURATION);
|
|
|
|
}
|
|
|
|
if (rc)
|
|
|
|
return -rc;
|
|
|
|
|
|
|
|
/* After the TPM has successfully received the register address
|
|
|
|
* it needs some time, thus we're sleeping here again, before
|
|
|
|
* retrieving the data
|
|
|
|
*/
|
|
|
|
for (count = 0; count < MAX_COUNT; count++) {
|
|
|
|
udelay(SLEEP_DURATION);
|
2015-08-23 00:31:25 +00:00
|
|
|
rc = dm_i2c_read(g_chip.dev, 0, buffer, len);
|
2013-04-12 11:04:36 +00:00
|
|
|
if (rc == 0)
|
|
|
|
break; /* success, break to skip sleep */
|
|
|
|
}
|
|
|
|
} else {
|
2013-04-12 11:04:37 +00:00
|
|
|
/*
|
|
|
|
* Use a combined read for newer chips.
|
|
|
|
* Unfortunately the smbus functions are not suitable due to
|
2013-04-12 11:04:36 +00:00
|
|
|
* the 32 byte limit of the smbus.
|
2013-04-12 11:04:37 +00:00
|
|
|
* Retries should usually not be needed, but are kept just to
|
2013-04-12 11:04:36 +00:00
|
|
|
* be safe on the safe side.
|
|
|
|
*/
|
|
|
|
for (count = 0; count < MAX_COUNT; count++) {
|
2015-08-23 00:31:25 +00:00
|
|
|
rc = dm_i2c_read(g_chip.dev, addr, buffer, len);
|
2013-04-12 11:04:36 +00:00
|
|
|
if (rc == 0)
|
|
|
|
break; /* break here to skip sleep */
|
|
|
|
udelay(SLEEP_DURATION);
|
|
|
|
}
|
2013-04-12 10:44:57 +00:00
|
|
|
}
|
|
|
|
|
2013-04-12 11:04:37 +00:00
|
|
|
/* Take care of 'guard time' */
|
2013-04-12 11:04:36 +00:00
|
|
|
udelay(SLEEP_DURATION);
|
2013-04-12 10:44:57 +00:00
|
|
|
if (rc)
|
|
|
|
return -rc;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int iic_tpm_write_generic(u8 addr, u8 *buffer, size_t len,
|
2013-04-12 11:04:37 +00:00
|
|
|
unsigned int sleep_time, u8 max_count)
|
2013-04-12 10:44:57 +00:00
|
|
|
{
|
|
|
|
int rc = 0;
|
|
|
|
int count;
|
|
|
|
|
|
|
|
for (count = 0; count < max_count; count++) {
|
2015-08-23 00:31:25 +00:00
|
|
|
rc = dm_i2c_write(g_chip.dev, addr, buffer, len);
|
2013-04-12 10:44:57 +00:00
|
|
|
if (rc == 0)
|
2013-04-12 11:04:37 +00:00
|
|
|
break; /* Success, break to skip sleep */
|
2013-04-12 10:44:57 +00:00
|
|
|
udelay(sleep_time);
|
|
|
|
}
|
|
|
|
|
2013-04-12 11:04:36 +00:00
|
|
|
/* take care of 'guard time' */
|
2015-05-04 17:30:59 +00:00
|
|
|
udelay(sleep_time);
|
2013-04-12 10:44:57 +00:00
|
|
|
if (rc)
|
|
|
|
return -rc;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* iic_tpm_write() - write to TPM register
|
|
|
|
* @addr: register address to write to
|
|
|
|
* @buffer: containing data to be written
|
|
|
|
* @len: number of bytes to write
|
|
|
|
*
|
|
|
|
* Write len bytes from provided buffer to TPM register (little
|
|
|
|
* endian format, i.e. buffer[0] is written as first byte).
|
|
|
|
*
|
|
|
|
* NOTE: TPM is big-endian for multi-byte values. Multi-byte
|
|
|
|
* values have to be swapped.
|
|
|
|
*
|
|
|
|
* NOTE: use this function instead of the iic_tpm_write_generic function.
|
|
|
|
*
|
|
|
|
* Return -EIO on error, 0 on success
|
|
|
|
*/
|
|
|
|
static int iic_tpm_write(u8 addr, u8 *buffer, size_t len)
|
|
|
|
{
|
|
|
|
return iic_tpm_write_generic(addr, buffer, len, SLEEP_DURATION,
|
|
|
|
MAX_COUNT);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This function is needed especially for the cleanup situation after
|
|
|
|
* sending TPM_READY
|
2013-04-12 11:04:37 +00:00
|
|
|
*/
|
2013-04-12 10:44:57 +00:00
|
|
|
static int iic_tpm_write_long(u8 addr, u8 *buffer, size_t len)
|
|
|
|
{
|
|
|
|
return iic_tpm_write_generic(addr, buffer, len, SLEEP_DURATION_LONG,
|
|
|
|
MAX_COUNT_LONG);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int check_locality(struct tpm_chip *chip, int loc)
|
|
|
|
{
|
2013-04-12 11:04:37 +00:00
|
|
|
const u8 mask = TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID;
|
2013-04-12 10:44:57 +00:00
|
|
|
u8 buf;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
rc = iic_tpm_read(TPM_ACCESS(loc), &buf, 1);
|
|
|
|
if (rc < 0)
|
|
|
|
return rc;
|
|
|
|
|
2013-04-12 11:04:37 +00:00
|
|
|
if ((buf & mask) == mask) {
|
2015-08-23 00:31:24 +00:00
|
|
|
chip->locality = loc;
|
2013-04-12 10:44:57 +00:00
|
|
|
return loc;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void release_locality(struct tpm_chip *chip, int loc, int force)
|
|
|
|
{
|
2013-04-12 11:04:37 +00:00
|
|
|
const u8 mask = TPM_ACCESS_REQUEST_PENDING | TPM_ACCESS_VALID;
|
2013-04-12 10:44:57 +00:00
|
|
|
u8 buf;
|
2013-04-12 11:04:37 +00:00
|
|
|
|
2013-04-12 10:44:57 +00:00
|
|
|
if (iic_tpm_read(TPM_ACCESS(loc), &buf, 1) < 0)
|
|
|
|
return;
|
|
|
|
|
2013-04-12 11:04:37 +00:00
|
|
|
if (force || (buf & mask) == mask) {
|
2013-04-12 10:44:57 +00:00
|
|
|
buf = TPM_ACCESS_ACTIVE_LOCALITY;
|
|
|
|
iic_tpm_write(TPM_ACCESS(loc), &buf, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int request_locality(struct tpm_chip *chip, int loc)
|
|
|
|
{
|
|
|
|
unsigned long start, stop;
|
|
|
|
u8 buf = TPM_ACCESS_REQUEST_USE;
|
2015-05-04 17:30:59 +00:00
|
|
|
int rc;
|
2013-04-12 10:44:57 +00:00
|
|
|
|
|
|
|
if (check_locality(chip, loc) >= 0)
|
2013-04-12 11:04:37 +00:00
|
|
|
return loc; /* We already have the locality */
|
2013-04-12 10:44:57 +00:00
|
|
|
|
2015-05-04 17:30:59 +00:00
|
|
|
rc = iic_tpm_write(TPM_ACCESS(loc), &buf, 1);
|
|
|
|
if (rc)
|
|
|
|
return rc;
|
2013-04-12 10:44:57 +00:00
|
|
|
|
2013-04-12 11:04:37 +00:00
|
|
|
/* Wait for burstcount */
|
2013-04-12 10:44:57 +00:00
|
|
|
start = get_timer(0);
|
2015-08-23 00:31:24 +00:00
|
|
|
stop = chip->timeout_a;
|
2013-04-12 10:44:57 +00:00
|
|
|
do {
|
|
|
|
if (check_locality(chip, loc) >= 0)
|
|
|
|
return loc;
|
2013-04-12 11:04:37 +00:00
|
|
|
udelay(TPM_TIMEOUT * 1000);
|
2013-04-12 10:44:57 +00:00
|
|
|
} while (get_timer(start) < stop);
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static u8 tpm_tis_i2c_status(struct tpm_chip *chip)
|
|
|
|
{
|
2013-04-12 11:04:37 +00:00
|
|
|
/* NOTE: Since i2c read may fail, return 0 in this case --> time-out */
|
2013-04-12 10:44:57 +00:00
|
|
|
u8 buf;
|
2013-04-12 11:04:37 +00:00
|
|
|
|
2015-08-23 00:31:24 +00:00
|
|
|
if (iic_tpm_read(TPM_STS(chip->locality), &buf, 1) < 0)
|
2013-04-12 10:44:57 +00:00
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void tpm_tis_i2c_ready(struct tpm_chip *chip)
|
|
|
|
{
|
2015-05-04 17:30:59 +00:00
|
|
|
int rc;
|
|
|
|
|
2013-04-12 11:04:37 +00:00
|
|
|
/* This causes the current command to be aborted */
|
2013-04-12 10:44:57 +00:00
|
|
|
u8 buf = TPM_STS_COMMAND_READY;
|
2013-04-12 11:04:37 +00:00
|
|
|
|
2015-05-04 17:30:59 +00:00
|
|
|
debug("%s\n", __func__);
|
2015-08-23 00:31:24 +00:00
|
|
|
rc = iic_tpm_write_long(TPM_STS(chip->locality), &buf, 1);
|
2015-05-04 17:30:59 +00:00
|
|
|
if (rc)
|
|
|
|
debug("%s: rc=%d\n", __func__, rc);
|
2013-04-12 10:44:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t get_burstcount(struct tpm_chip *chip)
|
|
|
|
{
|
|
|
|
unsigned long start, stop;
|
|
|
|
ssize_t burstcnt;
|
2013-04-12 11:04:37 +00:00
|
|
|
u8 addr, buf[3];
|
2013-04-12 10:44:57 +00:00
|
|
|
|
2013-04-12 11:04:37 +00:00
|
|
|
/* Wait for burstcount */
|
|
|
|
/* XXX: Which timeout value? Spec has 2 answers (c & d) */
|
2013-04-12 10:44:57 +00:00
|
|
|
start = get_timer(0);
|
2015-08-23 00:31:24 +00:00
|
|
|
stop = chip->timeout_d;
|
2013-04-12 10:44:57 +00:00
|
|
|
do {
|
|
|
|
/* Note: STS is little endian */
|
2015-08-23 00:31:24 +00:00
|
|
|
addr = TPM_STS(chip->locality) + 1;
|
2013-04-12 11:04:37 +00:00
|
|
|
if (iic_tpm_read(addr, buf, 3) < 0)
|
2013-04-12 10:44:57 +00:00
|
|
|
burstcnt = 0;
|
|
|
|
else
|
|
|
|
burstcnt = (buf[2] << 16) + (buf[1] << 8) + buf[0];
|
|
|
|
|
|
|
|
if (burstcnt)
|
|
|
|
return burstcnt;
|
2013-04-12 11:04:37 +00:00
|
|
|
udelay(TPM_TIMEOUT * 1000);
|
2013-04-12 10:44:57 +00:00
|
|
|
} while (get_timer(start) < stop);
|
|
|
|
|
|
|
|
return -EBUSY;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int wait_for_stat(struct tpm_chip *chip, u8 mask, unsigned long timeout,
|
2013-04-12 11:04:37 +00:00
|
|
|
int *status)
|
2013-04-12 10:44:57 +00:00
|
|
|
{
|
|
|
|
unsigned long start, stop;
|
|
|
|
|
2013-04-12 11:04:37 +00:00
|
|
|
/* Check current status */
|
2013-04-12 10:44:57 +00:00
|
|
|
*status = tpm_tis_i2c_status(chip);
|
|
|
|
if ((*status & mask) == mask)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
start = get_timer(0);
|
|
|
|
stop = timeout;
|
|
|
|
do {
|
2013-04-12 11:04:37 +00:00
|
|
|
udelay(TPM_TIMEOUT * 1000);
|
2013-04-12 10:44:57 +00:00
|
|
|
*status = tpm_tis_i2c_status(chip);
|
|
|
|
if ((*status & mask) == mask)
|
|
|
|
return 0;
|
|
|
|
} while (get_timer(start) < stop);
|
|
|
|
|
|
|
|
return -ETIME;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int recv_data(struct tpm_chip *chip, u8 *buf, size_t count)
|
|
|
|
{
|
|
|
|
size_t size = 0;
|
|
|
|
ssize_t burstcnt;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
while (size < count) {
|
|
|
|
burstcnt = get_burstcount(chip);
|
|
|
|
|
2013-04-12 11:04:37 +00:00
|
|
|
/* burstcount < 0 -> tpm is busy */
|
2013-04-12 10:44:57 +00:00
|
|
|
if (burstcnt < 0)
|
|
|
|
return burstcnt;
|
|
|
|
|
2013-04-12 11:04:37 +00:00
|
|
|
/* Limit received data to max left */
|
2013-04-12 10:44:57 +00:00
|
|
|
if (burstcnt > (count - size))
|
|
|
|
burstcnt = count - size;
|
|
|
|
|
2015-08-23 00:31:24 +00:00
|
|
|
rc = iic_tpm_read(TPM_DATA_FIFO(chip->locality),
|
2013-04-12 11:04:37 +00:00
|
|
|
&(buf[size]), burstcnt);
|
2013-04-12 10:44:57 +00:00
|
|
|
if (rc == 0)
|
|
|
|
size += burstcnt;
|
|
|
|
}
|
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int tpm_tis_i2c_recv(struct tpm_chip *chip, u8 *buf, size_t count)
|
|
|
|
{
|
|
|
|
int size = 0;
|
|
|
|
int expected, status;
|
|
|
|
|
|
|
|
if (count < TPM_HEADER_SIZE) {
|
|
|
|
size = -EIO;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2013-04-12 11:04:37 +00:00
|
|
|
/* Read first 10 bytes, including tag, paramsize, and result */
|
2013-04-12 10:44:57 +00:00
|
|
|
size = recv_data(chip, buf, TPM_HEADER_SIZE);
|
|
|
|
if (size < TPM_HEADER_SIZE) {
|
2013-04-12 11:04:37 +00:00
|
|
|
error("Unable to read header\n");
|
2013-04-12 10:44:57 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
expected = get_unaligned_be32(buf + TPM_RSP_SIZE_BYTE);
|
|
|
|
if ((size_t)expected > count) {
|
2015-05-04 17:30:59 +00:00
|
|
|
error("Error size=%x, expected=%x, count=%x\n", size, expected,
|
|
|
|
count);
|
2013-04-12 10:44:57 +00:00
|
|
|
size = -EIO;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
size += recv_data(chip, &buf[TPM_HEADER_SIZE],
|
2013-04-12 11:04:37 +00:00
|
|
|
expected - TPM_HEADER_SIZE);
|
2013-04-12 10:44:57 +00:00
|
|
|
if (size < expected) {
|
2013-04-12 11:04:37 +00:00
|
|
|
error("Unable to read remainder of result\n");
|
2013-04-12 10:44:57 +00:00
|
|
|
size = -ETIME;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2015-08-23 00:31:24 +00:00
|
|
|
wait_for_stat(chip, TPM_STS_VALID, chip->timeout_c, &status);
|
2013-04-12 11:04:37 +00:00
|
|
|
if (status & TPM_STS_DATA_AVAIL) { /* Retry? */
|
|
|
|
error("Error left over data\n");
|
2013-04-12 10:44:57 +00:00
|
|
|
size = -EIO;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
tpm_tis_i2c_ready(chip);
|
2013-04-12 11:04:37 +00:00
|
|
|
/*
|
|
|
|
* The TPM needs some time to clean up here,
|
2013-04-12 10:44:57 +00:00
|
|
|
* so we sleep rather than keeping the bus busy
|
|
|
|
*/
|
|
|
|
udelay(2000);
|
2015-08-23 00:31:24 +00:00
|
|
|
release_locality(chip, chip->locality, 0);
|
2013-04-12 10:44:57 +00:00
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int tpm_tis_i2c_send(struct tpm_chip *chip, u8 *buf, size_t len)
|
|
|
|
{
|
|
|
|
int rc, status;
|
2015-05-04 17:30:59 +00:00
|
|
|
size_t burstcnt;
|
2013-04-12 10:44:57 +00:00
|
|
|
size_t count = 0;
|
2013-04-12 11:04:37 +00:00
|
|
|
int retry = 0;
|
2013-04-12 10:44:57 +00:00
|
|
|
u8 sts = TPM_STS_GO;
|
|
|
|
|
2015-05-04 17:30:59 +00:00
|
|
|
debug("%s: len=%d\n", __func__, len);
|
2013-04-12 11:04:37 +00:00
|
|
|
if (len > TPM_DEV_BUFSIZE)
|
|
|
|
return -E2BIG; /* Command is too long for our tpm, sorry */
|
2013-04-12 10:44:57 +00:00
|
|
|
|
|
|
|
if (request_locality(chip, 0) < 0)
|
|
|
|
return -EBUSY;
|
|
|
|
|
|
|
|
status = tpm_tis_i2c_status(chip);
|
|
|
|
if ((status & TPM_STS_COMMAND_READY) == 0) {
|
|
|
|
tpm_tis_i2c_ready(chip);
|
2013-04-12 11:04:37 +00:00
|
|
|
if (wait_for_stat(chip, TPM_STS_COMMAND_READY,
|
2015-08-23 00:31:24 +00:00
|
|
|
chip->timeout_b, &status) < 0) {
|
2013-04-12 10:44:57 +00:00
|
|
|
rc = -ETIME;
|
|
|
|
goto out_err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-12 11:04:37 +00:00
|
|
|
burstcnt = get_burstcount(chip);
|
2013-04-12 10:44:57 +00:00
|
|
|
|
2013-04-12 11:04:37 +00:00
|
|
|
/* burstcount < 0 -> tpm is busy */
|
|
|
|
if (burstcnt < 0)
|
|
|
|
return burstcnt;
|
2013-04-12 10:44:57 +00:00
|
|
|
|
2015-05-04 17:30:59 +00:00
|
|
|
while (count < len) {
|
|
|
|
udelay(300);
|
|
|
|
if (burstcnt > len - count)
|
|
|
|
burstcnt = len - count;
|
2013-04-12 10:44:57 +00:00
|
|
|
|
2013-04-12 11:04:37 +00:00
|
|
|
#ifdef CONFIG_TPM_TIS_I2C_BURST_LIMITATION
|
|
|
|
if (retry && burstcnt > CONFIG_TPM_TIS_I2C_BURST_LIMITATION)
|
|
|
|
burstcnt = CONFIG_TPM_TIS_I2C_BURST_LIMITATION;
|
|
|
|
#endif /* CONFIG_TPM_TIS_I2C_BURST_LIMITATION */
|
2013-04-12 10:44:57 +00:00
|
|
|
|
2015-08-23 00:31:24 +00:00
|
|
|
rc = iic_tpm_write(TPM_DATA_FIFO(chip->locality),
|
2013-04-12 11:04:37 +00:00
|
|
|
&(buf[count]), burstcnt);
|
2013-04-12 10:44:57 +00:00
|
|
|
if (rc == 0)
|
|
|
|
count += burstcnt;
|
2013-04-12 11:04:37 +00:00
|
|
|
else {
|
2015-05-04 17:30:59 +00:00
|
|
|
debug("%s: error\n", __func__);
|
|
|
|
if (retry++ > 10) {
|
|
|
|
rc = -EIO;
|
|
|
|
goto out_err;
|
|
|
|
}
|
|
|
|
rc = wait_for_stat(chip, TPM_STS_VALID,
|
2015-08-23 00:31:24 +00:00
|
|
|
chip->timeout_c, &status);
|
2015-05-04 17:30:59 +00:00
|
|
|
if (rc)
|
|
|
|
goto out_err;
|
2013-04-12 11:04:37 +00:00
|
|
|
|
|
|
|
if ((status & TPM_STS_DATA_EXPECT) == 0) {
|
|
|
|
rc = -EIO;
|
|
|
|
goto out_err;
|
|
|
|
}
|
2013-04-12 10:44:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-12 11:04:37 +00:00
|
|
|
/* Go and do it */
|
2015-08-23 00:31:24 +00:00
|
|
|
iic_tpm_write(TPM_STS(chip->locality), &sts, 1);
|
2015-05-04 17:30:59 +00:00
|
|
|
debug("done\n");
|
2013-04-12 10:44:57 +00:00
|
|
|
|
|
|
|
return len;
|
2013-04-12 11:04:37 +00:00
|
|
|
|
2013-04-12 10:44:57 +00:00
|
|
|
out_err:
|
2015-05-04 17:30:59 +00:00
|
|
|
debug("%s: out_err\n", __func__);
|
2013-04-12 10:44:57 +00:00
|
|
|
tpm_tis_i2c_ready(chip);
|
2013-04-12 11:04:37 +00:00
|
|
|
/*
|
|
|
|
* The TPM needs some time to clean up here,
|
2013-04-12 10:44:57 +00:00
|
|
|
* so we sleep rather than keeping the bus busy
|
|
|
|
*/
|
|
|
|
udelay(2000);
|
2015-08-23 00:31:24 +00:00
|
|
|
release_locality(chip, chip->locality, 0);
|
2013-04-12 10:44:57 +00:00
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2015-08-23 00:31:25 +00:00
|
|
|
static enum i2c_chip_type tpm_tis_i2c_chip_type(void)
|
2013-04-12 11:04:36 +00:00
|
|
|
{
|
2015-08-11 22:31:55 +00:00
|
|
|
#if CONFIG_IS_ENABLED(OF_CONTROL)
|
2013-04-12 11:04:36 +00:00
|
|
|
const void *blob = gd->fdt_blob;
|
|
|
|
|
|
|
|
if (fdtdec_next_compatible(blob, 0, COMPAT_INFINEON_SLB9645_TPM) >= 0)
|
|
|
|
return SLB9645;
|
|
|
|
|
|
|
|
if (fdtdec_next_compatible(blob, 0, COMPAT_INFINEON_SLB9635_TPM) >= 0)
|
|
|
|
return SLB9635;
|
|
|
|
#endif
|
|
|
|
return UNKNOWN;
|
|
|
|
}
|
|
|
|
|
2015-08-23 00:31:25 +00:00
|
|
|
static int tpm_tis_i2c_init(struct udevice *dev)
|
2013-04-12 10:44:57 +00:00
|
|
|
{
|
2015-08-23 00:31:24 +00:00
|
|
|
struct tpm_chip *chip = &g_chip;
|
2013-04-12 10:44:57 +00:00
|
|
|
u32 vendor;
|
2013-04-12 11:04:36 +00:00
|
|
|
u32 expected_did_vid;
|
2013-04-12 10:44:57 +00:00
|
|
|
|
2015-08-23 00:31:25 +00:00
|
|
|
g_chip.dev = dev;
|
|
|
|
g_chip.chip_type = tpm_tis_i2c_chip_type();
|
2015-08-23 00:31:24 +00:00
|
|
|
chip->is_open = 1;
|
2013-04-12 10:44:57 +00:00
|
|
|
|
|
|
|
/* Disable interrupts (not supported) */
|
2015-08-23 00:31:24 +00:00
|
|
|
chip->irq = 0;
|
2013-04-12 10:44:57 +00:00
|
|
|
|
|
|
|
/* Default timeouts */
|
2015-08-23 00:31:24 +00:00
|
|
|
chip->timeout_a = TIS_SHORT_TIMEOUT;
|
|
|
|
chip->timeout_b = TIS_LONG_TIMEOUT;
|
|
|
|
chip->timeout_c = TIS_SHORT_TIMEOUT;
|
|
|
|
chip->timeout_d = TIS_SHORT_TIMEOUT;
|
|
|
|
chip->req_complete_mask = TPM_STS_DATA_AVAIL | TPM_STS_VALID;
|
|
|
|
chip->req_complete_val = TPM_STS_DATA_AVAIL | TPM_STS_VALID;
|
|
|
|
chip->req_canceled = TPM_STS_COMMAND_READY;
|
2013-04-12 10:44:57 +00:00
|
|
|
|
2015-05-04 17:30:59 +00:00
|
|
|
if (request_locality(chip, 0) < 0)
|
|
|
|
return -ENODEV;
|
2013-04-12 10:44:57 +00:00
|
|
|
|
2013-04-12 11:04:37 +00:00
|
|
|
/* Read four bytes from DID_VID register */
|
2013-04-12 10:44:57 +00:00
|
|
|
if (iic_tpm_read(TPM_DID_VID(0), (uchar *)&vendor, 4) < 0) {
|
2015-05-04 17:30:59 +00:00
|
|
|
release_locality(chip, 0, 1);
|
|
|
|
return -EIO;
|
2013-04-12 10:44:57 +00:00
|
|
|
}
|
|
|
|
|
2015-08-23 00:31:25 +00:00
|
|
|
if (g_chip.chip_type == SLB9635) {
|
2013-04-12 11:04:36 +00:00
|
|
|
vendor = be32_to_cpu(vendor);
|
|
|
|
expected_did_vid = TPM_TIS_I2C_DID_VID_9635;
|
|
|
|
} else {
|
|
|
|
/* device id and byte order has changed for newer i2c tpms */
|
|
|
|
expected_did_vid = TPM_TIS_I2C_DID_VID_9645;
|
|
|
|
}
|
2013-04-12 10:44:57 +00:00
|
|
|
|
2015-08-23 00:31:25 +00:00
|
|
|
if (g_chip.chip_type != UNKNOWN && vendor != expected_did_vid) {
|
2013-04-12 11:04:37 +00:00
|
|
|
error("Vendor id did not match! ID was %08x\n", vendor);
|
2015-05-04 17:30:59 +00:00
|
|
|
return -ENODEV;
|
2013-04-12 10:44:57 +00:00
|
|
|
}
|
|
|
|
|
2013-04-12 11:04:37 +00:00
|
|
|
debug("1.2 TPM (chip type %s device-id 0x%X)\n",
|
2015-08-23 00:31:25 +00:00
|
|
|
chip_name[g_chip.chip_type], vendor >> 16);
|
2013-04-12 10:44:57 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* A timeout query to TPM can be placed here.
|
|
|
|
* Standard timeout values are used so far
|
|
|
|
*/
|
|
|
|
|
|
|
|
return 0;
|
2015-05-04 17:30:59 +00:00
|
|
|
}
|
|
|
|
|
2015-08-23 00:31:22 +00:00
|
|
|
/* Returns max number of milliseconds to wait */
|
|
|
|
static unsigned long tpm_calc_ordinal_duration(struct tpm_chip *chip,
|
|
|
|
u32 ordinal)
|
|
|
|
{
|
|
|
|
int duration_idx = TPM_UNDEFINED;
|
|
|
|
int duration = 0;
|
|
|
|
|
|
|
|
if (ordinal < TPM_MAX_ORDINAL) {
|
|
|
|
duration_idx = tpm_ordinal_duration[ordinal];
|
|
|
|
} else if ((ordinal & TPM_PROTECTED_ORDINAL_MASK) <
|
|
|
|
TPM_MAX_PROTECTED_ORDINAL) {
|
|
|
|
duration_idx = tpm_protected_ordinal_duration[
|
|
|
|
ordinal & TPM_PROTECTED_ORDINAL_MASK];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (duration_idx != TPM_UNDEFINED)
|
2015-08-23 00:31:24 +00:00
|
|
|
duration = chip->duration[duration_idx];
|
2015-08-23 00:31:22 +00:00
|
|
|
|
|
|
|
if (duration <= 0)
|
|
|
|
return 2 * 60 * HZ; /* Two minutes timeout */
|
|
|
|
else
|
|
|
|
return duration;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t tpm_transmit(const unsigned char *buf, size_t bufsiz)
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
u32 count, ordinal;
|
|
|
|
unsigned long start, stop;
|
|
|
|
|
|
|
|
struct tpm_chip *chip = &g_chip;
|
|
|
|
|
|
|
|
/* switch endianess: big->little */
|
|
|
|
count = get_unaligned_be32(buf + TPM_CMD_COUNT_BYTE);
|
|
|
|
ordinal = get_unaligned_be32(buf + TPM_CMD_ORDINAL_BYTE);
|
|
|
|
|
|
|
|
if (count == 0) {
|
|
|
|
error("no data\n");
|
|
|
|
return -ENODATA;
|
|
|
|
}
|
|
|
|
if (count > bufsiz) {
|
|
|
|
error("invalid count value %x %zx\n", count, bufsiz);
|
|
|
|
return -E2BIG;
|
|
|
|
}
|
|
|
|
|
|
|
|
debug("Calling send\n");
|
2015-08-23 00:31:23 +00:00
|
|
|
rc = tpm_tis_i2c_send(chip, (u8 *)buf, count);
|
2015-08-23 00:31:22 +00:00
|
|
|
debug(" ... done calling send\n");
|
|
|
|
if (rc < 0) {
|
|
|
|
error("tpm_transmit: tpm_send: error %d\n", rc);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2015-08-23 00:31:24 +00:00
|
|
|
if (chip->irq)
|
2015-08-23 00:31:22 +00:00
|
|
|
goto out_recv;
|
|
|
|
|
|
|
|
start = get_timer(0);
|
|
|
|
stop = tpm_calc_ordinal_duration(chip, ordinal);
|
|
|
|
do {
|
|
|
|
debug("waiting for status... %ld %ld\n", start, stop);
|
2015-08-23 00:31:23 +00:00
|
|
|
u8 status = tpm_tis_i2c_status(chip);
|
2015-08-23 00:31:24 +00:00
|
|
|
if ((status & chip->req_complete_mask) ==
|
|
|
|
chip->req_complete_val) {
|
2015-08-23 00:31:22 +00:00
|
|
|
debug("...got it;\n");
|
|
|
|
goto out_recv;
|
|
|
|
}
|
|
|
|
|
2015-08-23 00:31:24 +00:00
|
|
|
if (status == chip->req_canceled) {
|
2015-08-23 00:31:22 +00:00
|
|
|
error("Operation Canceled\n");
|
|
|
|
rc = -ECANCELED;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
udelay(TPM_TIMEOUT * 1000);
|
|
|
|
} while (get_timer(start) < stop);
|
|
|
|
|
2015-08-23 00:31:23 +00:00
|
|
|
tpm_tis_i2c_ready(chip);
|
2015-08-23 00:31:22 +00:00
|
|
|
error("Operation Timed out\n");
|
|
|
|
rc = -ETIME;
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
out_recv:
|
|
|
|
debug("out_recv: reading response...\n");
|
2015-08-23 00:31:23 +00:00
|
|
|
rc = tpm_tis_i2c_recv(chip, (u8 *)buf, TPM_BUFSIZE);
|
2015-08-23 00:31:22 +00:00
|
|
|
if (rc < 0)
|
|
|
|
error("tpm_transmit: tpm_recv: error %d\n", rc);
|
|
|
|
|
|
|
|
out:
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Decode TPM configuration.
|
|
|
|
*
|
|
|
|
* @param dev Returns a configuration of TPM device
|
|
|
|
* @return 0 if ok, -1 on error
|
|
|
|
*/
|
2015-08-23 00:31:26 +00:00
|
|
|
static int tpm_decode_config(struct tpm_chip *chip)
|
2015-08-23 00:31:22 +00:00
|
|
|
{
|
|
|
|
const void *blob = gd->fdt_blob;
|
|
|
|
struct udevice *bus;
|
|
|
|
int chip_addr;
|
|
|
|
int parent;
|
|
|
|
int node;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
node = fdtdec_next_compatible(blob, 0, COMPAT_INFINEON_SLB9635_TPM);
|
|
|
|
if (node < 0) {
|
|
|
|
node = fdtdec_next_compatible(blob, 0,
|
|
|
|
COMPAT_INFINEON_SLB9645_TPM);
|
|
|
|
}
|
|
|
|
if (node < 0) {
|
|
|
|
debug("%s: Node not found\n", __func__);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
parent = fdt_parent_offset(blob, node);
|
|
|
|
if (parent < 0) {
|
|
|
|
debug("%s: Cannot find node parent\n", __func__);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* TODO(sjg@chromium.org): Remove this when driver model supports
|
|
|
|
* TPMs
|
|
|
|
*/
|
|
|
|
ret = uclass_get_device_by_of_offset(UCLASS_I2C, parent, &bus);
|
|
|
|
if (ret) {
|
|
|
|
debug("Cannot find bus for node '%s: ret=%d'\n",
|
|
|
|
fdt_get_name(blob, parent, NULL), ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
chip_addr = fdtdec_get_int(blob, node, "reg", -1);
|
|
|
|
if (chip_addr == -1) {
|
|
|
|
debug("Cannot find reg property for node '%s: ret=%d'\n",
|
|
|
|
fdt_get_name(blob, node, NULL), ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* TODO(sjg@chromium.org): Older TPMs will need to use the older method
|
|
|
|
* in iic_tpm_read() so the offset length needs to be 0 here.
|
|
|
|
*/
|
2015-08-23 00:31:26 +00:00
|
|
|
ret = i2c_get_chip(bus, chip_addr, 1, &chip->dev);
|
2015-08-23 00:31:22 +00:00
|
|
|
if (ret) {
|
|
|
|
debug("Cannot find device for node '%s: ret=%d'\n",
|
|
|
|
fdt_get_name(blob, node, NULL), ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int tis_init(void)
|
|
|
|
{
|
2015-08-23 00:31:26 +00:00
|
|
|
if (g_chip.inited)
|
2015-08-23 00:31:22 +00:00
|
|
|
return 0;
|
|
|
|
|
2015-08-23 00:31:26 +00:00
|
|
|
if (tpm_decode_config(&g_chip))
|
2015-08-23 00:31:22 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
debug("%s: done\n", __func__);
|
|
|
|
|
2015-08-23 00:31:26 +00:00
|
|
|
g_chip.inited = 1;
|
2015-08-23 00:31:22 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int tis_open(void)
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
|
2015-08-23 00:31:26 +00:00
|
|
|
if (!g_chip.inited)
|
2015-08-23 00:31:22 +00:00
|
|
|
return -1;
|
|
|
|
|
2015-08-23 00:31:28 +00:00
|
|
|
debug("%s: start\n", __func__);
|
|
|
|
if (g_chip.is_open)
|
|
|
|
return -EBUSY;
|
|
|
|
rc = tpm_tis_i2c_init(g_chip.dev);
|
|
|
|
if (rc < 0)
|
|
|
|
g_chip.is_open = 0;
|
2015-08-23 00:31:22 +00:00
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
int tis_close(void)
|
|
|
|
{
|
2015-08-23 00:31:26 +00:00
|
|
|
if (!g_chip.inited)
|
2015-08-23 00:31:22 +00:00
|
|
|
return -1;
|
|
|
|
|
2015-08-23 00:31:28 +00:00
|
|
|
if (g_chip.is_open) {
|
|
|
|
release_locality(&g_chip, g_chip.locality, 1);
|
|
|
|
g_chip.is_open = 0;
|
|
|
|
}
|
2015-08-23 00:31:22 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int tis_sendrecv(const uint8_t *sendbuf, size_t sbuf_size,
|
|
|
|
uint8_t *recvbuf, size_t *rbuf_len)
|
|
|
|
{
|
|
|
|
int len;
|
|
|
|
uint8_t buf[4096];
|
|
|
|
|
2015-08-23 00:31:26 +00:00
|
|
|
if (!g_chip.inited)
|
2015-08-23 00:31:22 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (sizeof(buf) < sbuf_size)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
memcpy(buf, sendbuf, sbuf_size);
|
|
|
|
|
|
|
|
len = tpm_transmit(buf, sbuf_size);
|
|
|
|
|
|
|
|
if (len < 10) {
|
|
|
|
*rbuf_len = 0;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(recvbuf, buf, len);
|
|
|
|
*rbuf_len = len;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|