1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2015 Infineon Technologies AG
4 * Copyright (C) 2016 STMicroelectronics SAS
7 * Peter Huewe <peter.huewe@infineon.com>
8 * Christophe Ricard <christophe-h.ricard@st.com>
10 * Maintained by: <tpmdd-devel@lists.sourceforge.net>
12 * Device driver for TCG/TCPA TPM (trusted platform module).
13 * Specifications at www.trustedcomputinggroup.org
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
19 * It is based on the original tpm_tis device driver from Leendert van
20 * Dorn and Kyleen Hall and Jarko Sakkinnen.
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>
31 #include <linux/of_device.h>
32 #include <linux/spi/spi.h>
33 #include <linux/tpm.h>
36 #include "tpm_tis_core.h"
37 #include "tpm_tis_spi.h"
39 #define MAX_SPI_FRAMESIZE 64
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.
46 * [1] https://trustedcomputinggroup.org/resource/pc-client-platform-tpm-profile-ptp-specification/
48 static int tpm_tis_spi_flow_control(struct tpm_tis_spi_phy
*phy
,
49 struct spi_transfer
*spi_xfer
)
54 if ((phy
->iobuf
[3] & 0x01) == 0) {
55 // handle SPI wait states
56 for (i
= 0; i
< TPM_RETRY
; i
++) {
59 spi_message_add_tail(spi_xfer
, &m
);
60 ret
= spi_sync_locked(phy
->spi_device
, &m
);
63 if (phy
->iobuf
[0] & 0x01)
74 int tpm_tis_spi_transfer(struct tpm_tis_data
*data
, u32 addr
, u16 len
,
75 u8
*in
, const u8
*out
)
77 struct tpm_tis_spi_phy
*phy
= to_tpm_tis_spi_phy(data
);
80 struct spi_transfer spi_xfer
;
83 spi_bus_lock(phy
->spi_device
->master
);
86 transfer_len
= min_t(u16
, len
, MAX_SPI_FRAMESIZE
);
88 phy
->iobuf
[0] = (in
? 0x80 : 0) | (transfer_len
- 1);
90 phy
->iobuf
[2] = addr
>> 8;
93 memset(&spi_xfer
, 0, sizeof(spi_xfer
));
94 spi_xfer
.tx_buf
= phy
->iobuf
;
95 spi_xfer
.rx_buf
= phy
->iobuf
;
97 spi_xfer
.cs_change
= 1;
100 spi_message_add_tail(&spi_xfer
, &m
);
101 ret
= spi_sync_locked(phy
->spi_device
, &m
);
105 /* Flow control transfers are receive only */
106 spi_xfer
.tx_buf
= NULL
;
107 ret
= phy
->flow_control(phy
, &spi_xfer
);
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
;
117 spi_xfer
.tx_buf
= phy
->iobuf
;
118 spi_xfer
.rx_buf
= NULL
;
119 memcpy(phy
->iobuf
, out
, transfer_len
);
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
);
131 memcpy(in
, phy
->iobuf
, transfer_len
);
139 spi_bus_unlock(phy
->spi_device
->master
);
143 static int tpm_tis_spi_read_bytes(struct tpm_tis_data
*data
, u32 addr
,
146 return tpm_tis_spi_transfer(data
, addr
, len
, result
, NULL
);
149 static int tpm_tis_spi_write_bytes(struct tpm_tis_data
*data
, u32 addr
,
150 u16 len
, const u8
*value
)
152 return tpm_tis_spi_transfer(data
, addr
, len
, NULL
, value
);
155 int tpm_tis_spi_read16(struct tpm_tis_data
*data
, u32 addr
, u16
*result
)
160 rc
= data
->phy_ops
->read_bytes(data
, addr
, sizeof(u16
),
163 *result
= le16_to_cpu(result_le
);
168 int tpm_tis_spi_read32(struct tpm_tis_data
*data
, u32 addr
, u32
*result
)
173 rc
= data
->phy_ops
->read_bytes(data
, addr
, sizeof(u32
),
176 *result
= le32_to_cpu(result_le
);
181 int tpm_tis_spi_write32(struct tpm_tis_data
*data
, u32 addr
, u32 value
)
186 value_le
= cpu_to_le32(value
);
187 rc
= data
->phy_ops
->write_bytes(data
, addr
, sizeof(u32
),
193 int tpm_tis_spi_init(struct spi_device
*spi
, struct tpm_tis_spi_phy
*phy
,
194 int irq
, const struct tpm_tis_phy_ops
*phy_ops
)
196 phy
->iobuf
= devm_kmalloc(&spi
->dev
, MAX_SPI_FRAMESIZE
, GFP_KERNEL
);
200 phy
->spi_device
= spi
;
202 return tpm_tis_core_init(&spi
->dev
, &phy
->priv
, irq
, phy_ops
, NULL
);
205 static const struct tpm_tis_phy_ops tpm_spi_phy_ops
= {
206 .read_bytes
= tpm_tis_spi_read_bytes
,
207 .write_bytes
= tpm_tis_spi_write_bytes
,
208 .read16
= tpm_tis_spi_read16
,
209 .read32
= tpm_tis_spi_read32
,
210 .write32
= tpm_tis_spi_write32
,
213 static int tpm_tis_spi_probe(struct spi_device
*dev
)
215 struct tpm_tis_spi_phy
*phy
;
218 phy
= devm_kzalloc(&dev
->dev
, sizeof(struct tpm_tis_spi_phy
),
223 phy
->flow_control
= tpm_tis_spi_flow_control
;
225 /* If the SPI device has an IRQ then use that */
231 init_completion(&phy
->ready
);
232 return tpm_tis_spi_init(dev
, phy
, irq
, &tpm_spi_phy_ops
);
235 typedef int (*tpm_tis_spi_probe_func
)(struct spi_device
*);
237 static int tpm_tis_spi_driver_probe(struct spi_device
*spi
)
239 const struct spi_device_id
*spi_dev_id
= spi_get_device_id(spi
);
240 tpm_tis_spi_probe_func probe_func
;
242 probe_func
= of_device_get_match_data(&spi
->dev
);
243 if (!probe_func
&& spi_dev_id
)
244 probe_func
= (tpm_tis_spi_probe_func
)spi_dev_id
->driver_data
;
248 return probe_func(spi
);
251 static SIMPLE_DEV_PM_OPS(tpm_tis_pm
, tpm_pm_suspend
, tpm_tis_spi_resume
);
253 static int tpm_tis_spi_remove(struct spi_device
*dev
)
255 struct tpm_chip
*chip
= spi_get_drvdata(dev
);
257 tpm_chip_unregister(chip
);
258 tpm_tis_remove(chip
);
262 static const struct spi_device_id tpm_tis_spi_id
[] = {
263 { "tpm_tis_spi", (unsigned long)tpm_tis_spi_probe
},
264 { "cr50", (unsigned long)cr50_spi_probe
},
267 MODULE_DEVICE_TABLE(spi
, tpm_tis_spi_id
);
269 static const struct of_device_id of_tis_spi_match
[] = {
270 { .compatible
= "st,st33htpm-spi", .data
= tpm_tis_spi_probe
},
271 { .compatible
= "infineon,slb9670", .data
= tpm_tis_spi_probe
},
272 { .compatible
= "tcg,tpm_tis-spi", .data
= tpm_tis_spi_probe
},
273 { .compatible
= "google,cr50", .data
= cr50_spi_probe
},
276 MODULE_DEVICE_TABLE(of
, of_tis_spi_match
);
278 static const struct acpi_device_id acpi_tis_spi_match
[] = {
282 MODULE_DEVICE_TABLE(acpi
, acpi_tis_spi_match
);
284 static struct spi_driver tpm_tis_spi_driver
= {
286 .name
= "tpm_tis_spi",
288 .of_match_table
= of_match_ptr(of_tis_spi_match
),
289 .acpi_match_table
= ACPI_PTR(acpi_tis_spi_match
),
290 .probe_type
= PROBE_PREFER_ASYNCHRONOUS
,
292 .probe
= tpm_tis_spi_driver_probe
,
293 .remove
= tpm_tis_spi_remove
,
294 .id_table
= tpm_tis_spi_id
,
296 module_spi_driver(tpm_tis_spi_driver
);
298 MODULE_DESCRIPTION("TPM Driver for native SPI access");
299 MODULE_LICENSE("GPL");