// SPDX-License-Identifier: GPL-2.0 /* * Copyright (c) 2014-2021 Nuvoton Technology corporation * Copyright (C) 2019-2022 Infineon Technologies AG * * This device driver implements the TPM interface as defined in the TCG PC * Client Platform TPM Profile (PTP) Specification for TPM 2.0 v1.04 * Revision 14. * * It is based on the tpm_tis_spi device driver.
*/
/* TIS-compatible register address to avoid clash with TPM_ACCESS (0x00) */ #define TPM_LOC_SEL 0x0FFF
/* Mask to extract the I2C register from TIS register addresses */ #define TPM_TIS_REGISTER_MASK 0x0FFF
/* Default Guard Time of 250µs until interface capability register is read */ #define GUARD_TIME_DEFAULT_MIN 250 #define GUARD_TIME_DEFAULT_MAX 300
/* Guard Time of 250µs after I2C slave NACK */ #define GUARD_TIME_ERR_MIN 250 #define GUARD_TIME_ERR_MAX 300
/* Guard Time bit masks; SR is repeated start, RW is read then write, etc. */ #define TPM_GUARD_TIME_SR_MASK 0x40000000 #define TPM_GUARD_TIME_RR_MASK 0x00100000 #define TPM_GUARD_TIME_RW_MASK 0x00080000 #define TPM_GUARD_TIME_WR_MASK 0x00040000 #define TPM_GUARD_TIME_WW_MASK 0x00020000 #define TPM_GUARD_TIME_MIN_MASK 0x0001FE00 #define TPM_GUARD_TIME_MIN_SHIFT 9
/* Masks with bits that must be read zero */ #define TPM_ACCESS_READ_ZERO 0x48 #define TPM_INT_ENABLE_ZERO 0x7FFFFF60 #define TPM_STS_READ_ZERO 0x23 #define TPM_INTF_CAPABILITY_ZERO 0x0FFFF000 #define TPM_I2C_INTERFACE_CAPABILITY_ZERO 0x80000000
/* * tpm_tis_core uses the register addresses as defined in Table 19 "Allocation * of Register Space for FIFO TPM Access" of the TCG PC Client PTP * Specification. In order for this code to work together with tpm_tis_core, * those addresses need to mapped to the registers defined for I2C TPMs in * Table 51 "I2C-TPM Register Overview". * * For most addresses this can be done by simply stripping off the locality * information from the address. A few addresses need to be mapped explicitly, * since the corresponding I2C registers have been moved around. TPM_LOC_SEL is * only defined for I2C TPMs and is also mapped explicitly here to distinguish * it from TPM_ACCESS(0). * * Locality information is ignored, since this driver assumes exclusive access * to the TPM and always uses locality 0.
*/ static u8 tpm_tis_i2c_address_to_register(u32 addr)
{
addr &= TPM_TIS_REGISTER_MASK;
switch (addr) { case TPM_ACCESS(0): return TPM_I2C_ACCESS; case TPM_LOC_SEL: return TPM_I2C_LOC_SEL; case TPM_DID_VID(0): return TPM_I2C_DID_VID; case TPM_RID(0): return TPM_I2C_RID; default: return addr;
}
}
staticint tpm_tis_i2c_retry_transfer_until_ack(struct tpm_tis_data *data, struct i2c_msg *msg)
{ struct tpm_tis_i2c_phy *phy = to_tpm_tis_i2c_phy(data); bool guard_time; int i = 0; int ret;
do {
ret = i2c_transfer(phy->i2c_client->adapter, msg, 1); if (ret < 0)
usleep_range(GUARD_TIME_ERR_MIN, GUARD_TIME_ERR_MAX); elseif (guard_time)
usleep_range(phy->guard_time_min, phy->guard_time_max); /* retry on TPM NACK */
} while (ret < 0 && i++ < TPM_RETRY);
return ret;
}
/* Check that bits which must be read zero are not set */ staticint tpm_tis_i2c_sanity_check_read(u8 reg, u16 len, u8 *buf)
{
u32 zero_mask;
u32 value;
switch (len) { casesizeof(u8):
value = buf[0]; break; casesizeof(u16):
value = le16_to_cpup((__le16 *)buf); break; casesizeof(u32):
value = le32_to_cpup((__le32 *)buf); break; default: /* unknown length, skip check */ return 0;
}
switch (reg) { case TPM_I2C_ACCESS:
zero_mask = TPM_ACCESS_READ_ZERO; break; case TPM_INT_ENABLE(0) & TPM_TIS_REGISTER_MASK:
zero_mask = TPM_INT_ENABLE_ZERO; break; case TPM_STS(0) & TPM_TIS_REGISTER_MASK:
zero_mask = TPM_STS_READ_ZERO; break; case TPM_INTF_CAPS(0) & TPM_TIS_REGISTER_MASK:
zero_mask = TPM_INTF_CAPABILITY_ZERO; break; case TPM_I2C_INTERFACE_CAPABILITY:
zero_mask = TPM_I2C_INTERFACE_CAPABILITY_ZERO; break; default: /* unknown register, skip check */ return 0;
}
phy->io_buf[0] = reg;
msg.buf = phy->io_buf; while (wrote < len) { /* write register and data in one go */
msg.len = sizeof(reg) + len - wrote; if (msg.len > I2C_SMBUS_BLOCK_MAX)
msg.len = I2C_SMBUS_BLOCK_MAX;
memcpy(phy->io_buf + sizeof(reg), value + wrote,
msg.len - sizeof(reg));
ret = tpm_tis_i2c_retry_transfer_until_ack(data, &msg); if (ret < 0) return ret;
wrote += msg.len - sizeof(reg);
}
/* reflect crc result, regardless of host endianness */
crc_host = swab16(crc_ccitt(0, value, len)); if (crc_tpm != crc_host) return -EIO;
return 0;
}
/* * Guard Time: * After each I2C operation, the TPM might require the master to wait. * The time period is vendor-specific and must be read from the * TPM_I2C_INTERFACE_CAPABILITY register. * * Before the Guard Time is read (or after the TPM failed to send an I2C NACK), * a Guard Time of 250µs applies. * * Various flags in the same register indicate if a guard time is needed: * - SR: <I2C read with repeated start> <guard time> <I2C read> * - RR: <I2C read> <guard time> <I2C read> * - RW: <I2C read> <guard time> <I2C write> * - WR: <I2C write> <guard time> <I2C read> * - WW: <I2C write> <guard time> <I2C write> * * See TCG PC Client PTP Specification v1.04, 8.1.10 GUARD_TIME
*/ staticint tpm_tis_i2c_init_guard_time(struct tpm_tis_i2c_phy *phy)
{
u32 i2c_caps; int ret;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.