blob: 03918ed6a7435c31b3fce8f8d48a42d9141ae6ca [file] [log] [blame]
Miquel Raynalc1f6d6c2018-05-15 11:57:21 +02001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Author:
4 * Miquel Raynal <miquel.raynal@bootlin.com>
5 *
6 * Description:
7 * SPI-level driver for TCG/TIS TPM (trusted platform module).
8 * Specifications at www.trustedcomputinggroup.org
9 *
10 * This device driver implements the TPM interface as defined in
11 * the TCG SPI protocol stack version 2.0.
12 *
13 * It is based on the U-Boot driver tpm_tis_infineon_i2c.c.
14 */
15
16#include <common.h>
17#include <dm.h>
18#include <fdtdec.h>
19#include <log.h>
20#include <spi.h>
21#include <tpm-v2.h>
Simon Glassdbd79542020-05-10 11:40:11 -060022#include <linux/delay.h>
Miquel Raynalc1f6d6c2018-05-15 11:57:21 +020023#include <linux/errno.h>
24#include <linux/compiler.h>
25#include <linux/types.h>
26#include <linux/unaligned/be_byteshift.h>
Miquel Raynal68f34a22018-05-16 08:59:16 +020027#include <asm-generic/gpio.h>
Miquel Raynalc1f6d6c2018-05-15 11:57:21 +020028
29#include "tpm_tis.h"
30#include "tpm_internal.h"
31
32DECLARE_GLOBAL_DATA_PTR;
33
34#define TPM_ACCESS(l) (0x0000 | ((l) << 12))
35#define TPM_INT_ENABLE(l) (0x0008 | ((l) << 12))
36#define TPM_STS(l) (0x0018 | ((l) << 12))
37#define TPM_DATA_FIFO(l) (0x0024 | ((l) << 12))
38#define TPM_DID_VID(l) (0x0F00 | ((l) << 12))
39#define TPM_RID(l) (0x0F04 | ((l) << 12))
40
41#define MAX_SPI_FRAMESIZE 64
42
43/* Number of wait states to wait for */
44#define TPM_WAIT_STATES 100
45
46/**
47 * struct tpm_tis_chip_data - Non-discoverable TPM information
48 *
49 * @pcr_count: Number of PCR per bank
50 * @pcr_select_min: Size in octets of the pcrSelect array
51 */
52struct tpm_tis_chip_data {
53 unsigned int pcr_count;
54 unsigned int pcr_select_min;
55 unsigned int time_before_first_cmd_ms;
56};
57
58/**
59 * tpm_tis_spi_read() - Read from TPM register
60 *
61 * @addr: register address to read from
62 * @buffer: provided by caller
63 * @len: number of bytes to read
64 *
65 * Read len bytes from TPM register and put them into
66 * buffer (little-endian format, i.e. first byte is put into buffer[0]).
67 *
68 * NOTE: TPM is big-endian for multi-byte values. Multi-byte
69 * values have to be swapped.
70 *
71 * @return -EIO on error, 0 on success.
72 */
73static int tpm_tis_spi_xfer(struct udevice *dev, u32 addr, const u8 *out,
74 u8 *in, u16 len)
75{
76 struct spi_slave *slave = dev_get_parent_priv(dev);
77 int transfer_len, ret;
78 u8 tx_buf[MAX_SPI_FRAMESIZE];
79 u8 rx_buf[MAX_SPI_FRAMESIZE];
80
81 if (in && out) {
82 log(LOGC_NONE, LOGL_ERR, "%s: can't do full duplex\n",
83 __func__);
84 return -EINVAL;
85 }
86
87 ret = spi_claim_bus(slave);
88 if (ret < 0) {
89 log(LOGC_NONE, LOGL_ERR, "%s: could not claim bus\n", __func__);
90 return ret;
91 }
92
93 while (len) {
94 /* Request */
95 transfer_len = min_t(u16, len, MAX_SPI_FRAMESIZE);
96 tx_buf[0] = (in ? BIT(7) : 0) | (transfer_len - 1);
97 tx_buf[1] = 0xD4;
98 tx_buf[2] = addr >> 8;
99 tx_buf[3] = addr;
100
101 ret = spi_xfer(slave, 4 * 8, tx_buf, rx_buf, SPI_XFER_BEGIN);
102 if (ret < 0) {
103 log(LOGC_NONE, LOGL_ERR,
104 "%s: spi request transfer failed (err: %d)\n",
105 __func__, ret);
106 goto release_bus;
107 }
108
109 /* Wait state */
110 if (!(rx_buf[3] & 0x1)) {
111 int i;
112
113 for (i = 0; i < TPM_WAIT_STATES; i++) {
114 ret = spi_xfer(slave, 1 * 8, NULL, rx_buf, 0);
115 if (ret) {
116 log(LOGC_NONE, LOGL_ERR,
117 "%s: wait state failed: %d\n",
118 __func__, ret);
119 goto release_bus;
120 }
121
122 if (rx_buf[0] & 0x1)
123 break;
124 }
125
126 if (i == TPM_WAIT_STATES) {
127 log(LOGC_NONE, LOGL_ERR,
128 "%s: timeout on wait state\n", __func__);
129 ret = -ETIMEDOUT;
130 goto release_bus;
131 }
132 }
133
134 /* Read/Write */
135 if (out) {
136 memcpy(tx_buf, out, transfer_len);
137 out += transfer_len;
138 }
139
140 ret = spi_xfer(slave, transfer_len * 8,
141 out ? tx_buf : NULL,
142 in ? rx_buf : NULL,
143 SPI_XFER_END);
144 if (ret) {
145 log(LOGC_NONE, LOGL_ERR,
146 "%s: spi read transfer failed (err: %d)\n",
147 __func__, ret);
148 goto release_bus;
149 }
150
151 if (in) {
152 memcpy(in, rx_buf, transfer_len);
153 in += transfer_len;
154 }
155
156 len -= transfer_len;
157 }
158
159release_bus:
160 /* If an error occurred, release the chip by deasserting the CS */
161 if (ret < 0)
162 spi_xfer(slave, 0, NULL, NULL, SPI_XFER_END);
163
164 spi_release_bus(slave);
165
166 return ret;
167}
168
169static int tpm_tis_spi_read(struct udevice *dev, u16 addr, u8 *in, u16 len)
170{
171 return tpm_tis_spi_xfer(dev, addr, NULL, in, len);
172}
173
174static int tpm_tis_spi_read32(struct udevice *dev, u32 addr, u32 *result)
175{
176 __le32 result_le;
177 int ret;
178
179 ret = tpm_tis_spi_read(dev, addr, (u8 *)&result_le, sizeof(u32));
180 if (!ret)
181 *result = le32_to_cpu(result_le);
182
183 return ret;
184}
185
186static int tpm_tis_spi_write(struct udevice *dev, u16 addr, const u8 *out,
187 u16 len)
188{
189 return tpm_tis_spi_xfer(dev, addr, out, NULL, len);
190}
191
192static int tpm_tis_spi_check_locality(struct udevice *dev, int loc)
193{
194 const u8 mask = TPM_ACCESS_ACTIVE_LOCALITY | TPM_ACCESS_VALID;
195 struct tpm_chip *chip = dev_get_priv(dev);
196 u8 buf;
197 int ret;
198
199 ret = tpm_tis_spi_read(dev, TPM_ACCESS(loc), &buf, 1);
200 if (ret)
201 return ret;
202
203 if ((buf & mask) == mask) {
204 chip->locality = loc;
205 return 0;
206 }
207
208 return -ENOENT;
209}
210
211static void tpm_tis_spi_release_locality(struct udevice *dev, int loc,
212 bool force)
213{
214 const u8 mask = TPM_ACCESS_REQUEST_PENDING | TPM_ACCESS_VALID;
215 u8 buf;
216
217 if (tpm_tis_spi_read(dev, TPM_ACCESS(loc), &buf, 1) < 0)
218 return;
219
220 if (force || (buf & mask) == mask) {
221 buf = TPM_ACCESS_ACTIVE_LOCALITY;
222 tpm_tis_spi_write(dev, TPM_ACCESS(loc), &buf, 1);
223 }
224}
225
226static int tpm_tis_spi_request_locality(struct udevice *dev, int loc)
227{
228 struct tpm_chip *chip = dev_get_priv(dev);
229 unsigned long start, stop;
230 u8 buf = TPM_ACCESS_REQUEST_USE;
231 int ret;
232
233 ret = tpm_tis_spi_check_locality(dev, loc);
234 if (!ret)
235 return 0;
236
237 if (ret != -ENOENT) {
238 log(LOGC_NONE, LOGL_ERR, "%s: Failed to get locality: %d\n",
239 __func__, ret);
240 return ret;
241 }
242
243 ret = tpm_tis_spi_write(dev, TPM_ACCESS(loc), &buf, 1);
244 if (ret) {
245 log(LOGC_NONE, LOGL_ERR, "%s: Failed to write to TPM: %d\n",
246 __func__, ret);
247 return ret;
248 }
249
250 start = get_timer(0);
251 stop = chip->timeout_a;
252 do {
253 ret = tpm_tis_spi_check_locality(dev, loc);
254 if (!ret)
255 return 0;
256
257 if (ret != -ENOENT) {
258 log(LOGC_NONE, LOGL_ERR,
259 "%s: Failed to get locality: %d\n", __func__, ret);
260 return ret;
261 }
262
263 mdelay(TPM_TIMEOUT_MS);
264 } while (get_timer(start) < stop);
265
266 log(LOGC_NONE, LOGL_ERR, "%s: Timeout getting locality: %d\n", __func__,
267 ret);
268
269 return ret;
270}
271
272static u8 tpm_tis_spi_status(struct udevice *dev, u8 *status)
273{
274 struct tpm_chip *chip = dev_get_priv(dev);
275
276 return tpm_tis_spi_read(dev, TPM_STS(chip->locality), status, 1);
277}
278
279static int tpm_tis_spi_wait_for_stat(struct udevice *dev, u8 mask,
280 unsigned long timeout, u8 *status)
281{
282 unsigned long start = get_timer(0);
283 unsigned long stop = timeout;
284 int ret;
285
286 do {
287 mdelay(TPM_TIMEOUT_MS);
288 ret = tpm_tis_spi_status(dev, status);
289 if (ret)
290 return ret;
291
292 if ((*status & mask) == mask)
293 return 0;
294 } while (get_timer(start) < stop);
295
296 return -ETIMEDOUT;
297}
298
Roman Kaplc84dd7f2019-05-16 18:32:48 +0200299static u8 tpm_tis_spi_valid_status(struct udevice *dev, u8 *status)
300{
301 struct tpm_chip *chip = dev_get_priv(dev);
302
303 return tpm_tis_spi_wait_for_stat(dev, TPM_STS_VALID,
304 chip->timeout_c, status);
305}
306
Miquel Raynalc1f6d6c2018-05-15 11:57:21 +0200307static int tpm_tis_spi_get_burstcount(struct udevice *dev)
308{
309 struct tpm_chip *chip = dev_get_priv(dev);
310 unsigned long start, stop;
311 u32 burstcount, ret;
312
313 /* wait for burstcount */
314 start = get_timer(0);
315 stop = chip->timeout_d;
316 do {
317 ret = tpm_tis_spi_read32(dev, TPM_STS(chip->locality),
318 &burstcount);
319 if (ret)
320 return -EBUSY;
321
322 burstcount = (burstcount >> 8) & 0xFFFF;
323 if (burstcount)
324 return burstcount;
325
326 mdelay(TPM_TIMEOUT_MS);
327 } while (get_timer(start) < stop);
328
329 return -EBUSY;
330}
331
332static int tpm_tis_spi_cancel(struct udevice *dev)
333{
334 struct tpm_chip *chip = dev_get_priv(dev);
335 u8 data = TPM_STS_COMMAND_READY;
336
337 return tpm_tis_spi_write(dev, TPM_STS(chip->locality), &data, 1);
338}
339
340static int tpm_tis_spi_recv_data(struct udevice *dev, u8 *buf, size_t count)
341{
342 struct tpm_chip *chip = dev_get_priv(dev);
343 int size = 0, burstcnt, len, ret;
344 u8 status;
345
346 while (size < count &&
347 tpm_tis_spi_wait_for_stat(dev,
348 TPM_STS_DATA_AVAIL | TPM_STS_VALID,
349 chip->timeout_c, &status) == 0) {
350 burstcnt = tpm_tis_spi_get_burstcount(dev);
351 if (burstcnt < 0)
352 return burstcnt;
353
354 len = min_t(int, burstcnt, count - size);
355 ret = tpm_tis_spi_read(dev, TPM_DATA_FIFO(chip->locality),
356 buf + size, len);
357 if (ret < 0)
358 return ret;
359
360 size += len;
361 }
362
363 return size;
364}
365
366static int tpm_tis_spi_recv(struct udevice *dev, u8 *buf, size_t count)
367{
368 struct tpm_chip *chip = dev_get_priv(dev);
369 int size, expected;
370
371 if (!chip)
372 return -ENODEV;
373
374 if (count < TPM_HEADER_SIZE) {
375 size = -EIO;
376 goto out;
377 }
378
379 size = tpm_tis_spi_recv_data(dev, buf, TPM_HEADER_SIZE);
380 if (size < TPM_HEADER_SIZE) {
381 log(LOGC_NONE, LOGL_ERR, "TPM error, unable to read header\n");
382 goto out;
383 }
384
385 expected = get_unaligned_be32(buf + 2);
386 if (expected > count) {
387 size = -EIO;
388 goto out;
389 }
390
391 size += tpm_tis_spi_recv_data(dev, &buf[TPM_HEADER_SIZE],
392 expected - TPM_HEADER_SIZE);
393 if (size < expected) {
394 log(LOGC_NONE, LOGL_ERR,
395 "TPM error, unable to read remaining bytes of result\n");
396 size = -EIO;
397 goto out;
398 }
399
400out:
401 tpm_tis_spi_cancel(dev);
402 tpm_tis_spi_release_locality(dev, chip->locality, false);
403
404 return size;
405}
406
407static int tpm_tis_spi_send(struct udevice *dev, const u8 *buf, size_t len)
408{
409 struct tpm_chip *chip = dev_get_priv(dev);
410 u32 i, size;
411 u8 status;
412 int burstcnt, ret;
413 u8 data;
414
415 if (!chip)
416 return -ENODEV;
417
418 if (len > TPM_DEV_BUFSIZE)
419 return -E2BIG; /* Command is too long for our tpm, sorry */
420
421 ret = tpm_tis_spi_request_locality(dev, 0);
422 if (ret < 0)
423 return -EBUSY;
424
425 /*
426 * Check if the TPM is ready. If not, if not, cancel the pending command
427 * and poll on the status to be finally ready.
428 */
429 ret = tpm_tis_spi_status(dev, &status);
430 if (ret)
431 return ret;
432
433 if (!(status & TPM_STS_COMMAND_READY)) {
434 /* Force the transition, usually this will be done at startup */
435 ret = tpm_tis_spi_cancel(dev);
436 if (ret) {
437 log(LOGC_NONE, LOGL_ERR,
438 "%s: Could not cancel previous operation\n",
439 __func__);
440 goto out_err;
441 }
442
443 ret = tpm_tis_spi_wait_for_stat(dev, TPM_STS_COMMAND_READY,
444 chip->timeout_b, &status);
445 if (ret < 0 || !(status & TPM_STS_COMMAND_READY)) {
446 log(LOGC_NONE, LOGL_ERR,
447 "status %d after wait for stat returned %d\n",
448 status, ret);
449 goto out_err;
450 }
451 }
452
453 for (i = 0; i < len - 1;) {
454 burstcnt = tpm_tis_spi_get_burstcount(dev);
455 if (burstcnt < 0)
456 return burstcnt;
457
458 size = min_t(int, len - i - 1, burstcnt);
459 ret = tpm_tis_spi_write(dev, TPM_DATA_FIFO(chip->locality),
460 buf + i, size);
461 if (ret < 0)
462 goto out_err;
463
464 i += size;
465 }
466
Roman Kaplc84dd7f2019-05-16 18:32:48 +0200467 ret = tpm_tis_spi_valid_status(dev, &status);
Miquel Raynalc1f6d6c2018-05-15 11:57:21 +0200468 if (ret)
469 goto out_err;
470
471 if ((status & TPM_STS_DATA_EXPECT) == 0) {
472 ret = -EIO;
473 goto out_err;
474 }
475
476 ret = tpm_tis_spi_write(dev, TPM_DATA_FIFO(chip->locality),
477 buf + len - 1, 1);
478 if (ret)
479 goto out_err;
480
Roman Kaplc84dd7f2019-05-16 18:32:48 +0200481 ret = tpm_tis_spi_valid_status(dev, &status);
Miquel Raynalc1f6d6c2018-05-15 11:57:21 +0200482 if (ret)
483 goto out_err;
484
485 if ((status & TPM_STS_DATA_EXPECT) != 0) {
486 ret = -EIO;
487 goto out_err;
488 }
489
490 data = TPM_STS_GO;
491 ret = tpm_tis_spi_write(dev, TPM_STS(chip->locality), &data, 1);
492 if (ret)
493 goto out_err;
494
495 return len;
496
497out_err:
498 tpm_tis_spi_cancel(dev);
499 tpm_tis_spi_release_locality(dev, chip->locality, false);
500
501 return ret;
502}
503
504static int tpm_tis_spi_cleanup(struct udevice *dev)
505{
506 struct tpm_chip *chip = dev_get_priv(dev);
507
508 tpm_tis_spi_cancel(dev);
509 /*
510 * The TPM needs some time to clean up here,
511 * so we sleep rather than keeping the bus busy
512 */
513 mdelay(2);
514 tpm_tis_spi_release_locality(dev, chip->locality, false);
515
516 return 0;
517}
518
519static int tpm_tis_spi_open(struct udevice *dev)
520{
521 struct tpm_chip *chip = dev_get_priv(dev);
Miquel Raynal79920d12018-07-19 22:35:09 +0200522 struct tpm_chip_priv *priv = dev_get_uclass_priv(dev);
Miquel Raynalc1f6d6c2018-05-15 11:57:21 +0200523
524 if (chip->is_open)
525 return -EBUSY;
526
527 chip->is_open = 1;
528
529 return 0;
530}
531
532static int tpm_tis_spi_close(struct udevice *dev)
533{
534 struct tpm_chip *chip = dev_get_priv(dev);
535
536 if (chip->is_open) {
537 tpm_tis_spi_release_locality(dev, chip->locality, true);
538 chip->is_open = 0;
539 }
540
541 return 0;
542}
543
544static int tpm_tis_get_desc(struct udevice *dev, char *buf, int size)
545{
546 struct tpm_chip *chip = dev_get_priv(dev);
547
548 if (size < 80)
549 return -ENOSPC;
550
551 return snprintf(buf, size,
552 "%s v2.0: VendorID 0x%04x, DeviceID 0x%04x, RevisionID 0x%02x [%s]",
553 dev->name, chip->vend_dev & 0xFFFF,
554 chip->vend_dev >> 16, chip->rid,
555 (chip->is_open ? "open" : "closed"));
556}
557
558static int tpm_tis_wait_init(struct udevice *dev, int loc)
559{
560 struct tpm_chip *chip = dev_get_priv(dev);
561 unsigned long start, stop;
562 u8 status;
563 int ret;
564
565 start = get_timer(0);
566 stop = chip->timeout_b;
567 do {
568 mdelay(TPM_TIMEOUT_MS);
569
570 ret = tpm_tis_spi_read(dev, TPM_ACCESS(loc), &status, 1);
571 if (ret)
572 break;
573
574 if (status & TPM_ACCESS_VALID)
575 return 0;
576 } while (get_timer(start) < stop);
577
578 return -EIO;
579}
580
581static int tpm_tis_spi_probe(struct udevice *dev)
582{
583 struct tpm_tis_chip_data *drv_data = (void *)dev_get_driver_data(dev);
584 struct tpm_chip_priv *priv = dev_get_uclass_priv(dev);
585 struct tpm_chip *chip = dev_get_priv(dev);
586 int ret;
587
Miquel Raynal79920d12018-07-19 22:35:09 +0200588 /* Use the TPM v2 stack */
589 priv->version = TPM_V2;
590
Simon Glassfa4689a2019-12-06 21:41:35 -0700591 if (CONFIG_IS_ENABLED(DM_GPIO)) {
Miquel Raynal68f34a22018-05-16 08:59:16 +0200592 struct gpio_desc reset_gpio;
593
594 ret = gpio_request_by_name(dev, "gpio-reset", 0,
595 &reset_gpio, GPIOD_IS_OUT);
596 if (ret) {
597 log(LOGC_NONE, LOGL_NOTICE, "%s: missing reset GPIO\n",
598 __func__);
599 } else {
Miquel Raynal68f34a22018-05-16 08:59:16 +0200600 dm_gpio_set_value(&reset_gpio, 1);
Kayla Theil7ef21ad2019-09-06 12:40:45 +0200601 mdelay(1);
602 dm_gpio_set_value(&reset_gpio, 0);
Miquel Raynal68f34a22018-05-16 08:59:16 +0200603 }
604 }
605
Miquel Raynalc1f6d6c2018-05-15 11:57:21 +0200606 /* Ensure a minimum amount of time elapsed since reset of the TPM */
607 mdelay(drv_data->time_before_first_cmd_ms);
608
609 chip->locality = 0;
610 chip->timeout_a = TIS_SHORT_TIMEOUT_MS;
611 chip->timeout_b = TIS_LONG_TIMEOUT_MS;
612 chip->timeout_c = TIS_SHORT_TIMEOUT_MS;
613 chip->timeout_d = TIS_SHORT_TIMEOUT_MS;
614 priv->pcr_count = drv_data->pcr_count;
615 priv->pcr_select_min = drv_data->pcr_select_min;
616
617 ret = tpm_tis_wait_init(dev, chip->locality);
618 if (ret) {
619 log(LOGC_DM, LOGL_ERR, "%s: no device found\n", __func__);
620 return ret;
621 }
622
623 ret = tpm_tis_spi_request_locality(dev, chip->locality);
624 if (ret) {
625 log(LOGC_NONE, LOGL_ERR, "%s: could not request locality %d\n",
626 __func__, chip->locality);
627 return ret;
628 }
629
630 ret = tpm_tis_spi_read32(dev, TPM_DID_VID(chip->locality),
631 &chip->vend_dev);
632 if (ret) {
633 log(LOGC_NONE, LOGL_ERR,
634 "%s: could not retrieve VendorID/DeviceID\n", __func__);
635 return ret;
636 }
637
638 ret = tpm_tis_spi_read(dev, TPM_RID(chip->locality), &chip->rid, 1);
639 if (ret) {
640 log(LOGC_NONE, LOGL_ERR, "%s: could not retrieve RevisionID\n",
641 __func__);
642 return ret;
643 }
644
645 log(LOGC_NONE, LOGL_ERR,
646 "SPI TPMv2.0 found (vid:%04x, did:%04x, rid:%02x)\n",
647 chip->vend_dev & 0xFFFF, chip->vend_dev >> 16, chip->rid);
648
649 return 0;
650}
651
652static int tpm_tis_spi_remove(struct udevice *dev)
653{
654 struct tpm_chip *chip = dev_get_priv(dev);
655
656 tpm_tis_spi_release_locality(dev, chip->locality, true);
657
658 return 0;
659}
660
661static const struct tpm_ops tpm_tis_spi_ops = {
662 .open = tpm_tis_spi_open,
663 .close = tpm_tis_spi_close,
664 .get_desc = tpm_tis_get_desc,
665 .send = tpm_tis_spi_send,
666 .recv = tpm_tis_spi_recv,
667 .cleanup = tpm_tis_spi_cleanup,
668};
669
670static const struct tpm_tis_chip_data tpm_tis_std_chip_data = {
671 .pcr_count = 24,
672 .pcr_select_min = 3,
673 .time_before_first_cmd_ms = 30,
674};
675
676static const struct udevice_id tpm_tis_spi_ids[] = {
677 {
678 .compatible = "tis,tpm2-spi",
679 .data = (ulong)&tpm_tis_std_chip_data,
680 },
681 { }
682};
683
684U_BOOT_DRIVER(tpm_tis_spi) = {
685 .name = "tpm_tis_spi",
686 .id = UCLASS_TPM,
687 .of_match = tpm_tis_spi_ids,
688 .ops = &tpm_tis_spi_ops,
689 .probe = tpm_tis_spi_probe,
690 .remove = tpm_tis_spi_remove,
691 .priv_auto_alloc_size = sizeof(struct tpm_chip),
692};