Loading...
Note: File does not exist in v6.8.
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/spi/spi.h>
37#include <linux/gpio.h>
38#include <linux/of_irq.h>
39#include <linux/of_gpio.h>
40#include <linux/tpm.h>
41#include "tpm.h"
42#include "tpm_tis_core.h"
43
44#define MAX_SPI_FRAMESIZE 64
45
46struct tpm_tis_spi_phy {
47 struct tpm_tis_data priv;
48 struct spi_device *spi_device;
49 u8 *iobuf;
50};
51
52static inline struct tpm_tis_spi_phy *to_tpm_tis_spi_phy(struct tpm_tis_data *data)
53{
54 return container_of(data, struct tpm_tis_spi_phy, priv);
55}
56
57static int tpm_tis_spi_transfer(struct tpm_tis_data *data, u32 addr, u16 len,
58 u8 *in, const u8 *out)
59{
60 struct tpm_tis_spi_phy *phy = to_tpm_tis_spi_phy(data);
61 int ret = 0;
62 int i;
63 struct spi_message m;
64 struct spi_transfer spi_xfer;
65 u8 transfer_len;
66
67 spi_bus_lock(phy->spi_device->master);
68
69 while (len) {
70 transfer_len = min_t(u16, len, MAX_SPI_FRAMESIZE);
71
72 phy->iobuf[0] = (in ? 0x80 : 0) | (transfer_len - 1);
73 phy->iobuf[1] = 0xd4;
74 phy->iobuf[2] = addr >> 8;
75 phy->iobuf[3] = addr;
76
77 memset(&spi_xfer, 0, sizeof(spi_xfer));
78 spi_xfer.tx_buf = phy->iobuf;
79 spi_xfer.rx_buf = phy->iobuf;
80 spi_xfer.len = 4;
81 spi_xfer.cs_change = 1;
82
83 spi_message_init(&m);
84 spi_message_add_tail(&spi_xfer, &m);
85 ret = spi_sync_locked(phy->spi_device, &m);
86 if (ret < 0)
87 goto exit;
88
89 if ((phy->iobuf[3] & 0x01) == 0) {
90 // handle SPI wait states
91 phy->iobuf[0] = 0;
92
93 for (i = 0; i < TPM_RETRY; i++) {
94 spi_xfer.len = 1;
95 spi_message_init(&m);
96 spi_message_add_tail(&spi_xfer, &m);
97 ret = spi_sync_locked(phy->spi_device, &m);
98 if (ret < 0)
99 goto exit;
100 if (phy->iobuf[0] & 0x01)
101 break;
102 }
103
104 if (i == TPM_RETRY) {
105 ret = -ETIMEDOUT;
106 goto exit;
107 }
108 }
109
110 spi_xfer.cs_change = 0;
111 spi_xfer.len = transfer_len;
112 spi_xfer.delay_usecs = 5;
113
114 if (in) {
115 spi_xfer.tx_buf = NULL;
116 } else if (out) {
117 spi_xfer.rx_buf = NULL;
118 memcpy(phy->iobuf, out, transfer_len);
119 out += transfer_len;
120 }
121
122 spi_message_init(&m);
123 spi_message_add_tail(&spi_xfer, &m);
124 ret = spi_sync_locked(phy->spi_device, &m);
125 if (ret < 0)
126 goto exit;
127
128 if (in) {
129 memcpy(in, phy->iobuf, transfer_len);
130 in += transfer_len;
131 }
132
133 len -= transfer_len;
134 }
135
136exit:
137 spi_bus_unlock(phy->spi_device->master);
138 return ret;
139}
140
141static int tpm_tis_spi_read_bytes(struct tpm_tis_data *data, u32 addr,
142 u16 len, u8 *result)
143{
144 return tpm_tis_spi_transfer(data, addr, len, result, NULL);
145}
146
147static int tpm_tis_spi_write_bytes(struct tpm_tis_data *data, u32 addr,
148 u16 len, const u8 *value)
149{
150 return tpm_tis_spi_transfer(data, addr, len, NULL, value);
151}
152
153static int tpm_tis_spi_read16(struct tpm_tis_data *data, u32 addr, u16 *result)
154{
155 __le16 result_le;
156 int rc;
157
158 rc = data->phy_ops->read_bytes(data, addr, sizeof(u16),
159 (u8 *)&result_le);
160 if (!rc)
161 *result = le16_to_cpu(result_le);
162
163 return rc;
164}
165
166static int tpm_tis_spi_read32(struct tpm_tis_data *data, u32 addr, u32 *result)
167{
168 __le32 result_le;
169 int rc;
170
171 rc = data->phy_ops->read_bytes(data, addr, sizeof(u32),
172 (u8 *)&result_le);
173 if (!rc)
174 *result = le32_to_cpu(result_le);
175
176 return rc;
177}
178
179static int tpm_tis_spi_write32(struct tpm_tis_data *data, u32 addr, u32 value)
180{
181 __le32 value_le;
182 int rc;
183
184 value_le = cpu_to_le32(value);
185 rc = data->phy_ops->write_bytes(data, addr, sizeof(u32),
186 (u8 *)&value_le);
187
188 return rc;
189}
190
191static const struct tpm_tis_phy_ops tpm_spi_phy_ops = {
192 .read_bytes = tpm_tis_spi_read_bytes,
193 .write_bytes = tpm_tis_spi_write_bytes,
194 .read16 = tpm_tis_spi_read16,
195 .read32 = tpm_tis_spi_read32,
196 .write32 = tpm_tis_spi_write32,
197};
198
199static int tpm_tis_spi_probe(struct spi_device *dev)
200{
201 struct tpm_tis_spi_phy *phy;
202
203 phy = devm_kzalloc(&dev->dev, sizeof(struct tpm_tis_spi_phy),
204 GFP_KERNEL);
205 if (!phy)
206 return -ENOMEM;
207
208 phy->spi_device = dev;
209
210 phy->iobuf = devm_kmalloc(&dev->dev, MAX_SPI_FRAMESIZE, GFP_KERNEL);
211 if (!phy->iobuf)
212 return -ENOMEM;
213
214 return tpm_tis_core_init(&dev->dev, &phy->priv, -1, &tpm_spi_phy_ops,
215 NULL);
216}
217
218static SIMPLE_DEV_PM_OPS(tpm_tis_pm, tpm_pm_suspend, tpm_tis_resume);
219
220static int tpm_tis_spi_remove(struct spi_device *dev)
221{
222 struct tpm_chip *chip = spi_get_drvdata(dev);
223
224 tpm_chip_unregister(chip);
225 tpm_tis_remove(chip);
226 return 0;
227}
228
229static const struct spi_device_id tpm_tis_spi_id[] = {
230 {"tpm_tis_spi", 0},
231 {}
232};
233MODULE_DEVICE_TABLE(spi, tpm_tis_spi_id);
234
235static const struct of_device_id of_tis_spi_match[] = {
236 { .compatible = "st,st33htpm-spi", },
237 { .compatible = "infineon,slb9670", },
238 { .compatible = "tcg,tpm_tis-spi", },
239 {}
240};
241MODULE_DEVICE_TABLE(of, of_tis_spi_match);
242
243static const struct acpi_device_id acpi_tis_spi_match[] = {
244 {"SMO0768", 0},
245 {}
246};
247MODULE_DEVICE_TABLE(acpi, acpi_tis_spi_match);
248
249static struct spi_driver tpm_tis_spi_driver = {
250 .driver = {
251 .owner = THIS_MODULE,
252 .name = "tpm_tis_spi",
253 .pm = &tpm_tis_pm,
254 .of_match_table = of_match_ptr(of_tis_spi_match),
255 .acpi_match_table = ACPI_PTR(acpi_tis_spi_match),
256 },
257 .probe = tpm_tis_spi_probe,
258 .remove = tpm_tis_spi_remove,
259 .id_table = tpm_tis_spi_id,
260};
261module_spi_driver(tpm_tis_spi_driver);
262
263MODULE_DESCRIPTION("TPM Driver for native SPI access");
264MODULE_LICENSE("GPL");