2 * Copyright (C) 2015 Infineon Technologies AG
3 * Copyright (C) 2016 STMicroelectronics SAS
6 * Peter Huewe <peter.huewe@infineon.com>
7 * Christophe Ricard <christophe-h.ricard@st.com>
9 * Maintained by: <tpmdd-devel@lists.sourceforge.net>
11 * Device driver for TCG/TCPA TPM (trusted platform module).
12 * Specifications at www.trustedcomputinggroup.org
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
18 * It is based on the original tpm_tis device driver from Leendert van
19 * Dorn and Kyleen Hall and Jarko Sakkinnen.
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
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>
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>
43 #include "tpm_tis_core.h"
45 #define MAX_SPI_FRAMESIZE 64
47 struct tpm_tis_spi_phy
{
48 struct tpm_tis_data priv
;
49 struct spi_device
*spi_device
;
51 u8 tx_buf
[MAX_SPI_FRAMESIZE
+ 4];
52 u8 rx_buf
[MAX_SPI_FRAMESIZE
+ 4];
55 static inline struct tpm_tis_spi_phy
*to_tpm_tis_spi_phy(struct tpm_tis_data
*data
)
57 return container_of(data
, struct tpm_tis_spi_phy
, priv
);
60 static int tpm_tis_spi_read_bytes(struct tpm_tis_data
*data
, u32 addr
,
63 struct tpm_tis_spi_phy
*phy
= to_tpm_tis_spi_phy(data
);
66 struct spi_transfer spi_xfer
= {
67 .tx_buf
= phy
->tx_buf
,
68 .rx_buf
= phy
->rx_buf
,
72 if (len
> MAX_SPI_FRAMESIZE
)
75 phy
->tx_buf
[0] = 0x80 | (len
- 1);
76 phy
->tx_buf
[1] = 0xd4;
77 phy
->tx_buf
[2] = (addr
>> 8) & 0xFF;
78 phy
->tx_buf
[3] = addr
& 0xFF;
80 spi_xfer
.cs_change
= 1;
82 spi_message_add_tail(&spi_xfer
, &m
);
84 spi_bus_lock(phy
->spi_device
->master
);
85 ret
= spi_sync_locked(phy
->spi_device
, &m
);
89 memset(phy
->tx_buf
, 0, len
);
91 /* According to TCG PTP specification, if there is no TPM present at
92 * all, then the design has a weak pull-up on MISO. If a TPM is not
93 * present, a pull-up on MISO means that the SB controller sees a 1,
94 * and will latch in 0xFF on the read.
96 for (i
= 0; (phy
->rx_buf
[0] & 0x01) == 0 && i
< TPM_RETRY
; i
++) {
99 spi_message_add_tail(&spi_xfer
, &m
);
100 ret
= spi_sync_locked(phy
->spi_device
, &m
);
105 spi_xfer
.cs_change
= 0;
107 spi_xfer
.rx_buf
= result
;
109 spi_message_init(&m
);
110 spi_message_add_tail(&spi_xfer
, &m
);
111 ret
= spi_sync_locked(phy
->spi_device
, &m
);
114 spi_bus_unlock(phy
->spi_device
->master
);
118 static int tpm_tis_spi_write_bytes(struct tpm_tis_data
*data
, u32 addr
,
121 struct tpm_tis_spi_phy
*phy
= to_tpm_tis_spi_phy(data
);
123 struct spi_message m
;
124 struct spi_transfer spi_xfer
= {
125 .tx_buf
= phy
->tx_buf
,
126 .rx_buf
= phy
->rx_buf
,
130 if (len
> MAX_SPI_FRAMESIZE
)
133 phy
->tx_buf
[0] = len
- 1;
134 phy
->tx_buf
[1] = 0xd4;
135 phy
->tx_buf
[2] = (addr
>> 8) & 0xFF;
136 phy
->tx_buf
[3] = addr
& 0xFF;
138 spi_xfer
.cs_change
= 1;
139 spi_message_init(&m
);
140 spi_message_add_tail(&spi_xfer
, &m
);
142 spi_bus_lock(phy
->spi_device
->master
);
143 ret
= spi_sync_locked(phy
->spi_device
, &m
);
147 memset(phy
->tx_buf
, 0, len
);
149 /* According to TCG PTP specification, if there is no TPM present at
150 * all, then the design has a weak pull-up on MISO. If a TPM is not
151 * present, a pull-up on MISO means that the SB controller sees a 1,
152 * and will latch in 0xFF on the read.
154 for (i
= 0; (phy
->rx_buf
[0] & 0x01) == 0 && i
< TPM_RETRY
; i
++) {
156 spi_message_init(&m
);
157 spi_message_add_tail(&spi_xfer
, &m
);
158 ret
= spi_sync_locked(phy
->spi_device
, &m
);
164 spi_xfer
.tx_buf
= value
;
165 spi_xfer
.cs_change
= 0;
166 spi_xfer
.tx_buf
= value
;
167 spi_message_init(&m
);
168 spi_message_add_tail(&spi_xfer
, &m
);
169 ret
= spi_sync_locked(phy
->spi_device
, &m
);
172 spi_bus_unlock(phy
->spi_device
->master
);
176 static int tpm_tis_spi_read16(struct tpm_tis_data
*data
, u32 addr
, u16
*result
)
180 rc
= data
->phy_ops
->read_bytes(data
, addr
, sizeof(u16
), (u8
*)result
);
182 *result
= le16_to_cpu(*result
);
186 static int tpm_tis_spi_read32(struct tpm_tis_data
*data
, u32 addr
, u32
*result
)
190 rc
= data
->phy_ops
->read_bytes(data
, addr
, sizeof(u32
), (u8
*)result
);
192 *result
= le32_to_cpu(*result
);
196 static int tpm_tis_spi_write32(struct tpm_tis_data
*data
, u32 addr
, u32 value
)
198 value
= cpu_to_le32(value
);
199 return data
->phy_ops
->write_bytes(data
, addr
, sizeof(u32
),
203 static const struct tpm_tis_phy_ops tpm_spi_phy_ops
= {
204 .read_bytes
= tpm_tis_spi_read_bytes
,
205 .write_bytes
= tpm_tis_spi_write_bytes
,
206 .read16
= tpm_tis_spi_read16
,
207 .read32
= tpm_tis_spi_read32
,
208 .write32
= tpm_tis_spi_write32
,
211 static int tpm_tis_spi_probe(struct spi_device
*dev
)
213 struct tpm_tis_spi_phy
*phy
;
215 phy
= devm_kzalloc(&dev
->dev
, sizeof(struct tpm_tis_spi_phy
),
220 phy
->spi_device
= dev
;
222 return tpm_tis_core_init(&dev
->dev
, &phy
->priv
, -1, &tpm_spi_phy_ops
,
226 static SIMPLE_DEV_PM_OPS(tpm_tis_pm
, tpm_pm_suspend
, tpm_tis_resume
);
228 static int tpm_tis_spi_remove(struct spi_device
*dev
)
230 struct tpm_chip
*chip
= spi_get_drvdata(dev
);
232 tpm_chip_unregister(chip
);
233 tpm_tis_remove(chip
);
237 static const struct spi_device_id tpm_tis_spi_id
[] = {
241 MODULE_DEVICE_TABLE(spi
, tpm_tis_spi_id
);
243 static const struct of_device_id of_tis_spi_match
[] = {
244 { .compatible
= "st,st33htpm-spi", },
245 { .compatible
= "infineon,slb9670", },
246 { .compatible
= "tcg,tpm_tis-spi", },
249 MODULE_DEVICE_TABLE(of
, of_tis_spi_match
);
251 static const struct acpi_device_id acpi_tis_spi_match
[] = {
255 MODULE_DEVICE_TABLE(acpi
, acpi_tis_spi_match
);
257 static struct spi_driver tpm_tis_spi_driver
= {
259 .owner
= THIS_MODULE
,
260 .name
= "tpm_tis_spi",
262 .of_match_table
= of_match_ptr(of_tis_spi_match
),
263 .acpi_match_table
= ACPI_PTR(acpi_tis_spi_match
),
265 .probe
= tpm_tis_spi_probe
,
266 .remove
= tpm_tis_spi_remove
,
267 .id_table
= tpm_tis_spi_id
,
269 module_spi_driver(tpm_tis_spi_driver
);
271 MODULE_DESCRIPTION("TPM Driver for native SPI access");
272 MODULE_LICENSE("GPL");