xref: /openbmc/u-boot/drivers/tpm/tpm_atmel_twi.c (revision 3ba98ed8)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2013 Guntermann & Drunck, GmbH
4  *
5  * Written by Dirk Eibach <dirk.eibach@gdsys.cc>
6  */
7 
8 #include <common.h>
9 #include <dm.h>
10 #include <tpm-v1.h>
11 #include <i2c.h>
12 #include <asm/unaligned.h>
13 
14 #include "tpm_internal.h"
15 
16 #define ATMEL_TPM_TIMEOUT_MS 5000 /* sufficient for anything but
17 				     generating/exporting keys */
18 
19 /*
20  * tpm_atmel_twi_open()
21  *
22  * Requests access to locality 0 for the caller. After all commands have been
23  * completed the caller is supposed to call tis_close().
24  *
25  * Returns 0 on success, -1 on failure.
26  */
27 static int tpm_atmel_twi_open(struct udevice *dev)
28 {
29 	return 0;
30 }
31 
32 /*
33  * tpm_atmel_twi_close()
34  *
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).
38  */
39 static int tpm_atmel_twi_close(struct udevice *dev)
40 {
41 	return 0;
42 }
43 
44 /*
45  * tpm_atmel_twi_get_desc()
46  *
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
51  */
52 static int tpm_atmel_twi_get_desc(struct udevice *dev, char *buf, int size)
53 {
54 	return 0;
55 }
56 
57 /*
58  * tpm_atmel_twi_xfer()
59  *
60  * Send the requested data to the TPM and then try to get its response
61  *
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
66  *
67  * Returns 0 on success (and places the number of response bytes at recv_len)
68  * or -1 on failure.
69  */
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)
73 {
74 	int res;
75 	unsigned long start;
76 
77 #ifdef DEBUG
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);
81 #endif
82 
83 #ifndef CONFIG_DM_I2C
84 	res = i2c_write(0x29, 0, 0, (uchar *)sendbuf, send_size);
85 #else
86 	res = dm_i2c_write(dev, 0, sendbuf, send_size);
87 #endif
88 	if (res) {
89 		printf("i2c_write returned %d\n", res);
90 		return -1;
91 	}
92 
93 	start = get_timer(0);
94 #ifndef CONFIG_DM_I2C
95 	while ((res = i2c_read(0x29, 0, 0, recvbuf, 10)))
96 #else
97 	while ((res = dm_i2c_read(dev, 0, recvbuf, 10)))
98 #endif
99 	{
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");
103 			return -1;
104 		}
105 		udelay(100);
106 	}
107 	if (!res) {
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");
112 			return -1;
113 		} else if (hdr_recv_len > *recv_len) {
114 			puts("tpm response length is bigger than receive buffer\n");
115 			return -1;
116 		} else {
117 			*recv_len = hdr_recv_len;
118 #ifndef CONFIG_DM_I2C
119 			res = i2c_read(0x29, 0, 0, recvbuf, *recv_len);
120 #else
121 			res = dm_i2c_read(dev, 0, recvbuf, *recv_len);
122 #endif
123 
124 		}
125 	}
126 	if (res) {
127 		printf("i2c_read returned %d (rlen=%d)\n", res, *recv_len);
128 #ifdef DEBUG
129 		print_buffer(0, recvbuf, 1, *recv_len, 0);
130 #endif
131 	}
132 
133 #ifdef DEBUG
134 	if (!res) {
135 		printf("read from TPM (%d bytes):\n", *recv_len);
136 		print_buffer(0, recvbuf, 1, *recv_len, 0);
137 	}
138 #endif
139 
140 	return res;
141 }
142 
143 static int tpm_atmel_twi_probe(struct udevice *dev)
144 {
145 	return 0;
146 }
147 
148 static const struct udevice_id tpm_atmel_twi_ids[] = {
149 	{ .compatible = "atmel,at97sc3204t"},
150 	{ }
151 };
152 
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,
158 };
159 
160 U_BOOT_DRIVER(tpm_atmel_twi) = {
161 	.name = "tpm_atmel_twi",
162 	.id = UCLASS_TPM,
163 	.of_match = tpm_atmel_twi_ids,
164 	.ops = &tpm_atmel_twi_ops,
165 	.probe = tpm_atmel_twi_probe,
166 };
167