blob: 74c5fcc5c397c88c6bf1f84082a2f4709fd393e5 [file] [log] [blame]
Christophe Ricardf042a312015-03-08 11:17:15 +01001/*
2 * STMicroelectronics TPM SPI Linux driver for TPM ST33ZP24
Christophe RICARD604e5782016-02-13 16:15:24 +01003 * Copyright (C) 2009 - 2015 STMicroelectronics
Christophe Ricardf042a312015-03-08 11:17:15 +01004 *
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.
9 *
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.
14 *
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/>.
17 */
18
19#include <linux/module.h>
20#include <linux/spi/spi.h>
21#include <linux/gpio.h>
22#include <linux/of_irq.h>
23#include <linux/of_gpio.h>
24#include <linux/tpm.h>
25#include <linux/platform_data/st33zp24.h>
26
27#include "st33zp24.h"
28
29#define TPM_DATA_FIFO 0x24
30#define TPM_INTF_CAPABILITY 0x14
31
32#define TPM_DUMMY_BYTE 0x00
33
34#define MAX_SPI_LATENCY 15
35#define LOCALITY0 0
36
37#define ST33ZP24_OK 0x5A
38#define ST33ZP24_UNDEFINED_ERR 0x80
39#define ST33ZP24_BADLOCALITY 0x81
40#define ST33ZP24_TISREGISTER_UKNOWN 0x82
41#define ST33ZP24_LOCALITY_NOT_ACTIVATED 0x83
42#define ST33ZP24_HASH_END_BEFORE_HASH_START 0x84
43#define ST33ZP24_BAD_COMMAND_ORDER 0x85
44#define ST33ZP24_INCORECT_RECEIVED_LENGTH 0x86
45#define ST33ZP24_TPM_FIFO_OVERFLOW 0x89
46#define ST33ZP24_UNEXPECTED_READ_FIFO 0x8A
47#define ST33ZP24_UNEXPECTED_WRITE_FIFO 0x8B
48#define ST33ZP24_CMDRDY_SET_WHEN_PROCESSING_HASH_END 0x90
49#define ST33ZP24_DUMMY_BYTES 0x00
50
51/*
52 * TPM command can be up to 2048 byte, A TPM response can be up to
53 * 1024 byte.
54 * Between command and response, there are latency byte (up to 15
55 * usually on st33zp24 2 are enough).
56 *
57 * Overall when sending a command and expecting an answer we need if
58 * worst case:
59 * 2048 (for the TPM command) + 1024 (for the TPM answer). We need
60 * some latency byte before the answer is available (max 15).
61 * We have 2048 + 1024 + 15.
62 */
63#define ST33ZP24_SPI_BUFFER_SIZE (TPM_BUFSIZE + (TPM_BUFSIZE / 2) +\
64 MAX_SPI_LATENCY)
65
66
67struct st33zp24_spi_phy {
68 struct spi_device *spi_device;
Christophe RICARD604e5782016-02-13 16:15:24 +010069 struct spi_transfer spi_xfer;
Christophe Ricardf042a312015-03-08 11:17:15 +010070 u8 tx_buf[ST33ZP24_SPI_BUFFER_SIZE];
71 u8 rx_buf[ST33ZP24_SPI_BUFFER_SIZE];
72
73 int io_lpcpd;
74 int latency;
75};
76
77static int st33zp24_status_to_errno(u8 code)
78{
79 switch (code) {
80 case ST33ZP24_OK:
81 return 0;
82 case ST33ZP24_UNDEFINED_ERR:
83 case ST33ZP24_BADLOCALITY:
84 case ST33ZP24_TISREGISTER_UKNOWN:
85 case ST33ZP24_LOCALITY_NOT_ACTIVATED:
86 case ST33ZP24_HASH_END_BEFORE_HASH_START:
87 case ST33ZP24_BAD_COMMAND_ORDER:
88 case ST33ZP24_UNEXPECTED_READ_FIFO:
89 case ST33ZP24_UNEXPECTED_WRITE_FIFO:
90 case ST33ZP24_CMDRDY_SET_WHEN_PROCESSING_HASH_END:
91 return -EPROTO;
92 case ST33ZP24_INCORECT_RECEIVED_LENGTH:
93 case ST33ZP24_TPM_FIFO_OVERFLOW:
94 return -EMSGSIZE;
95 case ST33ZP24_DUMMY_BYTES:
96 return -ENOSYS;
97 }
98 return code;
99}
100
101/*
102 * st33zp24_spi_send
103 * Send byte to the TIS register according to the ST33ZP24 SPI protocol.
104 * @param: phy_id, the phy description
105 * @param: tpm_register, the tpm tis register where the data should be written
106 * @param: tpm_data, the tpm_data to write inside the tpm_register
107 * @param: tpm_size, The length of the data
108 * @return: should be zero if success else a negative error code.
109 */
110static int st33zp24_spi_send(void *phy_id, u8 tpm_register, u8 *tpm_data,
111 int tpm_size)
112{
Christophe RICARD604e5782016-02-13 16:15:24 +0100113 u8 data = 0;
Christophe RICARD9feaab52016-02-13 16:15:24 +0100114 int total_length = 0, ret = 0;
Christophe Ricardf042a312015-03-08 11:17:15 +0100115 struct st33zp24_spi_phy *phy = phy_id;
116 struct spi_device *dev = phy->spi_device;
Christophe RICARD604e5782016-02-13 16:15:24 +0100117 u8 *tx_buf = (u8 *)phy->spi_xfer.tx_buf;
118 u8 *rx_buf = phy->spi_xfer.rx_buf;
Christophe Ricardf042a312015-03-08 11:17:15 +0100119
120 /* Pre-Header */
Christophe RICARD604e5782016-02-13 16:15:24 +0100121 data = TPM_WRITE_DIRECTION | LOCALITY0;
122 memcpy(tx_buf + total_length, &data, sizeof(data));
123 total_length++;
124 data = tpm_register;
125 memcpy(tx_buf + total_length, &data, sizeof(data));
126 total_length++;
Christophe Ricardf042a312015-03-08 11:17:15 +0100127
128 if (tpm_size > 0 && tpm_register == TPM_DATA_FIFO) {
Christophe RICARD604e5782016-02-13 16:15:24 +0100129 tx_buf[total_length++] = tpm_size >> 8;
130 tx_buf[total_length++] = tpm_size;
Christophe Ricardf042a312015-03-08 11:17:15 +0100131 }
132
Christophe RICARD604e5782016-02-13 16:15:24 +0100133 memcpy(&tx_buf[total_length], tpm_data, tpm_size);
Christophe Ricardf042a312015-03-08 11:17:15 +0100134 total_length += tpm_size;
135
Christophe RICARD604e5782016-02-13 16:15:24 +0100136 memset(&tx_buf[total_length], TPM_DUMMY_BYTE, phy->latency);
Christophe Ricardf042a312015-03-08 11:17:15 +0100137
Christophe RICARD604e5782016-02-13 16:15:24 +0100138 phy->spi_xfer.len = total_length + phy->latency;
Christophe Ricardf042a312015-03-08 11:17:15 +0100139
Christophe RICARD604e5782016-02-13 16:15:24 +0100140 ret = spi_sync_transfer(dev, &phy->spi_xfer, 1);
Christophe Ricardf042a312015-03-08 11:17:15 +0100141 if (ret == 0)
Christophe RICARD604e5782016-02-13 16:15:24 +0100142 ret = rx_buf[total_length + phy->latency - 1];
Christophe Ricardf042a312015-03-08 11:17:15 +0100143
144 return st33zp24_status_to_errno(ret);
145} /* st33zp24_spi_send() */
146
147/*
Christophe RICARD604e5782016-02-13 16:15:24 +0100148 * read8_recv
Christophe Ricardf042a312015-03-08 11:17:15 +0100149 * 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.
155 */
Christophe RICARD604e5782016-02-13 16:15:24 +0100156static int read8_reg(void *phy_id, u8 tpm_register, u8 *tpm_data, int tpm_size)
Christophe Ricardf042a312015-03-08 11:17:15 +0100157{
Christophe RICARD604e5782016-02-13 16:15:24 +0100158 u8 data = 0;
Christophe RICARD9feaab52016-02-13 16:15:24 +0100159 int total_length = 0, ret;
Christophe Ricardf042a312015-03-08 11:17:15 +0100160 struct st33zp24_spi_phy *phy = phy_id;
161 struct spi_device *dev = phy->spi_device;
Christophe RICARD604e5782016-02-13 16:15:24 +0100162 u8 *tx_buf = (u8 *)phy->spi_xfer.tx_buf;
163 u8 *rx_buf = phy->spi_xfer.rx_buf;
Christophe Ricardf042a312015-03-08 11:17:15 +0100164
165 /* Pre-Header */
Christophe RICARD604e5782016-02-13 16:15:24 +0100166 data = LOCALITY0;
167 memcpy(tx_buf + total_length, &data, sizeof(data));
168 total_length++;
169 data = tpm_register;
170 memcpy(tx_buf + total_length, &data, sizeof(data));
171 total_length++;
Christophe Ricardf042a312015-03-08 11:17:15 +0100172
Christophe RICARD604e5782016-02-13 16:15:24 +0100173 memset(&tx_buf[total_length], TPM_DUMMY_BYTE,
Christophe RICARD9feaab52016-02-13 16:15:24 +0100174 phy->latency + tpm_size);
Christophe Ricardf042a312015-03-08 11:17:15 +0100175
Christophe RICARD604e5782016-02-13 16:15:24 +0100176 phy->spi_xfer.len = total_length + phy->latency + tpm_size;
Christophe Ricardf042a312015-03-08 11:17:15 +0100177
178 /* header + status byte + size of the data + status byte */
Christophe RICARD604e5782016-02-13 16:15:24 +0100179 ret = spi_sync_transfer(dev, &phy->spi_xfer, 1);
Christophe Ricardf042a312015-03-08 11:17:15 +0100180 if (tpm_size > 0 && ret == 0) {
Christophe RICARD604e5782016-02-13 16:15:24 +0100181 ret = rx_buf[total_length + phy->latency - 1];
Christophe Ricardf042a312015-03-08 11:17:15 +0100182
Christophe RICARD604e5782016-02-13 16:15:24 +0100183 memcpy(tpm_data, rx_buf + total_length + phy->latency,
Christophe Ricardf042a312015-03-08 11:17:15 +0100184 tpm_size);
185 }
186
187 return ret;
Christophe RICARD604e5782016-02-13 16:15:24 +0100188} /* read8_reg() */
Christophe Ricardf042a312015-03-08 11:17:15 +0100189
190/*
191 * st33zp24_spi_recv
192 * Recv byte from the TIS register according to the ST33ZP24 SPI protocol.
193 * @param: phy_id, the phy description
194 * @param: tpm_register, the tpm tis register where the data should be read
195 * @param: tpm_data, the TPM response
196 * @param: tpm_size, tpm TPM response size to read.
197 * @return: number of byte read successfully: should be one if success.
198 */
199static int st33zp24_spi_recv(void *phy_id, u8 tpm_register, u8 *tpm_data,
200 int tpm_size)
201{
202 int ret;
203
Christophe RICARD604e5782016-02-13 16:15:24 +0100204 ret = read8_reg(phy_id, tpm_register, tpm_data, tpm_size);
Christophe Ricardf042a312015-03-08 11:17:15 +0100205 if (!st33zp24_status_to_errno(ret))
206 return tpm_size;
207 return ret;
208} /* st33zp24_spi_recv() */
209
Christophe RICARD604e5782016-02-13 16:15:24 +0100210static int evaluate_latency(void *phy_id)
Christophe Ricardf042a312015-03-08 11:17:15 +0100211{
212 struct st33zp24_spi_phy *phy = phy_id;
213 int latency = 1, status = 0;
214 u8 data = 0;
215
216 while (!status && latency < MAX_SPI_LATENCY) {
217 phy->latency = latency;
Christophe RICARD604e5782016-02-13 16:15:24 +0100218 status = read8_reg(phy_id, TPM_INTF_CAPABILITY, &data, 1);
Christophe Ricardf042a312015-03-08 11:17:15 +0100219 latency++;
220 }
221 return latency - 1;
222} /* evaluate_latency() */
223
224static const struct st33zp24_phy_ops spi_phy_ops = {
225 .send = st33zp24_spi_send,
226 .recv = st33zp24_spi_recv,
227};
228
Christophe RICARD604e5782016-02-13 16:15:24 +0100229#ifdef CONFIG_OF
230static int tpm_stm_spi_of_request_resources(struct st33zp24_spi_phy *phy)
Christophe RICARD160beb42016-02-23 22:25:49 +0100231{
Christophe Ricardf042a312015-03-08 11:17:15 +0100232 struct device_node *pp;
Christophe RICARD604e5782016-02-13 16:15:24 +0100233 struct spi_device *dev = phy->spi_device;
Christophe Ricardf042a312015-03-08 11:17:15 +0100234 int gpio;
235 int ret;
236
Christophe RICARD604e5782016-02-13 16:15:24 +0100237 pp = dev->dev.of_node;
Christophe Ricardf042a312015-03-08 11:17:15 +0100238 if (!pp) {
Christophe RICARD604e5782016-02-13 16:15:24 +0100239 dev_err(&dev->dev, "No platform data\n");
Christophe Ricardf042a312015-03-08 11:17:15 +0100240 return -ENODEV;
241 }
242
243 /* Get GPIO from device tree */
244 gpio = of_get_named_gpio(pp, "lpcpd-gpios", 0);
245 if (gpio < 0) {
Christophe RICARD604e5782016-02-13 16:15:24 +0100246 dev_err(&dev->dev,
Christophe Ricardf042a312015-03-08 11:17:15 +0100247 "Failed to retrieve lpcpd-gpios from dts.\n");
248 phy->io_lpcpd = -1;
249 /*
250 * lpcpd pin is not specified. This is not an issue as
251 * power management can be also managed by TPM specific
252 * commands. So leave with a success status code.
253 */
254 return 0;
255 }
256 /* GPIO request and configuration */
Christophe RICARD604e5782016-02-13 16:15:24 +0100257 ret = devm_gpio_request_one(&dev->dev, gpio,
Christophe Ricardf042a312015-03-08 11:17:15 +0100258 GPIOF_OUT_INIT_HIGH, "TPM IO LPCPD");
259 if (ret) {
Christophe RICARD604e5782016-02-13 16:15:24 +0100260 dev_err(&dev->dev, "Failed to request lpcpd pin\n");
Christophe Ricardf042a312015-03-08 11:17:15 +0100261 return -ENODEV;
262 }
263 phy->io_lpcpd = gpio;
264
265 return 0;
266}
Christophe RICARD604e5782016-02-13 16:15:24 +0100267#else
268static int tpm_stm_spi_of_request_resources(struct st33zp24_spi_phy *phy)
Christophe Ricardf042a312015-03-08 11:17:15 +0100269{
Christophe RICARD604e5782016-02-13 16:15:24 +0100270 return -ENODEV;
271}
272#endif
273
274static int tpm_stm_spi_request_resources(struct spi_device *dev,
275 struct st33zp24_spi_phy *phy)
276{
Christophe Ricardf042a312015-03-08 11:17:15 +0100277 struct st33zp24_platform_data *pdata;
278 int ret;
279
280 pdata = dev->dev.platform_data;
281 if (!pdata) {
282 dev_err(&dev->dev, "No platform data\n");
283 return -ENODEV;
284 }
285
286 /* store for late use */
287 phy->io_lpcpd = pdata->io_lpcpd;
288
289 if (gpio_is_valid(pdata->io_lpcpd)) {
290 ret = devm_gpio_request_one(&dev->dev,
291 pdata->io_lpcpd, GPIOF_OUT_INIT_HIGH,
292 "TPM IO_LPCPD");
293 if (ret) {
294 dev_err(&dev->dev, "%s : reset gpio_request failed\n",
295 __FILE__);
296 return ret;
297 }
298 }
299
300 return 0;
301}
302
303/*
Christophe RICARD604e5782016-02-13 16:15:24 +0100304 * tpm_st33_spi_probe initialize the TPM device
Christophe Ricardf042a312015-03-08 11:17:15 +0100305 * @param: dev, the spi_device drescription (TPM SPI description).
306 * @return: 0 in case of success.
307 * or a negative value describing the error.
308 */
Christophe RICARD604e5782016-02-13 16:15:24 +0100309static int
310tpm_st33_spi_probe(struct spi_device *dev)
Christophe Ricardf042a312015-03-08 11:17:15 +0100311{
312 int ret;
313 struct st33zp24_platform_data *pdata;
314 struct st33zp24_spi_phy *phy;
315
316 /* Check SPI platform functionnalities */
317 if (!dev) {
318 pr_info("%s: dev is NULL. Device is not accessible.\n",
319 __func__);
320 return -ENODEV;
321 }
322
323 phy = devm_kzalloc(&dev->dev, sizeof(struct st33zp24_spi_phy),
324 GFP_KERNEL);
325 if (!phy)
326 return -ENOMEM;
327
328 phy->spi_device = dev;
329 pdata = dev->dev.platform_data;
330 if (!pdata && dev->dev.of_node) {
Christophe RICARD604e5782016-02-13 16:15:24 +0100331 ret = tpm_stm_spi_of_request_resources(phy);
Christophe Ricardf042a312015-03-08 11:17:15 +0100332 if (ret)
333 return ret;
334 } else if (pdata) {
Christophe RICARD604e5782016-02-13 16:15:24 +0100335 ret = tpm_stm_spi_request_resources(dev, phy);
Christophe RICARD160beb42016-02-23 22:25:49 +0100336 if (ret)
337 return ret;
Christophe Ricardf042a312015-03-08 11:17:15 +0100338 }
339
Christophe RICARD604e5782016-02-13 16:15:24 +0100340 phy->spi_xfer.tx_buf = phy->tx_buf;
341 phy->spi_xfer.rx_buf = phy->rx_buf;
342
343 phy->latency = evaluate_latency(phy);
Christophe Ricardf042a312015-03-08 11:17:15 +0100344 if (phy->latency <= 0)
345 return -ENODEV;
346
347 return st33zp24_probe(phy, &spi_phy_ops, &dev->dev, dev->irq,
348 phy->io_lpcpd);
349}
350
351/*
Christophe RICARD604e5782016-02-13 16:15:24 +0100352 * tpm_st33_spi_remove remove the TPM device
Christophe Ricardf042a312015-03-08 11:17:15 +0100353 * @param: client, the spi_device drescription (TPM SPI description).
354 * @return: 0 in case of success.
355 */
Christophe RICARD604e5782016-02-13 16:15:24 +0100356static int tpm_st33_spi_remove(struct spi_device *dev)
Christophe Ricardf042a312015-03-08 11:17:15 +0100357{
358 struct tpm_chip *chip = spi_get_drvdata(dev);
359
360 return st33zp24_remove(chip);
361}
362
Christophe Ricard1018bc22015-03-23 22:29:58 +0100363static const struct spi_device_id st33zp24_spi_id[] = {
364 {TPM_ST33_SPI, 0},
365 {}
366};
367MODULE_DEVICE_TABLE(spi, st33zp24_spi_id);
368
Christophe RICARD604e5782016-02-13 16:15:24 +0100369#ifdef CONFIG_OF
Christophe Ricardf042a312015-03-08 11:17:15 +0100370static const struct of_device_id of_st33zp24_spi_match[] = {
371 { .compatible = "st,st33zp24-spi", },
372 {}
373};
374MODULE_DEVICE_TABLE(of, of_st33zp24_spi_match);
Christophe RICARD604e5782016-02-13 16:15:24 +0100375#endif
Christophe RICARD160beb42016-02-23 22:25:49 +0100376
Christophe Ricardf042a312015-03-08 11:17:15 +0100377static SIMPLE_DEV_PM_OPS(st33zp24_spi_ops, st33zp24_pm_suspend,
378 st33zp24_pm_resume);
379
Christophe RICARD604e5782016-02-13 16:15:24 +0100380static struct spi_driver tpm_st33_spi_driver = {
Christophe Ricardf042a312015-03-08 11:17:15 +0100381 .driver = {
Christophe Ricardf042a312015-03-08 11:17:15 +0100382 .name = TPM_ST33_SPI,
383 .pm = &st33zp24_spi_ops,
384 .of_match_table = of_match_ptr(of_st33zp24_spi_match),
385 },
Christophe RICARD604e5782016-02-13 16:15:24 +0100386 .probe = tpm_st33_spi_probe,
387 .remove = tpm_st33_spi_remove,
Christophe Ricard1018bc22015-03-23 22:29:58 +0100388 .id_table = st33zp24_spi_id,
Christophe Ricardf042a312015-03-08 11:17:15 +0100389};
390
Christophe RICARD604e5782016-02-13 16:15:24 +0100391module_spi_driver(tpm_st33_spi_driver);
Christophe Ricardf042a312015-03-08 11:17:15 +0100392
393MODULE_AUTHOR("TPM support (TPMsupport@list.st.com)");
394MODULE_DESCRIPTION("STM TPM 1.2 SPI ST33 Driver");
395MODULE_VERSION("1.3.0");
396MODULE_LICENSE("GPL");