blob: fa4fbec6bbdf479ec826a0e2453e860055d3108a [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Dirk Eibach20489092013-06-26 15:55:15 +02002/*
Simon Glassc98f7fe2015-08-22 18:31:21 -06003 * Copyright (C) 2013 Guntermann & Drunck, GmbH
Dirk Eibach20489092013-06-26 15:55:15 +02004 *
Mario Sixb4893582018-03-06 08:04:58 +01005 * Written by Dirk Eibach <dirk.eibach@gdsys.cc>
Dirk Eibach20489092013-06-26 15:55:15 +02006 */
7
8#include <common.h>
Christophe Ricard587bf7a2015-10-06 22:54:42 +02009#include <dm.h>
Miquel Raynal4c6759e2018-05-15 11:57:06 +020010#include <tpm-v1.h>
Dirk Eibach20489092013-06-26 15:55:15 +020011#include <i2c.h>
12#include <asm/unaligned.h>
Simon Glassdbd79542020-05-10 11:40:11 -060013#include <linux/delay.h>
Dirk Eibach20489092013-06-26 15:55:15 +020014
Christophe Ricard587bf7a2015-10-06 22:54:42 +020015#include "tpm_internal.h"
16
Dirk Eibach20489092013-06-26 15:55:15 +020017#define ATMEL_TPM_TIMEOUT_MS 5000 /* sufficient for anything but
18 generating/exporting keys */
19
20/*
Christophe Ricard587bf7a2015-10-06 22:54:42 +020021 * tpm_atmel_twi_open()
Dirk Eibach20489092013-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 Ricard587bf7a2015-10-06 22:54:42 +020028static int tpm_atmel_twi_open(struct udevice *dev)
Dirk Eibach20489092013-06-26 15:55:15 +020029{
30 return 0;
31}
32
33/*
Christophe Ricard587bf7a2015-10-06 22:54:42 +020034 * tpm_atmel_twi_close()
Dirk Eibach20489092013-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 Ricard587bf7a2015-10-06 22:54:42 +020040static int tpm_atmel_twi_close(struct udevice *dev)
41{
42 return 0;
43}
44
45/*
46 * 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)
Dirk Eibach20489092013-06-26 15:55:15 +020054{
Mathew McBride9b4cd472021-11-11 04:06:30 +000055 if (size < 50)
56 return -ENOSPC;
57
58 return snprintf(buf, size, "Atmel AT97SC3204T I2C 1.2 TPM (%s)", dev->name);
Dirk Eibach20489092013-06-26 15:55:15 +020059}
60
61/*
Christophe Ricard587bf7a2015-10-06 22:54:42 +020062 * tpm_atmel_twi_xfer()
Dirk Eibach20489092013-06-26 15:55:15 +020063 *
64 * Send the requested data to the TPM and then try to get its response
65 *
66 * @sendbuf - buffer of the data to send
67 * @send_size size of the data to send
68 * @recvbuf - memory to save the response to
69 * @recv_len - pointer to the size of the response buffer
70 *
71 * Returns 0 on success (and places the number of response bytes at recv_len)
72 * or -1 on failure.
73 */
Christophe Ricard587bf7a2015-10-06 22:54:42 +020074static int tpm_atmel_twi_xfer(struct udevice *dev,
75 const uint8_t *sendbuf, size_t send_size,
76 uint8_t *recvbuf, size_t *recv_len)
Dirk Eibach20489092013-06-26 15:55:15 +020077{
78 int res;
79 unsigned long start;
80
81#ifdef DEBUG
82 memset(recvbuf, 0xcc, *recv_len);
83 printf("send to TPM (%d bytes, recv_len=%d):\n", send_size, *recv_len);
84 print_buffer(0, (void *)sendbuf, 1, send_size, 0);
85#endif
86
mario.six@gdsys.ccec951ad2016-07-18 13:47:45 +020087 res = dm_i2c_write(dev, 0, sendbuf, send_size);
Dirk Eibach20489092013-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);
Mathew McBride16bfe892021-11-11 04:06:28 +000094
mario.six@gdsys.ccec951ad2016-07-18 13:47:45 +020095 while ((res = dm_i2c_read(dev, 0, recvbuf, 10)))
mario.six@gdsys.ccec951ad2016-07-18 13:47:45 +020096 {
Christophe Ricard587bf7a2015-10-06 22:54:42 +020097 /* TODO Use TIS_TIMEOUT from tpm_tis_infineon.h */
Dirk Eibach20489092013-06-26 15:55:15 +020098 if (get_timer(start) > ATMEL_TPM_TIMEOUT_MS) {
99 puts("tpm timed out\n");
100 return -1;
101 }
102 udelay(100);
103 }
104 if (!res) {
Jeremy Boone10e3d762018-02-12 17:56:37 -0500105 unsigned int hdr_recv_len;
106 hdr_recv_len = get_unaligned_be32(recvbuf + 2);
107 if (hdr_recv_len < 10) {
108 puts("tpm response header too small\n");
109 return -1;
110 } else if (hdr_recv_len > *recv_len) {
111 puts("tpm response length is bigger than receive buffer\n");
112 return -1;
113 } else {
114 *recv_len = hdr_recv_len;
mario.six@gdsys.ccec951ad2016-07-18 13:47:45 +0200115 res = dm_i2c_read(dev, 0, recvbuf, *recv_len);
Jeremy Boone10e3d762018-02-12 17:56:37 -0500116 }
Dirk Eibach20489092013-06-26 15:55:15 +0200117 }
118 if (res) {
Mathew McBride7a4038b2021-11-11 04:06:31 +0000119 printf("i2c_read returned %d (rlen=%zu)\n", res, *recv_len);
Dirk Eibach20489092013-06-26 15:55:15 +0200120#ifdef DEBUG
121 print_buffer(0, recvbuf, 1, *recv_len, 0);
122#endif
123 }
124
125#ifdef DEBUG
126 if (!res) {
127 printf("read from TPM (%d bytes):\n", *recv_len);
128 print_buffer(0, recvbuf, 1, *recv_len, 0);
129 }
130#endif
131
132 return res;
133}
Christophe Ricard587bf7a2015-10-06 22:54:42 +0200134
135static int tpm_atmel_twi_probe(struct udevice *dev)
136{
Mathew McBride175802c2021-11-11 04:06:29 +0000137 i2c_set_chip_offset_len(dev, 0);
Christophe Ricard587bf7a2015-10-06 22:54:42 +0200138 return 0;
139}
140
141static const struct udevice_id tpm_atmel_twi_ids[] = {
142 { .compatible = "atmel,at97sc3204t"},
143 { }
144};
145
146static const struct tpm_ops tpm_atmel_twi_ops = {
147 .open = tpm_atmel_twi_open,
148 .close = tpm_atmel_twi_close,
149 .xfer = tpm_atmel_twi_xfer,
150 .get_desc = tpm_atmel_twi_get_desc,
151};
152
153U_BOOT_DRIVER(tpm_atmel_twi) = {
154 .name = "tpm_atmel_twi",
155 .id = UCLASS_TPM,
156 .of_match = tpm_atmel_twi_ids,
157 .ops = &tpm_atmel_twi_ops,
158 .probe = tpm_atmel_twi_probe,
159};