0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #include <linux/i2c.h>
0014 #include <linux/crc-ccitt.h>
0015 #include "tpm_tis_core.h"
0016
0017
0018 #define TPM_I2C_LOC_SEL 0x00
0019 #define TPM_I2C_ACCESS 0x04
0020 #define TPM_I2C_INTERFACE_CAPABILITY 0x30
0021 #define TPM_I2C_DEVICE_ADDRESS 0x38
0022 #define TPM_I2C_DATA_CSUM_ENABLE 0x40
0023 #define TPM_DATA_CSUM 0x44
0024 #define TPM_I2C_DID_VID 0x48
0025 #define TPM_I2C_RID 0x4C
0026
0027
0028 #define TPM_LOC_SEL 0x0FFF
0029
0030
0031 #define TPM_TIS_REGISTER_MASK 0x0FFF
0032
0033
0034 #define GUARD_TIME_DEFAULT_MIN 250
0035 #define GUARD_TIME_DEFAULT_MAX 300
0036
0037
0038 #define GUARD_TIME_ERR_MIN 250
0039 #define GUARD_TIME_ERR_MAX 300
0040
0041
0042 #define TPM_GUARD_TIME_SR_MASK 0x40000000
0043 #define TPM_GUARD_TIME_RR_MASK 0x00100000
0044 #define TPM_GUARD_TIME_RW_MASK 0x00080000
0045 #define TPM_GUARD_TIME_WR_MASK 0x00040000
0046 #define TPM_GUARD_TIME_WW_MASK 0x00020000
0047 #define TPM_GUARD_TIME_MIN_MASK 0x0001FE00
0048 #define TPM_GUARD_TIME_MIN_SHIFT 9
0049
0050
0051 #define TPM_ACCESS_READ_ZERO 0x48
0052 #define TPM_INT_ENABLE_ZERO 0x7FFFFF6
0053 #define TPM_STS_READ_ZERO 0x23
0054 #define TPM_INTF_CAPABILITY_ZERO 0x0FFFF000
0055 #define TPM_I2C_INTERFACE_CAPABILITY_ZERO 0x80000000
0056
0057 struct tpm_tis_i2c_phy {
0058 struct tpm_tis_data priv;
0059 struct i2c_client *i2c_client;
0060 bool guard_time_read;
0061 bool guard_time_write;
0062 u16 guard_time_min;
0063 u16 guard_time_max;
0064 u8 *io_buf;
0065 };
0066
0067 static inline struct tpm_tis_i2c_phy *
0068 to_tpm_tis_i2c_phy(struct tpm_tis_data *data)
0069 {
0070 return container_of(data, struct tpm_tis_i2c_phy, priv);
0071 }
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087
0088
0089 static u8 tpm_tis_i2c_address_to_register(u32 addr)
0090 {
0091 addr &= TPM_TIS_REGISTER_MASK;
0092
0093 switch (addr) {
0094 case TPM_ACCESS(0):
0095 return TPM_I2C_ACCESS;
0096 case TPM_LOC_SEL:
0097 return TPM_I2C_LOC_SEL;
0098 case TPM_DID_VID(0):
0099 return TPM_I2C_DID_VID;
0100 case TPM_RID(0):
0101 return TPM_I2C_RID;
0102 default:
0103 return addr;
0104 }
0105 }
0106
0107 static int tpm_tis_i2c_retry_transfer_until_ack(struct tpm_tis_data *data,
0108 struct i2c_msg *msg)
0109 {
0110 struct tpm_tis_i2c_phy *phy = to_tpm_tis_i2c_phy(data);
0111 bool guard_time;
0112 int i = 0;
0113 int ret;
0114
0115 if (msg->flags & I2C_M_RD)
0116 guard_time = phy->guard_time_read;
0117 else
0118 guard_time = phy->guard_time_write;
0119
0120 do {
0121 ret = i2c_transfer(phy->i2c_client->adapter, msg, 1);
0122 if (ret < 0)
0123 usleep_range(GUARD_TIME_ERR_MIN, GUARD_TIME_ERR_MAX);
0124 else if (guard_time)
0125 usleep_range(phy->guard_time_min, phy->guard_time_max);
0126
0127 } while (ret < 0 && i++ < TPM_RETRY);
0128
0129 return ret;
0130 }
0131
0132
0133 static int tpm_tis_i2c_sanity_check_read(u8 reg, u16 len, u8 *buf)
0134 {
0135 u32 zero_mask;
0136 u32 value;
0137
0138 switch (len) {
0139 case sizeof(u8):
0140 value = buf[0];
0141 break;
0142 case sizeof(u16):
0143 value = le16_to_cpup((__le16 *)buf);
0144 break;
0145 case sizeof(u32):
0146 value = le32_to_cpup((__le32 *)buf);
0147 break;
0148 default:
0149
0150 return 0;
0151 }
0152
0153 switch (reg) {
0154 case TPM_I2C_ACCESS:
0155 zero_mask = TPM_ACCESS_READ_ZERO;
0156 break;
0157 case TPM_INT_ENABLE(0) & TPM_TIS_REGISTER_MASK:
0158 zero_mask = TPM_INT_ENABLE_ZERO;
0159 break;
0160 case TPM_STS(0) & TPM_TIS_REGISTER_MASK:
0161 zero_mask = TPM_STS_READ_ZERO;
0162 break;
0163 case TPM_INTF_CAPS(0) & TPM_TIS_REGISTER_MASK:
0164 zero_mask = TPM_INTF_CAPABILITY_ZERO;
0165 break;
0166 case TPM_I2C_INTERFACE_CAPABILITY:
0167 zero_mask = TPM_I2C_INTERFACE_CAPABILITY_ZERO;
0168 break;
0169 default:
0170
0171 return 0;
0172 }
0173
0174 if (unlikely((value & zero_mask) != 0x00)) {
0175 pr_debug("TPM I2C read of register 0x%02x failed sanity check: 0x%x\n", reg, value);
0176 return -EIO;
0177 }
0178
0179 return 0;
0180 }
0181
0182 static int tpm_tis_i2c_read_bytes(struct tpm_tis_data *data, u32 addr, u16 len,
0183 u8 *result, enum tpm_tis_io_mode io_mode)
0184 {
0185 struct tpm_tis_i2c_phy *phy = to_tpm_tis_i2c_phy(data);
0186 struct i2c_msg msg = { .addr = phy->i2c_client->addr };
0187 u8 reg = tpm_tis_i2c_address_to_register(addr);
0188 int i;
0189 int ret;
0190
0191 for (i = 0; i < TPM_RETRY; i++) {
0192
0193 msg.len = sizeof(reg);
0194 msg.buf = ®
0195 msg.flags = 0;
0196 ret = tpm_tis_i2c_retry_transfer_until_ack(data, &msg);
0197 if (ret < 0)
0198 return ret;
0199
0200
0201 msg.buf = result;
0202 msg.len = len;
0203 msg.flags = I2C_M_RD;
0204 ret = tpm_tis_i2c_retry_transfer_until_ack(data, &msg);
0205 if (ret < 0)
0206 return ret;
0207
0208 ret = tpm_tis_i2c_sanity_check_read(reg, len, result);
0209 if (ret == 0)
0210 return 0;
0211
0212 usleep_range(GUARD_TIME_ERR_MIN, GUARD_TIME_ERR_MAX);
0213 }
0214
0215 return ret;
0216 }
0217
0218 static int tpm_tis_i2c_write_bytes(struct tpm_tis_data *data, u32 addr, u16 len,
0219 const u8 *value,
0220 enum tpm_tis_io_mode io_mode)
0221 {
0222 struct tpm_tis_i2c_phy *phy = to_tpm_tis_i2c_phy(data);
0223 struct i2c_msg msg = { .addr = phy->i2c_client->addr };
0224 u8 reg = tpm_tis_i2c_address_to_register(addr);
0225 int ret;
0226
0227 if (len > TPM_BUFSIZE - 1)
0228 return -EIO;
0229
0230
0231 phy->io_buf[0] = reg;
0232 memcpy(phy->io_buf + sizeof(reg), value, len);
0233
0234 msg.len = sizeof(reg) + len;
0235 msg.buf = phy->io_buf;
0236 ret = tpm_tis_i2c_retry_transfer_until_ack(data, &msg);
0237 if (ret < 0)
0238 return ret;
0239
0240 return 0;
0241 }
0242
0243 static int tpm_tis_i2c_verify_crc(struct tpm_tis_data *data, size_t len,
0244 const u8 *value)
0245 {
0246 u16 crc_tpm, crc_host;
0247 int rc;
0248
0249 rc = tpm_tis_read16(data, TPM_DATA_CSUM, &crc_tpm);
0250 if (rc < 0)
0251 return rc;
0252
0253
0254 crc_host = swab16(crc_ccitt(0, value, len));
0255 if (crc_tpm != crc_host)
0256 return -EIO;
0257
0258 return 0;
0259 }
0260
0261
0262
0263
0264
0265
0266
0267
0268
0269
0270
0271
0272
0273
0274
0275
0276
0277
0278
0279 static int tpm_tis_i2c_init_guard_time(struct tpm_tis_i2c_phy *phy)
0280 {
0281 u32 i2c_caps;
0282 int ret;
0283
0284 phy->guard_time_read = true;
0285 phy->guard_time_write = true;
0286 phy->guard_time_min = GUARD_TIME_DEFAULT_MIN;
0287 phy->guard_time_max = GUARD_TIME_DEFAULT_MAX;
0288
0289 ret = tpm_tis_i2c_read_bytes(&phy->priv, TPM_I2C_INTERFACE_CAPABILITY,
0290 sizeof(i2c_caps), (u8 *)&i2c_caps,
0291 TPM_TIS_PHYS_32);
0292 if (ret)
0293 return ret;
0294
0295 phy->guard_time_read = (i2c_caps & TPM_GUARD_TIME_RR_MASK) ||
0296 (i2c_caps & TPM_GUARD_TIME_RW_MASK);
0297 phy->guard_time_write = (i2c_caps & TPM_GUARD_TIME_WR_MASK) ||
0298 (i2c_caps & TPM_GUARD_TIME_WW_MASK);
0299 phy->guard_time_min = (i2c_caps & TPM_GUARD_TIME_MIN_MASK) >>
0300 TPM_GUARD_TIME_MIN_SHIFT;
0301
0302 phy->guard_time_max = phy->guard_time_min + phy->guard_time_min / 5;
0303
0304 return 0;
0305 }
0306
0307 static SIMPLE_DEV_PM_OPS(tpm_tis_pm, tpm_pm_suspend, tpm_tis_resume);
0308
0309 static const struct tpm_tis_phy_ops tpm_i2c_phy_ops = {
0310 .read_bytes = tpm_tis_i2c_read_bytes,
0311 .write_bytes = tpm_tis_i2c_write_bytes,
0312 .verify_crc = tpm_tis_i2c_verify_crc,
0313 };
0314
0315 static int tpm_tis_i2c_probe(struct i2c_client *dev,
0316 const struct i2c_device_id *id)
0317 {
0318 struct tpm_tis_i2c_phy *phy;
0319 const u8 crc_enable = 1;
0320 const u8 locality = 0;
0321 int ret;
0322
0323 phy = devm_kzalloc(&dev->dev, sizeof(struct tpm_tis_i2c_phy),
0324 GFP_KERNEL);
0325 if (!phy)
0326 return -ENOMEM;
0327
0328 phy->io_buf = devm_kzalloc(&dev->dev, TPM_BUFSIZE, GFP_KERNEL);
0329 if (!phy->io_buf)
0330 return -ENOMEM;
0331
0332 phy->i2c_client = dev;
0333
0334
0335 ret = tpm_tis_i2c_init_guard_time(phy);
0336 if (ret)
0337 return ret;
0338
0339 ret = tpm_tis_i2c_write_bytes(&phy->priv, TPM_LOC_SEL, sizeof(locality),
0340 &locality, TPM_TIS_PHYS_8);
0341 if (ret)
0342 return ret;
0343
0344 ret = tpm_tis_i2c_write_bytes(&phy->priv, TPM_I2C_DATA_CSUM_ENABLE,
0345 sizeof(crc_enable), &crc_enable,
0346 TPM_TIS_PHYS_8);
0347 if (ret)
0348 return ret;
0349
0350 return tpm_tis_core_init(&dev->dev, &phy->priv, -1, &tpm_i2c_phy_ops,
0351 NULL);
0352 }
0353
0354 static int tpm_tis_i2c_remove(struct i2c_client *client)
0355 {
0356 struct tpm_chip *chip = i2c_get_clientdata(client);
0357
0358 tpm_chip_unregister(chip);
0359 tpm_tis_remove(chip);
0360 return 0;
0361 }
0362
0363 static const struct i2c_device_id tpm_tis_i2c_id[] = {
0364 { "tpm_tis_i2c", 0 },
0365 {}
0366 };
0367 MODULE_DEVICE_TABLE(i2c, tpm_tis_i2c_id);
0368
0369 #ifdef CONFIG_OF
0370 static const struct of_device_id of_tis_i2c_match[] = {
0371 { .compatible = "infineon,slb9673", },
0372 {}
0373 };
0374 MODULE_DEVICE_TABLE(of, of_tis_i2c_match);
0375 #endif
0376
0377 static struct i2c_driver tpm_tis_i2c_driver = {
0378 .driver = {
0379 .name = "tpm_tis_i2c",
0380 .pm = &tpm_tis_pm,
0381 .of_match_table = of_match_ptr(of_tis_i2c_match),
0382 },
0383 .probe = tpm_tis_i2c_probe,
0384 .remove = tpm_tis_i2c_remove,
0385 .id_table = tpm_tis_i2c_id,
0386 };
0387 module_i2c_driver(tpm_tis_i2c_driver);
0388
0389 MODULE_DESCRIPTION("TPM Driver for native I2C access");
0390 MODULE_LICENSE("GPL");