blob: b9b2a4a5cb6a599fb3ceafeac06e551113e5d145 [file] [log] [blame]
Patrick Georgi593124d2020-05-10 19:44:08 +02001/* SPDX-License-Identifier: BSD-3-Clause */
Vadim Bendeburye31d2432016-04-09 18:33:49 -07002
Vadim Bendeburye31d2432016-04-09 18:33:49 -07003#include <console/console.h>
Philipp Deppenwiesed88fb362017-10-18 20:26:18 +02004#include <security/tpm/tis.h>
Vadim Bendeburye31d2432016-04-09 18:33:49 -07005
6#include "tpm.h"
7
Sergii Dmytruk4ee03172022-12-22 19:35:25 +02008static unsigned int tpm_is_open;
9
Vadim Bendeburye31d2432016-04-09 18:33:49 -070010static const struct {
11 uint16_t vid;
12 uint16_t did;
13 const char *device_name;
14} dev_map[] = {
15 { 0x15d1, 0x001b, "SLB9670" },
16 { 0x1ae0, 0x0028, "CR50" },
Patrick Rudolph7bcd9a12020-03-20 09:55:43 +010017 { 0x104a, 0x0000, "ST33HTPH2E32" },
Yidi Lin93447c42022-08-16 15:19:32 +080018 { 0x6666, 0x504a, "TI50" },
Vadim Bendeburye31d2432016-04-09 18:33:49 -070019};
20
21static const char *tis_get_dev_name(struct tpm2_info *info)
22{
23 int i;
24
25 for (i = 0; i < ARRAY_SIZE(dev_map); i++)
26 if ((dev_map[i].vid == info->vendor_id) &&
27 (dev_map[i].did == info->device_id))
28 return dev_map[i].device_name;
29 return "Unknown";
30}
31
Sergii Dmytruk4ee03172022-12-22 19:35:25 +020032int tis_open(void)
33{
34 if (tpm_is_open) {
35 printk(BIOS_ERR, "%s() called twice.\n", __func__);
36 return -1;
37 }
38 return 0;
39}
40
41int tis_init(void)
42{
43 struct spi_slave spi;
44 struct tpm2_info info;
45
46 if (spi_setup_slave(CONFIG_DRIVER_TPM_SPI_BUS,
47 CONFIG_DRIVER_TPM_SPI_CHIP, &spi)) {
48 printk(BIOS_ERR, "Failed to setup TPM SPI slave\n");
49 return -1;
50 }
51
52 if (tpm2_init(&spi)) {
53 printk(BIOS_ERR, "Failed to initialize TPM SPI interface\n");
54 return -1;
55 }
56
57 tpm2_get_info(&info);
58
59 printk(BIOS_INFO, "Initialized TPM device %s revision %d\n",
60 tis_get_dev_name(&info), info.revision);
61
62 return 0;
63}
64
65int tis_sendrecv(const uint8_t *sendbuf, size_t sbuf_size,
66 uint8_t *recvbuf, size_t *rbuf_len)
Vadim Bendeburye31d2432016-04-09 18:33:49 -070067{
68 int len = tpm2_process_command(sendbuf, sbuf_size, recvbuf, *rbuf_len);
Aaron Durbin6ef52cd2017-03-27 17:04:26 -050069
70 if (len == 0)
71 return -1;
72
Vadim Bendeburye31d2432016-04-09 18:33:49 -070073 *rbuf_len = len;
Aaron Durbin6ef52cd2017-03-27 17:04:26 -050074
Vadim Bendeburye31d2432016-04-09 18:33:49 -070075 return 0;
76}