xref: /linux/drivers/char/tpm/tpm_tis_spi_main.c (revision e53524cdcc02d089e757b668da031ba06ff665c3)
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/acpi.h>
24 #include <linux/completion.h>
25 #include <linux/init.h>
26 #include <linux/interrupt.h>
27 #include <linux/kernel.h>
28 #include <linux/module.h>
29 #include <linux/slab.h>
30 
31 #include <linux/of_device.h>
32 #include <linux/spi/spi.h>
33 #include <linux/tpm.h>
34 
35 #include "tpm.h"
36 #include "tpm_tis_core.h"
37 #include "tpm_tis_spi.h"
38 
39 #define MAX_SPI_FRAMESIZE 64
40 
41 /*
42  * TCG SPI flow control is documented in section 6.4 of the spec[1]. In short,
43  * keep trying to read from the device until MISO goes high indicating the
44  * wait state has ended.
45  *
46  * [1] https://trustedcomputinggroup.org/resource/pc-client-platform-tpm-profile-ptp-specification/
47  */
48 static int tpm_tis_spi_flow_control(struct tpm_tis_spi_phy *phy,
49 				    struct spi_transfer *spi_xfer)
50 {
51 	struct spi_message m;
52 	int ret, i;
53 
54 	if ((phy->iobuf[3] & 0x01) == 0) {
55 		// handle SPI wait states
56 		for (i = 0; i < TPM_RETRY; i++) {
57 			spi_xfer->len = 1;
58 			spi_message_init(&m);
59 			spi_message_add_tail(spi_xfer, &m);
60 			ret = spi_sync_locked(phy->spi_device, &m);
61 			if (ret < 0)
62 				return ret;
63 			if (phy->iobuf[0] & 0x01)
64 				break;
65 		}
66 
67 		if (i == TPM_RETRY)
68 			return -ETIMEDOUT;
69 	}
70 
71 	return 0;
72 }
73 
74 int tpm_tis_spi_transfer(struct tpm_tis_data *data, u32 addr, u16 len,
75 			 u8 *in, const u8 *out)
76 {
77 	struct tpm_tis_spi_phy *phy = to_tpm_tis_spi_phy(data);
78 	int ret = 0;
79 	struct spi_message m;
80 	struct spi_transfer spi_xfer;
81 	u8 transfer_len;
82 
83 	spi_bus_lock(phy->spi_device->master);
84 
85 	while (len) {
86 		transfer_len = min_t(u16, len, MAX_SPI_FRAMESIZE);
87 
88 		phy->iobuf[0] = (in ? 0x80 : 0) | (transfer_len - 1);
89 		phy->iobuf[1] = 0xd4;
90 		phy->iobuf[2] = addr >> 8;
91 		phy->iobuf[3] = addr;
92 
93 		memset(&spi_xfer, 0, sizeof(spi_xfer));
94 		spi_xfer.tx_buf = phy->iobuf;
95 		spi_xfer.rx_buf = phy->iobuf;
96 		spi_xfer.len = 4;
97 		spi_xfer.cs_change = 1;
98 
99 		spi_message_init(&m);
100 		spi_message_add_tail(&spi_xfer, &m);
101 		ret = spi_sync_locked(phy->spi_device, &m);
102 		if (ret < 0)
103 			goto exit;
104 
105 		/* Flow control transfers are receive only */
106 		spi_xfer.tx_buf = NULL;
107 		ret = phy->flow_control(phy, &spi_xfer);
108 		if (ret < 0)
109 			goto exit;
110 
111 		spi_xfer.cs_change = 0;
112 		spi_xfer.len = transfer_len;
113 		spi_xfer.delay.value = 5;
114 		spi_xfer.delay.unit = SPI_DELAY_UNIT_USECS;
115 
116 		if (out) {
117 			spi_xfer.tx_buf = phy->iobuf;
118 			spi_xfer.rx_buf = NULL;
119 			memcpy(phy->iobuf, out, transfer_len);
120 			out += transfer_len;
121 		}
122 
123 		spi_message_init(&m);
124 		spi_message_add_tail(&spi_xfer, &m);
125 		reinit_completion(&phy->ready);
126 		ret = spi_sync_locked(phy->spi_device, &m);
127 		if (ret < 0)
128 			goto exit;
129 
130 		if (in) {
131 			memcpy(in, phy->iobuf, transfer_len);
132 			in += transfer_len;
133 		}
134 
135 		len -= transfer_len;
136 	}
137 
138 exit:
139 	if (ret < 0) {
140 		/* Deactivate chip select */
141 		memset(&spi_xfer, 0, sizeof(spi_xfer));
142 		spi_message_init(&m);
143 		spi_message_add_tail(&spi_xfer, &m);
144 		spi_sync_locked(phy->spi_device, &m);
145 	}
146 
147 	spi_bus_unlock(phy->spi_device->master);
148 	return ret;
149 }
150 
151 static int tpm_tis_spi_read_bytes(struct tpm_tis_data *data, u32 addr,
152 				  u16 len, u8 *result, enum tpm_tis_io_mode io_mode)
153 {
154 	return tpm_tis_spi_transfer(data, addr, len, result, NULL);
155 }
156 
157 static int tpm_tis_spi_write_bytes(struct tpm_tis_data *data, u32 addr,
158 				   u16 len, const u8 *value, enum tpm_tis_io_mode io_mode)
159 {
160 	return tpm_tis_spi_transfer(data, addr, len, NULL, value);
161 }
162 
163 int tpm_tis_spi_init(struct spi_device *spi, struct tpm_tis_spi_phy *phy,
164 		     int irq, const struct tpm_tis_phy_ops *phy_ops)
165 {
166 	phy->iobuf = devm_kmalloc(&spi->dev, MAX_SPI_FRAMESIZE, GFP_KERNEL);
167 	if (!phy->iobuf)
168 		return -ENOMEM;
169 
170 	phy->spi_device = spi;
171 
172 	return tpm_tis_core_init(&spi->dev, &phy->priv, irq, phy_ops, NULL);
173 }
174 
175 static const struct tpm_tis_phy_ops tpm_spi_phy_ops = {
176 	.read_bytes = tpm_tis_spi_read_bytes,
177 	.write_bytes = tpm_tis_spi_write_bytes,
178 };
179 
180 static int tpm_tis_spi_probe(struct spi_device *dev)
181 {
182 	struct tpm_tis_spi_phy *phy;
183 	int irq;
184 
185 	phy = devm_kzalloc(&dev->dev, sizeof(struct tpm_tis_spi_phy),
186 			   GFP_KERNEL);
187 	if (!phy)
188 		return -ENOMEM;
189 
190 	phy->flow_control = tpm_tis_spi_flow_control;
191 
192 	/* If the SPI device has an IRQ then use that */
193 	if (dev->irq > 0)
194 		irq = dev->irq;
195 	else
196 		irq = -1;
197 
198 	init_completion(&phy->ready);
199 	return tpm_tis_spi_init(dev, phy, irq, &tpm_spi_phy_ops);
200 }
201 
202 typedef int (*tpm_tis_spi_probe_func)(struct spi_device *);
203 
204 static int tpm_tis_spi_driver_probe(struct spi_device *spi)
205 {
206 	const struct spi_device_id *spi_dev_id = spi_get_device_id(spi);
207 	tpm_tis_spi_probe_func probe_func;
208 
209 	probe_func = of_device_get_match_data(&spi->dev);
210 	if (!probe_func) {
211 		if (spi_dev_id) {
212 			probe_func = (tpm_tis_spi_probe_func)spi_dev_id->driver_data;
213 			if (!probe_func)
214 				return -ENODEV;
215 		} else
216 			probe_func = tpm_tis_spi_probe;
217 	}
218 
219 	return probe_func(spi);
220 }
221 
222 static SIMPLE_DEV_PM_OPS(tpm_tis_pm, tpm_pm_suspend, tpm_tis_spi_resume);
223 
224 static void tpm_tis_spi_remove(struct spi_device *dev)
225 {
226 	struct tpm_chip *chip = spi_get_drvdata(dev);
227 
228 	tpm_chip_unregister(chip);
229 	tpm_tis_remove(chip);
230 }
231 
232 static const struct spi_device_id tpm_tis_spi_id[] = {
233 	{ "st33htpm-spi", (unsigned long)tpm_tis_spi_probe },
234 	{ "slb9670", (unsigned long)tpm_tis_spi_probe },
235 	{ "tpm_tis_spi", (unsigned long)tpm_tis_spi_probe },
236 	{ "tpm_tis-spi", (unsigned long)tpm_tis_spi_probe },
237 	{ "cr50", (unsigned long)cr50_spi_probe },
238 	{}
239 };
240 MODULE_DEVICE_TABLE(spi, tpm_tis_spi_id);
241 
242 static const struct of_device_id of_tis_spi_match[] __maybe_unused = {
243 	{ .compatible = "st,st33htpm-spi", .data = tpm_tis_spi_probe },
244 	{ .compatible = "infineon,slb9670", .data = tpm_tis_spi_probe },
245 	{ .compatible = "tcg,tpm_tis-spi", .data = tpm_tis_spi_probe },
246 	{ .compatible = "google,cr50", .data = cr50_spi_probe },
247 	{}
248 };
249 MODULE_DEVICE_TABLE(of, of_tis_spi_match);
250 
251 static const struct acpi_device_id acpi_tis_spi_match[] __maybe_unused = {
252 	{"SMO0768", 0},
253 	{}
254 };
255 MODULE_DEVICE_TABLE(acpi, acpi_tis_spi_match);
256 
257 static struct spi_driver tpm_tis_spi_driver = {
258 	.driver = {
259 		.name = "tpm_tis_spi",
260 		.pm = &tpm_tis_pm,
261 		.of_match_table = of_match_ptr(of_tis_spi_match),
262 		.acpi_match_table = ACPI_PTR(acpi_tis_spi_match),
263 		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
264 	},
265 	.probe = tpm_tis_spi_driver_probe,
266 	.remove = tpm_tis_spi_remove,
267 	.id_table = tpm_tis_spi_id,
268 };
269 module_spi_driver(tpm_tis_spi_driver);
270 
271 MODULE_DESCRIPTION("TPM Driver for native SPI access");
272 MODULE_LICENSE("GPL");
273