1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2013 Guntermann & Drunck, GmbH
5 * Written by Dirk Eibach <dirk.eibach@gdsys.cc>
12 #include <asm/unaligned.h>
14 #include "tpm_internal.h"
16 #define ATMEL_TPM_TIMEOUT_MS 5000 /* sufficient for anything but
17 generating/exporting keys */
20 * tpm_atmel_twi_open()
22 * Requests access to locality 0 for the caller. After all commands have been
23 * completed the caller is supposed to call tis_close().
25 * Returns 0 on success, -1 on failure.
27 static int tpm_atmel_twi_open(struct udevice *dev)
33 * tpm_atmel_twi_close()
35 * terminate the currect session with the TPM by releasing the locked
36 * locality. Returns 0 on success of -1 on failure (in case lock
37 * removal did not succeed).
39 static int tpm_atmel_twi_close(struct udevice *dev)
45 * tpm_atmel_twi_get_desc()
47 * @dev: Device to check
48 * @buf: Buffer to put the string
49 * @size: Maximum size of buffer
50 * @return length of string, or -ENOSPC it no space
52 static int tpm_atmel_twi_get_desc(struct udevice *dev, char *buf, int size)
58 * tpm_atmel_twi_xfer()
60 * Send the requested data to the TPM and then try to get its response
62 * @sendbuf - buffer of the data to send
63 * @send_size size of the data to send
64 * @recvbuf - memory to save the response to
65 * @recv_len - pointer to the size of the response buffer
67 * Returns 0 on success (and places the number of response bytes at recv_len)
70 static int tpm_atmel_twi_xfer(struct udevice *dev,
71 const uint8_t *sendbuf, size_t send_size,
72 uint8_t *recvbuf, size_t *recv_len)
78 memset(recvbuf, 0xcc, *recv_len);
79 printf("send to TPM (%d bytes, recv_len=%d):\n", send_size, *recv_len);
80 print_buffer(0, (void *)sendbuf, 1, send_size, 0);
84 res = i2c_write(0x29, 0, 0, (uchar *)sendbuf, send_size);
86 res = dm_i2c_write(dev, 0, sendbuf, send_size);
89 printf("i2c_write returned %d\n", res);
95 while ((res = i2c_read(0x29, 0, 0, recvbuf, 10)))
97 while ((res = dm_i2c_read(dev, 0, recvbuf, 10)))
100 /* TODO Use TIS_TIMEOUT from tpm_tis_infineon.h */
101 if (get_timer(start) > ATMEL_TPM_TIMEOUT_MS) {
102 puts("tpm timed out\n");
108 unsigned int hdr_recv_len;
109 hdr_recv_len = get_unaligned_be32(recvbuf + 2);
110 if (hdr_recv_len < 10) {
111 puts("tpm response header too small\n");
113 } else if (hdr_recv_len > *recv_len) {
114 puts("tpm response length is bigger than receive buffer\n");
117 *recv_len = hdr_recv_len;
118 #ifndef CONFIG_DM_I2C
119 res = i2c_read(0x29, 0, 0, recvbuf, *recv_len);
121 res = dm_i2c_read(dev, 0, recvbuf, *recv_len);
127 printf("i2c_read returned %d (rlen=%d)\n", res, *recv_len);
129 print_buffer(0, recvbuf, 1, *recv_len, 0);
135 printf("read from TPM (%d bytes):\n", *recv_len);
136 print_buffer(0, recvbuf, 1, *recv_len, 0);
143 static int tpm_atmel_twi_probe(struct udevice *dev)
148 static const struct udevice_id tpm_atmel_twi_ids[] = {
149 { .compatible = "atmel,at97sc3204t"},
153 static const struct tpm_ops tpm_atmel_twi_ops = {
154 .open = tpm_atmel_twi_open,
155 .close = tpm_atmel_twi_close,
156 .xfer = tpm_atmel_twi_xfer,
157 .get_desc = tpm_atmel_twi_get_desc,
160 U_BOOT_DRIVER(tpm_atmel_twi) = {
161 .name = "tpm_atmel_twi",
163 .of_match = tpm_atmel_twi_ids,
164 .ops = &tpm_atmel_twi_ops,
165 .probe = tpm_atmel_twi_probe,