Rename __attribute__((packed)) --> __packed
[coreboot.git] / src / drivers / i2c / tpm / tis_atmel.c
blobb6e13513f3fda63b85956696dbc9664791645f0c
1 /*
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>
16 #include <stdint.h>
17 #include <compiler.h>
18 #include <string.h>
19 #include <assert.h>
20 #include <commonlib/endian.h>
21 #include <console/console.h>
22 #include <delay.h>
23 #include <device/i2c.h>
24 #include <endian.h>
25 #include <lib.h>
26 #include <tpm.h>
27 #include <timer.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 {
34 uint16_t tag;
35 uint32_t length;
36 uint32_t return_code;
37 } __packed;
39 int tis_open(void)
41 return 0;
44 int tis_close(void)
46 return 0;
49 int tis_init(void)
51 return 0;
54 int tis_sendrecv(const uint8_t *sendbuf, size_t sbuf_size,
55 uint8_t *recvbuf, size_t *rbuf_len)
57 size_t hdr_bytes;
58 struct tpm_output_header *header;
59 size_t max_recv_bytes;
60 size_t recv_bytes;
61 int status;
62 struct stopwatch sw;
64 ASSERT(sbuf_size >= 10);
65 if (IS_ENABLED(CONFIG_DRIVER_TPM_DISPLAY_TIS_BYTES)) {
66 /* Display the TPM command */
67 if (sbuf_size >= 10)
68 printk(BIOS_DEBUG, "TPM Command: 0x%08x\n",
69 read_at_be32(sendbuf, sizeof(uint16_t)
70 + sizeof(uint32_t)));
71 hexdump(sendbuf, sbuf_size);
74 /* Send the command to the TPM */
75 stopwatch_init_msecs_expire(&sw, XMIT_TIMEOUT);
76 while (1) {
77 status = i2c_write_raw(CONFIG_DRIVER_TPM_I2C_BUS,
78 CONFIG_DRIVER_TPM_I2C_ADDR, (uint8_t *)sendbuf,
79 sbuf_size);
80 if ((status < 0) && (!stopwatch_expired(&sw)))
81 continue;
82 if (status < 0)
83 return status;
84 break;
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);
93 do {
94 status = i2c_read_raw(CONFIG_DRIVER_TPM_I2C_BUS,
95 CONFIG_DRIVER_TPM_I2C_ADDR, recvbuf, hdr_bytes);
96 if (status > 0)
97 break;
98 udelay(SLEEP_DURATION);
99 } while (!stopwatch_expired(&sw));
100 if (status != sizeof(*header))
101 return -1;
103 /* Determine the number of bytes remaining */
104 recv_bytes = min(be32_to_cpu(*(uint32_t *)&header->length),
105 max_recv_bytes);
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);
116 if (status < 0)
117 return status;
120 /* Return the number of bytes received */
121 *rbuf_len = status;
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 */
132 return 0;