1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * STMicroelectronics TPM SPI Linux driver for TPM ST33ZP24
4 * Copyright (C) 2009 - 2016 STMicroelectronics
7 #include <linux/module.h>
8 #include <linux/spi/spi.h>
10 #include <linux/acpi.h>
11 #include <linux/tpm.h>
16 #define TPM_DATA_FIFO 0x24
17 #define TPM_INTF_CAPABILITY 0x14
19 #define TPM_DUMMY_BYTE 0x00
21 #define MAX_SPI_LATENCY 15
24 #define ST33ZP24_OK 0x5A
25 #define ST33ZP24_UNDEFINED_ERR 0x80
26 #define ST33ZP24_BADLOCALITY 0x81
27 #define ST33ZP24_TISREGISTER_UNKNOWN 0x82
28 #define ST33ZP24_LOCALITY_NOT_ACTIVATED 0x83
29 #define ST33ZP24_HASH_END_BEFORE_HASH_START 0x84
30 #define ST33ZP24_BAD_COMMAND_ORDER 0x85
31 #define ST33ZP24_INCORECT_RECEIVED_LENGTH 0x86
32 #define ST33ZP24_TPM_FIFO_OVERFLOW 0x89
33 #define ST33ZP24_UNEXPECTED_READ_FIFO 0x8A
34 #define ST33ZP24_UNEXPECTED_WRITE_FIFO 0x8B
35 #define ST33ZP24_CMDRDY_SET_WHEN_PROCESSING_HASH_END 0x90
36 #define ST33ZP24_DUMMY_BYTES 0x00
39 * TPM command can be up to 2048 byte, A TPM response can be up to
41 * Between command and response, there are latency byte (up to 15
42 * usually on st33zp24 2 are enough).
44 * Overall when sending a command and expecting an answer we need if
46 * 2048 (for the TPM command) + 1024 (for the TPM answer). We need
47 * some latency byte before the answer is available (max 15).
48 * We have 2048 + 1024 + 15.
50 #define ST33ZP24_SPI_BUFFER_SIZE (ST33ZP24_BUFSIZE + (ST33ZP24_BUFSIZE / 2) +\
54 struct st33zp24_spi_phy
{
55 struct spi_device
*spi_device
;
57 u8 tx_buf
[ST33ZP24_SPI_BUFFER_SIZE
];
58 u8 rx_buf
[ST33ZP24_SPI_BUFFER_SIZE
];
63 static int st33zp24_status_to_errno(u8 code
)
68 case ST33ZP24_UNDEFINED_ERR
:
69 case ST33ZP24_BADLOCALITY
:
70 case ST33ZP24_TISREGISTER_UNKNOWN
:
71 case ST33ZP24_LOCALITY_NOT_ACTIVATED
:
72 case ST33ZP24_HASH_END_BEFORE_HASH_START
:
73 case ST33ZP24_BAD_COMMAND_ORDER
:
74 case ST33ZP24_UNEXPECTED_READ_FIFO
:
75 case ST33ZP24_UNEXPECTED_WRITE_FIFO
:
76 case ST33ZP24_CMDRDY_SET_WHEN_PROCESSING_HASH_END
:
78 case ST33ZP24_INCORECT_RECEIVED_LENGTH
:
79 case ST33ZP24_TPM_FIFO_OVERFLOW
:
81 case ST33ZP24_DUMMY_BYTES
:
89 * Send byte to the TIS register according to the ST33ZP24 SPI protocol.
90 * @param: phy_id, the phy description
91 * @param: tpm_register, the tpm tis register where the data should be written
92 * @param: tpm_data, the tpm_data to write inside the tpm_register
93 * @param: tpm_size, The length of the data
94 * @return: should be zero if success else a negative error code.
96 static int st33zp24_spi_send(void *phy_id
, u8 tpm_register
, u8
*tpm_data
,
99 int total_length
= 0, ret
= 0;
100 struct st33zp24_spi_phy
*phy
= phy_id
;
101 struct spi_device
*dev
= phy
->spi_device
;
102 struct spi_transfer spi_xfer
= {
103 .tx_buf
= phy
->tx_buf
,
104 .rx_buf
= phy
->rx_buf
,
108 phy
->tx_buf
[total_length
++] = TPM_WRITE_DIRECTION
| LOCALITY0
;
109 phy
->tx_buf
[total_length
++] = tpm_register
;
111 if (tpm_size
> 0 && tpm_register
== TPM_DATA_FIFO
) {
112 phy
->tx_buf
[total_length
++] = tpm_size
>> 8;
113 phy
->tx_buf
[total_length
++] = tpm_size
;
116 memcpy(&phy
->tx_buf
[total_length
], tpm_data
, tpm_size
);
117 total_length
+= tpm_size
;
119 memset(&phy
->tx_buf
[total_length
], TPM_DUMMY_BYTE
, phy
->latency
);
121 spi_xfer
.len
= total_length
+ phy
->latency
;
123 ret
= spi_sync_transfer(dev
, &spi_xfer
, 1);
125 ret
= phy
->rx_buf
[total_length
+ phy
->latency
- 1];
127 return st33zp24_status_to_errno(ret
);
128 } /* st33zp24_spi_send() */
131 * st33zp24_spi_read8_recv
132 * Recv byte from the TIS register according to the ST33ZP24 SPI protocol.
133 * @param: phy_id, the phy description
134 * @param: tpm_register, the tpm tis register where the data should be read
135 * @param: tpm_data, the TPM response
136 * @param: tpm_size, tpm TPM response size to read.
137 * @return: should be zero if success else a negative error code.
139 static int st33zp24_spi_read8_reg(void *phy_id
, u8 tpm_register
, u8
*tpm_data
,
142 int total_length
= 0, ret
;
143 struct st33zp24_spi_phy
*phy
= phy_id
;
144 struct spi_device
*dev
= phy
->spi_device
;
145 struct spi_transfer spi_xfer
= {
146 .tx_buf
= phy
->tx_buf
,
147 .rx_buf
= phy
->rx_buf
,
151 phy
->tx_buf
[total_length
++] = LOCALITY0
;
152 phy
->tx_buf
[total_length
++] = tpm_register
;
154 memset(&phy
->tx_buf
[total_length
], TPM_DUMMY_BYTE
,
155 phy
->latency
+ tpm_size
);
157 spi_xfer
.len
= total_length
+ phy
->latency
+ tpm_size
;
159 /* header + status byte + size of the data + status byte */
160 ret
= spi_sync_transfer(dev
, &spi_xfer
, 1);
161 if (tpm_size
> 0 && ret
== 0) {
162 ret
= phy
->rx_buf
[total_length
+ phy
->latency
- 1];
164 memcpy(tpm_data
, phy
->rx_buf
+ total_length
+ phy
->latency
,
169 } /* st33zp24_spi_read8_reg() */
173 * Recv byte from the TIS register according to the ST33ZP24 SPI protocol.
174 * @param: phy_id, the phy description
175 * @param: tpm_register, the tpm tis register where the data should be read
176 * @param: tpm_data, the TPM response
177 * @param: tpm_size, tpm TPM response size to read.
178 * @return: number of byte read successfully: should be one if success.
180 static int st33zp24_spi_recv(void *phy_id
, u8 tpm_register
, u8
*tpm_data
,
185 ret
= st33zp24_spi_read8_reg(phy_id
, tpm_register
, tpm_data
, tpm_size
);
186 if (!st33zp24_status_to_errno(ret
))
189 } /* st33zp24_spi_recv() */
191 static int st33zp24_spi_evaluate_latency(void *phy_id
)
193 struct st33zp24_spi_phy
*phy
= phy_id
;
194 int latency
= 1, status
= 0;
197 while (!status
&& latency
< MAX_SPI_LATENCY
) {
198 phy
->latency
= latency
;
199 status
= st33zp24_spi_read8_reg(phy_id
, TPM_INTF_CAPABILITY
,
205 if (latency
== MAX_SPI_LATENCY
)
209 } /* evaluate_latency() */
211 static const struct st33zp24_phy_ops spi_phy_ops
= {
212 .send
= st33zp24_spi_send
,
213 .recv
= st33zp24_spi_recv
,
217 * st33zp24_spi_probe initialize the TPM device
218 * @param: dev, the spi_device description (TPM SPI description).
219 * @return: 0 in case of success.
220 * or a negative value describing the error.
222 static int st33zp24_spi_probe(struct spi_device
*dev
)
224 struct st33zp24_spi_phy
*phy
;
226 phy
= devm_kzalloc(&dev
->dev
, sizeof(struct st33zp24_spi_phy
),
231 phy
->spi_device
= dev
;
233 phy
->latency
= st33zp24_spi_evaluate_latency(phy
);
234 if (phy
->latency
<= 0)
237 return st33zp24_probe(phy
, &spi_phy_ops
, &dev
->dev
, dev
->irq
);
241 * st33zp24_spi_remove remove the TPM device
242 * @param: client, the spi_device description (TPM SPI description).
243 * @return: 0 in case of success.
245 static void st33zp24_spi_remove(struct spi_device
*dev
)
247 struct tpm_chip
*chip
= spi_get_drvdata(dev
);
249 st33zp24_remove(chip
);
252 static const struct spi_device_id st33zp24_spi_id
[] = {
256 MODULE_DEVICE_TABLE(spi
, st33zp24_spi_id
);
258 static const struct of_device_id of_st33zp24_spi_match
[] __maybe_unused
= {
259 { .compatible
= "st,st33zp24-spi", },
262 MODULE_DEVICE_TABLE(of
, of_st33zp24_spi_match
);
264 static const struct acpi_device_id st33zp24_spi_acpi_match
[] __maybe_unused
= {
268 MODULE_DEVICE_TABLE(acpi
, st33zp24_spi_acpi_match
);
270 static SIMPLE_DEV_PM_OPS(st33zp24_spi_ops
, st33zp24_pm_suspend
,
273 static struct spi_driver st33zp24_spi_driver
= {
275 .name
= "st33zp24-spi",
276 .pm
= &st33zp24_spi_ops
,
277 .of_match_table
= of_match_ptr(of_st33zp24_spi_match
),
278 .acpi_match_table
= ACPI_PTR(st33zp24_spi_acpi_match
),
280 .probe
= st33zp24_spi_probe
,
281 .remove
= st33zp24_spi_remove
,
282 .id_table
= st33zp24_spi_id
,
285 module_spi_driver(st33zp24_spi_driver
);
287 MODULE_AUTHOR("TPM support <TPMsupport@list.st.com>");
288 MODULE_DESCRIPTION("STM TPM 1.2 SPI ST33 Driver");
289 MODULE_VERSION("1.3.0");
290 MODULE_LICENSE("GPL");