1
2
3
4
5
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
17
18
19
20
21
22
23
24
25
26
27static int tpm_atmel_twi_open(struct udevice *dev)
28{
29 return 0;
30}
31
32
33
34
35
36
37
38
39static int tpm_atmel_twi_close(struct udevice *dev)
40{
41 return 0;
42}
43
44
45
46
47
48
49
50
51
52static int tpm_atmel_twi_get_desc(struct udevice *dev, char *buf, int size)
53{
54 return 0;
55}
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70static 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
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
143static int tpm_atmel_twi_probe(struct udevice *dev)
144{
145 return 0;
146}
147
148static const struct udevice_id tpm_atmel_twi_ids[] = {
149 { .compatible = "atmel,at97sc3204t"},
150 { }
151};
152
153static 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
160U_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