{drivers,southbridge}: Replace min() with MIN()
[coreboot.git] / src / drivers / i2c / tpm / tis_atmel.c
blob74b4830b6a5c3a0b4f74d7f1584adef1add7facc
1 /*
2 * This file is part of the coreboot project.
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 <assert.h>
16 #include <commonlib/endian.h>
17 #include <commonlib/helpers.h>
18 #include <console/console.h>
19 #include <delay.h>
20 #include <device/i2c_simple.h>
21 #include <endian.h>
22 #include <lib.h>
23 #include <security/tpm/tis.h>
24 #include <timer.h>
25 #include <types.h>
27 #define RECV_TIMEOUT (1 * 1000) /* 1 second */
28 #define XMIT_TIMEOUT (1 * 1000) /* 1 second */
29 #define SLEEP_DURATION 1000 /* microseconds */
31 struct tpm_output_header {
32 uint16_t tag;
33 uint32_t length;
34 uint32_t return_code;
35 } __packed;
37 int tis_open(void)
39 return 0;
42 int tis_close(void)
44 return 0;
47 int tis_init(void)
49 return 0;
52 int tis_sendrecv(const uint8_t *sendbuf, size_t sbuf_size,
53 uint8_t *recvbuf, size_t *rbuf_len)
55 size_t hdr_bytes;
56 struct tpm_output_header *header;
57 size_t max_recv_bytes;
58 size_t recv_bytes;
59 int status;
60 struct stopwatch sw;
62 ASSERT(sbuf_size >= 10);
63 if (CONFIG(DRIVER_TPM_DISPLAY_TIS_BYTES)) {
64 /* Display the TPM command */
65 if (sbuf_size >= 10)
66 printk(BIOS_DEBUG, "TPM Command: 0x%08x\n",
67 read_at_be32(sendbuf, sizeof(uint16_t)
68 + sizeof(uint32_t)));
69 hexdump(sendbuf, sbuf_size);
72 /* Send the command to the TPM */
73 stopwatch_init_msecs_expire(&sw, XMIT_TIMEOUT);
74 while (1) {
75 status = i2c_write_raw(CONFIG_DRIVER_TPM_I2C_BUS,
76 CONFIG_DRIVER_TPM_I2C_ADDR, (uint8_t *)sendbuf,
77 sbuf_size);
78 if ((status < 0) && (!stopwatch_expired(&sw)))
79 continue;
80 if (status < 0)
81 return status;
82 break;
85 /* Read the TPM response header */
86 max_recv_bytes = *rbuf_len;
87 ASSERT(max_recv_bytes >= sizeof(*header));
88 hdr_bytes = sizeof(*header);
89 header = (struct tpm_output_header *)recvbuf;
90 stopwatch_init_msecs_expire(&sw, RECV_TIMEOUT);
91 do {
92 status = i2c_read_raw(CONFIG_DRIVER_TPM_I2C_BUS,
93 CONFIG_DRIVER_TPM_I2C_ADDR, recvbuf, hdr_bytes);
94 if (status > 0)
95 break;
96 udelay(SLEEP_DURATION);
97 } while (!stopwatch_expired(&sw));
98 if (status != sizeof(*header))
99 return -1;
101 /* Determine the number of bytes remaining */
102 recv_bytes = MIN(be32_to_cpu(*(uint32_t *)&header->length),
103 max_recv_bytes);
105 /* Determine if there is additional response data */
106 if (recv_bytes > hdr_bytes) {
107 /* Display the TPM response */
108 if (CONFIG(DRIVER_TPM_DISPLAY_TIS_BYTES))
109 hexdump(recvbuf, hdr_bytes);
111 /* Read the full TPM response */
112 status = i2c_read_raw(CONFIG_DRIVER_TPM_I2C_BUS,
113 CONFIG_DRIVER_TPM_I2C_ADDR, recvbuf, recv_bytes);
114 if (status < 0)
115 return status;
118 /* Return the number of bytes received */
119 *rbuf_len = status;
121 /* Display the TPM response */
122 if (CONFIG(DRIVER_TPM_DISPLAY_TIS_BYTES)) {
123 printk(BIOS_DEBUG, "TPM Response: 0x%08x\n",
124 read_at_be32(recvbuf, sizeof(uint16_t)
125 + sizeof(uint32_t)));
126 hexdump(recvbuf, *rbuf_len);
129 /* Successful transfer */
130 return 0;