11ccea77eSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2a2871c62SJason Gunthorpe /*
3a2871c62SJason Gunthorpe * ATMEL I2C TPM AT97SC3204T
4a2871c62SJason Gunthorpe *
5a2871c62SJason Gunthorpe * Copyright (C) 2012 V Lab Technologies
6a2871c62SJason Gunthorpe * Teddy Reed <teddy@prosauce.org>
7a2871c62SJason Gunthorpe * Copyright (C) 2013, Obsidian Research Corp.
8a2871c62SJason Gunthorpe * Jason Gunthorpe <jgunthorpe@obsidianresearch.com>
9a2871c62SJason Gunthorpe * Device driver for ATMEL I2C TPMs.
10a2871c62SJason Gunthorpe *
11a2871c62SJason Gunthorpe * Teddy Reed determined the basic I2C command flow, unlike other I2C TPM
12a2871c62SJason Gunthorpe * devices the raw TCG formatted TPM command data is written via I2C and then
13a2871c62SJason Gunthorpe * raw TCG formatted TPM command data is returned via I2C.
14a2871c62SJason Gunthorpe *
15a2871c62SJason Gunthorpe * TGC status/locality/etc functions seen in the LPC implementation do not
16a2871c62SJason Gunthorpe * seem to be present.
17a2871c62SJason Gunthorpe */
18a2871c62SJason Gunthorpe #include <linux/init.h>
19a2871c62SJason Gunthorpe #include <linux/module.h>
20a2871c62SJason Gunthorpe #include <linux/moduleparam.h>
21a2871c62SJason Gunthorpe #include <linux/slab.h>
22a2871c62SJason Gunthorpe #include <linux/i2c.h>
23a2871c62SJason Gunthorpe #include "tpm.h"
24a2871c62SJason Gunthorpe
25a2871c62SJason Gunthorpe #define I2C_DRIVER_NAME "tpm_i2c_atmel"
26a2871c62SJason Gunthorpe
27a2871c62SJason Gunthorpe #define TPM_I2C_SHORT_TIMEOUT 750 /* ms */
28a2871c62SJason Gunthorpe #define TPM_I2C_LONG_TIMEOUT 2000 /* 2 sec */
29a2871c62SJason Gunthorpe
30a2871c62SJason Gunthorpe #define ATMEL_STS_OK 1
31a2871c62SJason Gunthorpe
32a2871c62SJason Gunthorpe struct priv_data {
33a2871c62SJason Gunthorpe size_t len;
34a2871c62SJason Gunthorpe /* This is the amount we read on the first try. 25 was chosen to fit a
35a2871c62SJason Gunthorpe * fair number of read responses in the buffer so a 2nd retry can be
36a2871c62SJason Gunthorpe * avoided in small message cases. */
37b34b77a9SJarkko Sakkinen u8 buffer[sizeof(struct tpm_header) + 25];
38a2871c62SJason Gunthorpe };
39a2871c62SJason Gunthorpe
i2c_atmel_send(struct tpm_chip * chip,u8 * buf,size_t len)40a2871c62SJason Gunthorpe static int i2c_atmel_send(struct tpm_chip *chip, u8 *buf, size_t len)
41a2871c62SJason Gunthorpe {
429e0d39d8SChristophe Ricard struct priv_data *priv = dev_get_drvdata(&chip->dev);
438cfffc9dSJason Gunthorpe struct i2c_client *client = to_i2c_client(chip->dev.parent);
44a2871c62SJason Gunthorpe s32 status;
45a2871c62SJason Gunthorpe
46a2871c62SJason Gunthorpe priv->len = 0;
47a2871c62SJason Gunthorpe
48a2871c62SJason Gunthorpe if (len <= 2)
49a2871c62SJason Gunthorpe return -EIO;
50a2871c62SJason Gunthorpe
51a2871c62SJason Gunthorpe status = i2c_master_send(client, buf, len);
52a2871c62SJason Gunthorpe
538cfffc9dSJason Gunthorpe dev_dbg(&chip->dev,
54a2871c62SJason Gunthorpe "%s(buf=%*ph len=%0zx) -> sts=%d\n", __func__,
55a2871c62SJason Gunthorpe (int)min_t(size_t, 64, len), buf, len, status);
56f5595f5bSJarkko Sakkinen
57f5595f5bSJarkko Sakkinen if (status < 0)
58a2871c62SJason Gunthorpe return status;
59f5595f5bSJarkko Sakkinen
60442601e8SJarkko Sakkinen /* The upper layer does not support incomplete sends. */
61442601e8SJarkko Sakkinen if (status != len)
62442601e8SJarkko Sakkinen return -E2BIG;
63442601e8SJarkko Sakkinen
64f5595f5bSJarkko Sakkinen return 0;
65a2871c62SJason Gunthorpe }
66a2871c62SJason Gunthorpe
i2c_atmel_recv(struct tpm_chip * chip,u8 * buf,size_t count)67a2871c62SJason Gunthorpe static int i2c_atmel_recv(struct tpm_chip *chip, u8 *buf, size_t count)
68a2871c62SJason Gunthorpe {
699e0d39d8SChristophe Ricard struct priv_data *priv = dev_get_drvdata(&chip->dev);
708cfffc9dSJason Gunthorpe struct i2c_client *client = to_i2c_client(chip->dev.parent);
71b34b77a9SJarkko Sakkinen struct tpm_header *hdr = (struct tpm_header *)priv->buffer;
72a2871c62SJason Gunthorpe u32 expected_len;
73a2871c62SJason Gunthorpe int rc;
74a2871c62SJason Gunthorpe
75a2871c62SJason Gunthorpe if (priv->len == 0)
76a2871c62SJason Gunthorpe return -EIO;
77a2871c62SJason Gunthorpe
78a2871c62SJason Gunthorpe /* Get the message size from the message header, if we didn't get the
79a2871c62SJason Gunthorpe * whole message in read_status then we need to re-read the
80a2871c62SJason Gunthorpe * message. */
81a2871c62SJason Gunthorpe expected_len = be32_to_cpu(hdr->length);
82a2871c62SJason Gunthorpe if (expected_len > count)
83a2871c62SJason Gunthorpe return -ENOMEM;
84a2871c62SJason Gunthorpe
85a2871c62SJason Gunthorpe if (priv->len >= expected_len) {
868cfffc9dSJason Gunthorpe dev_dbg(&chip->dev,
87a2871c62SJason Gunthorpe "%s early(buf=%*ph count=%0zx) -> ret=%d\n", __func__,
88a2871c62SJason Gunthorpe (int)min_t(size_t, 64, expected_len), buf, count,
89a2871c62SJason Gunthorpe expected_len);
90a2871c62SJason Gunthorpe memcpy(buf, priv->buffer, expected_len);
91a2871c62SJason Gunthorpe return expected_len;
92a2871c62SJason Gunthorpe }
93a2871c62SJason Gunthorpe
94a2871c62SJason Gunthorpe rc = i2c_master_recv(client, buf, expected_len);
958cfffc9dSJason Gunthorpe dev_dbg(&chip->dev,
96a2871c62SJason Gunthorpe "%s reread(buf=%*ph count=%0zx) -> ret=%d\n", __func__,
97a2871c62SJason Gunthorpe (int)min_t(size_t, 64, expected_len), buf, count,
98a2871c62SJason Gunthorpe expected_len);
99a2871c62SJason Gunthorpe return rc;
100a2871c62SJason Gunthorpe }
101a2871c62SJason Gunthorpe
i2c_atmel_cancel(struct tpm_chip * chip)102a2871c62SJason Gunthorpe static void i2c_atmel_cancel(struct tpm_chip *chip)
103a2871c62SJason Gunthorpe {
1048cfffc9dSJason Gunthorpe dev_err(&chip->dev, "TPM operation cancellation was requested, but is not supported");
105a2871c62SJason Gunthorpe }
106a2871c62SJason Gunthorpe
i2c_atmel_read_status(struct tpm_chip * chip)107a2871c62SJason Gunthorpe static u8 i2c_atmel_read_status(struct tpm_chip *chip)
108a2871c62SJason Gunthorpe {
1099e0d39d8SChristophe Ricard struct priv_data *priv = dev_get_drvdata(&chip->dev);
1108cfffc9dSJason Gunthorpe struct i2c_client *client = to_i2c_client(chip->dev.parent);
111a2871c62SJason Gunthorpe int rc;
112a2871c62SJason Gunthorpe
113a2871c62SJason Gunthorpe /* The TPM fails the I2C read until it is ready, so we do the entire
114a2871c62SJason Gunthorpe * transfer here and buffer it locally. This way the common code can
115a2871c62SJason Gunthorpe * properly handle the timeouts. */
116a2871c62SJason Gunthorpe priv->len = 0;
117a2871c62SJason Gunthorpe memset(priv->buffer, 0, sizeof(priv->buffer));
118a2871c62SJason Gunthorpe
119a2871c62SJason Gunthorpe
120a2871c62SJason Gunthorpe /* Once the TPM has completed the command the command remains readable
121a2871c62SJason Gunthorpe * until another command is issued. */
122a2871c62SJason Gunthorpe rc = i2c_master_recv(client, priv->buffer, sizeof(priv->buffer));
1238cfffc9dSJason Gunthorpe dev_dbg(&chip->dev,
124a2871c62SJason Gunthorpe "%s: sts=%d", __func__, rc);
125a2871c62SJason Gunthorpe if (rc <= 0)
126a2871c62SJason Gunthorpe return 0;
127a2871c62SJason Gunthorpe
128a2871c62SJason Gunthorpe priv->len = rc;
129a2871c62SJason Gunthorpe
130a2871c62SJason Gunthorpe return ATMEL_STS_OK;
131a2871c62SJason Gunthorpe }
132a2871c62SJason Gunthorpe
i2c_atmel_req_canceled(struct tpm_chip * chip,u8 status)133a2871c62SJason Gunthorpe static bool i2c_atmel_req_canceled(struct tpm_chip *chip, u8 status)
134a2871c62SJason Gunthorpe {
135ba6a09d7SFengguang Wu return false;
136a2871c62SJason Gunthorpe }
137a2871c62SJason Gunthorpe
13801ad1fa7SJason Gunthorpe static const struct tpm_class_ops i2c_atmel = {
139cae8b441SJason Gunthorpe .flags = TPM_OPS_AUTO_STARTUP,
140a2871c62SJason Gunthorpe .status = i2c_atmel_read_status,
141a2871c62SJason Gunthorpe .recv = i2c_atmel_recv,
142a2871c62SJason Gunthorpe .send = i2c_atmel_send,
143a2871c62SJason Gunthorpe .cancel = i2c_atmel_cancel,
144a2871c62SJason Gunthorpe .req_complete_mask = ATMEL_STS_OK,
145a2871c62SJason Gunthorpe .req_complete_val = ATMEL_STS_OK,
146a2871c62SJason Gunthorpe .req_canceled = i2c_atmel_req_canceled,
147a2871c62SJason Gunthorpe };
148a2871c62SJason Gunthorpe
i2c_atmel_probe(struct i2c_client * client)149d787c95bSUwe Kleine-König static int i2c_atmel_probe(struct i2c_client *client)
150a2871c62SJason Gunthorpe {
151a2871c62SJason Gunthorpe struct tpm_chip *chip;
152a2871c62SJason Gunthorpe struct device *dev = &client->dev;
1539e0d39d8SChristophe Ricard struct priv_data *priv;
154a2871c62SJason Gunthorpe
155a2871c62SJason Gunthorpe if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
156a2871c62SJason Gunthorpe return -ENODEV;
157a2871c62SJason Gunthorpe
158afb5abc2SJarkko Sakkinen chip = tpmm_chip_alloc(dev, &i2c_atmel);
159afb5abc2SJarkko Sakkinen if (IS_ERR(chip))
160afb5abc2SJarkko Sakkinen return PTR_ERR(chip);
161a2871c62SJason Gunthorpe
1629e0d39d8SChristophe Ricard priv = devm_kzalloc(dev, sizeof(struct priv_data), GFP_KERNEL);
1639e0d39d8SChristophe Ricard if (!priv)
164afb5abc2SJarkko Sakkinen return -ENOMEM;
165a2871c62SJason Gunthorpe
166a2871c62SJason Gunthorpe /* Default timeouts */
167af782f33SChristophe Ricard chip->timeout_a = msecs_to_jiffies(TPM_I2C_SHORT_TIMEOUT);
168af782f33SChristophe Ricard chip->timeout_b = msecs_to_jiffies(TPM_I2C_LONG_TIMEOUT);
169af782f33SChristophe Ricard chip->timeout_c = msecs_to_jiffies(TPM_I2C_SHORT_TIMEOUT);
170af782f33SChristophe Ricard chip->timeout_d = msecs_to_jiffies(TPM_I2C_SHORT_TIMEOUT);
171a2871c62SJason Gunthorpe
1729e0d39d8SChristophe Ricard dev_set_drvdata(&chip->dev, priv);
1739e0d39d8SChristophe Ricard
174a2871c62SJason Gunthorpe /* There is no known way to probe for this device, and all version
175a2871c62SJason Gunthorpe * information seems to be read via TPM commands. Thus we rely on the
176a2871c62SJason Gunthorpe * TPM startup process in the common code to detect the device. */
177a2871c62SJason Gunthorpe
178afb5abc2SJarkko Sakkinen return tpm_chip_register(chip);
179a2871c62SJason Gunthorpe }
180a2871c62SJason Gunthorpe
i2c_atmel_remove(struct i2c_client * client)181ed5c2f5fSUwe Kleine-König static void i2c_atmel_remove(struct i2c_client *client)
182a2871c62SJason Gunthorpe {
183a2871c62SJason Gunthorpe struct device *dev = &(client->dev);
184a2871c62SJason Gunthorpe struct tpm_chip *chip = dev_get_drvdata(dev);
185afb5abc2SJarkko Sakkinen tpm_chip_unregister(chip);
186a2871c62SJason Gunthorpe }
187a2871c62SJason Gunthorpe
188a2871c62SJason Gunthorpe static const struct i2c_device_id i2c_atmel_id[] = {
189a2871c62SJason Gunthorpe {I2C_DRIVER_NAME, 0},
190a2871c62SJason Gunthorpe {}
191a2871c62SJason Gunthorpe };
192a2871c62SJason Gunthorpe MODULE_DEVICE_TABLE(i2c, i2c_atmel_id);
193a2871c62SJason Gunthorpe
194a2871c62SJason Gunthorpe #ifdef CONFIG_OF
195a2871c62SJason Gunthorpe static const struct of_device_id i2c_atmel_of_match[] = {
196a2871c62SJason Gunthorpe {.compatible = "atmel,at97sc3204t"},
197a2871c62SJason Gunthorpe {},
198a2871c62SJason Gunthorpe };
199a2871c62SJason Gunthorpe MODULE_DEVICE_TABLE(of, i2c_atmel_of_match);
200a2871c62SJason Gunthorpe #endif
201a2871c62SJason Gunthorpe
202a2871c62SJason Gunthorpe static SIMPLE_DEV_PM_OPS(i2c_atmel_pm_ops, tpm_pm_suspend, tpm_pm_resume);
203a2871c62SJason Gunthorpe
204a2871c62SJason Gunthorpe static struct i2c_driver i2c_atmel_driver = {
205a2871c62SJason Gunthorpe .id_table = i2c_atmel_id,
206*be6f48a7SUwe Kleine-König .probe = i2c_atmel_probe,
207a2871c62SJason Gunthorpe .remove = i2c_atmel_remove,
208a2871c62SJason Gunthorpe .driver = {
209a2871c62SJason Gunthorpe .name = I2C_DRIVER_NAME,
210a2871c62SJason Gunthorpe .pm = &i2c_atmel_pm_ops,
211a2871c62SJason Gunthorpe .of_match_table = of_match_ptr(i2c_atmel_of_match),
212a2871c62SJason Gunthorpe },
213a2871c62SJason Gunthorpe };
214a2871c62SJason Gunthorpe
215a2871c62SJason Gunthorpe module_i2c_driver(i2c_atmel_driver);
216a2871c62SJason Gunthorpe
217a2871c62SJason Gunthorpe MODULE_AUTHOR("Jason Gunthorpe <jgunthorpe@obsidianresearch.com>");
218a2871c62SJason Gunthorpe MODULE_DESCRIPTION("Atmel TPM I2C Driver");
219a2871c62SJason Gunthorpe MODULE_LICENSE("GPL");
220