xref: /aosp_15_r20/external/coreboot/src/drivers/spi/tpm/tis.c (revision b9411a12aaaa7e1e6a6fb7c5e057f44ee179a49c)
1 /* SPDX-License-Identifier: BSD-3-Clause */
2 
3 #include <console/console.h>
4 #include <security/tpm/tis.h>
5 
6 #include "tpm.h"
7 
8 static const struct {
9 	uint16_t vid;
10 	uint16_t did;
11 	const char *device_name;
12 } dev_map[] = {
13 	{ 0x15d1, 0x001b, "SLB9670" },
14 	{ 0x1ae0, 0x0028, "CR50" },
15 	{ 0x104a, 0x0000, "ST33HTPH2E32" },
16 	{ 0x6666, 0x504a, "TI50" },
17 };
18 
tis_get_dev_name(struct tpm2_info * info)19 static const char *tis_get_dev_name(struct tpm2_info *info)
20 {
21 	int i;
22 
23 	for (i = 0; i < ARRAY_SIZE(dev_map); i++)
24 		if ((dev_map[i].vid == info->vendor_id) &&
25 		    (dev_map[i].did == info->device_id))
26 			return dev_map[i].device_name;
27 	return "Unknown";
28 }
29 
tpm_sendrecv(const uint8_t * sendbuf,size_t sbuf_size,uint8_t * recvbuf,size_t * rbuf_len)30 static tpm_result_t tpm_sendrecv(const uint8_t *sendbuf, size_t sbuf_size,
31 				 uint8_t *recvbuf, size_t *rbuf_len)
32 {
33 	int len = tpm2_process_command(sendbuf, sbuf_size, recvbuf, *rbuf_len);
34 
35 	if (len == 0)
36 		return TPM_CB_FAIL;
37 
38 	*rbuf_len = len;
39 
40 	return TPM_SUCCESS;
41 }
42 
spi_tis_probe(enum tpm_family * family)43 tis_sendrecv_fn spi_tis_probe(enum tpm_family *family)
44 {
45 	struct spi_slave spi;
46 	struct tpm2_info info;
47 
48 	if (spi_setup_slave(CONFIG_DRIVER_TPM_SPI_BUS,
49 			    CONFIG_DRIVER_TPM_SPI_CHIP, &spi)) {
50 		printk(BIOS_ERR, "Failed to setup TPM SPI slave\n");
51 		return NULL;
52 	}
53 
54 	if (tpm2_init(&spi)) {
55 		printk(BIOS_ERR, "Failed to initialize TPM SPI interface\n");
56 		return NULL;
57 	}
58 
59 	/* tpm2_process_command() is used unconditionally in tpm_sendrecv() */
60 	if (family != NULL)
61 		*family = TPM_2;
62 
63 	tpm2_get_info(&info);
64 
65 	printk(BIOS_INFO, "Initialized TPM device %s revision %d\n",
66 	       tis_get_dev_name(&info), info.revision);
67 
68 	return &tpm_sendrecv;
69 }
70