• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2015 Infineon Technologies AG
4  * Copyright (C) 2016 STMicroelectronics SAS
5  *
6  * Authors:
7  * Peter Huewe <peter.huewe@infineon.com>
8  * Christophe Ricard <christophe-h.ricard@st.com>
9  *
10  * Maintained by: <tpmdd-devel@lists.sourceforge.net>
11  *
12  * Device driver for TCG/TCPA TPM (trusted platform module).
13  * Specifications at www.trustedcomputinggroup.org
14  *
15  * This device driver implements the TPM interface as defined in
16  * the TCG TPM Interface Spec version 1.3, revision 27 via _raw/native
17  * SPI access_.
18  *
19  * It is based on the original tpm_tis device driver from Leendert van
20  * Dorn and Kyleen Hall and Jarko Sakkinnen.
21  */
22 
23 #include <linux/init.h>
24 #include <linux/module.h>
25 #include <linux/moduleparam.h>
26 #include <linux/slab.h>
27 #include <linux/interrupt.h>
28 #include <linux/wait.h>
29 #include <linux/acpi.h>
30 #include <linux/freezer.h>
31 
32 #include <linux/spi/spi.h>
33 #include <linux/gpio.h>
34 #include <linux/of_irq.h>
35 #include <linux/of_gpio.h>
36 #include <linux/tpm.h>
37 #include "tpm.h"
38 #include "tpm_tis_core.h"
39 
40 #define MAX_SPI_FRAMESIZE 64
41 
42 struct tpm_tis_spi_phy {
43 	struct tpm_tis_data priv;
44 	struct spi_device *spi_device;
45 	u8 *iobuf;
46 };
47 
to_tpm_tis_spi_phy(struct tpm_tis_data * data)48 static inline struct tpm_tis_spi_phy *to_tpm_tis_spi_phy(struct tpm_tis_data *data)
49 {
50 	return container_of(data, struct tpm_tis_spi_phy, priv);
51 }
52 
tpm_tis_spi_transfer(struct tpm_tis_data * data,u32 addr,u16 len,u8 * in,const u8 * out)53 static int tpm_tis_spi_transfer(struct tpm_tis_data *data, u32 addr, u16 len,
54 				u8 *in, const u8 *out)
55 {
56 	struct tpm_tis_spi_phy *phy = to_tpm_tis_spi_phy(data);
57 	int ret = 0;
58 	int i;
59 	struct spi_message m;
60 	struct spi_transfer spi_xfer;
61 	u8 transfer_len;
62 
63 	spi_bus_lock(phy->spi_device->master);
64 
65 	while (len) {
66 		transfer_len = min_t(u16, len, MAX_SPI_FRAMESIZE);
67 
68 		phy->iobuf[0] = (in ? 0x80 : 0) | (transfer_len - 1);
69 		phy->iobuf[1] = 0xd4;
70 		phy->iobuf[2] = addr >> 8;
71 		phy->iobuf[3] = addr;
72 
73 		memset(&spi_xfer, 0, sizeof(spi_xfer));
74 		spi_xfer.tx_buf = phy->iobuf;
75 		spi_xfer.rx_buf = phy->iobuf;
76 		spi_xfer.len = 4;
77 		spi_xfer.cs_change = 1;
78 
79 		spi_message_init(&m);
80 		spi_message_add_tail(&spi_xfer, &m);
81 		ret = spi_sync_locked(phy->spi_device, &m);
82 		if (ret < 0)
83 			goto exit;
84 
85 		if ((phy->iobuf[3] & 0x01) == 0) {
86 			// handle SPI wait states
87 			phy->iobuf[0] = 0;
88 
89 			for (i = 0; i < TPM_RETRY; i++) {
90 				spi_xfer.len = 1;
91 				spi_message_init(&m);
92 				spi_message_add_tail(&spi_xfer, &m);
93 				ret = spi_sync_locked(phy->spi_device, &m);
94 				if (ret < 0)
95 					goto exit;
96 				if (phy->iobuf[0] & 0x01)
97 					break;
98 			}
99 
100 			if (i == TPM_RETRY) {
101 				ret = -ETIMEDOUT;
102 				goto exit;
103 			}
104 		}
105 
106 		spi_xfer.cs_change = 0;
107 		spi_xfer.len = transfer_len;
108 		spi_xfer.delay_usecs = 5;
109 
110 		if (in) {
111 			spi_xfer.tx_buf = NULL;
112 		} else if (out) {
113 			spi_xfer.rx_buf = NULL;
114 			memcpy(phy->iobuf, out, transfer_len);
115 			out += transfer_len;
116 		}
117 
118 		spi_message_init(&m);
119 		spi_message_add_tail(&spi_xfer, &m);
120 		ret = spi_sync_locked(phy->spi_device, &m);
121 		if (ret < 0)
122 			goto exit;
123 
124 		if (in) {
125 			memcpy(in, phy->iobuf, transfer_len);
126 			in += transfer_len;
127 		}
128 
129 		len -= transfer_len;
130 	}
131 
132 exit:
133 	spi_bus_unlock(phy->spi_device->master);
134 	return ret;
135 }
136 
tpm_tis_spi_read_bytes(struct tpm_tis_data * data,u32 addr,u16 len,u8 * result)137 static int tpm_tis_spi_read_bytes(struct tpm_tis_data *data, u32 addr,
138 				  u16 len, u8 *result)
139 {
140 	return tpm_tis_spi_transfer(data, addr, len, result, NULL);
141 }
142 
tpm_tis_spi_write_bytes(struct tpm_tis_data * data,u32 addr,u16 len,const u8 * value)143 static int tpm_tis_spi_write_bytes(struct tpm_tis_data *data, u32 addr,
144 				   u16 len, const u8 *value)
145 {
146 	return tpm_tis_spi_transfer(data, addr, len, NULL, value);
147 }
148 
tpm_tis_spi_read16(struct tpm_tis_data * data,u32 addr,u16 * result)149 static int tpm_tis_spi_read16(struct tpm_tis_data *data, u32 addr, u16 *result)
150 {
151 	__le16 result_le;
152 	int rc;
153 
154 	rc = data->phy_ops->read_bytes(data, addr, sizeof(u16),
155 				       (u8 *)&result_le);
156 	if (!rc)
157 		*result = le16_to_cpu(result_le);
158 
159 	return rc;
160 }
161 
tpm_tis_spi_read32(struct tpm_tis_data * data,u32 addr,u32 * result)162 static int tpm_tis_spi_read32(struct tpm_tis_data *data, u32 addr, u32 *result)
163 {
164 	__le32 result_le;
165 	int rc;
166 
167 	rc = data->phy_ops->read_bytes(data, addr, sizeof(u32),
168 				       (u8 *)&result_le);
169 	if (!rc)
170 		*result = le32_to_cpu(result_le);
171 
172 	return rc;
173 }
174 
tpm_tis_spi_write32(struct tpm_tis_data * data,u32 addr,u32 value)175 static int tpm_tis_spi_write32(struct tpm_tis_data *data, u32 addr, u32 value)
176 {
177 	__le32 value_le;
178 	int rc;
179 
180 	value_le = cpu_to_le32(value);
181 	rc = data->phy_ops->write_bytes(data, addr, sizeof(u32),
182 					(u8 *)&value_le);
183 
184 	return rc;
185 }
186 
187 static const struct tpm_tis_phy_ops tpm_spi_phy_ops = {
188 	.read_bytes = tpm_tis_spi_read_bytes,
189 	.write_bytes = tpm_tis_spi_write_bytes,
190 	.read16 = tpm_tis_spi_read16,
191 	.read32 = tpm_tis_spi_read32,
192 	.write32 = tpm_tis_spi_write32,
193 };
194 
tpm_tis_spi_probe(struct spi_device * dev)195 static int tpm_tis_spi_probe(struct spi_device *dev)
196 {
197 	struct tpm_tis_spi_phy *phy;
198 	int irq;
199 
200 	phy = devm_kzalloc(&dev->dev, sizeof(struct tpm_tis_spi_phy),
201 			   GFP_KERNEL);
202 	if (!phy)
203 		return -ENOMEM;
204 
205 	phy->spi_device = dev;
206 
207 	phy->iobuf = devm_kmalloc(&dev->dev, MAX_SPI_FRAMESIZE, GFP_KERNEL);
208 	if (!phy->iobuf)
209 		return -ENOMEM;
210 
211 	/* If the SPI device has an IRQ then use that */
212 	if (dev->irq > 0)
213 		irq = dev->irq;
214 	else
215 		irq = -1;
216 
217 	return tpm_tis_core_init(&dev->dev, &phy->priv, irq, &tpm_spi_phy_ops,
218 				 NULL);
219 }
220 
221 static SIMPLE_DEV_PM_OPS(tpm_tis_pm, tpm_pm_suspend, tpm_tis_resume);
222 
tpm_tis_spi_remove(struct spi_device * dev)223 static int tpm_tis_spi_remove(struct spi_device *dev)
224 {
225 	struct tpm_chip *chip = spi_get_drvdata(dev);
226 
227 	tpm_chip_unregister(chip);
228 	tpm_tis_remove(chip);
229 	return 0;
230 }
231 
232 static const struct spi_device_id tpm_tis_spi_id[] = {
233 	{"tpm_tis_spi", 0},
234 	{}
235 };
236 MODULE_DEVICE_TABLE(spi, tpm_tis_spi_id);
237 
238 static const struct of_device_id of_tis_spi_match[] = {
239 	{ .compatible = "st,st33htpm-spi", },
240 	{ .compatible = "infineon,slb9670", },
241 	{ .compatible = "tcg,tpm_tis-spi", },
242 	{}
243 };
244 MODULE_DEVICE_TABLE(of, of_tis_spi_match);
245 
246 static const struct acpi_device_id acpi_tis_spi_match[] = {
247 	{"SMO0768", 0},
248 	{}
249 };
250 MODULE_DEVICE_TABLE(acpi, acpi_tis_spi_match);
251 
252 static struct spi_driver tpm_tis_spi_driver = {
253 	.driver = {
254 		.owner = THIS_MODULE,
255 		.name = "tpm_tis_spi",
256 		.pm = &tpm_tis_pm,
257 		.of_match_table = of_match_ptr(of_tis_spi_match),
258 		.acpi_match_table = ACPI_PTR(acpi_tis_spi_match),
259 	},
260 	.probe = tpm_tis_spi_probe,
261 	.remove = tpm_tis_spi_remove,
262 	.id_table = tpm_tis_spi_id,
263 };
264 module_spi_driver(tpm_tis_spi_driver);
265 
266 MODULE_DESCRIPTION("TPM Driver for native SPI access");
267 MODULE_LICENSE("GPL");
268