2 * Copyright (C) 2017 Intel Corporation
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of
7 * the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but without any warranty; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
15 #include <arch/early_variables.h>
20 #include <commonlib/endian.h>
21 #include <console/console.h>
23 #include <device/i2c.h>
29 #define RECV_TIMEOUT (1 * 1000) /* 1 second */
30 #define XMIT_TIMEOUT (1 * 1000) /* 1 second */
31 #define SLEEP_DURATION 1000 /* microseconds */
33 struct tpm_output_header
{
54 int tis_sendrecv(const uint8_t *sendbuf
, size_t sbuf_size
,
55 uint8_t *recvbuf
, size_t *rbuf_len
)
58 struct tpm_output_header
*header
;
59 size_t max_recv_bytes
;
64 ASSERT(sbuf_size
>= 10);
65 if (IS_ENABLED(CONFIG_DRIVER_TPM_DISPLAY_TIS_BYTES
)) {
66 /* Display the TPM command */
68 printk(BIOS_DEBUG
, "TPM Command: 0x%08x\n",
69 read_at_be32(sendbuf
, sizeof(uint16_t)
71 hexdump(sendbuf
, sbuf_size
);
74 /* Send the command to the TPM */
75 stopwatch_init_msecs_expire(&sw
, XMIT_TIMEOUT
);
77 status
= i2c_write_raw(CONFIG_DRIVER_TPM_I2C_BUS
,
78 CONFIG_DRIVER_TPM_I2C_ADDR
, (uint8_t *)sendbuf
,
80 if ((status
< 0) && (!stopwatch_expired(&sw
)))
87 /* Read the TPM response header */
88 max_recv_bytes
= *rbuf_len
;
89 ASSERT(max_recv_bytes
>= sizeof(*header
));
90 hdr_bytes
= sizeof(*header
);
91 header
= (struct tpm_output_header
*)recvbuf
;
92 stopwatch_init_msecs_expire(&sw
, RECV_TIMEOUT
);
94 status
= i2c_read_raw(CONFIG_DRIVER_TPM_I2C_BUS
,
95 CONFIG_DRIVER_TPM_I2C_ADDR
, recvbuf
, hdr_bytes
);
98 udelay(SLEEP_DURATION
);
99 } while (!stopwatch_expired(&sw
));
100 if (status
!= sizeof(*header
))
103 /* Determine the number of bytes remaining */
104 recv_bytes
= min(be32_to_cpu(*(uint32_t *)&header
->length
),
107 /* Determine if there is additional response data */
108 if (recv_bytes
> hdr_bytes
) {
109 /* Display the TPM response */
110 if (IS_ENABLED(CONFIG_DRIVER_TPM_DISPLAY_TIS_BYTES
))
111 hexdump(recvbuf
, hdr_bytes
);
113 /* Read the full TPM response */
114 status
= i2c_read_raw(CONFIG_DRIVER_TPM_I2C_BUS
,
115 CONFIG_DRIVER_TPM_I2C_ADDR
, recvbuf
, recv_bytes
);
120 /* Return the number of bytes received */
123 /* Display the TPM response */
124 if (IS_ENABLED(CONFIG_DRIVER_TPM_DISPLAY_TIS_BYTES
)) {
125 printk(BIOS_DEBUG
, "TPM Response: 0x%08x\n",
126 read_at_be32(recvbuf
, sizeof(uint16_t)
127 + sizeof(uint32_t)));
128 hexdump(recvbuf
, *rbuf_len
);
131 /* Successful transfer */