blob: 2079ea913e4428d88e03a962c00e5a667aecbba5 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Dirk Eibachc01939c2013-06-26 15:55:15 +02002/*
Simon Glass07470d62015-08-22 18:31:21 -06003 * Copyright (C) 2013 Guntermann & Drunck, GmbH
Dirk Eibachc01939c2013-06-26 15:55:15 +02004 *
Mario Sixd38826a2018-03-06 08:04:58 +01005 * Written by Dirk Eibach <dirk.eibach@gdsys.cc>
Dirk Eibachc01939c2013-06-26 15:55:15 +02006 */
7
8#include <common.h>
Christophe Ricard302c5db2015-10-06 22:54:42 +02009#include <dm.h>
Miquel Raynald677bfe2018-05-15 11:57:06 +020010#include <tpm-v1.h>
Dirk Eibachc01939c2013-06-26 15:55:15 +020011#include <i2c.h>
12#include <asm/unaligned.h>
13
Christophe Ricard302c5db2015-10-06 22:54:42 +020014#include "tpm_internal.h"
15
Dirk Eibachc01939c2013-06-26 15:55:15 +020016#define ATMEL_TPM_TIMEOUT_MS 5000 /* sufficient for anything but
17 generating/exporting keys */
18
19/*
Christophe Ricard302c5db2015-10-06 22:54:42 +020020 * tpm_atmel_twi_open()
Dirk Eibachc01939c2013-06-26 15:55:15 +020021 *
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 */
Christophe Ricard302c5db2015-10-06 22:54:42 +020027static int tpm_atmel_twi_open(struct udevice *dev)
Dirk Eibachc01939c2013-06-26 15:55:15 +020028{
29 return 0;
30}
31
32/*
Christophe Ricard302c5db2015-10-06 22:54:42 +020033 * tpm_atmel_twi_close()
Dirk Eibachc01939c2013-06-26 15:55:15 +020034 *
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 */
Christophe Ricard302c5db2015-10-06 22:54:42 +020039static int tpm_atmel_twi_close(struct udevice *dev)
Dirk Eibachc01939c2013-06-26 15:55:15 +020040{
41 return 0;
42}
43
44/*
Christophe Ricard302c5db2015-10-06 22:54:42 +020045 * 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 */
52static 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()
Dirk Eibachc01939c2013-06-26 15:55:15 +020059 *
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 */
Christophe Ricard302c5db2015-10-06 22:54:42 +020070static 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)
Dirk Eibachc01939c2013-06-26 15:55:15 +020073{
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
mario.six@gdsys.cc03dcd412016-07-18 13:47:45 +020083#ifndef CONFIG_DM_I2C
Dirk Eibachc01939c2013-06-26 15:55:15 +020084 res = i2c_write(0x29, 0, 0, (uchar *)sendbuf, send_size);
mario.six@gdsys.cc03dcd412016-07-18 13:47:45 +020085#else
86 res = dm_i2c_write(dev, 0, sendbuf, send_size);
87#endif
Dirk Eibachc01939c2013-06-26 15:55:15 +020088 if (res) {
89 printf("i2c_write returned %d\n", res);
90 return -1;
91 }
92
93 start = get_timer(0);
mario.six@gdsys.cc03dcd412016-07-18 13:47:45 +020094#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 {
Christophe Ricard302c5db2015-10-06 22:54:42 +0200100 /* TODO Use TIS_TIMEOUT from tpm_tis_infineon.h */
Dirk Eibachc01939c2013-06-26 15:55:15 +0200101 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) {
Jeremy Booneb3f40702018-02-12 17:56:37 -0500108 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;
mario.six@gdsys.cc03dcd412016-07-18 13:47:45 +0200118#ifndef CONFIG_DM_I2C
Dirk Eibachc01939c2013-06-26 15:55:15 +0200119 res = i2c_read(0x29, 0, 0, recvbuf, *recv_len);
mario.six@gdsys.cc03dcd412016-07-18 13:47:45 +0200120#else
121 res = dm_i2c_read(dev, 0, recvbuf, *recv_len);
122#endif
Jeremy Booneb3f40702018-02-12 17:56:37 -0500123
124 }
Dirk Eibachc01939c2013-06-26 15:55:15 +0200125 }
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}
Christophe Ricard302c5db2015-10-06 22:54:42 +0200142
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};