1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * ATMEL I2C TPM AT97SC3204T
5 * Copyright (C) 2012 V Lab Technologies
7 * Copyright (C) 2013, Obsidian Research Corp.
9 * Device driver for ATMEL I2C TPMs.
11 * Teddy Reed determined the basic I2C command flow, unlike other I2C TPM
12 * devices the raw TCG formatted TPM command data is written via I2C and then
13 * raw TCG formatted TPM command data is returned via I2C.
15 * TGC status/locality/etc functions seen in the LPC implementation do not
18 #include <linux/init.h>
19 #include <linux/module.h>
20 #include <linux/moduleparam.h>
21 #include <linux/slab.h>
22 #include <linux/i2c.h>
25 #define I2C_DRIVER_NAME "tpm_i2c_atmel"
27 #define TPM_I2C_SHORT_TIMEOUT 750 /* ms */
28 #define TPM_I2C_LONG_TIMEOUT 2000 /* 2 sec */
30 #define ATMEL_STS_OK 1
34 /* This is the amount we read on the first try. 25 was chosen to fit a
35 * fair number of read responses in the buffer so a 2nd retry can be
36 * avoided in small message cases. */
37 u8 buffer[sizeof(struct tpm_header) + 25];
40 static int i2c_atmel_send(struct tpm_chip *chip, u8 *buf, size_t len)
42 struct priv_data *priv = dev_get_drvdata(&chip->dev);
43 struct i2c_client *client = to_i2c_client(chip->dev.parent);
51 status = i2c_master_send(client, buf, len);
54 "%s(buf=%*ph len=%0zx) -> sts=%d\n", __func__,
55 (int)min_t(size_t, 64, len), buf, len, status);
60 /* The upper layer does not support incomplete sends. */
67 static int i2c_atmel_recv(struct tpm_chip *chip, u8 *buf, size_t count)
69 struct priv_data *priv = dev_get_drvdata(&chip->dev);
70 struct i2c_client *client = to_i2c_client(chip->dev.parent);
71 struct tpm_header *hdr = (struct tpm_header *)priv->buffer;
78 /* Get the message size from the message header, if we didn't get the
79 * whole message in read_status then we need to re-read the
81 expected_len = be32_to_cpu(hdr->length);
82 if (expected_len > count)
85 if (priv->len >= expected_len) {
87 "%s early(buf=%*ph count=%0zx) -> ret=%d\n", __func__,
88 (int)min_t(size_t, 64, expected_len), buf, count,
90 memcpy(buf, priv->buffer, expected_len);
94 rc = i2c_master_recv(client, buf, expected_len);
96 "%s reread(buf=%*ph count=%0zx) -> ret=%d\n", __func__,
97 (int)min_t(size_t, 64, expected_len), buf, count,
102 static void i2c_atmel_cancel(struct tpm_chip *chip)
104 dev_err(&chip->dev, "TPM operation cancellation was requested, but is not supported");
107 static u8 i2c_atmel_read_status(struct tpm_chip *chip)
109 struct priv_data *priv = dev_get_drvdata(&chip->dev);
110 struct i2c_client *client = to_i2c_client(chip->dev.parent);
113 /* The TPM fails the I2C read until it is ready, so we do the entire
114 * transfer here and buffer it locally. This way the common code can
115 * properly handle the timeouts. */
117 memset(priv->buffer, 0, sizeof(priv->buffer));
120 /* Once the TPM has completed the command the command remains readable
121 * until another command is issued. */
122 rc = i2c_master_recv(client, priv->buffer, sizeof(priv->buffer));
124 "%s: sts=%d", __func__, rc);
133 static bool i2c_atmel_req_canceled(struct tpm_chip *chip, u8 status)
138 static const struct tpm_class_ops i2c_atmel = {
139 .flags = TPM_OPS_AUTO_STARTUP,
140 .status = i2c_atmel_read_status,
141 .recv = i2c_atmel_recv,
142 .send = i2c_atmel_send,
143 .cancel = i2c_atmel_cancel,
144 .req_complete_mask = ATMEL_STS_OK,
145 .req_complete_val = ATMEL_STS_OK,
146 .req_canceled = i2c_atmel_req_canceled,
149 static int i2c_atmel_probe(struct i2c_client *client)
151 struct tpm_chip *chip;
152 struct device *dev = &client->dev;
153 struct priv_data *priv;
155 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
158 chip = tpmm_chip_alloc(dev, &i2c_atmel);
160 return PTR_ERR(chip);
162 priv = devm_kzalloc(dev, sizeof(struct priv_data), GFP_KERNEL);
166 /* Default timeouts */
167 chip->timeout_a = msecs_to_jiffies(TPM_I2C_SHORT_TIMEOUT);
168 chip->timeout_b = msecs_to_jiffies(TPM_I2C_LONG_TIMEOUT);
169 chip->timeout_c = msecs_to_jiffies(TPM_I2C_SHORT_TIMEOUT);
170 chip->timeout_d = msecs_to_jiffies(TPM_I2C_SHORT_TIMEOUT);
172 dev_set_drvdata(&chip->dev, priv);
174 /* There is no known way to probe for this device, and all version
175 * information seems to be read via TPM commands. Thus we rely on the
176 * TPM startup process in the common code to detect the device. */
178 return tpm_chip_register(chip);
181 static void i2c_atmel_remove(struct i2c_client *client)
183 struct device *dev = &(client->dev);
184 struct tpm_chip *chip = dev_get_drvdata(dev);
185 tpm_chip_unregister(chip);
188 static const struct i2c_device_id i2c_atmel_id[] = {
189 {I2C_DRIVER_NAME, 0},
192 MODULE_DEVICE_TABLE(i2c, i2c_atmel_id);
195 static const struct of_device_id i2c_atmel_of_match[] = {
196 {.compatible = "atmel,at97sc3204t"},
199 MODULE_DEVICE_TABLE(of, i2c_atmel_of_match);
202 static SIMPLE_DEV_PM_OPS(i2c_atmel_pm_ops, tpm_pm_suspend, tpm_pm_resume);
204 static struct i2c_driver i2c_atmel_driver = {
205 .id_table = i2c_atmel_id,
206 .probe = i2c_atmel_probe,
207 .remove = i2c_atmel_remove,
209 .name = I2C_DRIVER_NAME,
210 .pm = &i2c_atmel_pm_ops,
211 .of_match_table = of_match_ptr(i2c_atmel_of_match),
215 module_i2c_driver(i2c_atmel_driver);
218 MODULE_DESCRIPTION("Atmel TPM I2C Driver");
219 MODULE_LICENSE("GPL");