2 * Copyright (C) 2013 Guntermann & Drunck, GmbH
4 * Written by Dirk Eibach <eibach@gdsys.de>
6 * SPDX-License-Identifier: GPL-2.0+
13 #include <asm/unaligned.h>
15 #include "tpm_internal.h"
17 #define ATMEL_TPM_TIMEOUT_MS 5000 /* sufficient for anything but
18 generating/exporting keys */
21 * tpm_atmel_twi_open()
23 * Requests access to locality 0 for the caller. After all commands have been
24 * completed the caller is supposed to call tis_close().
26 * Returns 0 on success, -1 on failure.
28 static int tpm_atmel_twi_open(struct udevice *dev)
34 * tpm_atmel_twi_close()
36 * terminate the currect session with the TPM by releasing the locked
37 * locality. Returns 0 on success of -1 on failure (in case lock
38 * removal did not succeed).
40 static int tpm_atmel_twi_close(struct udevice *dev)
46 * tpm_atmel_twi_get_desc()
48 * @dev: Device to check
49 * @buf: Buffer to put the string
50 * @size: Maximum size of buffer
51 * @return length of string, or -ENOSPC it no space
53 static int tpm_atmel_twi_get_desc(struct udevice *dev, char *buf, int size)
59 * tpm_atmel_twi_xfer()
61 * Send the requested data to the TPM and then try to get its response
63 * @sendbuf - buffer of the data to send
64 * @send_size size of the data to send
65 * @recvbuf - memory to save the response to
66 * @recv_len - pointer to the size of the response buffer
68 * Returns 0 on success (and places the number of response bytes at recv_len)
71 static int tpm_atmel_twi_xfer(struct udevice *dev,
72 const uint8_t *sendbuf, size_t send_size,
73 uint8_t *recvbuf, size_t *recv_len)
79 memset(recvbuf, 0xcc, *recv_len);
80 printf("send to TPM (%d bytes, recv_len=%d):\n", send_size, *recv_len);
81 print_buffer(0, (void *)sendbuf, 1, send_size, 0);
85 res = i2c_write(0x29, 0, 0, (uchar *)sendbuf, send_size);
87 res = dm_i2c_write(dev, 0, sendbuf, send_size);
90 printf("i2c_write returned %d\n", res);
96 while ((res = i2c_read(0x29, 0, 0, recvbuf, 10)))
98 while ((res = dm_i2c_read(dev, 0, recvbuf, 10)))
101 /* TODO Use TIS_TIMEOUT from tpm_tis_infineon.h */
102 if (get_timer(start) > ATMEL_TPM_TIMEOUT_MS) {
103 puts("tpm timed out\n");
109 *recv_len = get_unaligned_be32(recvbuf + 2);
111 #ifndef CONFIG_DM_I2C
112 res = i2c_read(0x29, 0, 0, recvbuf, *recv_len);
114 res = dm_i2c_read(dev, 0, recvbuf, *recv_len);
118 printf("i2c_read returned %d (rlen=%d)\n", res, *recv_len);
120 print_buffer(0, recvbuf, 1, *recv_len, 0);
126 printf("read from TPM (%d bytes):\n", *recv_len);
127 print_buffer(0, recvbuf, 1, *recv_len, 0);
134 static int tpm_atmel_twi_probe(struct udevice *dev)
139 static const struct udevice_id tpm_atmel_twi_ids[] = {
140 { .compatible = "atmel,at97sc3204t"},
144 static const struct tpm_ops tpm_atmel_twi_ops = {
145 .open = tpm_atmel_twi_open,
146 .close = tpm_atmel_twi_close,
147 .xfer = tpm_atmel_twi_xfer,
148 .get_desc = tpm_atmel_twi_get_desc,
151 U_BOOT_DRIVER(tpm_atmel_twi) = {
152 .name = "tpm_atmel_twi",
154 .of_match = tpm_atmel_twi_ids,
155 .ops = &tpm_atmel_twi_ops,
156 .probe = tpm_atmel_twi_probe,