0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018 #include <linux/init.h>
0019 #include <linux/module.h>
0020 #include <linux/moduleparam.h>
0021 #include <linux/slab.h>
0022 #include <linux/i2c.h>
0023 #include "tpm.h"
0024
0025 #define I2C_DRIVER_NAME "tpm_i2c_atmel"
0026
0027 #define TPM_I2C_SHORT_TIMEOUT 750
0028 #define TPM_I2C_LONG_TIMEOUT 2000
0029
0030 #define ATMEL_STS_OK 1
0031
0032 struct priv_data {
0033 size_t len;
0034
0035
0036
0037 u8 buffer[sizeof(struct tpm_header) + 25];
0038 };
0039
0040 static int i2c_atmel_send(struct tpm_chip *chip, u8 *buf, size_t len)
0041 {
0042 struct priv_data *priv = dev_get_drvdata(&chip->dev);
0043 struct i2c_client *client = to_i2c_client(chip->dev.parent);
0044 s32 status;
0045
0046 priv->len = 0;
0047
0048 if (len <= 2)
0049 return -EIO;
0050
0051 status = i2c_master_send(client, buf, len);
0052
0053 dev_dbg(&chip->dev,
0054 "%s(buf=%*ph len=%0zx) -> sts=%d\n", __func__,
0055 (int)min_t(size_t, 64, len), buf, len, status);
0056
0057 if (status < 0)
0058 return status;
0059
0060
0061 if (status != len)
0062 return -E2BIG;
0063
0064 return 0;
0065 }
0066
0067 static int i2c_atmel_recv(struct tpm_chip *chip, u8 *buf, size_t count)
0068 {
0069 struct priv_data *priv = dev_get_drvdata(&chip->dev);
0070 struct i2c_client *client = to_i2c_client(chip->dev.parent);
0071 struct tpm_header *hdr = (struct tpm_header *)priv->buffer;
0072 u32 expected_len;
0073 int rc;
0074
0075 if (priv->len == 0)
0076 return -EIO;
0077
0078
0079
0080
0081 expected_len = be32_to_cpu(hdr->length);
0082 if (expected_len > count)
0083 return -ENOMEM;
0084
0085 if (priv->len >= expected_len) {
0086 dev_dbg(&chip->dev,
0087 "%s early(buf=%*ph count=%0zx) -> ret=%d\n", __func__,
0088 (int)min_t(size_t, 64, expected_len), buf, count,
0089 expected_len);
0090 memcpy(buf, priv->buffer, expected_len);
0091 return expected_len;
0092 }
0093
0094 rc = i2c_master_recv(client, buf, expected_len);
0095 dev_dbg(&chip->dev,
0096 "%s reread(buf=%*ph count=%0zx) -> ret=%d\n", __func__,
0097 (int)min_t(size_t, 64, expected_len), buf, count,
0098 expected_len);
0099 return rc;
0100 }
0101
0102 static void i2c_atmel_cancel(struct tpm_chip *chip)
0103 {
0104 dev_err(&chip->dev, "TPM operation cancellation was requested, but is not supported");
0105 }
0106
0107 static u8 i2c_atmel_read_status(struct tpm_chip *chip)
0108 {
0109 struct priv_data *priv = dev_get_drvdata(&chip->dev);
0110 struct i2c_client *client = to_i2c_client(chip->dev.parent);
0111 int rc;
0112
0113
0114
0115
0116 priv->len = 0;
0117 memset(priv->buffer, 0, sizeof(priv->buffer));
0118
0119
0120
0121
0122 rc = i2c_master_recv(client, priv->buffer, sizeof(priv->buffer));
0123 dev_dbg(&chip->dev,
0124 "%s: sts=%d", __func__, rc);
0125 if (rc <= 0)
0126 return 0;
0127
0128 priv->len = rc;
0129
0130 return ATMEL_STS_OK;
0131 }
0132
0133 static bool i2c_atmel_req_canceled(struct tpm_chip *chip, u8 status)
0134 {
0135 return false;
0136 }
0137
0138 static const struct tpm_class_ops i2c_atmel = {
0139 .flags = TPM_OPS_AUTO_STARTUP,
0140 .status = i2c_atmel_read_status,
0141 .recv = i2c_atmel_recv,
0142 .send = i2c_atmel_send,
0143 .cancel = i2c_atmel_cancel,
0144 .req_complete_mask = ATMEL_STS_OK,
0145 .req_complete_val = ATMEL_STS_OK,
0146 .req_canceled = i2c_atmel_req_canceled,
0147 };
0148
0149 static int i2c_atmel_probe(struct i2c_client *client,
0150 const struct i2c_device_id *id)
0151 {
0152 struct tpm_chip *chip;
0153 struct device *dev = &client->dev;
0154 struct priv_data *priv;
0155
0156 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
0157 return -ENODEV;
0158
0159 chip = tpmm_chip_alloc(dev, &i2c_atmel);
0160 if (IS_ERR(chip))
0161 return PTR_ERR(chip);
0162
0163 priv = devm_kzalloc(dev, sizeof(struct priv_data), GFP_KERNEL);
0164 if (!priv)
0165 return -ENOMEM;
0166
0167
0168 chip->timeout_a = msecs_to_jiffies(TPM_I2C_SHORT_TIMEOUT);
0169 chip->timeout_b = msecs_to_jiffies(TPM_I2C_LONG_TIMEOUT);
0170 chip->timeout_c = msecs_to_jiffies(TPM_I2C_SHORT_TIMEOUT);
0171 chip->timeout_d = msecs_to_jiffies(TPM_I2C_SHORT_TIMEOUT);
0172
0173 dev_set_drvdata(&chip->dev, priv);
0174
0175
0176
0177
0178
0179 return tpm_chip_register(chip);
0180 }
0181
0182 static int i2c_atmel_remove(struct i2c_client *client)
0183 {
0184 struct device *dev = &(client->dev);
0185 struct tpm_chip *chip = dev_get_drvdata(dev);
0186 tpm_chip_unregister(chip);
0187 return 0;
0188 }
0189
0190 static const struct i2c_device_id i2c_atmel_id[] = {
0191 {I2C_DRIVER_NAME, 0},
0192 {}
0193 };
0194 MODULE_DEVICE_TABLE(i2c, i2c_atmel_id);
0195
0196 #ifdef CONFIG_OF
0197 static const struct of_device_id i2c_atmel_of_match[] = {
0198 {.compatible = "atmel,at97sc3204t"},
0199 {},
0200 };
0201 MODULE_DEVICE_TABLE(of, i2c_atmel_of_match);
0202 #endif
0203
0204 static SIMPLE_DEV_PM_OPS(i2c_atmel_pm_ops, tpm_pm_suspend, tpm_pm_resume);
0205
0206 static struct i2c_driver i2c_atmel_driver = {
0207 .id_table = i2c_atmel_id,
0208 .probe = i2c_atmel_probe,
0209 .remove = i2c_atmel_remove,
0210 .driver = {
0211 .name = I2C_DRIVER_NAME,
0212 .pm = &i2c_atmel_pm_ops,
0213 .of_match_table = of_match_ptr(i2c_atmel_of_match),
0214 },
0215 };
0216
0217 module_i2c_driver(i2c_atmel_driver);
0218
0219 MODULE_AUTHOR("Jason Gunthorpe <jgunthorpe@obsidianresearch.com>");
0220 MODULE_DESCRIPTION("Atmel TPM I2C Driver");
0221 MODULE_LICENSE("GPL");