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