11ccea77eSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2f042a315SChristophe Ricard /*
3f042a315SChristophe Ricard * STMicroelectronics TPM SPI Linux driver for TPM ST33ZP24
48bb273f2SChristophe RICARD * Copyright (C) 2009 - 2016 STMicroelectronics
5f042a315SChristophe Ricard */
6f042a315SChristophe Ricard
7f042a315SChristophe Ricard #include <linux/module.h>
8f042a315SChristophe Ricard #include <linux/spi/spi.h>
93f801909SDmitry Torokhov #include <linux/of.h>
1086ff205bSChristophe RICARD #include <linux/acpi.h>
11f042a315SChristophe Ricard #include <linux/tpm.h>
12f042a315SChristophe Ricard
139e0d39d8SChristophe Ricard #include "../tpm.h"
14f042a315SChristophe Ricard #include "st33zp24.h"
15f042a315SChristophe Ricard
16f042a315SChristophe Ricard #define TPM_DATA_FIFO 0x24
17f042a315SChristophe Ricard #define TPM_INTF_CAPABILITY 0x14
18f042a315SChristophe Ricard
19f042a315SChristophe Ricard #define TPM_DUMMY_BYTE 0x00
20f042a315SChristophe Ricard
21f042a315SChristophe Ricard #define MAX_SPI_LATENCY 15
22f042a315SChristophe Ricard #define LOCALITY0 0
23f042a315SChristophe Ricard
24f042a315SChristophe Ricard #define ST33ZP24_OK 0x5A
25f042a315SChristophe Ricard #define ST33ZP24_UNDEFINED_ERR 0x80
26f042a315SChristophe Ricard #define ST33ZP24_BADLOCALITY 0x81
27c922ff8eSColin Ian King #define ST33ZP24_TISREGISTER_UNKNOWN 0x82
28f042a315SChristophe Ricard #define ST33ZP24_LOCALITY_NOT_ACTIVATED 0x83
29f042a315SChristophe Ricard #define ST33ZP24_HASH_END_BEFORE_HASH_START 0x84
30f042a315SChristophe Ricard #define ST33ZP24_BAD_COMMAND_ORDER 0x85
31f042a315SChristophe Ricard #define ST33ZP24_INCORECT_RECEIVED_LENGTH 0x86
32f042a315SChristophe Ricard #define ST33ZP24_TPM_FIFO_OVERFLOW 0x89
33f042a315SChristophe Ricard #define ST33ZP24_UNEXPECTED_READ_FIFO 0x8A
34f042a315SChristophe Ricard #define ST33ZP24_UNEXPECTED_WRITE_FIFO 0x8B
35f042a315SChristophe Ricard #define ST33ZP24_CMDRDY_SET_WHEN_PROCESSING_HASH_END 0x90
36f042a315SChristophe Ricard #define ST33ZP24_DUMMY_BYTES 0x00
37f042a315SChristophe Ricard
38f042a315SChristophe Ricard /*
39f042a315SChristophe Ricard * TPM command can be up to 2048 byte, A TPM response can be up to
40f042a315SChristophe Ricard * 1024 byte.
41f042a315SChristophe Ricard * Between command and response, there are latency byte (up to 15
42f042a315SChristophe Ricard * usually on st33zp24 2 are enough).
43f042a315SChristophe Ricard *
44f042a315SChristophe Ricard * Overall when sending a command and expecting an answer we need if
45f042a315SChristophe Ricard * worst case:
46f042a315SChristophe Ricard * 2048 (for the TPM command) + 1024 (for the TPM answer). We need
47f042a315SChristophe Ricard * some latency byte before the answer is available (max 15).
48f042a315SChristophe Ricard * We have 2048 + 1024 + 15.
49f042a315SChristophe Ricard */
508ab547a2SJarkko Sakkinen #define ST33ZP24_SPI_BUFFER_SIZE (ST33ZP24_BUFSIZE + (ST33ZP24_BUFSIZE / 2) +\
51f042a315SChristophe Ricard MAX_SPI_LATENCY)
52f042a315SChristophe Ricard
53f042a315SChristophe Ricard
54f042a315SChristophe Ricard struct st33zp24_spi_phy {
55f042a315SChristophe Ricard struct spi_device *spi_device;
56a5392e91SChristophe RICARD
57f042a315SChristophe Ricard u8 tx_buf[ST33ZP24_SPI_BUFFER_SIZE];
58f042a315SChristophe Ricard u8 rx_buf[ST33ZP24_SPI_BUFFER_SIZE];
59f042a315SChristophe Ricard
60f042a315SChristophe Ricard int latency;
61f042a315SChristophe Ricard };
62f042a315SChristophe Ricard
st33zp24_status_to_errno(u8 code)63f042a315SChristophe Ricard static int st33zp24_status_to_errno(u8 code)
64f042a315SChristophe Ricard {
65f042a315SChristophe Ricard switch (code) {
66f042a315SChristophe Ricard case ST33ZP24_OK:
67f042a315SChristophe Ricard return 0;
68f042a315SChristophe Ricard case ST33ZP24_UNDEFINED_ERR:
69f042a315SChristophe Ricard case ST33ZP24_BADLOCALITY:
70c922ff8eSColin Ian King case ST33ZP24_TISREGISTER_UNKNOWN:
71f042a315SChristophe Ricard case ST33ZP24_LOCALITY_NOT_ACTIVATED:
72f042a315SChristophe Ricard case ST33ZP24_HASH_END_BEFORE_HASH_START:
73f042a315SChristophe Ricard case ST33ZP24_BAD_COMMAND_ORDER:
74f042a315SChristophe Ricard case ST33ZP24_UNEXPECTED_READ_FIFO:
75f042a315SChristophe Ricard case ST33ZP24_UNEXPECTED_WRITE_FIFO:
76f042a315SChristophe Ricard case ST33ZP24_CMDRDY_SET_WHEN_PROCESSING_HASH_END:
77f042a315SChristophe Ricard return -EPROTO;
78f042a315SChristophe Ricard case ST33ZP24_INCORECT_RECEIVED_LENGTH:
79f042a315SChristophe Ricard case ST33ZP24_TPM_FIFO_OVERFLOW:
80f042a315SChristophe Ricard return -EMSGSIZE;
81f042a315SChristophe Ricard case ST33ZP24_DUMMY_BYTES:
82f042a315SChristophe Ricard return -ENOSYS;
83f042a315SChristophe Ricard }
84f042a315SChristophe Ricard return code;
85f042a315SChristophe Ricard }
86f042a315SChristophe Ricard
87f042a315SChristophe Ricard /*
88f042a315SChristophe Ricard * st33zp24_spi_send
89f042a315SChristophe Ricard * Send byte to the TIS register according to the ST33ZP24 SPI protocol.
90f042a315SChristophe Ricard * @param: phy_id, the phy description
91f042a315SChristophe Ricard * @param: tpm_register, the tpm tis register where the data should be written
92f042a315SChristophe Ricard * @param: tpm_data, the tpm_data to write inside the tpm_register
93f042a315SChristophe Ricard * @param: tpm_size, The length of the data
94f042a315SChristophe Ricard * @return: should be zero if success else a negative error code.
95f042a315SChristophe Ricard */
st33zp24_spi_send(void * phy_id,u8 tpm_register,u8 * tpm_data,int tpm_size)96f042a315SChristophe Ricard static int st33zp24_spi_send(void *phy_id, u8 tpm_register, u8 *tpm_data,
97f042a315SChristophe Ricard int tpm_size)
98f042a315SChristophe Ricard {
999feaab5dSChristophe RICARD int total_length = 0, ret = 0;
100f042a315SChristophe Ricard struct st33zp24_spi_phy *phy = phy_id;
101f042a315SChristophe Ricard struct spi_device *dev = phy->spi_device;
102a5392e91SChristophe RICARD struct spi_transfer spi_xfer = {
103a5392e91SChristophe RICARD .tx_buf = phy->tx_buf,
104a5392e91SChristophe RICARD .rx_buf = phy->rx_buf,
105a5392e91SChristophe RICARD };
106f042a315SChristophe Ricard
107f042a315SChristophe Ricard /* Pre-Header */
108a5392e91SChristophe RICARD phy->tx_buf[total_length++] = TPM_WRITE_DIRECTION | LOCALITY0;
109a5392e91SChristophe RICARD phy->tx_buf[total_length++] = tpm_register;
110f042a315SChristophe Ricard
111f042a315SChristophe Ricard if (tpm_size > 0 && tpm_register == TPM_DATA_FIFO) {
112a5392e91SChristophe RICARD phy->tx_buf[total_length++] = tpm_size >> 8;
113a5392e91SChristophe RICARD phy->tx_buf[total_length++] = tpm_size;
114f042a315SChristophe Ricard }
115f042a315SChristophe Ricard
116a5392e91SChristophe RICARD memcpy(&phy->tx_buf[total_length], tpm_data, tpm_size);
117f042a315SChristophe Ricard total_length += tpm_size;
118f042a315SChristophe Ricard
119a5392e91SChristophe RICARD memset(&phy->tx_buf[total_length], TPM_DUMMY_BYTE, phy->latency);
120f042a315SChristophe Ricard
121a5392e91SChristophe RICARD spi_xfer.len = total_length + phy->latency;
122f042a315SChristophe Ricard
123a5392e91SChristophe RICARD ret = spi_sync_transfer(dev, &spi_xfer, 1);
124f042a315SChristophe Ricard if (ret == 0)
125a5392e91SChristophe RICARD ret = phy->rx_buf[total_length + phy->latency - 1];
126f042a315SChristophe Ricard
127f042a315SChristophe Ricard return st33zp24_status_to_errno(ret);
128f042a315SChristophe Ricard } /* st33zp24_spi_send() */
129f042a315SChristophe Ricard
130f042a315SChristophe Ricard /*
1318a745003SChristophe RICARD * st33zp24_spi_read8_recv
132f042a315SChristophe Ricard * Recv byte from the TIS register according to the ST33ZP24 SPI protocol.
133f042a315SChristophe Ricard * @param: phy_id, the phy description
134f042a315SChristophe Ricard * @param: tpm_register, the tpm tis register where the data should be read
135f042a315SChristophe Ricard * @param: tpm_data, the TPM response
136f042a315SChristophe Ricard * @param: tpm_size, tpm TPM response size to read.
137f042a315SChristophe Ricard * @return: should be zero if success else a negative error code.
138f042a315SChristophe Ricard */
st33zp24_spi_read8_reg(void * phy_id,u8 tpm_register,u8 * tpm_data,int tpm_size)1398a745003SChristophe RICARD static int st33zp24_spi_read8_reg(void *phy_id, u8 tpm_register, u8 *tpm_data,
1408a745003SChristophe RICARD int tpm_size)
141f042a315SChristophe Ricard {
1429feaab5dSChristophe RICARD int total_length = 0, ret;
143f042a315SChristophe Ricard struct st33zp24_spi_phy *phy = phy_id;
144f042a315SChristophe Ricard struct spi_device *dev = phy->spi_device;
145a5392e91SChristophe RICARD struct spi_transfer spi_xfer = {
146a5392e91SChristophe RICARD .tx_buf = phy->tx_buf,
147a5392e91SChristophe RICARD .rx_buf = phy->rx_buf,
148a5392e91SChristophe RICARD };
149f042a315SChristophe Ricard
150f042a315SChristophe Ricard /* Pre-Header */
151a5392e91SChristophe RICARD phy->tx_buf[total_length++] = LOCALITY0;
152a5392e91SChristophe RICARD phy->tx_buf[total_length++] = tpm_register;
153f042a315SChristophe Ricard
154a5392e91SChristophe RICARD memset(&phy->tx_buf[total_length], TPM_DUMMY_BYTE,
1559feaab5dSChristophe RICARD phy->latency + tpm_size);
156f042a315SChristophe Ricard
157a5392e91SChristophe RICARD spi_xfer.len = total_length + phy->latency + tpm_size;
158f042a315SChristophe Ricard
159f042a315SChristophe Ricard /* header + status byte + size of the data + status byte */
160a5392e91SChristophe RICARD ret = spi_sync_transfer(dev, &spi_xfer, 1);
161f042a315SChristophe Ricard if (tpm_size > 0 && ret == 0) {
162a5392e91SChristophe RICARD ret = phy->rx_buf[total_length + phy->latency - 1];
163f042a315SChristophe Ricard
164a5392e91SChristophe RICARD memcpy(tpm_data, phy->rx_buf + total_length + phy->latency,
165f042a315SChristophe Ricard tpm_size);
166f042a315SChristophe Ricard }
167f042a315SChristophe Ricard
168f042a315SChristophe Ricard return ret;
1698a745003SChristophe RICARD } /* st33zp24_spi_read8_reg() */
170f042a315SChristophe Ricard
171f042a315SChristophe Ricard /*
172f042a315SChristophe Ricard * st33zp24_spi_recv
173f042a315SChristophe Ricard * Recv byte from the TIS register according to the ST33ZP24 SPI protocol.
174f042a315SChristophe Ricard * @param: phy_id, the phy description
175f042a315SChristophe Ricard * @param: tpm_register, the tpm tis register where the data should be read
176f042a315SChristophe Ricard * @param: tpm_data, the TPM response
177f042a315SChristophe Ricard * @param: tpm_size, tpm TPM response size to read.
178f042a315SChristophe Ricard * @return: number of byte read successfully: should be one if success.
179f042a315SChristophe Ricard */
st33zp24_spi_recv(void * phy_id,u8 tpm_register,u8 * tpm_data,int tpm_size)180f042a315SChristophe Ricard static int st33zp24_spi_recv(void *phy_id, u8 tpm_register, u8 *tpm_data,
181f042a315SChristophe Ricard int tpm_size)
182f042a315SChristophe Ricard {
183f042a315SChristophe Ricard int ret;
184f042a315SChristophe Ricard
1858a745003SChristophe RICARD ret = st33zp24_spi_read8_reg(phy_id, tpm_register, tpm_data, tpm_size);
186f042a315SChristophe Ricard if (!st33zp24_status_to_errno(ret))
187f042a315SChristophe Ricard return tpm_size;
188f042a315SChristophe Ricard return ret;
189f042a315SChristophe Ricard } /* st33zp24_spi_recv() */
190f042a315SChristophe Ricard
st33zp24_spi_evaluate_latency(void * phy_id)1918a745003SChristophe RICARD static int st33zp24_spi_evaluate_latency(void *phy_id)
192f042a315SChristophe Ricard {
193f042a315SChristophe Ricard struct st33zp24_spi_phy *phy = phy_id;
194f042a315SChristophe Ricard int latency = 1, status = 0;
195f042a315SChristophe Ricard u8 data = 0;
196f042a315SChristophe Ricard
197f042a315SChristophe Ricard while (!status && latency < MAX_SPI_LATENCY) {
198f042a315SChristophe Ricard phy->latency = latency;
1998a745003SChristophe RICARD status = st33zp24_spi_read8_reg(phy_id, TPM_INTF_CAPABILITY,
2008a745003SChristophe RICARD &data, 1);
201f042a315SChristophe Ricard latency++;
202f042a315SChristophe Ricard }
2034ef2aa3cSChristophe RICARD if (status < 0)
2044ef2aa3cSChristophe RICARD return status;
2054ef2aa3cSChristophe RICARD if (latency == MAX_SPI_LATENCY)
2064ef2aa3cSChristophe RICARD return -ENODEV;
2074ef2aa3cSChristophe RICARD
208f042a315SChristophe Ricard return latency - 1;
209f042a315SChristophe Ricard } /* evaluate_latency() */
210f042a315SChristophe Ricard
211f042a315SChristophe Ricard static const struct st33zp24_phy_ops spi_phy_ops = {
212f042a315SChristophe Ricard .send = st33zp24_spi_send,
213f042a315SChristophe Ricard .recv = st33zp24_spi_recv,
214f042a315SChristophe Ricard };
215f042a315SChristophe Ricard
216f042a315SChristophe Ricard /*
2178a745003SChristophe RICARD * st33zp24_spi_probe initialize the TPM device
21882efeb16SBinbin Zhou * @param: dev, the spi_device description (TPM SPI description).
219f042a315SChristophe Ricard * @return: 0 in case of success.
220f042a315SChristophe Ricard * or a negative value describing the error.
221f042a315SChristophe Ricard */
st33zp24_spi_probe(struct spi_device * dev)2228a745003SChristophe RICARD static int st33zp24_spi_probe(struct spi_device *dev)
223f042a315SChristophe Ricard {
224f042a315SChristophe Ricard struct st33zp24_spi_phy *phy;
225f042a315SChristophe Ricard
226f042a315SChristophe Ricard phy = devm_kzalloc(&dev->dev, sizeof(struct st33zp24_spi_phy),
227f042a315SChristophe Ricard GFP_KERNEL);
228f042a315SChristophe Ricard if (!phy)
229f042a315SChristophe Ricard return -ENOMEM;
230f042a315SChristophe Ricard
231f042a315SChristophe Ricard phy->spi_device = dev;
2324d8007eeSChristophe RICARD
2338a745003SChristophe RICARD phy->latency = st33zp24_spi_evaluate_latency(phy);
234f042a315SChristophe Ricard if (phy->latency <= 0)
235f042a315SChristophe Ricard return -ENODEV;
236f042a315SChristophe Ricard
2373f801909SDmitry Torokhov return st33zp24_probe(phy, &spi_phy_ops, &dev->dev, dev->irq);
238f042a315SChristophe Ricard }
239f042a315SChristophe Ricard
240f042a315SChristophe Ricard /*
2418a745003SChristophe RICARD * st33zp24_spi_remove remove the TPM device
24282efeb16SBinbin Zhou * @param: client, the spi_device description (TPM SPI description).
243f042a315SChristophe Ricard * @return: 0 in case of success.
244f042a315SChristophe Ricard */
st33zp24_spi_remove(struct spi_device * dev)245a0386bbaSUwe Kleine-König static void st33zp24_spi_remove(struct spi_device *dev)
246f042a315SChristophe Ricard {
247f042a315SChristophe Ricard struct tpm_chip *chip = spi_get_drvdata(dev);
248f042a315SChristophe Ricard
249316f569dSUwe Kleine-König st33zp24_remove(chip);
250f042a315SChristophe Ricard }
251f042a315SChristophe Ricard
2521018bc26SChristophe Ricard static const struct spi_device_id st33zp24_spi_id[] = {
2531018bc26SChristophe Ricard {TPM_ST33_SPI, 0},
2541018bc26SChristophe Ricard {}
2551018bc26SChristophe Ricard };
2561018bc26SChristophe Ricard MODULE_DEVICE_TABLE(spi, st33zp24_spi_id);
2571018bc26SChristophe Ricard
258*c3985d8bSKrzysztof Kozlowski static const struct of_device_id of_st33zp24_spi_match[] __maybe_unused = {
259f042a315SChristophe Ricard { .compatible = "st,st33zp24-spi", },
260f042a315SChristophe Ricard {}
261f042a315SChristophe Ricard };
262f042a315SChristophe Ricard MODULE_DEVICE_TABLE(of, of_st33zp24_spi_match);
263160beb40SChristophe RICARD
264*c3985d8bSKrzysztof Kozlowski static const struct acpi_device_id st33zp24_spi_acpi_match[] __maybe_unused = {
26586ff205bSChristophe RICARD {"SMO3324"},
26686ff205bSChristophe RICARD {}
26786ff205bSChristophe RICARD };
26886ff205bSChristophe RICARD MODULE_DEVICE_TABLE(acpi, st33zp24_spi_acpi_match);
26986ff205bSChristophe RICARD
270f042a315SChristophe Ricard static SIMPLE_DEV_PM_OPS(st33zp24_spi_ops, st33zp24_pm_suspend,
271f042a315SChristophe Ricard st33zp24_pm_resume);
272f042a315SChristophe Ricard
2738a745003SChristophe RICARD static struct spi_driver st33zp24_spi_driver = {
274f042a315SChristophe Ricard .driver = {
27504593028SDmitry Torokhov .name = "st33zp24-spi",
276f042a315SChristophe Ricard .pm = &st33zp24_spi_ops,
277f042a315SChristophe Ricard .of_match_table = of_match_ptr(of_st33zp24_spi_match),
27886ff205bSChristophe RICARD .acpi_match_table = ACPI_PTR(st33zp24_spi_acpi_match),
279f042a315SChristophe Ricard },
2808a745003SChristophe RICARD .probe = st33zp24_spi_probe,
2818a745003SChristophe RICARD .remove = st33zp24_spi_remove,
2821018bc26SChristophe Ricard .id_table = st33zp24_spi_id,
283f042a315SChristophe Ricard };
284f042a315SChristophe Ricard
2858a745003SChristophe RICARD module_spi_driver(st33zp24_spi_driver);
286f042a315SChristophe Ricard
287f042a315SChristophe Ricard MODULE_AUTHOR("TPM support (TPMsupport@list.st.com)");
288f042a315SChristophe Ricard MODULE_DESCRIPTION("STM TPM 1.2 SPI ST33 Driver");
289f042a315SChristophe Ricard MODULE_VERSION("1.3.0");
290f042a315SChristophe Ricard MODULE_LICENSE("GPL");
291