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