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