1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2013 Guntermann & Drunck, GmbH
5 * Written by Dirk Eibach <dirk.eibach@gdsys.cc>
9 #include <display_options.h>
13 #include <asm/unaligned.h>
14 #include <linux/delay.h>
16 #include "tpm_internal.h"
18 #define ATMEL_TPM_TIMEOUT_MS 5000 /* sufficient for anything but
19 generating/exporting keys */
22 * tpm_atmel_twi_open()
24 * Requests access to locality 0 for the caller. After all commands have been
25 * completed the caller is supposed to call tis_close().
27 * Returns 0 on success, -1 on failure.
29 static int tpm_atmel_twi_open(struct udevice *dev)
35 * tpm_atmel_twi_close()
37 * terminate the currect session with the TPM by releasing the locked
38 * locality. Returns 0 on success of -1 on failure (in case lock
39 * removal did not succeed).
41 static int tpm_atmel_twi_close(struct udevice *dev)
47 * tpm_atmel_twi_get_desc()
49 * @dev: Device to check
50 * @buf: Buffer to put the string
51 * @size: Maximum size of buffer
52 * Return: length of string, or -ENOSPC it no space
54 static int tpm_atmel_twi_get_desc(struct udevice *dev, char *buf, int size)
59 return snprintf(buf, size, "Atmel AT97SC3204T I2C 1.2 TPM (%s)", dev->name);
63 * tpm_atmel_twi_xfer()
65 * Send the requested data to the TPM and then try to get its response
67 * @sendbuf - buffer of the data to send
68 * @send_size size of the data to send
69 * @recvbuf - memory to save the response to
70 * @recv_len - pointer to the size of the response buffer
72 * Returns 0 on success (and places the number of response bytes at recv_len)
75 static int tpm_atmel_twi_xfer(struct udevice *dev,
76 const uint8_t *sendbuf, size_t send_size,
77 uint8_t *recvbuf, size_t *recv_len)
83 memset(recvbuf, 0xcc, *recv_len);
84 printf("send to TPM (%d bytes, recv_len=%d):\n", send_size, *recv_len);
85 print_buffer(0, (void *)sendbuf, 1, send_size, 0);
88 res = dm_i2c_write(dev, 0, sendbuf, send_size);
90 printf("i2c_write returned %d\n", res);
96 while ((res = dm_i2c_read(dev, 0, recvbuf, 10)))
98 /* TODO Use TIS_TIMEOUT from tpm_tis_infineon.h */
99 if (get_timer(start) > ATMEL_TPM_TIMEOUT_MS) {
100 puts("tpm timed out\n");
106 unsigned int hdr_recv_len;
107 hdr_recv_len = get_unaligned_be32(recvbuf + 2);
108 if (hdr_recv_len < 10) {
109 puts("tpm response header too small\n");
111 } else if (hdr_recv_len > *recv_len) {
112 puts("tpm response length is bigger than receive buffer\n");
115 *recv_len = hdr_recv_len;
116 res = dm_i2c_read(dev, 0, recvbuf, *recv_len);
120 printf("i2c_read returned %d (rlen=%zu)\n", res, *recv_len);
122 print_buffer(0, recvbuf, 1, *recv_len, 0);
128 printf("read from TPM (%d bytes):\n", *recv_len);
129 print_buffer(0, recvbuf, 1, *recv_len, 0);
136 static int tpm_atmel_twi_probe(struct udevice *dev)
138 i2c_set_chip_offset_len(dev, 0);
142 static const struct udevice_id tpm_atmel_twi_ids[] = {
143 { .compatible = "atmel,at97sc3204t"},
147 static const struct tpm_ops tpm_atmel_twi_ops = {
148 .open = tpm_atmel_twi_open,
149 .close = tpm_atmel_twi_close,
150 .xfer = tpm_atmel_twi_xfer,
151 .get_desc = tpm_atmel_twi_get_desc,
154 U_BOOT_DRIVER(tpm_atmel_twi) = {
155 .name = "tpm_atmel_twi",
157 .of_match = tpm_atmel_twi_ids,
158 .ops = &tpm_atmel_twi_ops,
159 .probe = tpm_atmel_twi_probe,