blob: fd2a45d34b03cf9942213844ec1bf0ab2e288b60 [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>
Simon Glass4e4bf942022-07-31 12:28:48 -06009#include <display_options.h>
Christophe Ricard302c5db2015-10-06 22:54:42 +020010#include <dm.h>
Miquel Raynald677bfe2018-05-15 11:57:06 +020011#include <tpm-v1.h>
Dirk Eibachc01939c2013-06-26 15:55:15 +020012#include <i2c.h>
13#include <asm/unaligned.h>
Simon Glassc05ed002020-05-10 11:40:11 -060014#include <linux/delay.h>
Dirk Eibachc01939c2013-06-26 15:55:15 +020015
Christophe Ricard302c5db2015-10-06 22:54:42 +020016#include "tpm_internal.h"
17
Dirk Eibachc01939c2013-06-26 15:55:15 +020018#define ATMEL_TPM_TIMEOUT_MS 5000 /* sufficient for anything but
19 generating/exporting keys */
20
21/*
Christophe Ricard302c5db2015-10-06 22:54:42 +020022 * tpm_atmel_twi_open()
Dirk Eibachc01939c2013-06-26 15:55:15 +020023 *
24 * Requests access to locality 0 for the caller. After all commands have been
25 * completed the caller is supposed to call tis_close().
26 *
27 * Returns 0 on success, -1 on failure.
28 */
Christophe Ricard302c5db2015-10-06 22:54:42 +020029static int tpm_atmel_twi_open(struct udevice *dev)
Dirk Eibachc01939c2013-06-26 15:55:15 +020030{
31 return 0;
32}
33
34/*
Christophe Ricard302c5db2015-10-06 22:54:42 +020035 * tpm_atmel_twi_close()
Dirk Eibachc01939c2013-06-26 15:55:15 +020036 *
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).
40 */
Christophe Ricard302c5db2015-10-06 22:54:42 +020041static int tpm_atmel_twi_close(struct udevice *dev)
Dirk Eibachc01939c2013-06-26 15:55:15 +020042{
43 return 0;
44}
45
46/*
Christophe Ricard302c5db2015-10-06 22:54:42 +020047 * tpm_atmel_twi_get_desc()
48 *
49 * @dev: Device to check
50 * @buf: Buffer to put the string
51 * @size: Maximum size of buffer
Heinrich Schuchardt185f8122022-01-19 18:05:50 +010052 * Return: length of string, or -ENOSPC it no space
Christophe Ricard302c5db2015-10-06 22:54:42 +020053 */
54static int tpm_atmel_twi_get_desc(struct udevice *dev, char *buf, int size)
55{
Mathew McBridefb30d992021-11-11 04:06:30 +000056 if (size < 50)
57 return -ENOSPC;
58
59 return snprintf(buf, size, "Atmel AT97SC3204T I2C 1.2 TPM (%s)", dev->name);
Christophe Ricard302c5db2015-10-06 22:54:42 +020060}
61
62/*
63 * tpm_atmel_twi_xfer()
Dirk Eibachc01939c2013-06-26 15:55:15 +020064 *
65 * Send the requested data to the TPM and then try to get its response
66 *
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
71 *
72 * Returns 0 on success (and places the number of response bytes at recv_len)
73 * or -1 on failure.
74 */
Christophe Ricard302c5db2015-10-06 22:54:42 +020075static 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)
Dirk Eibachc01939c2013-06-26 15:55:15 +020078{
79 int res;
80 unsigned long start;
81
82#ifdef DEBUG
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);
86#endif
87
mario.six@gdsys.cc03dcd412016-07-18 13:47:45 +020088 res = dm_i2c_write(dev, 0, sendbuf, send_size);
Dirk Eibachc01939c2013-06-26 15:55:15 +020089 if (res) {
90 printf("i2c_write returned %d\n", res);
91 return -1;
92 }
93
94 start = get_timer(0);
Mathew McBride02f50d82021-11-11 04:06:28 +000095
mario.six@gdsys.cc03dcd412016-07-18 13:47:45 +020096 while ((res = dm_i2c_read(dev, 0, recvbuf, 10)))
mario.six@gdsys.cc03dcd412016-07-18 13:47:45 +020097 {
Christophe Ricard302c5db2015-10-06 22:54:42 +020098 /* TODO Use TIS_TIMEOUT from tpm_tis_infineon.h */
Dirk Eibachc01939c2013-06-26 15:55:15 +020099 if (get_timer(start) > ATMEL_TPM_TIMEOUT_MS) {
100 puts("tpm timed out\n");
101 return -1;
102 }
103 udelay(100);
104 }
105 if (!res) {
Jeremy Booneb3f40702018-02-12 17:56:37 -0500106 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");
110 return -1;
111 } else if (hdr_recv_len > *recv_len) {
112 puts("tpm response length is bigger than receive buffer\n");
113 return -1;
114 } else {
115 *recv_len = hdr_recv_len;
mario.six@gdsys.cc03dcd412016-07-18 13:47:45 +0200116 res = dm_i2c_read(dev, 0, recvbuf, *recv_len);
Jeremy Booneb3f40702018-02-12 17:56:37 -0500117 }
Dirk Eibachc01939c2013-06-26 15:55:15 +0200118 }
119 if (res) {
Mathew McBride4a08dba2021-11-11 04:06:31 +0000120 printf("i2c_read returned %d (rlen=%zu)\n", res, *recv_len);
Dirk Eibachc01939c2013-06-26 15:55:15 +0200121#ifdef DEBUG
122 print_buffer(0, recvbuf, 1, *recv_len, 0);
123#endif
124 }
125
126#ifdef DEBUG
127 if (!res) {
128 printf("read from TPM (%d bytes):\n", *recv_len);
129 print_buffer(0, recvbuf, 1, *recv_len, 0);
130 }
131#endif
132
133 return res;
134}
Christophe Ricard302c5db2015-10-06 22:54:42 +0200135
136static int tpm_atmel_twi_probe(struct udevice *dev)
137{
Mathew McBridefdb4a5f2021-11-11 04:06:29 +0000138 i2c_set_chip_offset_len(dev, 0);
Christophe Ricard302c5db2015-10-06 22:54:42 +0200139 return 0;
140}
141
142static const struct udevice_id tpm_atmel_twi_ids[] = {
143 { .compatible = "atmel,at97sc3204t"},
144 { }
145};
146
147static 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,
152};
153
154U_BOOT_DRIVER(tpm_atmel_twi) = {
155 .name = "tpm_atmel_twi",
156 .id = UCLASS_TPM,
157 .of_match = tpm_atmel_twi_ids,
158 .ops = &tpm_atmel_twi_ops,
159 .probe = tpm_atmel_twi_probe,
160};