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