2 * STMicroelectronics TPM SPI Linux driver for TPM ST33ZP24
3 * Copyright (C) 2009 - 2016 STMicroelectronics
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, see <http://www.gnu.org/licenses/>.
19 #include <linux/module.h>
20 #include <linux/spi/spi.h>
21 #include <linux/gpio.h>
22 #include <linux/gpio/consumer.h>
23 #include <linux/of_irq.h>
24 #include <linux/of_gpio.h>
25 #include <linux/acpi.h>
26 #include <linux/tpm.h>
27 #include <linux/platform_data/st33zp24.h>
32 #define TPM_DATA_FIFO 0x24
33 #define TPM_INTF_CAPABILITY 0x14
35 #define TPM_DUMMY_BYTE 0x00
37 #define MAX_SPI_LATENCY 15
40 #define ST33ZP24_OK 0x5A
41 #define ST33ZP24_UNDEFINED_ERR 0x80
42 #define ST33ZP24_BADLOCALITY 0x81
43 #define ST33ZP24_TISREGISTER_UKNOWN 0x82
44 #define ST33ZP24_LOCALITY_NOT_ACTIVATED 0x83
45 #define ST33ZP24_HASH_END_BEFORE_HASH_START 0x84
46 #define ST33ZP24_BAD_COMMAND_ORDER 0x85
47 #define ST33ZP24_INCORECT_RECEIVED_LENGTH 0x86
48 #define ST33ZP24_TPM_FIFO_OVERFLOW 0x89
49 #define ST33ZP24_UNEXPECTED_READ_FIFO 0x8A
50 #define ST33ZP24_UNEXPECTED_WRITE_FIFO 0x8B
51 #define ST33ZP24_CMDRDY_SET_WHEN_PROCESSING_HASH_END 0x90
52 #define ST33ZP24_DUMMY_BYTES 0x00
55 * TPM command can be up to 2048 byte, A TPM response can be up to
57 * Between command and response, there are latency byte (up to 15
58 * usually on st33zp24 2 are enough).
60 * Overall when sending a command and expecting an answer we need if
62 * 2048 (for the TPM command) + 1024 (for the TPM answer). We need
63 * some latency byte before the answer is available (max 15).
64 * We have 2048 + 1024 + 15.
66 #define ST33ZP24_SPI_BUFFER_SIZE (TPM_BUFSIZE + (TPM_BUFSIZE / 2) +\
70 struct st33zp24_spi_phy
{
71 struct spi_device
*spi_device
;
73 u8 tx_buf
[ST33ZP24_SPI_BUFFER_SIZE
];
74 u8 rx_buf
[ST33ZP24_SPI_BUFFER_SIZE
];
80 static int st33zp24_status_to_errno(u8 code
)
85 case ST33ZP24_UNDEFINED_ERR
:
86 case ST33ZP24_BADLOCALITY
:
87 case ST33ZP24_TISREGISTER_UKNOWN
:
88 case ST33ZP24_LOCALITY_NOT_ACTIVATED
:
89 case ST33ZP24_HASH_END_BEFORE_HASH_START
:
90 case ST33ZP24_BAD_COMMAND_ORDER
:
91 case ST33ZP24_UNEXPECTED_READ_FIFO
:
92 case ST33ZP24_UNEXPECTED_WRITE_FIFO
:
93 case ST33ZP24_CMDRDY_SET_WHEN_PROCESSING_HASH_END
:
95 case ST33ZP24_INCORECT_RECEIVED_LENGTH
:
96 case ST33ZP24_TPM_FIFO_OVERFLOW
:
98 case ST33ZP24_DUMMY_BYTES
:
106 * Send byte to the TIS register according to the ST33ZP24 SPI protocol.
107 * @param: phy_id, the phy description
108 * @param: tpm_register, the tpm tis register where the data should be written
109 * @param: tpm_data, the tpm_data to write inside the tpm_register
110 * @param: tpm_size, The length of the data
111 * @return: should be zero if success else a negative error code.
113 static int st33zp24_spi_send(void *phy_id
, u8 tpm_register
, u8
*tpm_data
,
116 int total_length
= 0, ret
= 0;
117 struct st33zp24_spi_phy
*phy
= phy_id
;
118 struct spi_device
*dev
= phy
->spi_device
;
119 struct spi_transfer spi_xfer
= {
120 .tx_buf
= phy
->tx_buf
,
121 .rx_buf
= phy
->rx_buf
,
125 phy
->tx_buf
[total_length
++] = TPM_WRITE_DIRECTION
| LOCALITY0
;
126 phy
->tx_buf
[total_length
++] = tpm_register
;
128 if (tpm_size
> 0 && tpm_register
== TPM_DATA_FIFO
) {
129 phy
->tx_buf
[total_length
++] = tpm_size
>> 8;
130 phy
->tx_buf
[total_length
++] = tpm_size
;
133 memcpy(&phy
->tx_buf
[total_length
], tpm_data
, tpm_size
);
134 total_length
+= tpm_size
;
136 memset(&phy
->tx_buf
[total_length
], TPM_DUMMY_BYTE
, phy
->latency
);
138 spi_xfer
.len
= total_length
+ phy
->latency
;
140 ret
= spi_sync_transfer(dev
, &spi_xfer
, 1);
142 ret
= phy
->rx_buf
[total_length
+ phy
->latency
- 1];
144 return st33zp24_status_to_errno(ret
);
145 } /* st33zp24_spi_send() */
148 * st33zp24_spi_read8_recv
149 * Recv byte from the TIS register according to the ST33ZP24 SPI protocol.
150 * @param: phy_id, the phy description
151 * @param: tpm_register, the tpm tis register where the data should be read
152 * @param: tpm_data, the TPM response
153 * @param: tpm_size, tpm TPM response size to read.
154 * @return: should be zero if success else a negative error code.
156 static int st33zp24_spi_read8_reg(void *phy_id
, u8 tpm_register
, u8
*tpm_data
,
159 int total_length
= 0, ret
;
160 struct st33zp24_spi_phy
*phy
= phy_id
;
161 struct spi_device
*dev
= phy
->spi_device
;
162 struct spi_transfer spi_xfer
= {
163 .tx_buf
= phy
->tx_buf
,
164 .rx_buf
= phy
->rx_buf
,
168 phy
->tx_buf
[total_length
++] = LOCALITY0
;
169 phy
->tx_buf
[total_length
++] = tpm_register
;
171 memset(&phy
->tx_buf
[total_length
], TPM_DUMMY_BYTE
,
172 phy
->latency
+ tpm_size
);
174 spi_xfer
.len
= total_length
+ phy
->latency
+ tpm_size
;
176 /* header + status byte + size of the data + status byte */
177 ret
= spi_sync_transfer(dev
, &spi_xfer
, 1);
178 if (tpm_size
> 0 && ret
== 0) {
179 ret
= phy
->rx_buf
[total_length
+ phy
->latency
- 1];
181 memcpy(tpm_data
, phy
->rx_buf
+ total_length
+ phy
->latency
,
186 } /* st33zp24_spi_read8_reg() */
190 * Recv byte from the TIS register according to the ST33ZP24 SPI protocol.
191 * @param: phy_id, the phy description
192 * @param: tpm_register, the tpm tis register where the data should be read
193 * @param: tpm_data, the TPM response
194 * @param: tpm_size, tpm TPM response size to read.
195 * @return: number of byte read successfully: should be one if success.
197 static int st33zp24_spi_recv(void *phy_id
, u8 tpm_register
, u8
*tpm_data
,
202 ret
= st33zp24_spi_read8_reg(phy_id
, tpm_register
, tpm_data
, tpm_size
);
203 if (!st33zp24_status_to_errno(ret
))
206 } /* st33zp24_spi_recv() */
208 static int st33zp24_spi_evaluate_latency(void *phy_id
)
210 struct st33zp24_spi_phy
*phy
= phy_id
;
211 int latency
= 1, status
= 0;
214 while (!status
&& latency
< MAX_SPI_LATENCY
) {
215 phy
->latency
= latency
;
216 status
= st33zp24_spi_read8_reg(phy_id
, TPM_INTF_CAPABILITY
,
222 if (latency
== MAX_SPI_LATENCY
)
226 } /* evaluate_latency() */
228 static const struct st33zp24_phy_ops spi_phy_ops
= {
229 .send
= st33zp24_spi_send
,
230 .recv
= st33zp24_spi_recv
,
233 static const struct acpi_gpio_params lpcpd_gpios
= { 1, 0, false };
235 static const struct acpi_gpio_mapping acpi_st33zp24_gpios
[] = {
236 { "lpcpd-gpios", &lpcpd_gpios
, 1 },
240 static int st33zp24_spi_acpi_request_resources(struct spi_device
*spi_dev
)
242 struct tpm_chip
*chip
= spi_get_drvdata(spi_dev
);
243 struct st33zp24_dev
*tpm_dev
= dev_get_drvdata(&chip
->dev
);
244 struct st33zp24_spi_phy
*phy
= tpm_dev
->phy_id
;
245 struct gpio_desc
*gpiod_lpcpd
;
246 struct device
*dev
= &spi_dev
->dev
;
249 ret
= acpi_dev_add_driver_gpios(ACPI_COMPANION(dev
), acpi_st33zp24_gpios
);
253 /* Get LPCPD GPIO from ACPI */
254 gpiod_lpcpd
= devm_gpiod_get(dev
, "lpcpd", GPIOD_OUT_HIGH
);
255 if (IS_ERR(gpiod_lpcpd
)) {
256 dev_err(dev
, "Failed to retrieve lpcpd-gpios from acpi.\n");
259 * lpcpd pin is not specified. This is not an issue as
260 * power management can be also managed by TPM specific
261 * commands. So leave with a success status code.
266 phy
->io_lpcpd
= desc_to_gpio(gpiod_lpcpd
);
271 static int st33zp24_spi_of_request_resources(struct spi_device
*spi_dev
)
273 struct tpm_chip
*chip
= spi_get_drvdata(spi_dev
);
274 struct st33zp24_dev
*tpm_dev
= dev_get_drvdata(&chip
->dev
);
275 struct st33zp24_spi_phy
*phy
= tpm_dev
->phy_id
;
276 struct device_node
*pp
;
280 pp
= spi_dev
->dev
.of_node
;
282 dev_err(&spi_dev
->dev
, "No platform data\n");
286 /* Get GPIO from device tree */
287 gpio
= of_get_named_gpio(pp
, "lpcpd-gpios", 0);
289 dev_err(&spi_dev
->dev
,
290 "Failed to retrieve lpcpd-gpios from dts.\n");
293 * lpcpd pin is not specified. This is not an issue as
294 * power management can be also managed by TPM specific
295 * commands. So leave with a success status code.
299 /* GPIO request and configuration */
300 ret
= devm_gpio_request_one(&spi_dev
->dev
, gpio
,
301 GPIOF_OUT_INIT_HIGH
, "TPM IO LPCPD");
303 dev_err(&spi_dev
->dev
, "Failed to request lpcpd pin\n");
306 phy
->io_lpcpd
= gpio
;
311 static int st33zp24_spi_request_resources(struct spi_device
*dev
)
313 struct tpm_chip
*chip
= spi_get_drvdata(dev
);
314 struct st33zp24_dev
*tpm_dev
= dev_get_drvdata(&chip
->dev
);
315 struct st33zp24_spi_phy
*phy
= tpm_dev
->phy_id
;
316 struct st33zp24_platform_data
*pdata
;
319 pdata
= dev
->dev
.platform_data
;
321 dev_err(&dev
->dev
, "No platform data\n");
325 /* store for late use */
326 phy
->io_lpcpd
= pdata
->io_lpcpd
;
328 if (gpio_is_valid(pdata
->io_lpcpd
)) {
329 ret
= devm_gpio_request_one(&dev
->dev
,
330 pdata
->io_lpcpd
, GPIOF_OUT_INIT_HIGH
,
333 dev_err(&dev
->dev
, "%s : reset gpio_request failed\n",
343 * st33zp24_spi_probe initialize the TPM device
344 * @param: dev, the spi_device drescription (TPM SPI description).
345 * @return: 0 in case of success.
346 * or a negative value describing the error.
348 static int st33zp24_spi_probe(struct spi_device
*dev
)
351 struct st33zp24_platform_data
*pdata
;
352 struct st33zp24_spi_phy
*phy
;
354 /* Check SPI platform functionnalities */
356 pr_info("%s: dev is NULL. Device is not accessible.\n",
361 phy
= devm_kzalloc(&dev
->dev
, sizeof(struct st33zp24_spi_phy
),
366 phy
->spi_device
= dev
;
368 pdata
= dev
->dev
.platform_data
;
369 if (!pdata
&& dev
->dev
.of_node
) {
370 ret
= st33zp24_spi_of_request_resources(dev
);
374 ret
= st33zp24_spi_request_resources(dev
);
377 } else if (ACPI_HANDLE(&dev
->dev
)) {
378 ret
= st33zp24_spi_acpi_request_resources(dev
);
383 phy
->latency
= st33zp24_spi_evaluate_latency(phy
);
384 if (phy
->latency
<= 0)
387 return st33zp24_probe(phy
, &spi_phy_ops
, &dev
->dev
, dev
->irq
,
392 * st33zp24_spi_remove remove the TPM device
393 * @param: client, the spi_device drescription (TPM SPI description).
394 * @return: 0 in case of success.
396 static int st33zp24_spi_remove(struct spi_device
*dev
)
398 struct tpm_chip
*chip
= spi_get_drvdata(dev
);
401 ret
= st33zp24_remove(chip
);
405 acpi_dev_remove_driver_gpios(ACPI_COMPANION(&dev
->dev
));
409 static const struct spi_device_id st33zp24_spi_id
[] = {
413 MODULE_DEVICE_TABLE(spi
, st33zp24_spi_id
);
415 static const struct of_device_id of_st33zp24_spi_match
[] = {
416 { .compatible
= "st,st33zp24-spi", },
419 MODULE_DEVICE_TABLE(of
, of_st33zp24_spi_match
);
421 static const struct acpi_device_id st33zp24_spi_acpi_match
[] = {
425 MODULE_DEVICE_TABLE(acpi
, st33zp24_spi_acpi_match
);
427 static SIMPLE_DEV_PM_OPS(st33zp24_spi_ops
, st33zp24_pm_suspend
,
430 static struct spi_driver st33zp24_spi_driver
= {
432 .name
= TPM_ST33_SPI
,
433 .pm
= &st33zp24_spi_ops
,
434 .of_match_table
= of_match_ptr(of_st33zp24_spi_match
),
435 .acpi_match_table
= ACPI_PTR(st33zp24_spi_acpi_match
),
437 .probe
= st33zp24_spi_probe
,
438 .remove
= st33zp24_spi_remove
,
439 .id_table
= st33zp24_spi_id
,
442 module_spi_driver(st33zp24_spi_driver
);
444 MODULE_AUTHOR("TPM support (TPMsupport@list.st.com)");
445 MODULE_DESCRIPTION("STM TPM 1.2 SPI ST33 Driver");
446 MODULE_VERSION("1.3.0");
447 MODULE_LICENSE("GPL");