blob: 2aa9381b11a5816d4e6178280a8b4c7df6052f25 [file] [log] [blame]
Dirk Eibach20489092013-06-26 15:55:15 +02001/*
Simon Glassc98f7fe2015-08-22 18:31:21 -06002 * Copyright (C) 2013 Guntermann & Drunck, GmbH
Dirk Eibach20489092013-06-26 15:55:15 +02003 *
Simon Glassc98f7fe2015-08-22 18:31:21 -06004 * Written by Dirk Eibach <eibach@gdsys.de>
Dirk Eibach20489092013-06-26 15:55:15 +02005 *
Simon Glassc98f7fe2015-08-22 18:31:21 -06006 * SPDX-License-Identifier: GPL-2.0+
Dirk Eibach20489092013-06-26 15:55:15 +02007 */
8
9#include <common.h>
Christophe Ricard587bf7a2015-10-06 22:54:42 +020010#include <dm.h>
Dirk Eibach20489092013-06-26 15:55:15 +020011#include <tpm.h>
12#include <i2c.h>
13#include <asm/unaligned.h>
14
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{
55 return 0;
56}
57
58/*
Christophe Ricard587bf7a2015-10-06 22:54:42 +020059 * tpm_atmel_twi_xfer()
Dirk Eibach20489092013-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 Ricard587bf7a2015-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 Eibach20489092013-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
84 res = i2c_write(0x29, 0, 0, (uchar *)sendbuf, send_size);
85 if (res) {
86 printf("i2c_write returned %d\n", res);
87 return -1;
88 }
89
90 start = get_timer(0);
91 while ((res = i2c_read(0x29, 0, 0, recvbuf, 10))) {
Christophe Ricard587bf7a2015-10-06 22:54:42 +020092 /* TODO Use TIS_TIMEOUT from tpm_tis_infineon.h */
Dirk Eibach20489092013-06-26 15:55:15 +020093 if (get_timer(start) > ATMEL_TPM_TIMEOUT_MS) {
94 puts("tpm timed out\n");
95 return -1;
96 }
97 udelay(100);
98 }
99 if (!res) {
100 *recv_len = get_unaligned_be32(recvbuf + 2);
101 if (*recv_len > 10)
102 res = i2c_read(0x29, 0, 0, recvbuf, *recv_len);
103 }
104 if (res) {
105 printf("i2c_read returned %d (rlen=%d)\n", res, *recv_len);
106#ifdef DEBUG
107 print_buffer(0, recvbuf, 1, *recv_len, 0);
108#endif
109 }
110
111#ifdef DEBUG
112 if (!res) {
113 printf("read from TPM (%d bytes):\n", *recv_len);
114 print_buffer(0, recvbuf, 1, *recv_len, 0);
115 }
116#endif
117
118 return res;
119}
Christophe Ricard587bf7a2015-10-06 22:54:42 +0200120
121static int tpm_atmel_twi_probe(struct udevice *dev)
122{
123 return 0;
124}
125
126static const struct udevice_id tpm_atmel_twi_ids[] = {
127 { .compatible = "atmel,at97sc3204t"},
128 { }
129};
130
131static const struct tpm_ops tpm_atmel_twi_ops = {
132 .open = tpm_atmel_twi_open,
133 .close = tpm_atmel_twi_close,
134 .xfer = tpm_atmel_twi_xfer,
135 .get_desc = tpm_atmel_twi_get_desc,
136};
137
138U_BOOT_DRIVER(tpm_atmel_twi) = {
139 .name = "tpm_atmel_twi",
140 .id = UCLASS_TPM,
141 .of_match = tpm_atmel_twi_ids,
142 .ops = &tpm_atmel_twi_ops,
143 .probe = tpm_atmel_twi_probe,
144};