blob: 4ff4cf4cd423ed0d1c22e6e9df49e8352f2fd5f4 [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>
Simon Glassc05ed002020-05-10 11:40:11 -060013#include <linux/delay.h>
Dirk Eibachc01939c2013-06-26 15:55:15 +020014
Christophe Ricard302c5db2015-10-06 22:54:42 +020015#include "tpm_internal.h"
16
Dirk Eibachc01939c2013-06-26 15:55:15 +020017#define ATMEL_TPM_TIMEOUT_MS 5000 /* sufficient for anything but
18 generating/exporting keys */
19
20/*
Christophe Ricard302c5db2015-10-06 22:54:42 +020021 * tpm_atmel_twi_open()
Dirk Eibachc01939c2013-06-26 15:55:15 +020022 *
23 * Requests access to locality 0 for the caller. After all commands have been
24 * completed the caller is supposed to call tis_close().
25 *
26 * Returns 0 on success, -1 on failure.
27 */
Christophe Ricard302c5db2015-10-06 22:54:42 +020028static int tpm_atmel_twi_open(struct udevice *dev)
Dirk Eibachc01939c2013-06-26 15:55:15 +020029{
30 return 0;
31}
32
33/*
Christophe Ricard302c5db2015-10-06 22:54:42 +020034 * tpm_atmel_twi_close()
Dirk Eibachc01939c2013-06-26 15:55:15 +020035 *
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).
39 */
Christophe Ricard302c5db2015-10-06 22:54:42 +020040static int tpm_atmel_twi_close(struct udevice *dev)
Dirk Eibachc01939c2013-06-26 15:55:15 +020041{
42 return 0;
43}
44
45/*
Christophe Ricard302c5db2015-10-06 22:54:42 +020046 * tpm_atmel_twi_get_desc()
47 *
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
52 */
53static int tpm_atmel_twi_get_desc(struct udevice *dev, char *buf, int size)
54{
55 return 0;
56}
57
58/*
59 * tpm_atmel_twi_xfer()
Dirk Eibachc01939c2013-06-26 15:55:15 +020060 *
61 * Send the requested data to the TPM and then try to get its response
62 *
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
67 *
68 * Returns 0 on success (and places the number of response bytes at recv_len)
69 * or -1 on failure.
70 */
Christophe Ricard302c5db2015-10-06 22:54:42 +020071static 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)
Dirk Eibachc01939c2013-06-26 15:55:15 +020074{
75 int res;
76 unsigned long start;
77
78#ifdef DEBUG
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);
82#endif
83
mario.six@gdsys.cc03dcd412016-07-18 13:47:45 +020084 res = dm_i2c_write(dev, 0, sendbuf, send_size);
Dirk Eibachc01939c2013-06-26 15:55:15 +020085 if (res) {
86 printf("i2c_write returned %d\n", res);
87 return -1;
88 }
89
90 start = get_timer(0);
Mathew McBride02f50d82021-11-11 04:06:28 +000091
mario.six@gdsys.cc03dcd412016-07-18 13:47:45 +020092 while ((res = dm_i2c_read(dev, 0, recvbuf, 10)))
mario.six@gdsys.cc03dcd412016-07-18 13:47:45 +020093 {
Christophe Ricard302c5db2015-10-06 22:54:42 +020094 /* TODO Use TIS_TIMEOUT from tpm_tis_infineon.h */
Dirk Eibachc01939c2013-06-26 15:55:15 +020095 if (get_timer(start) > ATMEL_TPM_TIMEOUT_MS) {
96 puts("tpm timed out\n");
97 return -1;
98 }
99 udelay(100);
100 }
101 if (!res) {
Jeremy Booneb3f40702018-02-12 17:56:37 -0500102 unsigned int hdr_recv_len;
103 hdr_recv_len = get_unaligned_be32(recvbuf + 2);
104 if (hdr_recv_len < 10) {
105 puts("tpm response header too small\n");
106 return -1;
107 } else if (hdr_recv_len > *recv_len) {
108 puts("tpm response length is bigger than receive buffer\n");
109 return -1;
110 } else {
111 *recv_len = hdr_recv_len;
mario.six@gdsys.cc03dcd412016-07-18 13:47:45 +0200112 res = dm_i2c_read(dev, 0, recvbuf, *recv_len);
Jeremy Booneb3f40702018-02-12 17:56:37 -0500113 }
Dirk Eibachc01939c2013-06-26 15:55:15 +0200114 }
115 if (res) {
116 printf("i2c_read returned %d (rlen=%d)\n", res, *recv_len);
117#ifdef DEBUG
118 print_buffer(0, recvbuf, 1, *recv_len, 0);
119#endif
120 }
121
122#ifdef DEBUG
123 if (!res) {
124 printf("read from TPM (%d bytes):\n", *recv_len);
125 print_buffer(0, recvbuf, 1, *recv_len, 0);
126 }
127#endif
128
129 return res;
130}
Christophe Ricard302c5db2015-10-06 22:54:42 +0200131
132static int tpm_atmel_twi_probe(struct udevice *dev)
133{
134 return 0;
135}
136
137static const struct udevice_id tpm_atmel_twi_ids[] = {
138 { .compatible = "atmel,at97sc3204t"},
139 { }
140};
141
142static const struct tpm_ops tpm_atmel_twi_ops = {
143 .open = tpm_atmel_twi_open,
144 .close = tpm_atmel_twi_close,
145 .xfer = tpm_atmel_twi_xfer,
146 .get_desc = tpm_atmel_twi_get_desc,
147};
148
149U_BOOT_DRIVER(tpm_atmel_twi) = {
150 .name = "tpm_atmel_twi",
151 .id = UCLASS_TPM,
152 .of_match = tpm_atmel_twi_ids,
153 .ops = &tpm_atmel_twi_ops,
154 .probe = tpm_atmel_twi_probe,
155};